diff --git a/js/xpconnect/src/XPCComponents.cpp b/js/xpconnect/src/XPCComponents.cpp index 2ab8b65d3a72..45eb639824f7 100644 --- a/js/xpconnect/src/XPCComponents.cpp +++ b/js/xpconnect/src/XPCComponents.cpp @@ -3021,7 +3021,7 @@ nsXPCComponents_Utils::GetCompartmentLocation(HandleValue val, obj = js::CheckedUnwrap(obj); MOZ_ASSERT(obj); - result = xpc::CompartmentPrivate::Get(obj)->GetLocation(); + result = xpc::RealmPrivate::Get(obj)->GetLocation(); return NS_OK; } diff --git a/js/xpconnect/src/XPCJSRuntime.cpp b/js/xpconnect/src/XPCJSRuntime.cpp index 2b0a62abf6b8..5d2ecd60f030 100644 --- a/js/xpconnect/src/XPCJSRuntime.cpp +++ b/js/xpconnect/src/XPCJSRuntime.cpp @@ -206,14 +206,14 @@ RealmPrivate::RealmPrivate(JS::Realm* realm) static bool TryParseLocationURICandidate(const nsACString& uristr, - CompartmentPrivate::LocationHint aLocationHint, + RealmPrivate::LocationHint aLocationHint, nsIURI** aURI) { static NS_NAMED_LITERAL_CSTRING(kGRE, "resource://gre/"); static NS_NAMED_LITERAL_CSTRING(kToolkit, "chrome://global/"); static NS_NAMED_LITERAL_CSTRING(kBrowser, "chrome://browser/"); - if (aLocationHint == CompartmentPrivate::LocationHintAddon) { + if (aLocationHint == RealmPrivate::LocationHintAddon) { // Blacklist some known locations which are clearly not add-on related. if (StringBeginsWith(uristr, kGRE) || StringBeginsWith(uristr, kToolkit) || @@ -249,8 +249,9 @@ TryParseLocationURICandidate(const nsACString& uristr, return true; } -bool CompartmentPrivate::TryParseLocationURI(CompartmentPrivate::LocationHint aLocationHint, - nsIURI** aURI) +bool +RealmPrivate::TryParseLocationURI(RealmPrivate::LocationHint aLocationHint, + nsIURI** aURI) { if (!aURI) return false; @@ -1089,13 +1090,13 @@ GetCompartmentName(JSCompartment* c, nsCString& name, int* anonymizeID, name.AssignLiteral("(unknown)"); } - // If the compartment's location (name) differs from the principal's - // script location, append the compartment's location to allow - // differentiation of multiple compartments owned by the same principal - // (e.g. components owned by the system or null principal). - CompartmentPrivate* compartmentPrivate = CompartmentPrivate::Get(c); - if (compartmentPrivate) { - const nsACString& location = compartmentPrivate->GetLocation(); + // If the realm's location (name) differs from the principal's script + // location, append the realm's location to allow differentiation of + // multiple realms owned by the same principal (e.g. components owned + // by the system or null principal). + RealmPrivate* realmPrivate = RealmPrivate::Get(realm); + if (realmPrivate) { + const nsACString& location = realmPrivate->GetLocation(); if (!location.IsEmpty() && !location.Equals(name)) { name.AppendLiteral(", "); name.Append(location); diff --git a/js/xpconnect/src/nsXPConnect.cpp b/js/xpconnect/src/nsXPConnect.cpp index 68c68f2f1198..70ea10448944 100644 --- a/js/xpconnect/src/nsXPConnect.cpp +++ b/js/xpconnect/src/nsXPConnect.cpp @@ -964,14 +964,14 @@ void SetLocationForGlobal(JSObject* global, const nsACString& location) { MOZ_ASSERT(global); - CompartmentPrivate::Get(global)->SetLocation(location); + RealmPrivate::Get(global)->SetLocation(location); } void SetLocationForGlobal(JSObject* global, nsIURI* locationURI) { MOZ_ASSERT(global); - CompartmentPrivate::Get(global)->SetLocationURI(locationURI); + RealmPrivate::Get(global)->SetLocationURI(locationURI); } } // namespace xpc diff --git a/js/xpconnect/src/xpcprivate.h b/js/xpconnect/src/xpcprivate.h index 110c874f5a14..498f85690188 100644 --- a/js/xpconnect/src/xpcprivate.h +++ b/js/xpconnect/src/xpcprivate.h @@ -2834,11 +2834,6 @@ class CompartmentPrivate CompartmentPrivate(const CompartmentPrivate&) = delete; public: - enum LocationHint { - LocationHintRegular, - LocationHintAddon - }; - explicit CompartmentPrivate(JSCompartment* c); ~CompartmentPrivate(); @@ -2911,48 +2906,6 @@ public: // by a security wrapper. See XrayWrapper.cpp. bool wrapperDenialWarnings[WrapperDenialTypeCount]; - const nsACString& GetLocation() { - if (location.IsEmpty() && locationURI) { - - nsCOMPtr jsLocationURI = - do_QueryInterface(locationURI); - if (jsLocationURI) { - // We cannot call into JS-implemented nsIURI objects, because - // we are iterating over the JS heap at this point. - location = - NS_LITERAL_CSTRING(""); - } else if (NS_FAILED(locationURI->GetSpec(location))) { - location = NS_LITERAL_CSTRING(""); - } - } - return location; - } - bool GetLocationURI(nsIURI** aURI) { - return GetLocationURI(LocationHintRegular, aURI); - } - bool GetLocationURI(LocationHint aLocationHint, nsIURI** aURI) { - if (locationURI) { - nsCOMPtr rval = locationURI; - rval.forget(aURI); - return true; - } - return TryParseLocationURI(aLocationHint, aURI); - } - void SetLocation(const nsACString& aLocation) { - if (aLocation.IsEmpty()) - return; - if (!location.IsEmpty() || locationURI) - return; - location = aLocation; - } - void SetLocationURI(nsIURI* aLocationURI) { - if (!aLocationURI) - return; - if (locationURI) - return; - locationURI = aLocationURI; - } - JSObject2WrappedJSMap* GetWrappedJSMap() const { return mWrappedJSMap; } void UpdateWeakPointersAfterGC(); @@ -2961,11 +2914,7 @@ public: size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf); private: - nsCString location; - nsCOMPtr locationURI; JSObject2WrappedJSMap* mWrappedJSMap; - - bool TryParseLocationURI(LocationHint aType, nsIURI** aURI); }; bool IsUniversalXPConnectEnabled(JSCompartment* compartment); @@ -2984,13 +2933,18 @@ CrashIfNotInAutomation() // // Following the ECMAScript spec, a realm contains a global (e.g. an inner // Window) and its associated scripts and objects; a compartment may contain -// several same-origin, same-principal realms. +// several same-origin realms. class RealmPrivate { RealmPrivate() = delete; RealmPrivate(const RealmPrivate&) = delete; public: + enum LocationHint { + LocationHintRegular, + LocationHintAddon + }; + explicit RealmPrivate(JS::Realm* realm); static RealmPrivate* Get(JS::Realm* realm) @@ -3015,6 +2969,55 @@ public: // Our XPCWrappedNativeScope. This is non-null if and only if this is an // XPConnect realm. XPCWrappedNativeScope* scope; + + const nsACString& GetLocation() { + if (location.IsEmpty() && locationURI) { + + nsCOMPtr jsLocationURI = + do_QueryInterface(locationURI); + if (jsLocationURI) { + // We cannot call into JS-implemented nsIURI objects, because + // we are iterating over the JS heap at this point. + location = + NS_LITERAL_CSTRING(""); + } else if (NS_FAILED(locationURI->GetSpec(location))) { + location = NS_LITERAL_CSTRING(""); + } + } + return location; + } + bool GetLocationURI(LocationHint aLocationHint, nsIURI** aURI) { + if (locationURI) { + nsCOMPtr rval = locationURI; + rval.forget(aURI); + return true; + } + return TryParseLocationURI(aLocationHint, aURI); + } + bool GetLocationURI(nsIURI** aURI) { + return GetLocationURI(LocationHintRegular, aURI); + } + + void SetLocation(const nsACString& aLocation) { + if (aLocation.IsEmpty()) + return; + if (!location.IsEmpty() || locationURI) + return; + location = aLocation; + } + void SetLocationURI(nsIURI* aLocationURI) { + if (!aLocationURI) + return; + if (locationURI) + return; + locationURI = aLocationURI; + } + +private: + nsCString location; + nsCOMPtr locationURI; + + bool TryParseLocationURI(LocationHint aType, nsIURI** aURI); }; inline XPCWrappedNativeScope*