Bug 884951 - Use XPCWrappedNative::Get more; r=bholley
authorMs2ger <ms2ger@gmail.com>
Sun, 23 Jun 2013 09:15:55 +0200
changeset 143275 2599d05cd405953dccabbcb7e03c2ac49bf19c8d
parent 143274 6fe13dc22c5697b72d66ee48ad692d39c350f5ee
child 143276 f899dba1c97bdd72d93154082fb9c97959f706f8
push idunknown
push userunknown
push dateunknown
reviewersbholley
bugs884951
milestone24.0a1
Bug 884951 - Use XPCWrappedNative::Get more; r=bholley
js/xpconnect/src/XPCCallContext.cpp
js/xpconnect/src/XPCConvert.cpp
js/xpconnect/src/XPCQuickStubs.cpp
js/xpconnect/src/XPCWrappedNative.cpp
js/xpconnect/src/XPCWrappedNativeJSOps.cpp
js/xpconnect/src/nsXPConnect.cpp
js/xpconnect/wrappers/AccessCheck.cpp
js/xpconnect/wrappers/WrapperFactory.cpp
js/xpconnect/wrappers/XrayWrapper.cpp
--- a/js/xpconnect/src/XPCCallContext.cpp
+++ b/js/xpconnect/src/XPCCallContext.cpp
@@ -392,17 +392,17 @@ XPCCallContext::UnwrapThisIfAllowed(Hand
     // here, potentially an outer window proxy, and then an XPCWN.
     MOZ_ASSERT(js::IsWrapper(obj));
     RootedObject unwrapped(mJSContext, js::UncheckedUnwrap(obj, /* stopAtOuter = */ false));
     MOZ_ASSERT(unwrapped == JS_ObjectToInnerObject(mJSContext, js::Wrapper::wrappedObject(obj)));
 
     // Make sure we have an XPCWN, and grab it.
     if (!IS_WN_REFLECTOR(unwrapped))
         return nullptr;
-    XPCWrappedNative *wn = (XPCWrappedNative*)js::GetObjectPrivate(unwrapped);
+    XPCWrappedNative *wn = XPCWrappedNative::Get(unwrapped);
 
     // Next, get the call info off the function object.
     XPCNativeInterface *interface;
     XPCNativeMember *member;
     XPCNativeMember::GetCallInfo(fun, &interface, &member);
 
     // To be extra safe, make sure that the underlying native implements the
     // interface before unwrapping. Even if we didn't check this, we'd still
--- a/js/xpconnect/src/XPCConvert.cpp
+++ b/js/xpconnect/src/XPCConvert.cpp
@@ -867,17 +867,17 @@ XPCConvert::NativeInterface2JSObject(jsv
     if (!flat) {
         rv = XPCWrappedNative::GetNewOrUsed(aHelper, xpcscope, iface,
                                             getter_AddRefs(strongWrapper));
 
         wrapper = strongWrapper;
     } else {
         MOZ_ASSERT(IS_WN_REFLECTOR(flat));
 
-        wrapper = static_cast<XPCWrappedNative*>(xpc_GetJSPrivate(flat));
+        wrapper = XPCWrappedNative::Get(flat);
 
         // If asked to return the wrapper we'll return a strong reference,
         // otherwise we'll just return its JSObject in d (which should be
         // rooted in that case).
         if (dest)
             strongWrapper = wrapper;
         if (iface)
             wrapper->FindTearOff(iface, false, &rv);
--- a/js/xpconnect/src/XPCQuickStubs.cpp
+++ b/js/xpconnect/src/XPCQuickStubs.cpp
@@ -174,18 +174,17 @@ GetMemberInfo(JSObject *obj, jsid member
 {
     *ifaceName = "Unknown";
 
     // Don't try to generate a useful name if there are security wrappers,
     // because it isn't worth the risk of something going wrong just to generate
     // an error message. Instead, only handle the simple case where we have the
     // reflector in hand.
     if (IS_WN_REFLECTOR(obj)) {
-        XPCWrappedNative *wrapper =
-            static_cast<XPCWrappedNative *>(js::GetObjectPrivate(obj));
+        XPCWrappedNative *wrapper = XPCWrappedNative::Get(obj);
         XPCWrappedNativeProto *proto = wrapper->GetProto();
         if (proto) {
             XPCNativeSet *set = proto->GetSet();
             if (set) {
                 XPCNativeMember *member;
                 XPCNativeInterface *iface;
 
                 if (set->FindMember(memberId, &member, &iface))
@@ -611,17 +610,17 @@ castNativeFromWrapper(JSContext *cx,
                       nsresult *rv)
 {
     XPCWrappedNative *wrapper;
     XPCWrappedNativeTearOff *tearoff;
     JSObject *cur;
 
     if (IS_WN_REFLECTOR(obj)) {
         cur = obj;
-        wrapper = (XPCWrappedNative*)xpc_GetJSPrivate(obj);
+        wrapper = XPCWrappedNative::Get(obj);
         tearoff = nullptr;
     } else {
         *rv = getWrapper(cx, obj, &wrapper, &cur, &tearoff);
         if (NS_FAILED(*rv))
             return nullptr;
     }
 
     nsISupports *native;
--- a/js/xpconnect/src/XPCWrappedNative.cpp
+++ b/js/xpconnect/src/XPCWrappedNative.cpp
@@ -33,19 +33,17 @@ using namespace mozilla::dom;
 using namespace JS;
 
 bool
 xpc_OkToHandOutWrapper(nsWrapperCache *cache)
 {
     NS_ABORT_IF_FALSE(cache->GetWrapper(), "Must have wrapper");
     NS_ABORT_IF_FALSE(IS_WN_REFLECTOR(cache->GetWrapper()),
                       "Must have XPCWrappedNative wrapper");
-    return
-        !static_cast<XPCWrappedNative*>(xpc_GetJSPrivate(cache->GetWrapper()))->
-            NeedsSOW();
+    return !XPCWrappedNative::Get(cache->GetWrapper())->NeedsSOW();
 }
 
 /***************************************************************************/
 
 NS_IMETHODIMP
 NS_CYCLE_COLLECTION_CLASSNAME(XPCWrappedNative)::UnlinkImpl(void *p)
 {
     XPCWrappedNative *tmp = static_cast<XPCWrappedNative*>(p);
@@ -528,17 +526,17 @@ XPCWrappedNative::GetNewOrUsed(xpcObject
 
         // Take the performance hit of checking the hashtable again in case
         // the preCreate call caused the wrapper to get created through some
         // interesting path (the DOM code tends to make this happen sometimes).
 
         if (cache) {
             RootedObject cached(cx, cache->GetWrapper());
             if (cached)
-                wrapper = static_cast<XPCWrappedNative*>(xpc_GetJSPrivate(cached));
+                wrapper = XPCWrappedNative::Get(cached);
         } else {
             // scoped lock
             XPCAutoLock lock(mapLock);
             wrapper = map->Find(identity);
         }
 
         if (wrapper) {
             if (Interface && !wrapper->FindTearOff(Interface, false, &rv)) {
@@ -709,27 +707,26 @@ nsresult
 XPCWrappedNative::GetUsedOnly(nsISupports* Object,
                               XPCWrappedNativeScope* Scope,
                               XPCNativeInterface* Interface,
                               XPCWrappedNative** resultWrapper)
 {
     AutoJSContext cx;
     NS_ASSERTION(Object, "XPCWrappedNative::GetUsedOnly was called with a null Object");
 
-    XPCWrappedNative* wrapper;
+    nsRefPtr<XPCWrappedNative> wrapper;
     nsWrapperCache* cache = nullptr;
     CallQueryInterface(Object, &cache);
     if (cache) {
         RootedObject flat(cx, cache->GetWrapper());
         if (!flat) {
             *resultWrapper = nullptr;
             return NS_OK;
         }
-        wrapper = static_cast<XPCWrappedNative*>(xpc_GetJSPrivate(flat));
-        NS_ADDREF(wrapper);
+        wrapper = XPCWrappedNative::Get(flat);
     } else {
         nsCOMPtr<nsISupports> identity = do_QueryInterface(Object);
 
         if (!identity) {
             NS_ERROR("This XPCOM object fails in QueryInterface to nsISupports!");
             return NS_ERROR_FAILURE;
         }
 
@@ -737,28 +734,26 @@ XPCWrappedNative::GetUsedOnly(nsISupport
 
         {   // scoped lock
             XPCAutoLock lock(Scope->GetRuntime()->GetMapLock());
             wrapper = map->Find(identity);
             if (!wrapper) {
                 *resultWrapper = nullptr;
                 return NS_OK;
             }
-            NS_ADDREF(wrapper);
         }
     }
 
     nsresult rv;
     if (Interface && !wrapper->FindTearOff(Interface, false, &rv)) {
-        NS_RELEASE(wrapper);
         NS_ASSERTION(NS_FAILED(rv), "returning NS_OK on failure");
         return rv;
     }
 
-    *resultWrapper = wrapper;
+    wrapper.forget(resultWrapper);
     return NS_OK;
 }
 
 // This ctor is used if this object will have a proto.
 XPCWrappedNative::XPCWrappedNative(already_AddRefed<nsISupports> aIdentity,
                                    XPCWrappedNativeProto* aProto)
     : mMaybeProto(aProto),
       mSet(aProto->GetSet()),
@@ -1317,17 +1312,17 @@ XPCWrappedNative::ReparentWrapperIfFound
 
     nsRefPtr<XPCWrappedNative> wrapper;
     RootedObject flat(cx);
     nsWrapperCache* cache = nullptr;
     CallQueryInterface(aCOMObj, &cache);
     if (cache) {
         flat = cache->GetWrapper();
         if (flat) {
-            wrapper = static_cast<XPCWrappedNative*>(xpc_GetJSPrivate(flat));
+            wrapper = XPCWrappedNative::Get(flat);
             NS_ASSERTION(wrapper->GetScope() == aOldScope,
                          "Incorrect scope passed");
         }
     } else {
         rv = XPCWrappedNative::GetUsedOnly(aCOMObj, aOldScope, iface,
                                            getter_AddRefs(wrapper));
         if (NS_FAILED(rv))
             return rv;
--- a/js/xpconnect/src/XPCWrappedNativeJSOps.cpp
+++ b/js/xpconnect/src/XPCWrappedNativeJSOps.cpp
@@ -646,18 +646,17 @@ XPC_WN_NoHelper_Resolve(JSContext *cx, H
                                  JSPROP_PERMANENT, nullptr);
 }
 
 static JSObject *
 XPC_WN_OuterObject(JSContext *cx, HandleObject objArg)
 {
     JSObject *obj = objArg;
 
-    XPCWrappedNative *wrapper =
-        static_cast<XPCWrappedNative *>(js::GetObjectPrivate(obj));
+    XPCWrappedNative *wrapper = XPCWrappedNative::Get(obj);
     if (!wrapper) {
         Throw(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO, cx);
 
         return nullptr;
     }
 
     if (!wrapper->IsValid()) {
         Throw(NS_ERROR_XPC_HAS_BEEN_SHUTDOWN, cx);
--- a/js/xpconnect/src/nsXPConnect.cpp
+++ b/js/xpconnect/src/nsXPConnect.cpp
@@ -1304,18 +1304,17 @@ IsJSContextOnStack(JSContext *aCx)
 
 } // namespace xpc
 
 nsIPrincipal*
 nsXPConnect::GetPrincipal(JSObject* obj, bool allowShortCircuit) const
 {
     NS_ASSERTION(IS_WN_REFLECTOR(obj), "What kind of wrapper is this?");
 
-    XPCWrappedNative *xpcWrapper =
-        (XPCWrappedNative *)xpc_GetJSPrivate(obj);
+    XPCWrappedNative *xpcWrapper = XPCWrappedNative::Get(obj);
     if (xpcWrapper) {
         if (allowShortCircuit) {
             nsIPrincipal *result = xpcWrapper->GetObjectPrincipal();
             if (result) {
                 return result;
             }
         }
 
--- a/js/xpconnect/wrappers/AccessCheck.cpp
+++ b/js/xpconnect/wrappers/AccessCheck.cpp
@@ -270,17 +270,17 @@ AccessCheck::needsSystemOnlyWrapper(JSOb
 {
     JSObject* wrapper = obj;
     if (dom::GetSameCompartmentWrapperForDOMBinding(wrapper))
         return wrapper != obj;
 
     if (!IS_WN_REFLECTOR(obj))
         return false;
 
-    XPCWrappedNative *wn = static_cast<XPCWrappedNative *>(js::GetObjectPrivate(obj));
+    XPCWrappedNative *wn = XPCWrappedNative::Get(obj);
     return wn->NeedsSOW();
 }
 
 enum Access { READ = (1<<0), WRITE = (1<<1), NO_ACCESS = 0 };
 
 static void
 EnterAndThrow(JSContext *cx, JSObject *wrapper, const char *msg)
 {
--- a/js/xpconnect/wrappers/WrapperFactory.cpp
+++ b/js/xpconnect/wrappers/WrapperFactory.cpp
@@ -194,17 +194,17 @@ WrapperFactory::PrepareForWrapping(JSCon
     // Now, our object is ready to be wrapped, but several objects (notably
     // nsJSIIDs) have a wrapper per scope. If we are about to wrap one of
     // those objects in a security wrapper, then we need to hand back the
     // wrapper for the new scope instead. Also, global objects don't move
     // between scopes so for those we also want to return the wrapper. So...
     if (!IS_WN_REFLECTOR(obj) || !js::GetObjectParent(obj))
         return DoubleWrap(cx, obj, flags);
 
-    XPCWrappedNative *wn = static_cast<XPCWrappedNative *>(xpc_GetJSPrivate(obj));
+    XPCWrappedNative *wn = XPCWrappedNative::Get(obj);
 
     JSAutoCompartment ac(cx, obj);
     XPCCallContext ccx(JS_CALLER, cx, obj);
     RootedObject wrapScope(cx, scope);
 
     {
         if (NATIVE_HAS_FLAG(&ccx, WantPreCreate)) {
             // We have a precreate hook. This object might enforce that we only
@@ -301,17 +301,17 @@ WrapperFactory::PrepareForWrapping(JSCon
     // to a new (or possibly pre-existing) XPCWN in our compartment.
     // This could be a problem for chrome code that passes XPCOM objects
     // across compartments, because the effects of QI would disappear across
     // compartments.
     //
     // So whenever we pull an XPCWN across compartments in this manner, we
     // give the destination object the union of the two native sets. We try
     // to do this cleverly in the common case to avoid too much overhead.
-    XPCWrappedNative *newwn = static_cast<XPCWrappedNative *>(xpc_GetJSPrivate(obj));
+    XPCWrappedNative *newwn = XPCWrappedNative::Get(obj);
     XPCNativeSet *unionSet = XPCNativeSet::GetNewOrUsed(newwn->GetSet(),
                                                         wn->GetSet(), false);
     if (!unionSet)
         return nullptr;
     newwn->SetSet(unionSet);
 
     return DoubleWrap(cx, obj, flags);
 }
@@ -516,17 +516,17 @@ WrapperFactory::WrapForSameCompartment(J
     }
 
     MOZ_ASSERT(!dom::IsDOMObject(obj));
 
     if (!IS_WN_REFLECTOR(obj))
         return obj;
 
     // Extract the WN. It should exist.
-    XPCWrappedNative *wn = static_cast<XPCWrappedNative *>(xpc_GetJSPrivate(obj));
+    XPCWrappedNative *wn = XPCWrappedNative::Get(obj);
     MOZ_ASSERT(wn, "Trying to wrap a dead WN!");
 
     // The WN knows what to do.
     RootedObject wrapper(cx, wn->GetSameCompartmentSecurityWrapper(cx));
     MOZ_ASSERT_IF(wrapper != obj && IsComponentsObject(js::UncheckedUnwrap(obj)),
                   !Wrapper::wrapperHandler(wrapper)->isSafeToUnwrap());
     return wrapper;
 }
--- a/js/xpconnect/wrappers/XrayWrapper.cpp
+++ b/js/xpconnect/wrappers/XrayWrapper.cpp
@@ -34,18 +34,16 @@ using js::Wrapper;
 using js::IsCrossCompartmentWrapper;
 using js::UncheckedUnwrap;
 using js::CheckedUnwrap;
 
 namespace xpc {
 
 static const uint32_t JSSLOT_RESOLVING = 0;
 
-static XPCWrappedNative *GetWrappedNative(JSObject *obj);
-
 namespace XrayUtils {
 
 JSClass HolderClass = {
     "NativePropertyHolder",
     JSCLASS_HAS_RESERVED_SLOTS(2),
     JS_PropertyStub,        JS_DeletePropertyStub, holder_get,      holder_set,
     JS_EnumerateStub,       JS_ResolveStub,  JS_ConvertStub
 };
@@ -208,17 +206,17 @@ public:
 
     static bool isResolving(JSContext *cx, JSObject *holder, jsid id);
 
     static bool resolveDOMCollectionProperty(JSContext *cx, HandleObject wrapper,
                                              HandleObject holder, HandleId id,
                                              PropertyDescriptor *desc, unsigned flags);
 
     static XPCWrappedNative* getWN(JSObject *wrapper) {
-        return GetWrappedNative(getTargetObject(wrapper));
+        return XPCWrappedNative::Get(getTargetObject(wrapper));
     }
 
     virtual void preserveWrapper(JSObject *target);
 
     typedef ResolvingId ResolvingIdImpl;
 
     virtual JSObject* createHolder(JSContext *cx, JSObject *wrapper);
     virtual JSObject* getExpandoChain(JSObject *obj) {
@@ -501,23 +499,16 @@ bool CloneExpandoChain(JSContext *cx, JS
 }
 
 static JSObject *
 GetHolder(JSObject *obj)
 {
     return &js::GetProxyExtra(obj, 0).toObject();
 }
 
-static XPCWrappedNative *
-GetWrappedNative(JSObject *obj)
-{
-    MOZ_ASSERT(IS_WN_REFLECTOR(obj));
-    return static_cast<XPCWrappedNative *>(js::GetObjectPrivate(obj));
-}
-
 JSObject*
 XrayTraits::getHolder(JSObject *wrapper)
 {
     MOZ_ASSERT(WrapperFactory::IsXrayWrapper(wrapper));
     js::Value v = js::GetProxyExtra(wrapper, 0);
     return v.isObject() ? &v.toObject() : nullptr;
 }
 
@@ -726,18 +717,17 @@ mozMatchesSelectorStub(JSContext *cx, un
 
     JS_SET_RVAL(cx, vp, BOOLEAN_TO_JSVAL(ret));
     return true;
 }
 
 void
 XPCWrappedNativeXrayTraits::preserveWrapper(JSObject *target)
 {
-    XPCWrappedNative *wn =
-      static_cast<XPCWrappedNative *>(xpc_GetJSPrivate(target));
+    XPCWrappedNative *wn = XPCWrappedNative::Get(target);
     nsRefPtr<nsXPCClassInfo> ci;
     CallQueryInterface(wn->Native(), getter_AddRefs(ci));
     if (ci)
         ci->PreserveWrapper(wn->Native());
 }
 
 bool
 XPCWrappedNativeXrayTraits::resolveNativeProperty(JSContext *cx, HandleObject wrapper,
@@ -1936,17 +1926,17 @@ static nsQueryInterface
 do_QueryInterfaceNative(JSContext* cx, HandleObject wrapper)
 {
     nsISupports* nativeSupports;
     if (IsWrapper(wrapper) && WrapperFactory::IsXrayWrapper(wrapper)) {
         RootedObject target(cx, XrayTraits::getTargetObject(wrapper));
         if (GetXrayType(target) == XrayForDOMObject) {
             nativeSupports = UnwrapDOMObjectToISupports(target);
         } else {
-            XPCWrappedNative *wn = GetWrappedNative(target);
+            XPCWrappedNative *wn = XPCWrappedNative::Get(target);
             nativeSupports = wn->Native();
         }
     } else {
         nsIXPConnect *xpc = nsXPConnect::XPConnect();
         nativeSupports = xpc->GetNativeOfWrapper(cx, wrapper);
     }
 
     return nsQueryInterface(nativeSupports);