Bug 999297 - Implement xpc::UnwrapReflectorToISupports. r=bz

I kind of thought we already had something like this, but I couldn't find
anything.
This commit is contained in:
Bobby Holley 2014-05-01 11:03:11 -07:00
parent 2645da308a
commit 8ad042014e
3 changed files with 33 additions and 17 deletions

View File

@ -628,28 +628,34 @@ nsXPConnect::GetWrappedNativeOfJSObject(JSContext * aJSContext,
return NS_OK;
}
nsISupports*
xpc::UnwrapReflectorToISupports(JSObject *reflector)
{
// Unwrap security wrappers, if allowed.
reflector = js::CheckedUnwrap(reflector, /* stopAtOuter = */ false);
if (!reflector)
return nullptr;
// Try XPCWrappedNatives.
if (IS_WN_REFLECTOR(reflector)) {
XPCWrappedNative *wn = XPCWrappedNative::Get(reflector);
if (!wn)
return nullptr;
return wn->Native();
}
// Try DOM objects.
nsCOMPtr<nsISupports> canonical =
do_QueryInterface(mozilla::dom::UnwrapDOMObjectToISupports(reflector));
return canonical;
}
/* nsISupports getNativeOfWrapper(in JSContextPtr aJSContext, in JSObjectPtr aJSObj); */
NS_IMETHODIMP_(nsISupports*)
nsXPConnect::GetNativeOfWrapper(JSContext *aJSContext,
JSObject *aJSObj)
{
MOZ_ASSERT(aJSContext, "bad param");
MOZ_ASSERT(aJSObj, "bad param");
aJSObj = js::CheckedUnwrap(aJSObj, /* stopAtOuter = */ false);
if (!aJSObj) {
JS_ReportError(aJSContext, "Permission denied to get native of security wrapper");
return nullptr;
}
if (IS_WN_REFLECTOR(aJSObj)) {
if (XPCWrappedNative *wn = XPCWrappedNative::Get(aJSObj))
return wn->Native();
return nullptr;
}
nsCOMPtr<nsISupports> canonical =
do_QueryInterface(mozilla::dom::UnwrapDOMObjectToISupports(aJSObj));
return canonical;
return UnwrapReflectorToISupports(aJSObj);
}
/* nsIXPConnectWrappedNative getWrappedNativeOfNativeObject (in JSContextPtr aJSContext, in JSObjectPtr aScope, in nsISupports aCOMObj, in nsIIDRef aIID); */

View File

@ -2540,6 +2540,9 @@ public:
const nsID* iid,
nsISupports* aOuter,
nsresult* pErr);
// Note - This return the XPCWrappedNative, rather than the native itself,
// for the WN case. You probably want UnwrapReflectorToISupports.
static bool GetISupportsFromJSObject(JSObject* obj, nsISupports** iface);
/**

View File

@ -413,6 +413,13 @@ Throw(JSContext *cx, nsresult rv);
nsIGlobalObject *
GetNativeForGlobal(JSObject *global);
/**
* Returns the nsISupports native behind a given reflector (either DOM or
* XPCWN).
*/
nsISupports *
UnwrapReflectorToISupports(JSObject *reflector);
/**
* In some cases a native object does not really belong to any compartment (XBL,
* document created from by XHR of a worker, etc.). But when for some reason we