1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
7 #include "nsNSSModule.h"
9 #include "ContentSignatureVerifier.h"
10 #include "OSKeyStore.h"
11 #include "OSReauthenticator.h"
12 #include "PKCS11ModuleDB.h"
13 #include "SecretDecoderRing.h"
14 #include "mozilla/MacroArgs.h"
15 #include "mozilla/ModuleUtils.h"
16 #include "mozilla/SyncRunnable.h"
17 #include "nsCertTree.h"
18 #include "nsNSSCertificateDB.h"
19 #include "nsPK11TokenDB.h"
20 #include "nsRandomGenerator.h"
21 #include "nsXULAppAPI.h"
26 // Many of the implementations in this module call NSS functions and as a result
27 // require that PSM has successfully initialized NSS before being used.
28 // Additionally, some of the implementations have various restrictions on which
29 // process and threads they can be used on (e.g. some can only be used in the
30 // parent process and some must be initialized only on the main thread).
31 // The following initialization framework allows these requirements to be
32 // succinctly expressed and implemented.
34 template <class InstanceClass
, nsresult (InstanceClass::*InitMethod
)()>
35 MOZ_ALWAYS_INLINE
static nsresult
Instantiate(REFNSIID aIID
, void** aResult
) {
36 InstanceClass
* inst
= new InstanceClass();
38 nsresult rv
= InitMethod
!= nullptr ? (inst
->*InitMethod
)() : NS_OK
;
39 if (NS_SUCCEEDED(rv
)) {
40 rv
= inst
->QueryInterface(aIID
, aResult
);
46 enum class ThreadRestriction
{
47 // must be initialized on the main thread (but can be used on any thread)
49 // can be initialized and used on any thread
53 enum class ProcessRestriction
{
58 template <class InstanceClass
,
59 nsresult (InstanceClass::*InitMethod
)() = nullptr,
60 ProcessRestriction processRestriction
=
61 ProcessRestriction::ParentProcessOnly
,
62 ThreadRestriction threadRestriction
= ThreadRestriction::AnyThread
>
63 static nsresult
Constructor(REFNSIID aIID
, void** aResult
) {
66 if (processRestriction
== ProcessRestriction::ParentProcessOnly
&&
67 !XRE_IsParentProcess()) {
68 return NS_ERROR_NOT_AVAILABLE
;
71 if (!EnsureNSSInitializedChromeOrContent()) {
72 return NS_ERROR_FAILURE
;
75 if (threadRestriction
== ThreadRestriction::MainThreadOnly
&&
77 return NS_ERROR_NOT_SAME_THREAD
;
80 return Instantiate
<InstanceClass
, InitMethod
>(aIID
, aResult
);
83 #define IMPL(type, ...) \
85 nsresult NSSConstructor<type>(const nsIID& aIID, void** aResult) { \
86 return Constructor<type, __VA_ARGS__>(aIID, aResult); \
89 // Components that require main thread initialization could cause a deadlock
90 // in necko code (bug 1418752). To prevent it we initialize all such components
91 // on main thread in advance in net_EnsurePSMInit(). Update that function when
92 // new component with ThreadRestriction::MainThreadOnly is added.
93 IMPL(SecretDecoderRing
, nullptr)
94 IMPL(nsPK11TokenDB
, nullptr)
95 IMPL(PKCS11ModuleDB
, nullptr)
96 IMPL(nsNSSCertificateDB
, nullptr)
97 IMPL(nsCertTree
, nullptr)
98 IMPL(ContentSignatureVerifier
, nullptr)
99 IMPL(nsRandomGenerator
, nullptr, ProcessRestriction::AnyProcess
)
100 IMPL(OSKeyStore
, nullptr, ProcessRestriction::ParentProcessOnly
,
101 ThreadRestriction::MainThreadOnly
)
102 IMPL(OSReauthenticator
, nullptr, ProcessRestriction::ParentProcessOnly
,
103 ThreadRestriction::MainThreadOnly
)
107 } // namespace mozilla