Bug 1070696 - Rename xpc::GetNativeForGlobal to xpc::NativeGlobal and make it accept non-global JSObjects. r=smaug
authorBobby Holley <bobbyholley@gmail.com>
Sun, 21 Sep 2014 21:31:53 +0200
changeset 206325 e1f3be2c48f65c462dc0a11b9852c03ef7200605
parent 206324 c902ebaa8520ae9ebf36276283b6e28a850ae57c
child 206326 cafe6d84215b803077e99d2379cbbe0cf15d82c2
push id49409
push userbobbyholley@gmail.com
push dateSun, 21 Sep 2014 19:32:16 +0000
treeherdermozilla-inbound@cafe6d84215b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1070696
milestone35.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 1070696 - Rename xpc::GetNativeForGlobal to xpc::NativeGlobal and make it accept non-global JSObjects. r=smaug The method is infallible, and making the callers invoke js::GetGlobalForObjectCrossCompartment manually is dumb.
content/base/src/nsDOMFileReader.cpp
content/base/src/nsDocument.cpp
content/base/src/nsFrameMessageManager.cpp
content/base/src/nsXMLHttpRequest.cpp
dom/base/ScriptSettings.cpp
dom/base/nsJSEnvironment.cpp
dom/base/nsStructuredCloneContainer.cpp
dom/bindings/CallbackObject.cpp
dom/crypto/WebCryptoTask.cpp
dom/promise/Promise.cpp
ipc/testshell/TestShellParent.cpp
js/xpconnect/loader/mozJSComponentLoader.cpp
js/xpconnect/src/XPCWrappedJSClass.cpp
js/xpconnect/src/nsXPConnect.cpp
js/xpconnect/src/xpcpublic.h
js/xpconnect/wrappers/WrapperFactory.cpp
--- a/content/base/src/nsDOMFileReader.cpp
+++ b/content/base/src/nsDOMFileReader.cpp
@@ -102,17 +102,17 @@ nsDOMFileReader::~nsDOMFileReader()
  * This Init method is called from the factory constructor.
  */
 nsresult
 nsDOMFileReader::Init()
 {
   // Instead of grabbing some random global from the context stack,
   // let's use the default one (junk scope) for now.
   // We should move away from this Init...
-  BindToOwner(xpc::GetNativeForGlobal(xpc::PrivilegedJunkScope()));
+  BindToOwner(xpc::NativeGlobal(xpc::PrivilegedJunkScope()));
   return NS_OK;
 }
 
 /* static */ already_AddRefed<nsDOMFileReader>
 nsDOMFileReader::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
 {
   nsRefPtr<nsDOMFileReader> fileReader = new nsDOMFileReader();
 
--- a/content/base/src/nsDocument.cpp
+++ b/content/base/src/nsDocument.cpp
@@ -2149,17 +2149,17 @@ nsDocument::Init()
                     "Bad NodeType in aNodeInfo");
 
   NS_ASSERTION(OwnerDoc() == this, "Our nodeinfo is busted!");
 
   // If after creation the owner js global is not set for a document
   // we use the default compartment for this document, instead of creating
   // wrapper in some random compartment when the document is exposed to js
   // via some events.
-  nsCOMPtr<nsIGlobalObject> global = xpc::GetNativeForGlobal(xpc::PrivilegedJunkScope());
+  nsCOMPtr<nsIGlobalObject> global = xpc::NativeGlobal(xpc::PrivilegedJunkScope());
   NS_ENSURE_TRUE(global, NS_ERROR_FAILURE);
   mScopeObject = do_GetWeakReference(global);
   MOZ_ASSERT(mScopeObject);
 
   mScriptLoader = new nsScriptLoader(this);
 
   mozilla::HoldJSObjects(this);
 
--- a/content/base/src/nsFrameMessageManager.cpp
+++ b/content/base/src/nsFrameMessageManager.cpp
@@ -963,17 +963,17 @@ nsFrameMessageManager::ReceiveMessage(ns
       // Note - The ergonomics here will get a lot better with bug 971673:
       //
       // AutoEntryScript aes;
       // if (!aes.Init(wrappedJS->GetJSObject())) {
       //   continue;
       // }
       // JSContext* cx = aes.cx();
       nsIGlobalObject* nativeGlobal =
-        xpc::GetNativeForGlobal(js::GetGlobalForObjectCrossCompartment(wrappedJS->GetJSObject()));
+        xpc::NativeGlobal(js::GetGlobalForObjectCrossCompartment(wrappedJS->GetJSObject()));
       AutoEntryScript aes(nativeGlobal);
       JSContext* cx = aes.cx();
       JS::Rooted<JSObject*> object(cx, wrappedJS->GetJSObject());
 
       // The parameter for the listener function.
       JS::Rooted<JSObject*> param(cx,
         JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr()));
       NS_ENSURE_TRUE(param, NS_ERROR_OUT_OF_MEMORY);
--- a/content/base/src/nsXMLHttpRequest.cpp
+++ b/content/base/src/nsXMLHttpRequest.cpp
@@ -347,17 +347,17 @@ nsXMLHttpRequest::Init()
   if (secMan) {
     secMan->GetSystemPrincipal(getter_AddRefs(subjectPrincipal));
   }
   NS_ENSURE_STATE(subjectPrincipal);
 
   // Instead of grabbing some random global from the context stack,
   // let's use the default one (junk scope) for now.
   // We should move away from this Init...
-  Construct(subjectPrincipal, xpc::GetNativeForGlobal(xpc::PrivilegedJunkScope()));
+  Construct(subjectPrincipal, xpc::NativeGlobal(xpc::PrivilegedJunkScope()));
   return NS_OK;
 }
 
 /**
  * This Init method should only be called by C++ consumers.
  */
 NS_IMETHODIMP
 nsXMLHttpRequest::Init(nsIPrincipal* aPrincipal,
--- a/dom/base/ScriptSettings.cpp
+++ b/dom/base/ScriptSettings.cpp
@@ -149,17 +149,17 @@ GetIncumbentGlobal()
     return nullptr;
   }
 
   // See what the JS engine has to say. If we've got a scripted caller
   // override in place, the JS engine will lie to us and pretend that
   // there's nothing on the JS stack, which will cause us to check the
   // incumbent script stack below.
   if (JSObject *global = JS::GetScriptedCallerGlobal(cx)) {
-    return xpc::GetNativeForGlobal(global);
+    return xpc::NativeGlobal(global);
   }
 
   // Ok, nothing from the JS engine. Let's use whatever's on the
   // explicit stack.
   return ScriptSettingsStack::IncumbentGlobal();
 }
 
 nsIGlobalObject*
@@ -170,17 +170,17 @@ GetCurrentGlobal()
     return nullptr;
   }
 
   JSObject *global = JS::CurrentGlobalOrNull(cx);
   if (!global) {
     return nullptr;
   }
 
-  return xpc::GetNativeForGlobal(global);
+  return xpc::NativeGlobal(global);
 }
 
 nsIPrincipal*
 GetWebIDLCallerPrincipal()
 {
   MOZ_ASSERT(NS_IsMainThread());
   ScriptSettingsStackEntry *entry = ScriptSettingsStack::EntryPoint();
 
--- a/dom/base/nsJSEnvironment.cpp
+++ b/dom/base/nsJSEnvironment.cpp
@@ -479,17 +479,17 @@ SystemErrorReporter(JSContext *cx, const
   // that the global of the current compartment is the correct global with which
   // to report the error. But in practice this is probably fine for the time
   // being, and will get cleaned up soon when we fix bug 981187.
   if (!globalObject && JS::CurrentGlobalOrNull(cx)) {
     globalObject = xpc::AddonWindowOrNull(JS::CurrentGlobalOrNull(cx));
   }
 
   if (!globalObject) {
-    globalObject = xpc::GetNativeForGlobal(xpc::PrivilegedJunkScope());
+    globalObject = xpc::NativeGlobal(xpc::PrivilegedJunkScope());
   }
 
   if (globalObject) {
     nsRefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
     xpcReport->Init(report, message, globalObject);
 
     // If there's no window to fire an event at, report it to the console
     // directly.
@@ -2576,17 +2576,17 @@ NS_DOMReadStructuredClone(JSContext* cx,
                           JSStructuredCloneReader* reader,
                           uint32_t tag,
                           uint32_t data,
                           void* closure)
 {
   if (tag == SCTAG_DOM_IMAGEDATA) {
     return ReadStructuredCloneImageData(cx, reader);
   } else if (tag == SCTAG_DOM_WEBCRYPTO_KEY) {
-    nsIGlobalObject *global = xpc::GetNativeForGlobal(JS::CurrentGlobalOrNull(cx));
+    nsIGlobalObject *global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(cx));
     if (!global) {
       return nullptr;
     }
 
     // Prevent the return value from being trashed by a GC during ~nsRefPtr.
     JS::Rooted<JSObject*> result(cx);
     {
       nsRefPtr<CryptoKey> key = new CryptoKey(global);
--- a/dom/base/nsStructuredCloneContainer.cpp
+++ b/dom/base/nsStructuredCloneContainer.cpp
@@ -56,17 +56,17 @@ nsStructuredCloneContainer::InitFromJSVa
     success = JS_WriteStructuredClone(jsapi.cx(), aData, &jsBytes, &mSize,
                                       nullptr, nullptr,
                                       JS::UndefinedHandleValue);
   } else {
     // |aData| is an object and the structured clone algorithm can run script as
     // part of the "own" "deep clone" sub-steps, so we need an AutoEntryScript.
     // http://www.whatwg.org/specs/web-apps/current-work/#internal-structured-cloning-algorithm
     nsIGlobalObject* nativeGlobal =
-      xpc::GetNativeForGlobal(js::GetGlobalForObjectCrossCompartment(&aData.toObject()));
+      xpc::NativeGlobal(js::GetGlobalForObjectCrossCompartment(&aData.toObject()));
     dom::AutoEntryScript aes(nativeGlobal);
     success = JS_WriteStructuredClone(aes.cx(), aData, &jsBytes, &mSize,
                                       nullptr, nullptr,
                                       JS::UndefinedHandleValue);
   }
   NS_ENSURE_STATE(success);
   NS_ENSURE_STATE(jsBytes);
 
--- a/dom/bindings/CallbackObject.cpp
+++ b/dom/bindings/CallbackObject.cpp
@@ -104,17 +104,17 @@ CallbackObject::CallSetup::CallSetup(Cal
         cx = win->GetContext() ? win->GetContext()->GetNativeContext()
                                // This happens - Removing it causes
                                // test_bug293235.xul to go orange.
                                : nsContentUtils::GetSafeJSContext();
         globalObject = win;
       } else {
         // No DOM Window. Store the global and use the SafeJSContext.
         JSObject* glob = js::GetGlobalForObjectCrossCompartment(realCallback);
-        globalObject = xpc::GetNativeForGlobal(glob);
+        globalObject = xpc::NativeGlobal(glob);
         MOZ_ASSERT(globalObject);
         cx = nsContentUtils::GetSafeJSContext();
       }
     } else {
       cx = workers::GetCurrentThreadJSContext();
       globalObject = workers::GetCurrentThreadWorkerPrivate()->GlobalScope();
     }
 
--- a/dom/crypto/WebCryptoTask.cpp
+++ b/dom/crypto/WebCryptoTask.cpp
@@ -1281,17 +1281,17 @@ public:
       const nsAString& aFormat,
       const ObjectOrString& aAlgorithm, bool aExtractable,
       const Sequence<nsString>& aKeyUsages)
   {
     mFormat = aFormat;
     mDataIsSet = false;
 
     // Get the current global object from the context
-    nsIGlobalObject *global = xpc::GetNativeForGlobal(JS::CurrentGlobalOrNull(aCx));
+    nsIGlobalObject *global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
     if (!global) {
       mEarlyRv = NS_ERROR_DOM_UNKNOWN_ERR;
       return;
     }
 
     // This stuff pretty much always happens, so we'll do it here
     mKey = new CryptoKey(global);
     mKey->SetExtractable(aExtractable);
@@ -1950,17 +1950,17 @@ private:
 
 class GenerateSymmetricKeyTask : public WebCryptoTask
 {
 public:
   GenerateSymmetricKeyTask(JSContext* aCx,
       const ObjectOrString& aAlgorithm, bool aExtractable,
       const Sequence<nsString>& aKeyUsages)
   {
-    nsIGlobalObject* global = xpc::GetNativeForGlobal(JS::CurrentGlobalOrNull(aCx));
+    nsIGlobalObject* global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
     if (!global) {
       mEarlyRv = NS_ERROR_DOM_UNKNOWN_ERR;
       return;
     }
 
     // Create an empty key and set easy attributes
     mKey = new CryptoKey(global);
     mKey->SetExtractable(aExtractable);
@@ -2077,17 +2077,17 @@ private:
 
 class GenerateAsymmetricKeyTask : public WebCryptoTask
 {
 public:
   GenerateAsymmetricKeyTask(JSContext* aCx,
       const ObjectOrString& aAlgorithm, bool aExtractable,
       const Sequence<nsString>& aKeyUsages)
   {
-    nsIGlobalObject* global = xpc::GetNativeForGlobal(JS::CurrentGlobalOrNull(aCx));
+    nsIGlobalObject* global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(aCx));
     if (!global) {
       mEarlyRv = NS_ERROR_DOM_UNKNOWN_ERR;
       return;
     }
 
     // Create an empty key and set easy attributes
     mKeyPair = new CryptoKeyPair(global);
 
--- a/dom/promise/Promise.cpp
+++ b/dom/promise/Promise.cpp
@@ -1019,17 +1019,17 @@ Promise::MaybeReportRejected()
   js::ErrorReport report(cx);
   if (!report.init(cx, val)) {
     JS_ClearPendingException(cx);
     return;
   }
 
   nsRefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
   if (MOZ_LIKELY(NS_IsMainThread())) {
-    nsIGlobalObject* global = xpc::GetNativeForGlobal(js::GetGlobalForObjectCrossCompartment(obj));
+    nsIGlobalObject* global = xpc::NativeGlobal(js::GetGlobalForObjectCrossCompartment(obj));
     xpcReport->Init(report.report(), report.message(), global);
   } else {
     xpcReport->InitOnWorkerThread(report.report(), report.message(),
                                   GetCurrentThreadWorkerPrivate()->IsChromeWorker());
   }
 
   // Now post an event to do the real reporting async
   // Since Promises preserve their wrapper, it is essential to nsRefPtr<> the
--- a/ipc/testshell/TestShellParent.cpp
+++ b/ipc/testshell/TestShellParent.cpp
@@ -65,17 +65,17 @@ TestShellCommandParent::SetCallback(JSCo
 
 bool
 TestShellCommandParent::RunCallback(const nsString& aResponse)
 {
   NS_ENSURE_TRUE(mCallback.ToJSObject(), false);
 
   // We're about to run script via JS_CallFunctionValue, so we need an
   // AutoEntryScript. This is just for testing and not in any spec.
-  dom::AutoEntryScript aes(xpc::GetNativeForGlobal(js::GetGlobalForObjectCrossCompartment(mCallback.ToJSObject())));
+  dom::AutoEntryScript aes(xpc::NativeGlobal(js::GetGlobalForObjectCrossCompartment(mCallback.ToJSObject())));
   JSContext* cx = aes.cx();
   JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
 
   JSString* str = JS_NewUCStringCopyN(cx, aResponse.get(), aResponse.Length());
   NS_ENSURE_TRUE(str, false);
 
   JS::Rooted<JS::Value> strVal(cx, JS::StringValue(str));
 
--- a/js/xpconnect/loader/mozJSComponentLoader.cpp
+++ b/js/xpconnect/loader/mozJSComponentLoader.cpp
@@ -723,17 +723,17 @@ mozJSComponentLoader::PrepareObjectForLo
     NS_ENSURE_TRUE(exposedUri, nullptr);
 
     if (!JS_DefineProperty(aCx, obj, "__URI__", exposedUri, 0))
         return nullptr;
 
     if (createdNewGlobal) {
         // AutoEntryScript required to invoke debugger hook, which is a
         // Gecko-specific concept at present.
-        dom::AutoEntryScript aes(GetNativeForGlobal(holder->GetJSObject()));
+        dom::AutoEntryScript aes(NativeGlobal(holder->GetJSObject()));
         RootedObject global(aes.cx(), holder->GetJSObject());
         JS_FireOnNewGlobalObject(aes.cx(), global);
     }
 
     return obj;
 }
 
 nsresult
@@ -1001,17 +1001,17 @@ mozJSComponentLoader::ObjectForLocation(
     aTableScript.set(tableScript);
 
     bool ok = false;
 
     {
         // We're going to run script via JS_ExecuteScriptVersion or
         // JS_CallFunction, so we need an AutoEntryScript.
         // This is Gecko-specific and not in any spec.
-        dom::AutoEntryScript aes(GetNativeForGlobal(CurrentGlobalOrNull(cx)));
+        dom::AutoEntryScript aes(NativeGlobal(CurrentGlobalOrNull(cx)));
         AutoSaveContextOptions asco(cx);
         if (aPropagateExceptions)
             ContextOptionsRef(cx).setDontReportUncaught(true);
         if (script) {
             ok = JS_ExecuteScriptVersion(cx, obj, script, JSVERSION_LATEST);
         } else {
             RootedValue rval(cx);
             ok = JS_CallFunction(cx, obj, function, JS::HandleValueArray::empty(), &rval);
--- a/js/xpconnect/src/XPCWrappedJSClass.cpp
+++ b/js/xpconnect/src/XPCWrappedJSClass.cpp
@@ -484,17 +484,17 @@ nsXPCWrappedJSClass::DelegatedQueryInter
     if (aIID.Equals(NS_GET_IID(nsWrapperCache))) {
         *aInstancePtr = nullptr;
         return NS_NOINTERFACE;
     }
 
     // QI on an XPCWrappedJS can run script, so we need an AutoEntryScript.
     // This is inherently Gecko-specific.
     nsIGlobalObject* nativeGlobal =
-      GetNativeForGlobal(js::GetGlobalForObjectCrossCompartment(self->GetJSObject()));
+      NativeGlobal(js::GetGlobalForObjectCrossCompartment(self->GetJSObject()));
     AutoEntryScript aes(nativeGlobal, /* aIsMainThread = */ true);
     XPCCallContext ccx(NATIVE_CALLER, aes.cx());
     if (!ccx.IsValid()) {
         *aInstancePtr = nullptr;
         return NS_NOINTERFACE;
     }
 
     // We support nsISupportsWeakReference iff the root wrapped JSObject
@@ -885,17 +885,17 @@ nsXPCWrappedJSClass::CallMethod(nsXPCWra
     // the whole nsIXPCFunctionThisTranslator bit.  That code uses ccx to
     // convert natives to JSObjects, but we do NOT plan to pass those JSObjects
     // to our real callee.
     //
     // We're about to call into script via an XPCWrappedJS, so we need an
     // AutoEntryScript. This is probably Gecko-specific at this point, and
     // definitely will be when we turn off XPConnect for the web.
     nsIGlobalObject* nativeGlobal =
-      GetNativeForGlobal(js::GetGlobalForObjectCrossCompartment(wrapper->GetJSObject()));
+      NativeGlobal(js::GetGlobalForObjectCrossCompartment(wrapper->GetJSObject()));
     AutoEntryScript aes(nativeGlobal, /* aIsMainThread = */ true);
     XPCCallContext ccx(NATIVE_CALLER, aes.cx());
     if (!ccx.IsValid())
         return retval;
 
     XPCContext *xpcc = ccx.GetXPCContext();
     JSContext *cx = ccx.GetJSContext();
 
--- a/js/xpconnect/src/nsXPConnect.cpp
+++ b/js/xpconnect/src/nsXPConnect.cpp
@@ -1519,17 +1519,17 @@ NewAddonId(JSContext *cx, const nsACStri
 bool
 SetAddonInterposition(const nsACString &addonIdStr, nsIAddonInterposition *interposition)
 {
     JSAddonId *addonId;
     {
         // We enter the junk scope just to allocate a string, which actually will go
         // in the system zone.
         AutoJSAPI jsapi;
-        jsapi.Init(xpc::GetNativeForGlobal(xpc::PrivilegedJunkScope()));
+        jsapi.Init(xpc::NativeGlobal(xpc::PrivilegedJunkScope()));
         addonId = NewAddonId(jsapi.cx(), addonIdStr);
         if (!addonId)
             return false;
     }
 
     return XPCWrappedNativeScope::SetAddonInterposition(addonId, interposition);
 }
 
--- a/js/xpconnect/src/xpcpublic.h
+++ b/js/xpconnect/src/xpcpublic.h
@@ -420,22 +420,16 @@ ReportJSRuntimeExplicitTreeStats(const J
 
 /**
  * Throws an exception on cx and returns false.
  */
 bool
 Throw(JSContext *cx, nsresult rv);
 
 /**
- * Every global should hold a native that implements the nsIGlobalObject interface.
- */
-nsIGlobalObject *
-GetNativeForGlobal(JSObject *global);
-
-/**
  * Returns the nsISupports native behind a given reflector (either DOM or
  * XPCWN).
  */
 nsISupports *
 UnwrapReflectorToISupports(JSObject *reflector);
 
 /**
  * Singleton scopes for stuff that really doesn't fit anywhere else.
@@ -454,16 +448,22 @@ PrivilegedJunkScope();
  * Shared compilation scope for XUL prototype documents and XBL
  * precompilation. This compartment has a null principal. No code may run, and
  * it is invisible to the debugger.
  */
 JSObject *
 CompilationScope();
 
 /**
+ * Returns the nsIGlobalObject corresponding to |aObj|'s JS global.
+ */
+nsIGlobalObject*
+NativeGlobal(JSObject *aObj);
+
+/**
  * If |aObj| is a window, returns the associated nsGlobalWindow.
  * Otherwise, returns null.
  */
 nsGlobalWindow*
 WindowOrNull(JSObject *aObj);
 
 /**
  * If |aObj| has a window for a global, returns the associated nsGlobalWindow.
--- a/js/xpconnect/wrappers/WrapperFactory.cpp
+++ b/js/xpconnect/wrappers/WrapperFactory.cpp
@@ -639,19 +639,19 @@ TransplantObject(JSContext *cx, JS::Hand
        return newIdentity;
 
     if (!FixWaiverAfterTransplant(cx, oldWaiver, newIdentity))
         return nullptr;
     return newIdentity;
 }
 
 nsIGlobalObject *
-GetNativeForGlobal(JSObject *obj)
+NativeGlobal(JSObject *obj)
 {
-    MOZ_ASSERT(JS_IsGlobalObject(obj));
+    obj = js::GetGlobalForObjectCrossCompartment(obj);
 
     // Every global needs to hold a native as its private or be a
     // WebIDL object with an nsISupports DOM object.
     MOZ_ASSERT((GetObjectClass(obj)->flags & (JSCLASS_PRIVATE_IS_NSISUPPORTS |
                                              JSCLASS_HAS_PRIVATE)) ||
                dom::UnwrapDOMObjectToISupports(obj));
 
     nsISupports *native = dom::UnwrapDOMObjectToISupports(obj);