Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
7 #ifndef mozilla__CryptoTask_h
8 #define mozilla__CryptoTask_h
10 #include "mozilla/Attributes.h"
11 #include "nsThreadUtils.h"
12 #include "nsNSSShutDown.h"
14 namespace mozilla {
16 /**
17 * Frequently we need to run a task on a background thread without blocking
18 * the main thread, and then call a callback on the main thread with the
19 * result. This class provides the framework for that. Subclasses must:
20 *
21 * (1) Override CalculateResult for the off-the-main-thread computation.
22 * NSS functionality may only be accessed within CalculateResult.
23 * (2) Override ReleaseNSSResources to release references to all NSS
24 * resources (that do implement nsNSSShutDownObject themselves).
25 * (3) Override CallCallback() for the on-the-main-thread call of the
26 * callback.
27 *
28 * CalculateResult, ReleaseNSSResources, and CallCallback are called in order,
29 * except CalculateResult might be skipped if NSS is shut down before it can
30 * be called; in that case ReleaseNSSResources will be called and then
31 * CallCallback will be called with an error code.
32 */
33 class CryptoTask : public nsRunnable,
34 public nsNSSShutDownObject
35 {
36 public:
37 template <size_t LEN>
38 nsresult Dispatch(const char (&taskThreadName)[LEN])
39 {
40 static_assert(LEN <= 15,
41 "Thread name must be no more than 15 characters");
42 // Can't add 'this' as the event to run, since mThread may not be set yet
43 nsresult rv = NS_NewNamedThread(taskThreadName, getter_AddRefs(mThread));
44 if (NS_SUCCEEDED(rv)) {
45 // Note: event must not null out mThread!
46 rv = mThread->Dispatch(this, NS_DISPATCH_NORMAL);
47 }
48 return rv;
49 }
51 protected:
52 CryptoTask()
53 : mRv(NS_ERROR_NOT_INITIALIZED),
54 mReleasedNSSResources(false)
55 {
56 }
58 virtual ~CryptoTask();
60 /**
61 * Called on a background thread (never the main thread). If CalculateResult
62 * is called, then its result will be passed to CallCallback on the main
63 * thread.
64 */
65 virtual nsresult CalculateResult() = 0;
67 /**
68 * Called on the main thread during NSS shutdown or just before CallCallback
69 * has been called. All NSS resources must be released. Usually, this just
70 * means assigning nullptr to the ScopedNSSType-based memory variables.
71 */
72 virtual void ReleaseNSSResources() = 0;
74 /**
75 * Called on the main thread with the result from CalculateResult() or
76 * with an error code if NSS was shut down before CalculateResult could
77 * be called.
78 */
79 virtual void CallCallback(nsresult rv) = 0;
81 private:
82 NS_IMETHOD Run() MOZ_OVERRIDE MOZ_FINAL;
83 virtual void virtualDestroyNSSReference() MOZ_OVERRIDE MOZ_FINAL;
85 nsresult mRv;
86 bool mReleasedNSSResources;
88 nsCOMPtr<nsIThread> mThread;
89 };
91 } // namespace mozilla
93 #endif // mozilla__CryptoTask_h