Bug 1065452 - Add an API to access the associated window of addon scopes. r=billm

This commit is contained in:
Bobby Holley 2014-09-15 14:12:12 +02:00
parent ba247c6cd4
commit c0c7f8a06e
4 changed files with 38 additions and 0 deletions

View File

@ -395,6 +395,14 @@ js::GetGlobalForObjectCrossCompartment(JSObject *obj)
return &obj->global();
}
JS_FRIEND_API(JSObject *)
js::GetPrototypeNoProxy(JSObject *obj)
{
JS_ASSERT(!obj->is<js::ProxyObject>());
JS_ASSERT(!obj->getTaggedProto().isLazy());
return obj->getTaggedProto().toObjectOrNull();
}
JS_FRIEND_API(void)
js::SetPendingExceptionCrossContext(JSContext *cx, JS::HandleValue v)
{

View File

@ -706,6 +706,9 @@ GetObjectParentMaybeScope(JSObject *obj);
JS_FRIEND_API(JSObject *)
GetGlobalForObjectCrossCompartment(JSObject *obj);
JS_FRIEND_API(JSObject *)
GetPrototypeNoProxy(JSObject *obj);
// Sidestep the activeContext checking implicitly performed in
// JS_SetPendingException.
JS_FRIEND_API(void)

View File

@ -575,6 +575,25 @@ WindowGlobalOrNull(JSObject *aObj)
return WindowOrNull(glob);
}
nsGlobalWindow*
AddonWindowOrNull(JSObject *aObj)
{
if (!IsInAddonScope(aObj))
return nullptr;
JSObject *global = js::GetGlobalForObjectCrossCompartment(aObj);
JSObject *proto = js::GetPrototypeNoProxy(global);
// Addons could theoretically change the prototype of the addon scope, but
// we pretty much just want to crash if that happens so that we find out
// about it and get them to change their code.
MOZ_RELEASE_ASSERT(js::IsCrossCompartmentWrapper(proto));
JSObject *mainGlobal = js::UncheckedUnwrap(proto, /* stopAtOuter = */ false);
MOZ_RELEASE_ASSERT(JS_IsGlobalObject(mainGlobal));
return WindowOrNull(mainGlobal);
}
}
static void

View File

@ -472,6 +472,14 @@ WindowOrNull(JSObject *aObj);
nsGlobalWindow*
WindowGlobalOrNull(JSObject *aObj);
/**
* If |aObj| is in an addon scope and that addon scope is associated with a
* live DOM Window, returns the associated nsGlobalWindow. Otherwise, returns
* null.
*/
nsGlobalWindow*
AddonWindowOrNull(JSObject *aObj);
// Error reporter used when there is no associated DOM window on to which to
// report errors and warnings.
//