Bug 1465728 part 1 - Move location and locationURI fields from CompartmentPrivate to RealmPrivate. r=bz
authorJan de Mooij <jdemooij@mozilla.com>
Wed, 06 Jun 2018 11:44:17 +0200
changeset 421577 2d422fb97474917892bc35d37532d33fd1a39fa0
parent 421576 4ab0fba87583700c837de47c7a1ecbd55d0c7664
child 421578 84aa50d4663a10d18cb305b9157134139f4cc867
push id34098
push usernbeleuzu@mozilla.com
push dateWed, 06 Jun 2018 17:00:32 +0000
treeherdermozilla-central@04cc917f68c5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1465728
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1465728 part 1 - Move location and locationURI fields from CompartmentPrivate to RealmPrivate. r=bz The xpcprivate.h changes are just code motion, except I changed the order of the GetLocationURI methods to get a much more readable diff.
js/xpconnect/src/XPCComponents.cpp
js/xpconnect/src/XPCJSRuntime.cpp
js/xpconnect/src/nsXPConnect.cpp
js/xpconnect/src/xpcprivate.h
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -3016,17 +3016,17 @@ nsXPCComponents_Utils::GetCompartmentLoc
                                               nsACString& result)
 {
     if (!val.isObject())
         return NS_ERROR_INVALID_ARG;
     RootedObject obj(cx, &val.toObject());
     obj = js::CheckedUnwrap(obj);
     MOZ_ASSERT(obj);
 
-    result = xpc::CompartmentPrivate::Get(obj)->GetLocation();
+    result = xpc::RealmPrivate::Get(obj)->GetLocation();
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsXPCComponents_Utils::ReadUTF8File(nsIFile* aFile, nsACString& aResult)
 {
     NS_ENSURE_TRUE(aFile, NS_ERROR_INVALID_ARG);
 
--- a/js/xpconnect/src/XPCJSRuntime.cpp
+++ b/js/xpconnect/src/XPCJSRuntime.cpp
@@ -201,24 +201,24 @@ CompartmentPrivate::SystemIsBeingShutDow
 RealmPrivate::RealmPrivate(JS::Realm* realm)
     : scriptability(realm)
     , scope(nullptr)
 {
 }
 
 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) ||
             StringBeginsWith(uristr, kBrowser))
             return false;
 
         // -- GROSS HACK ALERT --
         // The Yandex Elements 8.10.2 extension implements its own "xb://" URL
@@ -244,18 +244,19 @@ TryParseLocationURICandidate(const nsACS
     // for memory reporter use.
     if (scheme.EqualsLiteral("data") || scheme.EqualsLiteral("blob"))
         return false;
 
     uri.forget(aURI);
     return true;
 }
 
-bool CompartmentPrivate::TryParseLocationURI(CompartmentPrivate::LocationHint aLocationHint,
-                                             nsIURI** aURI)
+bool
+RealmPrivate::TryParseLocationURI(RealmPrivate::LocationHint aLocationHint,
+                                  nsIURI** aURI)
 {
     if (!aURI)
         return false;
 
     // Need to parse the URI.
     if (location.IsEmpty())
         return false;
 
@@ -1084,23 +1085,23 @@ GetCompartmentName(JSCompartment* c, nsC
         name.AppendPrintf("<anonymized-%d>", *anonymizeID);
         *anonymizeID += 1;
     } else if (JSPrincipals* principals = JS::GetRealmPrincipals(realm)) {
         nsresult rv = nsJSPrincipals::get(principals)->GetScriptLocation(name);
         if (NS_FAILED(rv)) {
             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);
             }
         }
 
         if (*anonymizeID) {
             // We might have a file:// URL that includes a path from the local
--- a/js/xpconnect/src/nsXPConnect.cpp
+++ b/js/xpconnect/src/nsXPConnect.cpp
@@ -959,24 +959,24 @@ Base64Decode(JSContext* cx, HandleValue 
     out.setString(str);
     return true;
 }
 
 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
 
 static nsresult
 WriteScriptOrFunction(nsIObjectOutputStream* stream, JSContext* cx,
                       JSScript* scriptArg, HandleObject functionObj)
 {
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -2829,21 +2829,16 @@ bool ReportWrapperDenial(JSContext* cx, 
 // information needed to select the right security policy for cross-compartment
 // wrappers.
 class CompartmentPrivate
 {
     CompartmentPrivate() = delete;
     CompartmentPrivate(const CompartmentPrivate&) = delete;
 
 public:
-    enum LocationHint {
-        LocationHintRegular,
-        LocationHintAddon
-    };
-
     explicit CompartmentPrivate(JSCompartment* c);
 
     ~CompartmentPrivate();
 
     static CompartmentPrivate* Get(JSCompartment* compartment)
     {
         MOZ_ASSERT(compartment);
         void* priv = JS_GetCompartmentPrivate(compartment);
@@ -2906,71 +2901,25 @@ public:
     // True if this compartment has been nuked. If true, any wrappers into or
     // out of it should be considered invalid.
     bool wasNuked;
 
     // Whether we've emitted a warning about a property that was filtered out
     // by a security wrapper. See XrayWrapper.cpp.
     bool wrapperDenialWarnings[WrapperDenialTypeCount];
 
-    const nsACString& GetLocation() {
-        if (location.IsEmpty() && locationURI) {
-
-            nsCOMPtr<nsIXPConnectWrappedJS> 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("<JS-implemented nsIURI location>");
-            } else if (NS_FAILED(locationURI->GetSpec(location))) {
-                location = NS_LITERAL_CSTRING("<unknown location>");
-            }
-        }
-        return location;
-    }
-    bool GetLocationURI(nsIURI** aURI) {
-        return GetLocationURI(LocationHintRegular, aURI);
-    }
-    bool GetLocationURI(LocationHint aLocationHint, nsIURI** aURI) {
-        if (locationURI) {
-            nsCOMPtr<nsIURI> 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();
 
     void SystemIsBeingShutDown();
 
     size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf);
 
 private:
-    nsCString location;
-    nsCOMPtr<nsIURI> locationURI;
     JSObject2WrappedJSMap* mWrappedJSMap;
-
-    bool TryParseLocationURI(LocationHint aType, nsIURI** aURI);
 };
 
 bool IsUniversalXPConnectEnabled(JSCompartment* compartment);
 bool IsUniversalXPConnectEnabled(JSContext* cx);
 bool EnableUniversalXPConnect(JSContext* cx);
 
 inline void
 CrashIfNotInAutomation()
@@ -2979,23 +2928,28 @@ CrashIfNotInAutomation()
 }
 
 // XPConnect-specific data associated with each JavaScript realm. Per-Window
 // settings live here; security-wrapper-related settings live in the
 // CompartmentPrivate.
 //
 // 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)
     {
         MOZ_ASSERT(realm);
         void* priv = JS::GetRealmPrivate(realm);
         return static_cast<RealmPrivate*>(priv);
     }
@@ -3010,16 +2964,65 @@ public:
     }
 
     // The scriptability of this realm.
     Scriptability scriptability;
 
     // 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<nsIXPConnectWrappedJS> 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("<JS-implemented nsIURI location>");
+            } else if (NS_FAILED(locationURI->GetSpec(location))) {
+                location = NS_LITERAL_CSTRING("<unknown location>");
+            }
+        }
+        return location;
+    }
+    bool GetLocationURI(LocationHint aLocationHint, nsIURI** aURI) {
+        if (locationURI) {
+            nsCOMPtr<nsIURI> 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<nsIURI> locationURI;
+
+    bool TryParseLocationURI(LocationHint aType, nsIURI** aURI);
 };
 
 inline XPCWrappedNativeScope*
 ObjectScope(JSObject* obj)
 {
     return RealmPrivate::Get(obj)->scope;
 }