Bug 903891 - Default the iid to nsISupports in NativeInterface2JSObject, and assert |Interface| when getting an XPCWrappedNative. r=mrbkap
authorBobby Holley <bobbyholley@gmail.com>
Thu, 22 Aug 2013 15:51:35 -0700
changeset 143979 49c8f6b3f06237017871f76c8fd8a7e42fae063d
parent 143978 d1d616c2ef4e80a919cc9e9a9f26bfaa480c2d67
child 143980 5f764ef54601b0086d04491d440d375cbe95f217
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmrbkap
bugs903891
milestone26.0a1
Bug 903891 - Default the iid to nsISupports in NativeInterface2JSObject, and assert |Interface| when getting an XPCWrappedNative. r=mrbkap
js/xpconnect/src/XPCConvert.cpp
js/xpconnect/src/XPCWrappedNative.cpp
--- a/js/xpconnect/src/XPCConvert.cpp
+++ b/js/xpconnect/src/XPCConvert.cpp
@@ -796,18 +796,19 @@ bool
 XPCConvert::NativeInterface2JSObject(jsval* d,
                                      nsIXPConnectJSObjectHolder** dest,
                                      xpcObjectHelper& aHelper,
                                      const nsID* iid,
                                      XPCNativeInterface** Interface,
                                      bool allowNativeWrapper,
                                      nsresult* pErr)
 {
-    MOZ_ASSERT(!Interface || iid,
-               "Need the iid if you pass in an XPCNativeInterface cache.");
+    MOZ_ASSERT_IF(Interface, iid);
+    if (!iid)
+        iid = &NS_GET_IID(nsISupports);
 
     *d = JSVAL_NULL;
     if (dest)
         *dest = nullptr;
     if (!aHelper.Object())
         return true;
     if (pErr)
         *pErr = NS_ERROR_XPC_BAD_CONVERT_NATIVE;
--- a/js/xpconnect/src/XPCWrappedNative.cpp
+++ b/js/xpconnect/src/XPCWrappedNative.cpp
@@ -418,16 +418,17 @@ XPCWrappedNative::WrapNewGlobal(xpcObjec
 
 // static
 nsresult
 XPCWrappedNative::GetNewOrUsed(xpcObjectHelper& helper,
                                XPCWrappedNativeScope* Scope,
                                XPCNativeInterface* Interface,
                                XPCWrappedNative** resultWrapper)
 {
+    MOZ_ASSERT(Interface);
     AutoJSContext cx;
     nsWrapperCache *cache = helper.GetWrapperCache();
 
     MOZ_ASSERT(!cache || !cache->GetWrapperPreserveColor(),
                "We assume the caller already checked if it could get the "
                "wrapper from the cache.");
 
     nsresult rv;
@@ -451,18 +452,17 @@ XPCWrappedNative::GetNewOrUsed(xpcObject
     // ahead and check our map even if we have a cache and it has no existing
     // wrapper: we might have an XPCWrappedNative anyway.
     {   // scoped lock
         XPCAutoLock lock(mapLock);
         wrapper = map->Find(identity);
     }
 
     if (wrapper) {
-        if (Interface &&
-            !wrapper->FindTearOff(Interface, false, &rv)) {
+        if (!wrapper->FindTearOff(Interface, false, &rv)) {
             MOZ_ASSERT(NS_FAILED(rv), "returning NS_OK on failure");
             return rv;
         }
         *resultWrapper = wrapper.forget().get();
         return NS_OK;
     }
 
     // There is a chance that the object wants to have the self-same JSObject
@@ -472,18 +472,17 @@ XPCWrappedNative::GetNewOrUsed(xpcObject
     //
     // To handle this we need to get the scriptable helper early and ask it.
     // It is possible that we will then end up forwarding this entire call
     // to this same function but with a different scope.
 
     // If we are making a wrapper for the nsIClassInfo interface then
     // We *don't* want to have it use the prototype meant for instances
     // of that class.
-    bool iidIsClassInfo = Interface &&
-                          Interface->GetIID()->Equals(NS_GET_IID(nsIClassInfo));
+    bool iidIsClassInfo = Interface->GetIID()->Equals(NS_GET_IID(nsIClassInfo));
     uint32_t classInfoFlags;
     bool isClassInfoSingleton = helper.GetClassInfo() == helper.Object() &&
                                 NS_SUCCEEDED(helper.GetClassInfo()
                                                    ->GetFlags(&classInfoFlags)) &&
                                 (classInfoFlags & nsIClassInfo::SINGLETON_CLASSINFO);
     bool isClassInfo = iidIsClassInfo || isClassInfoSingleton;
 
     nsIClassInfo *info = helper.GetClassInfo();
@@ -547,17 +546,17 @@ XPCWrappedNative::GetNewOrUsed(xpcObject
                 wrapper = XPCWrappedNative::Get(cached);
         } else {
             // scoped lock
             XPCAutoLock lock(mapLock);
             wrapper = map->Find(identity);
         }
 
         if (wrapper) {
-            if (Interface && !wrapper->FindTearOff(Interface, false, &rv)) {
+            if (wrapper->FindTearOff(Interface, false, &rv)) {
                 MOZ_ASSERT(NS_FAILED(rv), "returning NS_OK on failure");
                 return rv;
             }
             *resultWrapper = wrapper.forget().get();
             return NS_OK;
         }
     } else {
         ac.construct(static_cast<JSContext*>(cx), parent);
@@ -610,17 +609,17 @@ XPCWrappedNative::GetNewOrUsed(xpcObject
     // after we have Init'd the wrapper but *before* we add it to the hashtable.
     // This would cause the mSet to get collected and we'd later crash. I've
     // *seen* this happen.
     AutoMarkingWrappedNativePtr wrapperMarker(cx, wrapper);
 
     if (!wrapper->Init(parent, &sciWrapper))
         return NS_ERROR_FAILURE;
 
-    if (Interface && !wrapper->FindTearOff(Interface, false, &rv)) {
+    if (!wrapper->FindTearOff(Interface, false, &rv)) {
         MOZ_ASSERT(NS_FAILED(rv), "returning NS_OK on failure");
         return rv;
     }
 
     if (needsSOW)
         wrapper->SetNeedsSOW();
     if (needsCOW)
         wrapper->SetNeedsCOW();
@@ -719,16 +718,17 @@ FinishCreate(XPCWrappedNativeScope* Scop
 nsresult
 XPCWrappedNative::GetUsedOnly(nsISupports* Object,
                               XPCWrappedNativeScope* Scope,
                               XPCNativeInterface* Interface,
                               XPCWrappedNative** resultWrapper)
 {
     AutoJSContext cx;
     MOZ_ASSERT(Object, "XPCWrappedNative::GetUsedOnly was called with a null Object");
+    MOZ_ASSERT(Interface);
 
     nsRefPtr<XPCWrappedNative> wrapper;
     nsWrapperCache* cache = nullptr;
     CallQueryInterface(Object, &cache);
     if (cache) {
         RootedObject flat(cx, cache->GetWrapper());
         if (!flat) {
             *resultWrapper = nullptr;
@@ -751,17 +751,17 @@ XPCWrappedNative::GetUsedOnly(nsISupport
             if (!wrapper) {
                 *resultWrapper = nullptr;
                 return NS_OK;
             }
         }
     }
 
     nsresult rv;
-    if (Interface && !wrapper->FindTearOff(Interface, false, &rv)) {
+    if (!wrapper->FindTearOff(Interface, false, &rv)) {
         MOZ_ASSERT(NS_FAILED(rv), "returning NS_OK on failure");
         return rv;
     }
 
     wrapper.forget(resultWrapper);
     return NS_OK;
 }