From 93412f5166d720f350af53c2419f9ed833e331b3 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Mon, 24 Sep 2012 14:46:27 +0200 Subject: [PATCH] Bug 792036 - Factor out glue code to get the JS Components object for a scope. r=mrbkap --- js/xpconnect/src/XPCComponents.cpp | 35 ++++------------------ js/xpconnect/src/XPCWrappedNativeScope.cpp | 30 ++++++++++++++----- js/xpconnect/src/xpcprivate.h | 6 ++-- 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/js/xpconnect/src/XPCComponents.cpp b/js/xpconnect/src/XPCComponents.cpp index 273d20bf7d96..720b24a3b03f 100644 --- a/js/xpconnect/src/XPCComponents.cpp +++ b/js/xpconnect/src/XPCComponents.cpp @@ -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 cholder(components); - - AutoMarkingNativeInterfacePtr iface(ccx); - iface = XPCNativeInterface::GetNewOrUsed(ccx, &NS_GET_IID(nsIXPCComponents)); - - if (!iface) - return false; - - nsCOMPtr 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); } diff --git a/js/xpconnect/src/XPCWrappedNativeScope.cpp b/js/xpconnect/src/XPCWrappedNativeScope.cpp index 63b14f9b5fb6..4a009a168e63 100644 --- a/js/xpconnect/src/XPCWrappedNativeScope.cpp +++ b/js/xpconnect/src/XPCWrappedNativeScope.cpp @@ -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 cholder(mComponents); + xpcObjectHelper helper(cholder); + nsCOMPtr 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 diff --git a/js/xpconnect/src/xpcprivate.h b/js/xpconnect/src/xpcprivate.h index e2dd2889796e..c0d646dbf3bb 100644 --- a/js/xpconnect/src/xpcprivate.h +++ b/js/xpconnect/src/xpcprivate.h @@ -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; }