Bug 1547923 part 3. Add nsIGlobalObject::GetGlobalJSObjectPreserveColor(). r=mccr8
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 02 May 2019 21:23:35 +0000
changeset 531315 358e6f08d9c5612f6cffdcda1ff2457696312886
parent 531314 64fb8460f0f87df8c59a5f49ebb4837dde1adb90
child 531316 2d60837b8f3c38281997ab84a80a490f7f478496
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8
bugs1547923
milestone68.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 1547923 part 3. Add nsIGlobalObject::GetGlobalJSObjectPreserveColor(). r=mccr8 This can be used in things like assertions or some other rare circumstances where not exposing the object to active JS is OK. Differential Revision: https://phabricator.services.mozilla.com/D29704
dom/base/nsGlobalWindowInner.cpp
dom/base/nsGlobalWindowInner.h
dom/base/nsGlobalWindowOuter.cpp
dom/base/nsGlobalWindowOuter.h
dom/base/nsIGlobalObject.h
dom/bindings/SimpleGlobalObject.h
dom/workers/WorkerScope.h
dom/worklet/WorkletGlobalScope.h
js/src/devtools/rootAnalysis/annotations.js
js/xpconnect/src/BackstagePass.h
js/xpconnect/src/SandboxPrivate.h
js/xpconnect/src/XPCRuntimeService.cpp
--- a/dom/base/nsGlobalWindowInner.cpp
+++ b/dom/base/nsGlobalWindowInner.cpp
@@ -1573,16 +1573,20 @@ nsIScriptContext* nsGlobalWindowInner::G
   }
   return outer->GetScriptContext();
 }
 
 JSObject* nsGlobalWindowInner::GetGlobalJSObject() {
   return FastGetGlobalJSObject();
 }
 
+JSObject* nsGlobalWindowInner::GetGlobalJSObjectPreserveColor() const {
+  return FastGetGlobalJSObject();
+}
+
 void nsGlobalWindowInner::TraceGlobalJSObject(JSTracer* aTrc) {
   TraceWrapper(aTrc, "active window global");
 }
 
 bool nsGlobalWindowInner::ShouldResetBrowsingContextUserGestureActivation() {
   // Reset user gesture activation flag only when the top level document changes
   // and its corresponding browsing context has been activated by user gesture.
   return mWindowGlobalChild && GetOuterWindowInternal() &&
--- a/dom/base/nsGlobalWindowInner.h
+++ b/dom/base/nsGlobalWindowInner.h
@@ -238,18 +238,19 @@ class nsGlobalWindowInner final : public
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
 
   // nsWrapperCache
   virtual JSObject* WrapObject(JSContext* cx,
                                JS::Handle<JSObject*> aGivenProto) override {
     return GetWrapper();
   }
 
-  // nsIGlobalJSObjectHolder
-  virtual JSObject* GetGlobalJSObject() override;
+  // nsIGlobalObject
+  JSObject* GetGlobalJSObject() override;
+  JSObject* GetGlobalJSObjectPreserveColor() const override;
 
   // nsIScriptGlobalObject
   JSObject* FastGetGlobalJSObject() const { return GetWrapperPreserveColor(); }
 
   void TraceGlobalJSObject(JSTracer* aTrc);
 
   virtual nsresult EnsureScriptEnvironment() override;
 
--- a/dom/base/nsGlobalWindowOuter.cpp
+++ b/dom/base/nsGlobalWindowOuter.cpp
@@ -1505,16 +1505,20 @@ nsresult nsGlobalWindowOuter::EnsureScri
 }
 
 nsIScriptContext* nsGlobalWindowOuter::GetScriptContext() { return mContext; }
 
 JSObject* nsGlobalWindowOuter::GetGlobalJSObject() {
   return FastGetGlobalJSObject();
 }
 
+JSObject* nsGlobalWindowOuter::GetGlobalJSObjectPreserveColor() const {
+  return FastGetGlobalJSObject();
+}
+
 bool nsGlobalWindowOuter::WouldReuseInnerWindow(Document* aNewDocument) {
   // We reuse the inner window when:
   // a. We are currently at our original document.
   // b. At least one of the following conditions are true:
   // -- The new document is the same as the old document. This means that we're
   //    getting called from document.open().
   // -- The new document has the same origin as what we have loaded right now.
 
--- a/dom/base/nsGlobalWindowOuter.h
+++ b/dom/base/nsGlobalWindowOuter.h
@@ -226,17 +226,18 @@ class nsGlobalWindowOuter final : public
 
   // nsWrapperCache
   virtual JSObject* WrapObject(JSContext* cx,
                                JS::Handle<JSObject*> aGivenProto) override {
     return EnsureInnerWindow() ? GetWrapper() : nullptr;
   }
 
   // nsIGlobalJSObjectHolder
-  virtual JSObject* GetGlobalJSObject() override;
+  JSObject* GetGlobalJSObject() override;
+  JSObject* GetGlobalJSObjectPreserveColor() const override;
 
   // nsIScriptGlobalObject
   JSObject* FastGetGlobalJSObject() const { return GetWrapperPreserveColor(); }
 
   virtual nsresult EnsureScriptEnvironment() override;
 
   virtual nsIScriptContext* GetScriptContext() override;
 
--- a/dom/base/nsIGlobalObject.h
+++ b/dom/base/nsIGlobalObject.h
@@ -77,16 +77,30 @@ class nsIGlobalObject : public nsISuppor
   // GetGlobalJSObject may return a gray object.  If this ever changes so that
   // it stops doing that, please simplify the code in FindAssociatedGlobal in
   // BindingUtils.h that does JS::ExposeObjectToActiveJS on the return value of
   // GetGlobalJSObject.  Also, in that case the JS::ExposeObjectToActiveJS in
   // AutoJSAPI::InitInternal can probably be removed.  And also the similar
   // calls in XrayWrapper and nsGlobalWindow.
   virtual JSObject* GetGlobalJSObject() = 0;
 
+  /**
+   * Return the JSObject for this global _without_ exposing it to active JS.
+   * This may return a gray object.
+   *
+   * This method is appropriate to use in assertions (so there is less of a
+   * difference in GC/CC marking between debug and optimized builds) and in
+   * situations where we are sure no CC activity can happen while the return
+   * value is used and the return value does not end up escaping to the heap in
+   * any way.  In all other cases, and in particular in cases where the return
+   * value is held in a JS::Rooted or passed to the JSAutoRealm constructor, use
+   * GetGlobalJSObject.
+   */
+  virtual JSObject* GetGlobalJSObjectPreserveColor() const = 0;
+
   // This method is not meant to be overridden.
   nsIPrincipal* PrincipalOrNull();
 
   void RegisterHostObjectURI(const nsACString& aURI);
 
   void UnregisterHostObjectURI(const nsACString& aURI);
 
   // Any CC class inheriting nsIGlobalObject should call these 2 methods if it
--- a/dom/bindings/SimpleGlobalObject.h
+++ b/dom/bindings/SimpleGlobalObject.h
@@ -56,17 +56,20 @@ class SimpleGlobalObject : public nsIGlo
   // Gets the GlobalType of this SimpleGlobalObject.
   GlobalType Type() const { return mType; }
 
   // Gets the GlobalType of the SimpleGlobalObject for the given JSObject*, if
   // the given JSObject* is the global corresponding to a SimpleGlobalObject.
   // Oherwise, returns GlobalType::NotSimpleGlobal.
   static GlobalType SimpleGlobalType(JSObject* obj);
 
-  virtual JSObject* GetGlobalJSObject() override { return GetWrapper(); }
+  JSObject* GetGlobalJSObject() override { return GetWrapper(); }
+  JSObject* GetGlobalJSObjectPreserveColor() const override {
+    return GetWrapperPreserveColor();
+  }
 
   virtual JSObject* WrapObject(JSContext* cx,
                                JS::Handle<JSObject*> aGivenProto) override {
     MOZ_CRASH("SimpleGlobalObject doesn't use DOM bindings!");
   }
 
  private:
   SimpleGlobalObject(JSObject* global, GlobalType type) : mType(type) {
--- a/dom/workers/WorkerScope.h
+++ b/dom/workers/WorkerScope.h
@@ -72,17 +72,20 @@ class WorkerGlobalScope : public DOMEven
 
  public:
   virtual JSObject* WrapObject(JSContext* aCx,
                                JS::Handle<JSObject*> aGivenProto) override;
 
   virtual bool WrapGlobalObject(JSContext* aCx,
                                 JS::MutableHandle<JSObject*> aReflector) = 0;
 
-  virtual JSObject* GetGlobalJSObject(void) override { return GetWrapper(); }
+  JSObject* GetGlobalJSObject() override { return GetWrapper(); }
+  JSObject* GetGlobalJSObjectPreserveColor() const override {
+    return GetWrapperPreserveColor();
+  }
 
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WorkerGlobalScope,
                                                          DOMEventTargetHelper)
 
   WorkerGlobalScope* Self() { return this; }
 
   void NoteTerminating();
@@ -297,17 +300,20 @@ class WorkerDebuggerGlobalScope final : 
   virtual JSObject* WrapObject(JSContext* aCx,
                                JS::Handle<JSObject*> aGivenProto) override {
     MOZ_CRASH("Shouldn't get here!");
   }
 
   virtual bool WrapGlobalObject(JSContext* aCx,
                                 JS::MutableHandle<JSObject*> aReflector);
 
-  virtual JSObject* GetGlobalJSObject(void) override { return GetWrapper(); }
+  JSObject* GetGlobalJSObject(void) override { return GetWrapper(); }
+  JSObject* GetGlobalJSObjectPreserveColor(void) const override {
+    return GetWrapperPreserveColor();
+  }
 
   void GetGlobal(JSContext* aCx, JS::MutableHandle<JSObject*> aGlobal,
                  ErrorResult& aRv);
 
   void CreateSandbox(JSContext* aCx, const nsAString& aName,
                      JS::Handle<JSObject*> aPrototype,
                      JS::MutableHandle<JSObject*> aResult, ErrorResult& aRv);
 
--- a/dom/worklet/WorkletGlobalScope.h
+++ b/dom/worklet/WorkletGlobalScope.h
@@ -42,17 +42,20 @@ class WorkletGlobalScope : public nsIGlo
   nsIGlobalObject* GetParentObject() const { return nullptr; }
 
   virtual JSObject* WrapObject(JSContext* aCx,
                                JS::Handle<JSObject*> aGivenProto) override;
 
   virtual bool WrapGlobalObject(JSContext* aCx,
                                 JS::MutableHandle<JSObject*> aReflector) = 0;
 
-  virtual JSObject* GetGlobalJSObject() override { return GetWrapper(); }
+  JSObject* GetGlobalJSObject() override { return GetWrapper(); }
+  JSObject* GetGlobalJSObjectPreserveColor() const override {
+    return GetWrapperPreserveColor();
+  }
 
   already_AddRefed<Console> GetConsole(JSContext* aCx, ErrorResult& aRv);
 
   virtual WorkletImpl* Impl() const = 0;
 
   void Dump(const Optional<nsAString>& aString) const;
 
   DOMHighResTimeStamp TimeStampToDOMHighRes(const TimeStamp& aTimeStamp) const {
--- a/js/src/devtools/rootAnalysis/annotations.js
+++ b/js/src/devtools/rootAnalysis/annotations.js
@@ -402,16 +402,18 @@ function isOverridableField(initialCSU, 
     if (field == 'GetCurrentJSContext')
         return false;
     if (field == 'IsOnCurrentThread')
         return false;
     if (field == 'GetNativeContext')
         return false;
     if (field == "GetGlobalJSObject")
         return false;
+    if (field == "GetGlobalJSObjectPreserveColor")
+        return false;
     if (field == "GetIsMainThread")
         return false;
     if (field == "GetThreadFromPRThread")
         return false;
     if (field == "DocAddSizeOfIncludingThis")
         return false;
     if (field == "ConstructUbiNode")
         return false;
--- a/js/xpconnect/src/BackstagePass.h
+++ b/js/xpconnect/src/BackstagePass.h
@@ -28,17 +28,18 @@ class BackstagePass : public nsIGlobalOb
   NS_DECL_NSICLASSINFO
 
   virtual nsIPrincipal* GetPrincipal() override { return mPrincipal; }
 
   virtual nsIPrincipal* GetEffectiveStoragePrincipal() override {
     return nullptr;
   }
 
-  virtual JSObject* GetGlobalJSObject() override;
+  JSObject* GetGlobalJSObject() override;
+  JSObject* GetGlobalJSObjectPreserveColor() const override;
 
   void ForgetGlobalObject() { mWrapper = nullptr; }
 
   void SetGlobalObject(JSObject* global);
 
   explicit BackstagePass(nsIPrincipal* prin)
       : mPrincipal(prin), mWrapper(nullptr) {}
 
--- a/js/xpconnect/src/SandboxPrivate.h
+++ b/js/xpconnect/src/SandboxPrivate.h
@@ -43,16 +43,19 @@ class SandboxPrivate : public nsIGlobalO
         static_cast<nsIScriptObjectPrincipal*>(JS_GetPrivate(obj)));
   }
 
   nsIPrincipal* GetPrincipal() override { return mPrincipal; }
 
   nsIPrincipal* GetEffectiveStoragePrincipal() override { return nullptr; }
 
   JSObject* GetGlobalJSObject() override { return GetWrapper(); }
+  JSObject* GetGlobalJSObjectPreserveColor() const override {
+    return GetWrapperPreserveColor();
+  }
 
   void ForgetGlobalObject(JSObject* obj) { ClearWrapper(obj); }
 
   virtual JSObject* WrapObject(JSContext* cx,
                                JS::Handle<JSObject*> aGivenProto) override {
     MOZ_CRASH("SandboxPrivate doesn't use DOM bindings!");
   }
 
--- a/js/xpconnect/src/XPCRuntimeService.cpp
+++ b/js/xpconnect/src/XPCRuntimeService.cpp
@@ -37,16 +37,23 @@ NS_IMPL_ISUPPORTS(BackstagePass, nsIXPCS
 
 JSObject* BackstagePass::GetGlobalJSObject() {
   if (mWrapper) {
     return mWrapper->GetFlatJSObject();
   }
   return nullptr;
 }
 
+JSObject* BackstagePass::GetGlobalJSObjectPreserveColor() const {
+  if (mWrapper) {
+    return mWrapper->GetFlatJSObjectPreserveColor();
+  }
+  return nullptr;
+}
+
 void BackstagePass::SetGlobalObject(JSObject* global) {
   nsISupports* p = XPCWrappedNative::Get(global);
   MOZ_ASSERT(p);
   mWrapper = static_cast<XPCWrappedNative*>(p);
 }
 
 NS_IMETHODIMP
 BackstagePass::Resolve(nsIXPConnectWrappedNative* wrapper, JSContext* cx,