Bug 1443925 - Part 5: Make it possible to get the system principal from any thread, r=ckerschb

This is required because the script security manager which currently owns the
singleton is main-thread only. This change still ties the lifecycle of the
static to that service, but also makes it generally available from any thread.

Differential Revision: https://phabricator.services.mozilla.com/D163035
This commit is contained in:
Nika Layzell 2022-12-02 00:53:51 +00:00
parent 59c29d8099
commit b582df1238
4 changed files with 36 additions and 9 deletions

View File

@ -7,6 +7,7 @@
#include "nscore.h"
#include "SystemPrincipal.h"
#include "mozilla/ClearOnShutdown.h"
#include "nsCOMPtr.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"
@ -29,9 +30,31 @@ SystemPrincipal::SystemPrincipal()
: BasePrincipal(eSystemPrincipal, kSystemPrincipalSpec,
OriginAttributes()) {}
already_AddRefed<SystemPrincipal> SystemPrincipal::Create() {
RefPtr<SystemPrincipal> sp = new SystemPrincipal();
return sp.forget();
static StaticMutex sSystemPrincipalMutex;
static StaticRefPtr<SystemPrincipal> sSystemPrincipal
MOZ_GUARDED_BY(sSystemPrincipalMutex);
already_AddRefed<SystemPrincipal> SystemPrincipal::Get() {
StaticMutexAutoLock lock(sSystemPrincipalMutex);
return do_AddRef(sSystemPrincipal);
}
already_AddRefed<SystemPrincipal> SystemPrincipal::Init() {
AssertIsOnMainThread();
StaticMutexAutoLock lock(sSystemPrincipalMutex);
if (MOZ_UNLIKELY(sSystemPrincipal)) {
MOZ_ASSERT_UNREACHABLE("SystemPrincipal::Init() may only be called once");
} else {
sSystemPrincipal = new SystemPrincipal();
}
return do_AddRef(sSystemPrincipal);
}
void SystemPrincipal::Shutdown() {
AssertIsOnMainThread();
StaticMutexAutoLock lock(sSystemPrincipalMutex);
MOZ_ASSERT(sSystemPrincipal);
sSystemPrincipal = nullptr;
}
nsresult SystemPrincipal::GetScriptLocation(nsACString& aStr) {

View File

@ -22,6 +22,8 @@
}
#define NS_SYSTEMPRINCIPAL_CONTRACTID "@mozilla.org/systemprincipal;1"
class nsScriptSecurityManager;
namespace Json {
class Value;
}
@ -32,7 +34,7 @@ class SystemPrincipal final : public BasePrincipal, public nsISerializable {
SystemPrincipal();
public:
static already_AddRefed<SystemPrincipal> Create();
static already_AddRefed<SystemPrincipal> Get();
static PrincipalKind Kind() { return eSystemPrincipal; }
@ -61,8 +63,13 @@ class SystemPrincipal final : public BasePrincipal, public nsISerializable {
}
protected:
friend class ::nsScriptSecurityManager;
virtual ~SystemPrincipal() = default;
static already_AddRefed<SystemPrincipal> Init();
static void Shutdown();
bool SubsumesInternal(nsIPrincipal* aOther,
DocumentDomainConsideration aConsideration) override {
return true;

View File

@ -1554,9 +1554,7 @@ nsresult nsScriptSecurityManager::Init() {
InitPrefs();
// Create our system principal singleton
RefPtr<SystemPrincipal> system = SystemPrincipal::Create();
mSystemPrincipal = system;
mSystemPrincipal = SystemPrincipal::Init();
return NS_OK;
}
@ -1600,6 +1598,7 @@ nsScriptSecurityManager::~nsScriptSecurityManager(void) {
void nsScriptSecurityManager::Shutdown() {
NS_IF_RELEASE(sIOService);
BundleHelper::Shutdown();
SystemPrincipal::Shutdown();
}
nsScriptSecurityManager* nsScriptSecurityManager::GetScriptSecurityManager() {

View File

@ -15,8 +15,6 @@ namespace mozilla::ipc {
void IPDLParamTraits<nsIPrincipal*>::Write(IPC::MessageWriter* aWriter,
IProtocol* aActor,
nsIPrincipal* aParam) {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
Maybe<PrincipalInfo> info;
if (aParam) {
info.emplace();