Skip to content

Commit

Permalink
Bug 1443925 - Part 5: Make it possible to get the system principal fr…
Browse files Browse the repository at this point in the history
…om 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
  • Loading branch information
mystor committed Dec 2, 2022
1 parent 0d86718 commit f8615e4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
29 changes: 26 additions & 3 deletions caps/SystemPrincipal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "nscore.h"
#include "SystemPrincipal.h"
#include "mozilla/ClearOnShutdown.h"
#include "nsCOMPtr.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"
Expand All @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion caps/SystemPrincipal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
}
#define NS_SYSTEMPRINCIPAL_CONTRACTID "@mozilla.org/systemprincipal;1"

class nsScriptSecurityManager;

namespace Json {
class Value;
}
Expand All @@ -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; }

Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions caps/nsScriptSecurityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1600,6 +1598,7 @@ nsScriptSecurityManager::~nsScriptSecurityManager(void) {
void nsScriptSecurityManager::Shutdown() {
NS_IF_RELEASE(sIOService);
BundleHelper::Shutdown();
SystemPrincipal::Shutdown();
}

nsScriptSecurityManager* nsScriptSecurityManager::GetScriptSecurityManager() {
Expand Down
2 changes: 0 additions & 2 deletions dom/ipc/PermissionMessageUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f8615e4

Please sign in to comment.