/src/mozilla-central/security/manager/ssl/CryptoTask.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=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 | | #include "nsNSSComponent.h" |
9 | | |
10 | | namespace mozilla { |
11 | | |
12 | | nsresult |
13 | | CryptoTask::Dispatch(const nsACString& taskThreadName) |
14 | 0 | { |
15 | 0 | MOZ_ASSERT(taskThreadName.Length() <= 15); |
16 | 0 |
|
17 | 0 | // Ensure that NSS is initialized, since presumably CalculateResult |
18 | 0 | // will use NSS functions |
19 | 0 | if (!EnsureNSSInitializedChromeOrContent()) { |
20 | 0 | return NS_ERROR_FAILURE; |
21 | 0 | } |
22 | 0 | |
23 | 0 | // Can't add 'this' as the event to run, since mThread may not be set yet |
24 | 0 | nsresult rv = NS_NewNamedThread(taskThreadName, getter_AddRefs(mThread), |
25 | 0 | nullptr, |
26 | 0 | nsIThreadManager::DEFAULT_STACK_SIZE); |
27 | 0 | if (NS_FAILED(rv)) { |
28 | 0 | return rv; |
29 | 0 | } |
30 | 0 | |
31 | 0 | // Note: event must not null out mThread! |
32 | 0 | return mThread->Dispatch(this, NS_DISPATCH_NORMAL); |
33 | 0 | } |
34 | | |
35 | | NS_IMETHODIMP |
36 | | CryptoTask::Run() |
37 | 0 | { |
38 | 0 | if (!NS_IsMainThread()) { |
39 | 0 | mRv = CalculateResult(); |
40 | 0 | NS_DispatchToMainThread(this); |
41 | 0 | } else { |
42 | 0 | // back on the main thread |
43 | 0 |
|
44 | 0 | CallCallback(mRv); |
45 | 0 |
|
46 | 0 | // Not all uses of CryptoTask use a transient thread |
47 | 0 | if (mThread) { |
48 | 0 | // Don't leak threads! |
49 | 0 | mThread->Shutdown(); // can't Shutdown from the thread itself, darn |
50 | 0 | // Don't null out mThread! |
51 | 0 | // See bug 999104. We must hold a ref to the thread across Dispatch() |
52 | 0 | // since the internal mThread ref could be released while processing |
53 | 0 | // the Dispatch(), and Dispatch/PutEvent itself doesn't hold a ref; it |
54 | 0 | // assumes the caller does. |
55 | 0 | } |
56 | 0 | } |
57 | 0 |
|
58 | 0 | return NS_OK; |
59 | 0 | } |
60 | | |
61 | | } // namespace mozilla |