CryptoTask.h (1449B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla__CryptoTask_h 8 #define mozilla__CryptoTask_h 9 10 #include "nsThreadUtils.h" 11 12 namespace mozilla { 13 14 /** 15 * Frequently we need to run a task on a background thread without blocking 16 * the main thread, and then call a callback on the main thread with the 17 * result. This class provides the framework for that. Subclasses must: 18 * 19 * (1) Override CalculateResult for the off-the-main-thread computation. 20 * (2) Override CallCallback() for the on-the-main-thread call of the 21 * callback. 22 */ 23 class CryptoTask : public Runnable { 24 public: 25 nsresult Dispatch(); 26 27 protected: 28 CryptoTask() : Runnable("CryptoTask"), mRv(NS_ERROR_NOT_INITIALIZED) {} 29 30 virtual ~CryptoTask() = default; 31 32 /** 33 * Called on a background thread (never the main thread). Its result will be 34 * passed to CallCallback on the main thread. 35 */ 36 virtual nsresult CalculateResult() = 0; 37 38 /** 39 * Called on the main thread with the result from CalculateResult(). 40 */ 41 virtual void CallCallback(nsresult rv) = 0; 42 43 private: 44 NS_IMETHOD Run() final; 45 46 nsresult mRv; 47 }; 48 49 } // namespace mozilla 50 51 #endif // mozilla__CryptoTask_h