From d1dcccb925c3a6412e82fe1f1be7ca6696b58919 Mon Sep 17 00:00:00 2001 From: Gabor Krizsanits Date: Wed, 2 Nov 2011 10:20:00 -0700 Subject: [PATCH] Bug 677294 - need a way to create new JS scopes and run scripts against them (same compartment) --- js/xpconnect/src/XPCComponents.cpp | 39 ++++++++++++++++++++++++++---- js/xpconnect/src/xpcprivate.h | 9 +++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/js/xpconnect/src/XPCComponents.cpp b/js/xpconnect/src/XPCComponents.cpp index 3a95fadcfbc9..41d323159120 100644 --- a/js/xpconnect/src/XPCComponents.cpp +++ b/js/xpconnect/src/XPCComponents.cpp @@ -56,7 +56,9 @@ #include "nsJSUtils.h" #include "mozJSComponentLoader.h" #include "nsContentUtils.h" - +#include "jsgc.h" + +using namespace js; /***************************************************************************/ // stuff used by all @@ -3064,7 +3066,7 @@ NS_IMPL_ISUPPORTS0(Identity) nsresult xpc_CreateSandboxObject(JSContext * cx, jsval * vp, nsISupports *prinOrSop, JSObject *proto, - bool wantXrays, const nsACString &sandboxName) + bool wantXrays, const nsACString &sandboxName, nsISupports *identityPtr) { // Create the sandbox global object nsresult rv; @@ -3101,8 +3103,13 @@ xpc_CreateSandboxObject(JSContext * cx, jsval * vp, nsISupports *prinOrSop, JSOb JSCompartment *compartment; JSObject *sandbox; - nsRefPtr identity = new Identity(); - rv = xpc_CreateGlobalObject(cx, &SandboxClass, principal, identity, + nsRefPtr identity; + if (!identityPtr) { + identity = new Identity(); + identityPtr = identity; + } + + rv = xpc_CreateGlobalObject(cx, &SandboxClass, principal, identityPtr, wantXrays, &sandbox, &compartment); NS_ENSURE_SUCCESS(rv, rv); @@ -3212,6 +3219,7 @@ nsXPCComponents_utils_Sandbox::CallOrConstruct(nsIXPConnectWrappedNative *wrappe nsCOMPtr sop; nsCOMPtr principal; nsISupports *prinOrSop = nsnull; + nsISupports *identity = nsnull; if (JSVAL_IS_STRING(argv[0])) { JSString *codebaseStr = JSVAL_TO_STRING(argv[0]); size_t codebaseLength; @@ -3316,9 +3324,30 @@ nsXPCComponents_utils_Sandbox::CallOrConstruct(nsIXPConnectWrappedNative *wrappe sandboxName.Adopt(tmp, strlen(tmp)); } + + // see Bug 677294: + if (!JS_HasProperty(cx, optionsObject, "sameGroupAs", &found)) + return NS_ERROR_INVALID_ARG; + + if (found) { + if (!JS_GetProperty(cx, optionsObject, "sameGroupAs", &option) || + JSVAL_IS_PRIMITIVE(option)) { + return ThrowAndFail(NS_ERROR_INVALID_ARG, cx, _retval); + } + + void* privateValue = + JS_GetCompartmentPrivate(cx,GetObjectCompartment(JSVAL_TO_OBJECT(option))); + xpc::CompartmentPrivate *compartmentPrivate = + static_cast(privateValue); + + if (!compartmentPrivate || !compartmentPrivate->key) + return ThrowAndFail(NS_ERROR_INVALID_ARG, cx, _retval); + + identity = compartmentPrivate->key->GetPtr(); + } } - rv = xpc_CreateSandboxObject(cx, vp, prinOrSop, proto, wantXrays, sandboxName); + rv = xpc_CreateSandboxObject(cx, vp, prinOrSop, proto, wantXrays, sandboxName, identity); if (NS_FAILED(rv)) { return ThrowAndFail(rv, cx, _retval); diff --git a/js/xpconnect/src/xpcprivate.h b/js/xpconnect/src/xpcprivate.h index 16904681ad3a..c006b7e4b7ef 100644 --- a/js/xpconnect/src/xpcprivate.h +++ b/js/xpconnect/src/xpcprivate.h @@ -277,6 +277,11 @@ class PtrAndPrincipalHashKey : public PLDHashEntryHdr return aKey->mSavedHash; } + nsISupports* GetPtr() + { + return mPtr; + } + enum { ALLOW_MEMMOVE = true }; protected: @@ -4360,8 +4365,8 @@ xpc_GetJSPrivate(JSObject *obj) // and used. nsresult xpc_CreateSandboxObject(JSContext * cx, jsval * vp, nsISupports *prinOrSop, - JSObject *proto, bool preferXray, const nsACString &sandboxName); - + JSObject *proto, bool preferXray, const nsACString &sandboxName, + nsISupports *identityPtr = nsnull); // Helper for evaluating scripts in a sandbox object created with // xpc_CreateSandboxObject(). The caller is responsible of ensuring // that *rval doesn't get collected during the call or usage after the