1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* The privileged system principal. */
9 #include "SystemPrincipal.h"
10 #include "mozilla/ClearOnShutdown.h"
12 #include "nsReadableUtils.h"
15 #include "nsIClassInfoImpl.h"
18 using namespace mozilla
;
20 NS_IMPL_CLASSINFO(SystemPrincipal
, nullptr, nsIClassInfo::SINGLETON
,
21 NS_SYSTEMPRINCIPAL_CID
)
22 NS_IMPL_QUERY_INTERFACE_CI(SystemPrincipal
, nsIPrincipal
, nsISerializable
)
23 NS_IMPL_CI_INTERFACE_GETTER(SystemPrincipal
, nsIPrincipal
, nsISerializable
)
25 static constexpr nsLiteralCString kSystemPrincipalSpec
=
26 "[System Principal]"_ns
;
28 SystemPrincipal::SystemPrincipal()
29 : BasePrincipal(eSystemPrincipal
, kSystemPrincipalSpec
,
30 OriginAttributes()) {}
32 static StaticMutex sSystemPrincipalMutex
;
33 static StaticRefPtr
<SystemPrincipal
> sSystemPrincipal
34 MOZ_GUARDED_BY(sSystemPrincipalMutex
);
36 already_AddRefed
<SystemPrincipal
> SystemPrincipal::Get() {
37 StaticMutexAutoLock
lock(sSystemPrincipalMutex
);
38 return do_AddRef(sSystemPrincipal
);
41 already_AddRefed
<SystemPrincipal
> SystemPrincipal::Init() {
42 AssertIsOnMainThread();
43 StaticMutexAutoLock
lock(sSystemPrincipalMutex
);
44 if (MOZ_UNLIKELY(sSystemPrincipal
)) {
45 MOZ_ASSERT_UNREACHABLE("SystemPrincipal::Init() may only be called once");
47 sSystemPrincipal
= new SystemPrincipal();
49 return do_AddRef(sSystemPrincipal
);
52 void SystemPrincipal::Shutdown() {
53 AssertIsOnMainThread();
54 StaticMutexAutoLock
lock(sSystemPrincipalMutex
);
55 MOZ_ASSERT(sSystemPrincipal
);
56 sSystemPrincipal
= nullptr;
59 nsresult
SystemPrincipal::GetScriptLocation(nsACString
& aStr
) {
60 aStr
.Assign(kSystemPrincipalSpec
);
64 ///////////////////////////////////////
65 // Methods implementing nsIPrincipal //
66 ///////////////////////////////////////
68 uint32_t SystemPrincipal::GetHashValue() { return NS_PTR_TO_INT32(this); }
71 SystemPrincipal::GetURI(nsIURI
** aURI
) {
77 SystemPrincipal::GetIsOriginPotentiallyTrustworthy(bool* aResult
) {
83 SystemPrincipal::GetDomain(nsIURI
** aDomain
) {
89 SystemPrincipal::SetDomain(nsIURI
* aDomain
) { return NS_OK
; }
92 SystemPrincipal::GetBaseDomain(nsACString
& aBaseDomain
) {
93 // No base domain for chrome.
98 SystemPrincipal::GetAddonId(nsAString
& aAddonId
) {
103 //////////////////////////////////////////
104 // Methods implementing nsISerializable //
105 //////////////////////////////////////////
108 SystemPrincipal::Read(nsIObjectInputStream
* aStream
) {
109 // no-op: CID is sufficient to identify the mSystemPrincipal singleton
114 SystemPrincipal::Write(nsIObjectOutputStream
* aStream
) {
115 // Read is used still for legacy principals
116 MOZ_RELEASE_ASSERT(false, "Old style serialization is removed");