Bug 792036 - Factor out glue code to get the JS Components object for a scope. r=mrbkap

This commit is contained in:
Bobby Holley 2012-09-24 14:46:27 +02:00
Родитель 0cc9deb20c
Коммит 93412f5166
3 изменённых файлов: 32 добавлений и 39 удалений

Просмотреть файл

@ -4764,41 +4764,18 @@ nsXPCComponents::AttachComponentsObject(XPCCallContext& ccx,
XPCWrappedNativeScope* aScope,
JSObject* aTarget)
{
JSObject *components = aScope->GetComponentsJSObject(ccx);
if (!components)
return false;
JSObject *global = aScope->GetGlobalJSObject();
MOZ_ASSERT(js::IsObjectInContextCompartment(global, ccx));
if (!aTarget)
aTarget = global;
nsXPCComponents* components = aScope->GetComponents();
if (!components) {
components = new nsXPCComponents(aScope);
if (!components)
return false;
aScope->SetComponents(components);
}
nsCOMPtr<nsIXPCComponents> cholder(components);
AutoMarkingNativeInterfacePtr iface(ccx);
iface = XPCNativeInterface::GetNewOrUsed(ccx, &NS_GET_IID(nsIXPCComponents));
if (!iface)
return false;
nsCOMPtr<XPCWrappedNative> wrapper;
xpcObjectHelper helper(cholder);
XPCWrappedNative::GetNewOrUsed(ccx, helper, aScope, iface, getter_AddRefs(wrapper));
if (!wrapper)
return false;
// The call to wrap() here is necessary even though the object is same-
// compartment, because it applies our security wrapper.
js::Value v = ObjectValue(*wrapper->GetFlatJSObject());
if (!JS_WrapValue(ccx, &v))
return false;
jsid id = ccx.GetRuntime()->GetStringID(XPCJSRuntime::IDX_COMPONENTS);
return JS_DefinePropertyById(ccx, aTarget, id, v, nullptr, nullptr,
return JS_DefinePropertyById(ccx, aTarget, id, js::ObjectValue(*components),
nullptr, nullptr,
JSPROP_PERMANENT | JSPROP_READONLY);
}

Просмотреть файл

@ -153,16 +153,30 @@ XPCWrappedNativeScope::IsDyingScope(XPCWrappedNativeScope *scope)
return false;
}
void
XPCWrappedNativeScope::SetComponents(nsXPCComponents* aComponents)
JSObject*
XPCWrappedNativeScope::GetComponentsJSObject(XPCCallContext& ccx)
{
mComponents = aComponents;
}
if (!mComponents)
mComponents = new nsXPCComponents(this);
nsXPCComponents*
XPCWrappedNativeScope::GetComponents()
{
return mComponents;
AutoMarkingNativeInterfacePtr iface(ccx);
iface = XPCNativeInterface::GetNewOrUsed(ccx, &NS_GET_IID(nsIXPCComponents));
if (!iface)
return nullptr;
nsCOMPtr<nsIXPCComponents> cholder(mComponents);
xpcObjectHelper helper(cholder);
nsCOMPtr<XPCWrappedNative> wrapper;
XPCWrappedNative::GetNewOrUsed(ccx, helper, this, iface, getter_AddRefs(wrapper));
if (!wrapper)
return nullptr;
// The call to wrap() here is necessary even though the object is same-
// compartment, because it applies our security wrapper.
JSObject *obj = wrapper->GetFlatJSObject();
if (!JS_WrapObject(ccx, &obj))
return nullptr;
return obj;
}
// Dummy JS class to let wrappers w/o an xpc prototype share

Просмотреть файл

@ -1586,6 +1586,10 @@ public:
nsXPCComponents*
GetComponents() const {return mComponents;}
// Returns the JS object reflection of the Components object.
JSObject*
GetComponentsJSObject(XPCCallContext& ccx);
JSObject*
GetGlobalJSObject() const
{return xpc_UnmarkGrayObject(mGlobalJSObject);}
@ -1682,8 +1686,6 @@ public:
static JSBool
IsDyingScope(XPCWrappedNativeScope *scope);
void SetComponents(nsXPCComponents* aComponents);
nsXPCComponents *GetComponents();
void SetGlobal(XPCCallContext& ccx, JSObject* aGlobal, nsISupports* aNative);
static void InitStatics() { gScopes = nullptr; gDyingScopes = nullptr; }