Backed out 2 changesets (bug 1881078, bug 1879806) for causing dt failures @ devtools...
[gecko.git] / caps / SystemPrincipal.cpp
blob5c2ed63a9aa2f94e2dddbd270a69c961d0ffd725
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. */
8 #include "nscore.h"
9 #include "SystemPrincipal.h"
10 #include "mozilla/ClearOnShutdown.h"
11 #include "nsCOMPtr.h"
12 #include "nsReadableUtils.h"
13 #include "nsCRT.h"
14 #include "nsString.h"
15 #include "nsIClassInfoImpl.h"
16 #include "pratom.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");
46 } else {
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);
61 return NS_OK;
64 ///////////////////////////////////////
65 // Methods implementing nsIPrincipal //
66 ///////////////////////////////////////
68 uint32_t SystemPrincipal::GetHashValue() { return NS_PTR_TO_INT32(this); }
70 NS_IMETHODIMP
71 SystemPrincipal::GetURI(nsIURI** aURI) {
72 *aURI = nullptr;
73 return NS_OK;
76 NS_IMETHODIMP
77 SystemPrincipal::GetIsOriginPotentiallyTrustworthy(bool* aResult) {
78 *aResult = true;
79 return NS_OK;
82 NS_IMETHODIMP
83 SystemPrincipal::GetDomain(nsIURI** aDomain) {
84 *aDomain = nullptr;
85 return NS_OK;
88 NS_IMETHODIMP
89 SystemPrincipal::SetDomain(nsIURI* aDomain) { return NS_OK; }
91 NS_IMETHODIMP
92 SystemPrincipal::GetBaseDomain(nsACString& aBaseDomain) {
93 // No base domain for chrome.
94 return NS_OK;
97 NS_IMETHODIMP
98 SystemPrincipal::GetAddonId(nsAString& aAddonId) {
99 aAddonId.Truncate();
100 return NS_OK;
103 //////////////////////////////////////////
104 // Methods implementing nsISerializable //
105 //////////////////////////////////////////
107 NS_IMETHODIMP
108 SystemPrincipal::Read(nsIObjectInputStream* aStream) {
109 // no-op: CID is sufficient to identify the mSystemPrincipal singleton
110 return NS_OK;
113 NS_IMETHODIMP
114 SystemPrincipal::Write(nsIObjectOutputStream* aStream) {
115 // Read is used still for legacy principals
116 MOZ_RELEASE_ASSERT(false, "Old style serialization is removed");
117 return NS_OK;