Bug 797821 - Address Ms2ger's review comments. r=me
authorBobby Holley <bobbyholley@gmail.com>
Thu, 25 Oct 2012 17:01:09 +0200
changeset 111536 b011b2c888d14956b1d923b231c51c8371b1091a
parent 111535 dcd3f731be3d467e92948314f94a5749302c53cb
child 111537 9a9ae8ce3b8dfed7e013155cab96fe7a02e7b7c1
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersme
bugs797821
milestone19.0a1
Bug 797821 - Address Ms2ger's review comments. r=me
content/base/src/nsContentUtils.cpp
dom/base/nsGlobalWindow.cpp
dom/bindings/Codegen.py
dom/bindings/DOMJSProxyHandler.cpp
js/xpconnect/src/XPCComponents.cpp
js/xpconnect/src/XPCConvert.cpp
js/xpconnect/src/XPCWrappedJSClass.cpp
js/xpconnect/src/XPCWrappedNative.cpp
js/xpconnect/src/XPCWrappedNativeScope.cpp
js/xpconnect/src/nsXPConnect.cpp
js/xpconnect/src/xpcprivate.h
js/xpconnect/wrappers/WrapperFactory.cpp
--- a/content/base/src/nsContentUtils.cpp
+++ b/content/base/src/nsContentUtils.cpp
@@ -6922,17 +6922,17 @@ nsContentUtils::ReleaseWrapper(void* aSc
                                nsWrapperCache* aCache)
 {
   if (aCache->PreservingWrapper()) {
     // PreserveWrapper puts new DOM bindings in the JS holders hash, but they
     // can also be in the DOM expando hash, so we need to try to remove them
     // from both here.
     JSObject* obj = aCache->GetWrapperPreserveColor();
     if (aCache->IsDOMBinding() && obj) {
-      xpc::ObjectScope(obj)->RemoveDOMExpandoObject(obj);
+      xpc::GetObjectScope(obj)->RemoveDOMExpandoObject(obj);
     }
     DropJSObjects(aScriptObjectHolder);
 
     aCache->SetPreservingWrapper(false);
   }
 }
 
 // static
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -2039,19 +2039,20 @@ nsGlobalWindow::SetNewDocument(nsIDocume
 
         JS_SetParent(cx, mJSObject, newInnerWindow->mJSObject);
 
         rv = SetOuterObject(cx, mJSObject);
         NS_ENSURE_SUCCESS(rv, rv);
 
         NS_ASSERTION(!JS_IsExceptionPending(cx),
                      "We might overwrite a pending exception!");
-        XPCWrappedNativeScope* scope = xpc::ObjectScope(mJSObject);
-        if (scope->mWaiverWrapperMap)
+        XPCWrappedNativeScope* scope = xpc::GetObjectScope(mJSObject);
+        if (scope->mWaiverWrapperMap) {
           scope->mWaiverWrapperMap->Reparent(cx, newInnerWindow->mJSObject);
+        }
       }
     }
 
     // Enter the new global's compartment.
     JSAutoCompartment ac(cx, mJSObject);
 
     // If we created a new inner window above, we need to do the last little bit
     // of initialization now that the dust has settled.
--- a/dom/bindings/Codegen.py
+++ b/dom/bindings/Codegen.py
@@ -1419,17 +1419,17 @@ def CheckPref(descriptor, globalName, va
        wrapperCache = "      %s->ClearIsDOMBinding();\n" % (wrapperCache)
     else:
         wrapperCache = ""
 
     failureCode = ("      %s = false;\n" +
                    "      return %s;") % (varName, retval)
     return """
   {
-    XPCWrappedNativeScope* scope = xpc::ObjectScope(%s);
+    XPCWrappedNativeScope* scope = xpc::GetObjectScope(%s);
     if (!scope) {
 %s
     }
 
     if (!scope->ExperimentalBindingsEnabled()) {
 %s%s
     }
   }
--- a/dom/bindings/DOMJSProxyHandler.cpp
+++ b/dom/bindings/DOMJSProxyHandler.cpp
@@ -56,17 +56,17 @@ DOMProxyHandler::EnsureExpandoObject(JSC
   JSObject* expando = GetExpandoObject(obj);
   if (!expando) {
     expando = JS_NewObjectWithGivenProto(cx, nullptr, nullptr,
                                          js::GetObjectParent(obj));
     if (!expando) {
       return NULL;
     }
 
-    XPCWrappedNativeScope* scope = xpc::ObjectScope(obj);
+    XPCWrappedNativeScope* scope = xpc::GetObjectScope(obj);
     if (!scope->RegisterDOMExpandoObject(obj)) {
       return NULL;
     }
 
     nsWrapperCache* cache;
     CallQueryInterface(UnwrapDOMObject<nsISupports>(obj, eProxyDOMObject), &cache);
     cache->SetPreservingWrapper(true);
 
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -2466,17 +2466,17 @@ nsXPCComponents_Constructor::CallOrConst
     // get the various other object pointers we need
 
     XPCCallContext ccx(JS_CALLER, cx);
     if (!ccx.IsValid())
         return ThrowAndFail(NS_ERROR_XPC_UNEXPECTED, cx, _retval);
 
     nsXPConnect* xpc = ccx.GetXPConnect();
     XPCContext* xpcc = ccx.GetXPCContext();
-    XPCWrappedNativeScope* scope = ObjectScope(obj);
+    XPCWrappedNativeScope* scope = GetObjectScope(obj);
     nsXPCComponents* comp;
 
     if (!xpc || !xpcc || !scope || !(comp = scope->GetComponents()))
         return ThrowAndFail(NS_ERROR_XPC_UNEXPECTED, cx, _retval);
 
     // Do the security check if necessary
 
     nsIXPCSecurityManager* sm =
@@ -3255,17 +3255,16 @@ xpc_CreateSandboxObject(JSContext *cx, j
 
     nsIPrincipal *principal = sop->GetPrincipal();
 
     JSObject *sandbox;
 
     sandbox = xpc::CreateGlobalObject(cx, &SandboxClass, principal);
     if (!sandbox)
         return NS_ERROR_FAILURE;
-    NS_ENSURE_SUCCESS(rv, rv);
     xpc::GetCompartmentPrivate(sandbox)->wantXrays = options.wantXrays;
 
     JS::AutoObjectRooter tvr(cx, sandbox);
 
     {
         JSAutoCompartment ac(cx, sandbox);
 
         if (options.proto) {
@@ -3304,17 +3303,17 @@ xpc_CreateSandboxObject(JSContext *cx, j
 
         XPCCallContext ccx(NATIVE_CALLER, cx);
         if (!ccx.IsValid())
             return NS_ERROR_XPC_UNEXPECTED;
 
         {
           JSAutoCompartment ac(ccx, sandbox);
           if (options.wantComponents &&
-              !nsXPCComponents::AttachComponentsObject(ccx, ObjectScope(sandbox)))
+              !nsXPCComponents::AttachComponentsObject(ccx, GetObjectScope(sandbox)))
               return NS_ERROR_XPC_UNEXPECTED;
 
           if (!XPCNativeWrapper::AttachNewConstructorObject(ccx, sandbox))
               return NS_ERROR_XPC_UNEXPECTED;
         }
 
         if (!JS_DefineFunctions(cx, sandbox, SandboxFunctions))
             return NS_ERROR_XPC_UNEXPECTED;
@@ -4297,17 +4296,17 @@ nsXPCComponents_Utils::RecomputeWrappers
 /* jsval getComponentsForScope(jsval vscope); */
 NS_IMETHODIMP
 nsXPCComponents_Utils::GetComponentsForScope(const jsval &vscope, JSContext *cx,
                                              jsval *rval)
 {
     if (!vscope.isObject())
         return NS_ERROR_INVALID_ARG;
     JSObject *scopeObj = js::UnwrapObject(&vscope.toObject());
-    XPCWrappedNativeScope *scope = ObjectScope(scopeObj);
+    XPCWrappedNativeScope *scope = GetObjectScope(scopeObj);
     XPCCallContext ccx(NATIVE_CALLER, cx);
     JSObject *components = scope->GetComponentsJSObject(ccx);
     if (!components)
         return NS_ERROR_FAILURE;
     *rval = ObjectValue(*components);
     if (!JS_WrapValue(cx, rval))
         return NS_ERROR_FAILURE;
     return NS_OK;
--- a/js/xpconnect/src/XPCConvert.cpp
+++ b/js/xpconnect/src/XPCConvert.cpp
@@ -807,17 +807,17 @@ XPCConvert::NativeInterface2JSObject(XPC
     // (that means an XPCWrappedNative around an nsXPCWrappedJS). This isn't
     // optimal -- we could detect this and roll the functionality into a
     // single wrapper, but the current solution is good enough for now.
     JSContext* cx = lccx.GetJSContext();
     NS_ABORT_IF_FALSE(js::IsObjectInContextCompartment(lccx.GetScopeForNewJSObjects(), cx),
                       "bad scope for new JSObjects");
 
     JSObject *jsscope = lccx.GetScopeForNewJSObjects();
-    XPCWrappedNativeScope* xpcscope = ObjectScope(jsscope);
+    XPCWrappedNativeScope* xpcscope = GetObjectScope(jsscope);
     if (!xpcscope)
         return false;
 
     // First, see if this object supports the wrapper cache.
     // Note: If |cache->IsProxy()| is true, then it means that the object
     // implementing it doesn't want a wrapped native as its JS Object, but
     // instead it provides its own proxy object. In that case, the object
     // to use is found as cache->GetWrapper(). If that is null, then the
--- a/js/xpconnect/src/XPCWrappedJSClass.cpp
+++ b/js/xpconnect/src/XPCWrappedJSClass.cpp
@@ -502,17 +502,17 @@ GetContextFromObject(JSObject *obj)
         return nullptr;
 
     // In order to get a context, we need a context.
     XPCCallContext ccx(NATIVE_CALLER);
     if (!ccx.IsValid())
         return nullptr;
 
     JSAutoCompartment ac(ccx, obj);
-    XPCWrappedNativeScope* scope = ObjectScope(obj);
+    XPCWrappedNativeScope* scope = GetObjectScope(obj);
     XPCContext *xpcc = scope->GetContext();
 
     if (xpcc) {
         JSContext *cx = xpcc->GetJSContext();
         JS_AbortIfWrongThread(JS_GetRuntime(cx));
         return cx;
     }
 
--- a/js/xpconnect/src/XPCWrappedNative.cpp
+++ b/js/xpconnect/src/XPCWrappedNative.cpp
@@ -307,17 +307,17 @@ XPCWrappedNative::WrapNewGlobal(XPCCallC
         si(ccx, XPCNativeScriptableInfo::Construct(ccx, &sciWrapper));
     MOZ_ASSERT(si.get());
 
     // Finally, we get to the JSClass.
     JSClass *clasp = si->GetJSClass();
     MOZ_ASSERT(clasp->flags & JSCLASS_IS_GLOBAL);
 
     // Create the global.
-    JSObject *global =  xpc::CreateGlobalObject(ccx, clasp, principal);
+    JSObject *global = xpc::CreateGlobalObject(ccx, clasp, principal);
     if (!global)
         return NS_ERROR_FAILURE;
     XPCWrappedNativeScope *scope = GetCompartmentPrivate(global)->scope;
 
     // Immediately enter the global's compartment, so that everything else we
     // create ends up there.
     JSAutoCompartment ac(ccx, global);
 
@@ -520,17 +520,17 @@ XPCWrappedNative::GetNewOrUsed(XPCCallCo
         rv = NS_OK;
 
         NS_ASSERTION(!xpc::WrapperFactory::IsXrayWrapper(parent),
                      "Xray wrapper being used to parent XPCWrappedNative?");
 
         ac.construct(ccx, parent);
 
         if (parent != plannedParent) {
-            XPCWrappedNativeScope* betterScope = ObjectScope(parent);
+            XPCWrappedNativeScope* betterScope = GetObjectScope(parent);
             if (betterScope != Scope)
                 return GetNewOrUsed(ccx, helper, betterScope, Interface, resultWrapper);
 
             newParentVal = OBJECT_TO_JSVAL(parent);
         }
 
         // Take the performance hit of checking the hashtable again in case
         // the preCreate call caused the wrapper to get created through some
@@ -1782,17 +1782,18 @@ XPCWrappedNative::RescueOrphans(XPCCallC
     // been orphaned. If not, we have nothing to do.
     if (!IsOrphan())
         return NS_OK;
 
     // We've been orphaned. Find where our parent went, and follow it.
     JSObject *parentGhost = js::GetObjectParent(mFlatJSObject);
     JSObject *realParent = js::UnwrapObject(parentGhost);
     nsRefPtr<XPCWrappedNative> ignored;
-    return ReparentWrapperIfFound(ccx, ObjectScope(parentGhost), ObjectScope(realParent),
+    return ReparentWrapperIfFound(ccx, GetObjectScope(parentGhost),
+                                  GetObjectScope(realParent),
                                   realParent, mIdentity, getter_AddRefs(ignored));
 }
 
 #define IS_TEAROFF_CLASS(clazz)                                               \
           ((clazz) == &XPC_WN_Tearoff_JSClass)
 
 // static
 XPCWrappedNative*
@@ -3806,17 +3807,17 @@ ConstructSlimWrapper(XPCCallContext &ccx
         SLIM_LOG_NOT_CREATED(ccx, identityObj, "wrong compartment");
 
         return false;
     }
 
     JSAutoCompartment ac(ccx, parent);
 
     if (parent != plannedParent) {
-        XPCWrappedNativeScope *newXpcScope = ObjectScope(parent);
+        XPCWrappedNativeScope *newXpcScope = GetObjectScope(parent);
         if (newXpcScope != xpcScope) {
             SLIM_LOG_NOT_CREATED(ccx, identityObj, "crossing origins");
 
             return false;
         }
     }
 
     // The PreCreate hook could have forced the creation of a wrapper, need
--- a/js/xpconnect/src/XPCWrappedNativeScope.cpp
+++ b/js/xpconnect/src/XPCWrappedNativeScope.cpp
@@ -79,17 +79,17 @@ static void DEBUG_TrackScopeShutdown()
 
 XPCWrappedNativeScope* XPCWrappedNativeScope::gScopes = nullptr;
 XPCWrappedNativeScope* XPCWrappedNativeScope::gDyingScopes = nullptr;
 
 // static
 XPCWrappedNativeScope*
 XPCWrappedNativeScope::GetNewOrUsed(JSContext *cx, JSObject* aGlobal)
 {
-    XPCWrappedNativeScope* scope = ObjectScope(aGlobal);
+    XPCWrappedNativeScope* scope = GetObjectScope(aGlobal);
     if (!scope) {
         scope = new XPCWrappedNativeScope(cx, aGlobal);
     } else {
         // We need to call SetGlobal in order to clear mPrototypeNoHelper (so we
         // get a new new one if requested in the new scope) in the case where
         // the global object is being reused (JS_SetAllNonReservedSlotsToUndefined
         // has been called). NOTE: We are only called by nsXPConnect::InitClasses.
         scope->SetGlobal(cx, aGlobal);
--- a/js/xpconnect/src/nsXPConnect.cpp
+++ b/js/xpconnect/src/nsXPConnect.cpp
@@ -1022,17 +1022,17 @@ TraceXPCGlobal(JSTracer *trc, JSObject *
     if (trc->callback == VerifyTraceXPCGlobalCalled) {
         // We don't do anything here, we only want to verify that TraceXPCGlobal
         // was called.
         reinterpret_cast<VerifyTraceXPCGlobalCalledTracer*>(trc)->ok = true;
         return;
     }
 #endif
 
-    if (XPCWrappedNativeScope *scope = ObjectScope(obj))
+    if (XPCWrappedNativeScope *scope = GetObjectScope(obj))
         scope->TraceDOMPrototypes(trc);
 
     if (js::GetObjectClass(obj)->flags & JSCLASS_DOM_GLOBAL)
         mozilla::dom::TraceProtoOrIfaceCache(trc, obj);
 }
 
 #ifdef DEBUG
 #include "mozilla/Preferences.h"
@@ -1441,17 +1441,17 @@ nsXPConnect::GetWrappedNativeOfNativeObj
     NS_ASSERTION(_retval, "bad param");
 
     *_retval = nullptr;
 
     XPCCallContext ccx(NATIVE_CALLER, aJSContext);
     if (!ccx.IsValid())
         return UnexpectedFailure(NS_ERROR_FAILURE);
 
-    XPCWrappedNativeScope* scope = ObjectScope(aScope);
+    XPCWrappedNativeScope* scope = GetObjectScope(aScope);
     if (!scope)
         return UnexpectedFailure(NS_ERROR_FAILURE);
 
     AutoMarkingNativeInterfacePtr iface(ccx);
     iface = XPCNativeInterface::GetNewOrUsed(ccx, &aIID);
     if (!iface)
         return NS_ERROR_FAILURE;
 
@@ -1472,18 +1472,18 @@ nsXPConnect::ReparentWrappedNativeIfFoun
                                           JSObject * aNewParent,
                                           nsISupports *aCOMObj,
                                           nsIXPConnectJSObjectHolder **_retval)
 {
     XPCCallContext ccx(NATIVE_CALLER, aJSContext);
     if (!ccx.IsValid())
         return UnexpectedFailure(NS_ERROR_FAILURE);
 
-    XPCWrappedNativeScope* scope = ObjectScope(aScope);
-    XPCWrappedNativeScope* scope2 = ObjectScope(aNewParent);
+    XPCWrappedNativeScope* scope = GetObjectScope(aScope);
+    XPCWrappedNativeScope* scope2 = GetObjectScope(aNewParent);
     if (!scope || !scope2)
         return UnexpectedFailure(NS_ERROR_FAILURE);
 
     return XPCWrappedNative::
         ReparentWrapperIfFound(ccx, scope, scope2, aNewParent, aCOMObj,
                                (XPCWrappedNative**) _retval);
 }
 
@@ -1505,17 +1505,17 @@ MoveableWrapperFinder(JSDHashTable *tabl
 /* void rescueOrphansInScope(in JSContextPtr aJSContext, in JSObjectPtr  aScope); */
 NS_IMETHODIMP
 nsXPConnect::RescueOrphansInScope(JSContext *aJSContext, JSObject *aScope)
 {
     XPCCallContext ccx(NATIVE_CALLER, aJSContext);
     if (!ccx.IsValid())
         return UnexpectedFailure(NS_ERROR_FAILURE);
 
-    XPCWrappedNativeScope *scope = ObjectScope(aScope);
+    XPCWrappedNativeScope *scope = GetObjectScope(aScope);
     if (!scope)
         return UnexpectedFailure(NS_ERROR_FAILURE);
 
     // First, look through the old scope and find all of the wrappers that we
     // might need to rescue.
     nsTArray<nsRefPtr<XPCWrappedNative> > wrappersToMove;
 
     {   // scoped lock
@@ -1757,17 +1757,17 @@ nsXPConnect::GetWrappedNativePrototype(J
                                        nsIXPConnectJSObjectHolder **_retval)
 {
     XPCCallContext ccx(NATIVE_CALLER, aJSContext);
     if (!ccx.IsValid())
         return UnexpectedFailure(NS_ERROR_FAILURE);
 
     JSAutoCompartment ac(aJSContext, aScope);
 
-    XPCWrappedNativeScope* scope = ObjectScope(aScope);
+    XPCWrappedNativeScope* scope = GetObjectScope(aScope);
     if (!scope)
         return UnexpectedFailure(NS_ERROR_FAILURE);
 
     XPCNativeScriptableCreateInfo sciProto;
     XPCWrappedNative::GatherProtoScriptableCreateInfo(aClassInfo, sciProto);
 
     AutoMarkingWrappedNativeProtoPtr proto(ccx);
     proto = XPCWrappedNativeProto::GetNewOrUsed(ccx, scope, aClassInfo, &sciProto);
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -4381,18 +4381,20 @@ inline bool EnableUniversalXPConnect(JSC
     priv->universalXPConnectEnabled = true;
 
     // Recompute all the cross-compartment wrappers leaving the newly-privileged
     // compartment.
     return js::RecomputeWrappers(cx, js::SingleCompartment(compartment),
                                  js::AllCompartments());
 }
 
+// This returns null if and only if it is called on an object in a non-XPConnect
+// compartment.
 inline XPCWrappedNativeScope*
-ObjectScope(JSObject *obj)
+GetObjectScope(JSObject *obj)
 {
     return EnsureCompartmentPrivate(obj)->scope;
 }
 }
 
 /***************************************************************************/
 // Inlines use the above - include last.
 
--- a/js/xpconnect/wrappers/WrapperFactory.cpp
+++ b/js/xpconnect/wrappers/WrapperFactory.cpp
@@ -54,31 +54,31 @@ GetCurrentOuter(JSContext *cx, JSObject 
 }
 
 JSObject *
 WrapperFactory::GetXrayWaiver(JSObject *obj)
 {
     // Object should come fully unwrapped but outerized.
     MOZ_ASSERT(obj == UnwrapObject(obj));
     MOZ_ASSERT(!js::GetObjectClass(obj)->ext.outerObject);
-    XPCWrappedNativeScope *scope = ObjectScope(obj);
+    XPCWrappedNativeScope *scope = GetObjectScope(obj);
     MOZ_ASSERT(scope);
 
     if (!scope->mWaiverWrapperMap)
         return NULL;
     return xpc_UnmarkGrayObject(scope->mWaiverWrapperMap->Find(obj));
 }
 
 JSObject *
 WrapperFactory::CreateXrayWaiver(JSContext *cx, JSObject *obj)
 {
     // The caller is required to have already done a lookup.
     // NB: This implictly performs the assertions of GetXrayWaiver.
     MOZ_ASSERT(!GetXrayWaiver(obj));
-    XPCWrappedNativeScope *scope = ObjectScope(obj);
+    XPCWrappedNativeScope *scope = GetObjectScope(obj);
 
     // Get a waiver for the proto.
     JSObject *proto;
     if (!js::GetObjectProto(cx, obj, &proto))
         return nullptr;
     if (proto && !(proto = WaiveXray(cx, proto)))
         return nullptr;
 
@@ -601,17 +601,17 @@ FixWaiverAfterTransplant(JSContext *cx, 
     // Update all the cross-compartment references to oldWaiver to point to
     // newWaiver.
     if (!js::RemapAllWrappersForObject(cx, oldWaiver, newWaiver))
         return false;
 
     // There should be no same-compartment references to oldWaiver, and we
     // just remapped all cross-compartment references. It's dead, so we can
     // remove it from the map.
-    XPCWrappedNativeScope *scope = ObjectScope(oldWaiver);
+    XPCWrappedNativeScope *scope = GetObjectScope(oldWaiver);
     JSObject *key = Wrapper::wrappedObject(oldWaiver);
     MOZ_ASSERT(scope->mWaiverWrapperMap->Find(key));
     scope->mWaiverWrapperMap->Remove(key);
     return true;
 }
 
 JSObject *
 TransplantObject(JSContext *cx, JSObject *origobj, JSObject *target)