/src/mozilla-central/dom/crypto/WebCryptoThreadPool.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=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 |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "mozilla/dom/WebCryptoThreadPool.h" |
8 | | |
9 | | #include "MainThreadUtils.h" |
10 | | #include "mozilla/Services.h" |
11 | | #include "mozilla/StaticPtr.h" |
12 | | #include "nsComponentManagerUtils.h" |
13 | | #include "nsNSSComponent.h" |
14 | | #include "nsXPCOMCIDInternal.h" |
15 | | #include "nsXPCOMPrivate.h" |
16 | | #include "nsIObserverService.h" |
17 | | #include "nsThreadPool.h" |
18 | | |
19 | | namespace mozilla { |
20 | | namespace dom { |
21 | | |
22 | | StaticRefPtr<WebCryptoThreadPool> gInstance; |
23 | | |
24 | | NS_IMPL_ISUPPORTS(WebCryptoThreadPool, nsIObserver) |
25 | | |
26 | | /* static */ void |
27 | | WebCryptoThreadPool::Initialize() |
28 | 3 | { |
29 | 3 | MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!"); |
30 | 3 | MOZ_ASSERT(!gInstance, "More than one instance!"); |
31 | 3 | |
32 | 3 | gInstance = new WebCryptoThreadPool(); |
33 | 3 | NS_WARNING_ASSERTION(gInstance, "Failed create thread pool!"); |
34 | 3 | |
35 | 3 | if (gInstance && NS_FAILED(gInstance->Init())) { |
36 | 0 | NS_WARNING("Failed to initialize thread pool!"); |
37 | 0 | gInstance = nullptr; |
38 | 0 | } |
39 | 3 | } |
40 | | |
41 | | /* static */ nsresult |
42 | | WebCryptoThreadPool::Dispatch(nsIRunnable* aRunnable) |
43 | 0 | { |
44 | 0 | if (gInstance) { |
45 | 0 | return gInstance->DispatchInternal(aRunnable); |
46 | 0 | } |
47 | 0 | |
48 | 0 | // Fail if called on shutdown. |
49 | 0 | return NS_ERROR_FAILURE; |
50 | 0 | } |
51 | | |
52 | | nsresult |
53 | | WebCryptoThreadPool::Init() |
54 | 3 | { |
55 | 3 | MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!"); |
56 | 3 | |
57 | 3 | nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); |
58 | 3 | NS_ENSURE_TRUE(obs, NS_ERROR_FAILURE); |
59 | 3 | |
60 | 3 | // Need this observer to know when to shut down the thread pool. |
61 | 3 | return obs->AddObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID, false); |
62 | 3 | } |
63 | | |
64 | | nsresult |
65 | | WebCryptoThreadPool::DispatchInternal(nsIRunnable* aRunnable) |
66 | 0 | { |
67 | 0 | MutexAutoLock lock(mMutex); |
68 | 0 |
|
69 | 0 | if (mShutdown) { |
70 | 0 | return NS_ERROR_FAILURE; |
71 | 0 | } |
72 | 0 | |
73 | 0 | if (!mPool) { |
74 | 0 | NS_ENSURE_TRUE(EnsureNSSInitializedChromeOrContent(), NS_ERROR_FAILURE); |
75 | 0 |
|
76 | 0 | nsCOMPtr<nsIThreadPool> pool(new nsThreadPool()); |
77 | 0 |
|
78 | 0 | nsresult rv = pool->SetName(NS_LITERAL_CSTRING("SubtleCrypto")); |
79 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
80 | 0 |
|
81 | 0 | pool.swap(mPool); |
82 | 0 | } |
83 | 0 |
|
84 | 0 | return mPool->Dispatch(aRunnable, NS_DISPATCH_NORMAL); |
85 | 0 | } |
86 | | |
87 | | void |
88 | | WebCryptoThreadPool::Shutdown() |
89 | 0 | { |
90 | 0 | MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!"); |
91 | 0 |
|
92 | 0 | // Limit the scope of locking to avoid deadlocking if DispatchInternal ends |
93 | 0 | // up getting called during shutdown event processing. |
94 | 0 | nsCOMPtr<nsIThreadPool> pool; |
95 | 0 | { |
96 | 0 | MutexAutoLock lock(mMutex); |
97 | 0 | if (mShutdown) { |
98 | 0 | return; |
99 | 0 | } |
100 | 0 | pool = mPool; |
101 | 0 | mShutdown = true; |
102 | 0 | } |
103 | 0 |
|
104 | 0 | if (pool) { |
105 | 0 | pool->Shutdown(); |
106 | 0 | } |
107 | 0 |
|
108 | 0 | nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); |
109 | 0 | NS_WARNING_ASSERTION(obs, "Failed to retrieve observer service!"); |
110 | 0 |
|
111 | 0 | if (obs) { |
112 | 0 | if (NS_FAILED(obs->RemoveObserver(this, |
113 | 0 | NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID))) { |
114 | 0 | NS_WARNING("Failed to remove shutdown observer!"); |
115 | 0 | } |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | | NS_IMETHODIMP |
120 | | WebCryptoThreadPool::Observe(nsISupports* aSubject, |
121 | | const char* aTopic, |
122 | | const char16_t* aData) |
123 | 0 | { |
124 | 0 | MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!"); |
125 | 0 |
|
126 | 0 | if (gInstance) { |
127 | 0 | gInstance->Shutdown(); |
128 | 0 | gInstance = nullptr; |
129 | 0 | } |
130 | 0 |
|
131 | 0 | return NS_OK; |
132 | 0 | } |
133 | | |
134 | | } // namespace dom |
135 | | } // namespace mozilla |