Bug 1363200 - JSAPI for realms: Move XPCWrappedNativeScope::mIsAddonScope to CompartmentPrivate. r=mrbkap
authorJason Orendorff <jorendorff@mozilla.com>
Wed, 12 Jul 2017 16:41:57 -0500
changeset 429594 0495d0052c24fcc9fb7f3d6d96b06314e8fd3234
parent 429593 0cc8ef854b497929161250f9aa067228abe85e63
child 429595 1852c4dfd856e5adcf98d00da8204c7f2ee250ac
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs1363200
milestone57.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 1363200 - JSAPI for realms: Move XPCWrappedNativeScope::mIsAddonScope to CompartmentPrivate. r=mrbkap
js/xpconnect/src/XPCJSRuntime.cpp
js/xpconnect/src/XPCWrappedNativeScope.cpp
js/xpconnect/src/xpcprivate.h
js/xpconnect/src/xpcpublic.h
--- a/js/xpconnect/src/XPCJSRuntime.cpp
+++ b/js/xpconnect/src/XPCJSRuntime.cpp
@@ -173,16 +173,17 @@ CompartmentPrivate::CompartmentPrivate(J
     , writeToGlobalPrototype(false)
     , skipWriteToGlobalPrototype(false)
     , isWebExtensionContentScript(false)
     , hasInterposition(false)
     , waiveInterposition(false)
     , addonCallInterposition(false)
     , allowCPOWs(false)
     , isContentXBLCompartment(false)
+    , isAddonCompartment(false)
     , universalXPConnectEnabled(false)
     , forcePermissiveCOWs(false)
     , wasNuked(false)
     , scriptability(c)
     , scope(nullptr)
     , mWrappedJSMap(JSObject2WrappedJSMap::newMap(XPC_JS_MAP_LENGTH))
 {
     MOZ_COUNT_CTOR(xpc::CompartmentPrivate);
@@ -441,19 +442,25 @@ IsContentXBLScope(JS::Realm* realm)
 
 bool
 IsInContentXBLScope(JSObject* obj)
 {
     return IsContentXBLCompartment(js::GetObjectCompartment(obj));
 }
 
 bool
+IsAddonCompartment(JSCompartment* compartment)
+{
+    return CompartmentPrivate::Get(compartment)->isAddonCompartment;
+}
+
+bool
 IsInAddonScope(JSObject* obj)
 {
-    return ObjectScope(obj)->IsAddonScope();
+    return IsAddonCompartment(js::GetObjectCompartment(obj));
 }
 
 bool
 IsUniversalXPConnectEnabled(JSCompartment* compartment)
 {
     CompartmentPrivate* priv = CompartmentPrivate::Get(compartment);
     if (!priv)
         return false;
--- a/js/xpconnect/src/XPCWrappedNativeScope.cpp
+++ b/js/xpconnect/src/XPCWrappedNativeScope.cpp
@@ -91,18 +91,17 @@ RemoteXULForbidsXBLScope(nsIPrincipal* a
 }
 
 XPCWrappedNativeScope::XPCWrappedNativeScope(JSContext* cx,
                                              JS::HandleObject aGlobal)
       : mWrappedNativeMap(Native2WrappedNativeMap::newMap(XPC_NATIVE_MAP_LENGTH)),
         mWrappedNativeProtoMap(ClassInfo2WrappedNativeProtoMap::newMap(XPC_NATIVE_PROTO_MAP_LENGTH)),
         mComponents(nullptr),
         mNext(nullptr),
-        mGlobalJSObject(aGlobal),
-        mIsAddonScope(false)
+        mGlobalJSObject(aGlobal)
 {
     // add ourselves to the scopes list
     {
         MOZ_ASSERT(aGlobal);
         DebugOnly<const js::Class*> clasp = js::GetObjectClass(aGlobal);
         MOZ_ASSERT(clasp->flags & (JSCLASS_PRIVATE_IS_NSISUPPORTS |
                                    JSCLASS_HAS_PRIVATE) ||
                    mozilla::dom::IsDOMClass(clasp));
@@ -392,17 +391,17 @@ ClearContentXBLScope(JSObject* global)
 } /* namespace xpc */
 
 JSObject*
 XPCWrappedNativeScope::EnsureAddonScope(JSContext* cx, JSAddonId* addonId)
 {
     JS::RootedObject global(cx, GetGlobalJSObject());
     MOZ_ASSERT(js::IsObjectInContextCompartment(global, cx));
     MOZ_ASSERT(!IsContentXBLScope());
-    MOZ_ASSERT(!mIsAddonScope);
+    MOZ_ASSERT(!IsAddonScope());
     MOZ_ASSERT(addonId);
     MOZ_ASSERT(nsContentUtils::IsSystemPrincipal(GetPrincipal()));
 
     // In bug 1092156, we found that add-on scopes don't work correctly when the
     // window navigates. The add-on global's prototype is an outer window, so,
     // after the navigation, looking up window properties in the add-on scope
     // will fail. However, in most cases where the window can be navigated, the
     // entire window is part of the add-on. To solve the problem, we avoid
@@ -424,17 +423,17 @@ XPCWrappedNativeScope::EnsureAddonScope(
     options.addonId = JS::StringOfAddonId(addonId);
     options.writeToGlobalPrototype = true;
 
     RootedValue v(cx);
     nsresult rv = CreateSandboxObject(cx, &v, GetPrincipal(), options);
     NS_ENSURE_SUCCESS(rv, nullptr);
     mAddonScopes.AppendElement(&v.toObject());
 
-    CompartmentPrivate::Get(js::UncheckedUnwrap(&v.toObject()))->scope->mIsAddonScope = true;
+    CompartmentPrivate::Get(js::UncheckedUnwrap(&v.toObject()))->isAddonCompartment = true;
     return &v.toObject();
 }
 
 JSObject*
 xpc::GetAddonScope(JSContext* cx, JS::HandleObject contentScope, JSAddonId* addonId)
 {
     MOZ_RELEASE_ASSERT(!IsInAddonScope(contentScope));
 
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -1013,17 +1013,17 @@ public:
 
     JSCompartment* Compartment() const { return js::GetObjectCompartment(mGlobalJSObject); }
 
     bool IsContentXBLScope() { return xpc::IsContentXBLCompartment(Compartment()); }
     bool AllowContentXBLScope();
     bool UseContentXBLScope() { return mUseContentXBLScope; }
     void ClearContentXBLScope() { mContentXBLScope = nullptr; }
 
-    bool IsAddonScope() { return mIsAddonScope; }
+    bool IsAddonScope() { return xpc::IsAddonCompartment(Compartment()); }
 
     inline bool HasInterposition() { return mInterposition; }
     nsCOMPtr<nsIAddonInterposition> GetInterposition();
 
     static bool SetAddonInterposition(JSContext* cx,
                                       JSAddonId* addonId,
                                       nsIAddonInterposition* interp);
 
@@ -1075,18 +1075,16 @@ private:
     nsTArray<JS::ObjectPtr>          mAddonScopes;
 
     // This is a service that will be use to interpose on some property accesses on
     // objects from other scope, for add-on compatibility reasons.
     nsCOMPtr<nsIAddonInterposition>  mInterposition;
 
     JS::WeakMapPtr<JSObject*, JSObject*> mXrayExpandos;
 
-    bool mIsAddonScope;
-
     // For remote XUL domains, we run all XBL in the content scope for compat
     // reasons (though we sometimes pref this off for automation). We separately
     // track the result of this decision (mAllowContentXBLScope), from the decision
     // of whether to actually _use_ an XBL scope (mUseContentXBLScope), which depends
     // on the type of global and whether the compartment is system principal
     // or not.
     //
     // This distinction is useful primarily because, if true, we know that we
@@ -3094,16 +3092,21 @@ public:
     // to opt into CPOWs. It's necessary for the implementation of
     // RemoteAddonsParent.jsm.
     bool allowCPOWs;
 
     // True if this compartment is a content XBL compartment. Every global in
     // such a compartment is a content XBL scope.
     bool isContentXBLCompartment;
 
+    // True if EnsureAddonCompartment has been called for this compartment.
+    // Note that this is false for extensions that ship with the browser, like
+    // browser/extensions/activity-stream.
+    bool isAddonCompartment;
+
     // This is only ever set during mochitest runs when enablePrivilege is called.
     // It's intended as a temporary stopgap measure until we can finish ripping out
     // enablePrivilege. Once set, this value is never unset (i.e., it doesn't follow
     // the old scoping rules of enablePrivilege).
     //
     // Using it in production is inherently unsafe.
     bool universalXPConnectEnabled;
 
--- a/js/xpconnect/src/xpcpublic.h
+++ b/js/xpconnect/src/xpcpublic.h
@@ -125,16 +125,19 @@ bool
 UseContentXBLScope(JSCompartment* c);
 
 // Clear out the content XBL scope (if any) on the given global.  This will
 // force creation of a new one if one is needed again.
 void
 ClearContentXBLScope(JSObject* global);
 
 bool
+IsAddonCompartment(JSCompartment* c);
+
+bool
 IsInAddonScope(JSObject* obj);
 
 JSObject*
 GetAddonScope(JSContext* cx, JS::HandleObject contentScope, JSAddonId* addonId);
 
 bool
 IsSandboxPrototypeProxy(JSObject* obj);