|
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 #include "CryptoTask.h" |
|
8 |
|
9 namespace mozilla { |
|
10 |
|
11 CryptoTask::~CryptoTask() |
|
12 { |
|
13 MOZ_ASSERT(mReleasedNSSResources); |
|
14 |
|
15 nsNSSShutDownPreventionLock lock; |
|
16 if (!isAlreadyShutDown()) { |
|
17 shutdown(calledFromObject); |
|
18 } |
|
19 } |
|
20 |
|
21 NS_IMETHODIMP |
|
22 CryptoTask::Run() |
|
23 { |
|
24 if (!NS_IsMainThread()) { |
|
25 nsNSSShutDownPreventionLock locker; |
|
26 if (isAlreadyShutDown()) { |
|
27 mRv = NS_ERROR_NOT_AVAILABLE; |
|
28 } else { |
|
29 mRv = CalculateResult(); |
|
30 } |
|
31 NS_DispatchToMainThread(this); |
|
32 } else { |
|
33 // back on the main thread |
|
34 |
|
35 // call ReleaseNSSResources now, before calling CallCallback, so that |
|
36 // CryptoTasks have consistent behavior regardless of whether NSS is shut |
|
37 // down between CalculateResult being called and CallCallback being called. |
|
38 if (!mReleasedNSSResources) { |
|
39 mReleasedNSSResources = true; |
|
40 ReleaseNSSResources(); |
|
41 } |
|
42 |
|
43 CallCallback(mRv); |
|
44 |
|
45 // Not all uses of CryptoTask use a transient thread |
|
46 if (mThread) { |
|
47 // Don't leak threads! |
|
48 mThread->Shutdown(); // can't Shutdown from the thread itself, darn |
|
49 // Don't null out mThread! |
|
50 // See bug 999104. We must hold a ref to the thread across Dispatch() |
|
51 // since the internal mThread ref could be released while processing |
|
52 // the Dispatch(), and Dispatch/PutEvent itself doesn't hold a ref; it |
|
53 // assumes the caller does. |
|
54 } |
|
55 } |
|
56 |
|
57 return NS_OK; |
|
58 } |
|
59 |
|
60 void |
|
61 CryptoTask::virtualDestroyNSSReference() |
|
62 { |
|
63 NS_ABORT_IF_FALSE(NS_IsMainThread(), |
|
64 "virtualDestroyNSSReference called off the main thread"); |
|
65 if (!mReleasedNSSResources) { |
|
66 mReleasedNSSResources = true; |
|
67 ReleaseNSSResources(); |
|
68 } |
|
69 } |
|
70 |
|
71 } // namespace mozilla |