Bug 677294 - need a way to create new JS scopes and run scripts against them (same compartment)

This commit is contained in:
Gabor Krizsanits 2011-11-02 10:20:00 -07:00
parent 709ddbf93e
commit d1dcccb925
2 changed files with 41 additions and 7 deletions

View File

@ -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> identity = new Identity();
rv = xpc_CreateGlobalObject(cx, &SandboxClass, principal, identity,
nsRefPtr<Identity> 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<nsIScriptObjectPrincipal> sop;
nsCOMPtr<nsIPrincipal> 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<xpc::CompartmentPrivate*>(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);

View File

@ -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