security/manager/ssl/src/CryptoTask.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef mozilla__CryptoTask_h
michael@0 8 #define mozilla__CryptoTask_h
michael@0 9
michael@0 10 #include "mozilla/Attributes.h"
michael@0 11 #include "nsThreadUtils.h"
michael@0 12 #include "nsNSSShutDown.h"
michael@0 13
michael@0 14 namespace mozilla {
michael@0 15
michael@0 16 /**
michael@0 17 * Frequently we need to run a task on a background thread without blocking
michael@0 18 * the main thread, and then call a callback on the main thread with the
michael@0 19 * result. This class provides the framework for that. Subclasses must:
michael@0 20 *
michael@0 21 * (1) Override CalculateResult for the off-the-main-thread computation.
michael@0 22 * NSS functionality may only be accessed within CalculateResult.
michael@0 23 * (2) Override ReleaseNSSResources to release references to all NSS
michael@0 24 * resources (that do implement nsNSSShutDownObject themselves).
michael@0 25 * (3) Override CallCallback() for the on-the-main-thread call of the
michael@0 26 * callback.
michael@0 27 *
michael@0 28 * CalculateResult, ReleaseNSSResources, and CallCallback are called in order,
michael@0 29 * except CalculateResult might be skipped if NSS is shut down before it can
michael@0 30 * be called; in that case ReleaseNSSResources will be called and then
michael@0 31 * CallCallback will be called with an error code.
michael@0 32 */
michael@0 33 class CryptoTask : public nsRunnable,
michael@0 34 public nsNSSShutDownObject
michael@0 35 {
michael@0 36 public:
michael@0 37 template <size_t LEN>
michael@0 38 nsresult Dispatch(const char (&taskThreadName)[LEN])
michael@0 39 {
michael@0 40 static_assert(LEN <= 15,
michael@0 41 "Thread name must be no more than 15 characters");
michael@0 42 // Can't add 'this' as the event to run, since mThread may not be set yet
michael@0 43 nsresult rv = NS_NewNamedThread(taskThreadName, getter_AddRefs(mThread));
michael@0 44 if (NS_SUCCEEDED(rv)) {
michael@0 45 // Note: event must not null out mThread!
michael@0 46 rv = mThread->Dispatch(this, NS_DISPATCH_NORMAL);
michael@0 47 }
michael@0 48 return rv;
michael@0 49 }
michael@0 50
michael@0 51 protected:
michael@0 52 CryptoTask()
michael@0 53 : mRv(NS_ERROR_NOT_INITIALIZED),
michael@0 54 mReleasedNSSResources(false)
michael@0 55 {
michael@0 56 }
michael@0 57
michael@0 58 virtual ~CryptoTask();
michael@0 59
michael@0 60 /**
michael@0 61 * Called on a background thread (never the main thread). If CalculateResult
michael@0 62 * is called, then its result will be passed to CallCallback on the main
michael@0 63 * thread.
michael@0 64 */
michael@0 65 virtual nsresult CalculateResult() = 0;
michael@0 66
michael@0 67 /**
michael@0 68 * Called on the main thread during NSS shutdown or just before CallCallback
michael@0 69 * has been called. All NSS resources must be released. Usually, this just
michael@0 70 * means assigning nullptr to the ScopedNSSType-based memory variables.
michael@0 71 */
michael@0 72 virtual void ReleaseNSSResources() = 0;
michael@0 73
michael@0 74 /**
michael@0 75 * Called on the main thread with the result from CalculateResult() or
michael@0 76 * with an error code if NSS was shut down before CalculateResult could
michael@0 77 * be called.
michael@0 78 */
michael@0 79 virtual void CallCallback(nsresult rv) = 0;
michael@0 80
michael@0 81 private:
michael@0 82 NS_IMETHOD Run() MOZ_OVERRIDE MOZ_FINAL;
michael@0 83 virtual void virtualDestroyNSSReference() MOZ_OVERRIDE MOZ_FINAL;
michael@0 84
michael@0 85 nsresult mRv;
michael@0 86 bool mReleasedNSSResources;
michael@0 87
michael@0 88 nsCOMPtr<nsIThread> mThread;
michael@0 89 };
michael@0 90
michael@0 91 } // namespace mozilla
michael@0 92
michael@0 93 #endif // mozilla__CryptoTask_h

mercurial