1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/manager/ssl/src/CryptoTask.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,93 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla__CryptoTask_h 1.11 +#define mozilla__CryptoTask_h 1.12 + 1.13 +#include "mozilla/Attributes.h" 1.14 +#include "nsThreadUtils.h" 1.15 +#include "nsNSSShutDown.h" 1.16 + 1.17 +namespace mozilla { 1.18 + 1.19 +/** 1.20 + * Frequently we need to run a task on a background thread without blocking 1.21 + * the main thread, and then call a callback on the main thread with the 1.22 + * result. This class provides the framework for that. Subclasses must: 1.23 + * 1.24 + * (1) Override CalculateResult for the off-the-main-thread computation. 1.25 + * NSS functionality may only be accessed within CalculateResult. 1.26 + * (2) Override ReleaseNSSResources to release references to all NSS 1.27 + * resources (that do implement nsNSSShutDownObject themselves). 1.28 + * (3) Override CallCallback() for the on-the-main-thread call of the 1.29 + * callback. 1.30 + * 1.31 + * CalculateResult, ReleaseNSSResources, and CallCallback are called in order, 1.32 + * except CalculateResult might be skipped if NSS is shut down before it can 1.33 + * be called; in that case ReleaseNSSResources will be called and then 1.34 + * CallCallback will be called with an error code. 1.35 + */ 1.36 +class CryptoTask : public nsRunnable, 1.37 + public nsNSSShutDownObject 1.38 +{ 1.39 +public: 1.40 + template <size_t LEN> 1.41 + nsresult Dispatch(const char (&taskThreadName)[LEN]) 1.42 + { 1.43 + static_assert(LEN <= 15, 1.44 + "Thread name must be no more than 15 characters"); 1.45 + // Can't add 'this' as the event to run, since mThread may not be set yet 1.46 + nsresult rv = NS_NewNamedThread(taskThreadName, getter_AddRefs(mThread)); 1.47 + if (NS_SUCCEEDED(rv)) { 1.48 + // Note: event must not null out mThread! 1.49 + rv = mThread->Dispatch(this, NS_DISPATCH_NORMAL); 1.50 + } 1.51 + return rv; 1.52 + } 1.53 + 1.54 +protected: 1.55 + CryptoTask() 1.56 + : mRv(NS_ERROR_NOT_INITIALIZED), 1.57 + mReleasedNSSResources(false) 1.58 + { 1.59 + } 1.60 + 1.61 + virtual ~CryptoTask(); 1.62 + 1.63 + /** 1.64 + * Called on a background thread (never the main thread). If CalculateResult 1.65 + * is called, then its result will be passed to CallCallback on the main 1.66 + * thread. 1.67 + */ 1.68 + virtual nsresult CalculateResult() = 0; 1.69 + 1.70 + /** 1.71 + * Called on the main thread during NSS shutdown or just before CallCallback 1.72 + * has been called. All NSS resources must be released. Usually, this just 1.73 + * means assigning nullptr to the ScopedNSSType-based memory variables. 1.74 + */ 1.75 + virtual void ReleaseNSSResources() = 0; 1.76 + 1.77 + /** 1.78 + * Called on the main thread with the result from CalculateResult() or 1.79 + * with an error code if NSS was shut down before CalculateResult could 1.80 + * be called. 1.81 + */ 1.82 + virtual void CallCallback(nsresult rv) = 0; 1.83 + 1.84 +private: 1.85 + NS_IMETHOD Run() MOZ_OVERRIDE MOZ_FINAL; 1.86 + virtual void virtualDestroyNSSReference() MOZ_OVERRIDE MOZ_FINAL; 1.87 + 1.88 + nsresult mRv; 1.89 + bool mReleasedNSSResources; 1.90 + 1.91 + nsCOMPtr<nsIThread> mThread; 1.92 +}; 1.93 + 1.94 +} // namespace mozilla 1.95 + 1.96 +#endif // mozilla__CryptoTask_h