Bug 764389 - Add an API to change the principals of a compartment. r=luke

This commit is contained in:
Bobby Holley 2012-06-15 08:30:09 +02:00
parent a3f69ac10a
commit 19f0a769ef
3 changed files with 31 additions and 7 deletions

View File

@ -161,6 +161,31 @@ JS_GetCompartmentPrincipals(JSCompartment *compartment)
return compartment->principals;
}
JS_FRIEND_API(void)
JS_SetCompartmentPrincipals(JSCompartment *compartment, JSPrincipals *principals)
{
// Short circuit if there's no change.
if (principals == compartment->principals)
return;
// Clear out the old principals, if any.
if (compartment->principals) {
JS_DropPrincipals(compartment->rt, compartment->principals);
compartment->principals = NULL;
}
// Set up the new principals.
if (principals) {
JS_HoldPrincipals(principals);
compartment->principals = principals;
}
// Any compartment with the trusted principals -- and there can be
// multiple -- is a system compartment.
JSPrincipals *trusted = compartment->rt->trustedPrincipals();
compartment->isSystemCompartment = principals && principals == trusted;
}
JS_FRIEND_API(JSBool)
JS_WrapPropertyDescriptor(JSContext *cx, js::PropertyDescriptor *desc)
{

View File

@ -84,6 +84,9 @@ JS_SetAccumulateTelemetryCallback(JSRuntime *rt, JSAccumulateTelemetryDataCallba
extern JS_FRIEND_API(JSPrincipals *)
JS_GetCompartmentPrincipals(JSCompartment *compartment);
extern JS_FRIEND_API(void)
JS_SetCompartmentPrincipals(JSCompartment *compartment, JSPrincipals *principals);
/* Safe to call with input obj == NULL. Returns non-NULL iff obj != NULL. */
extern JS_FRIEND_API(JSObject *)
JS_ObjectToInnerObject(JSContext *cx, JSObject *obj);

View File

@ -4043,13 +4043,9 @@ NewCompartment(JSContext *cx, JSPrincipals *principals)
JSCompartment *compartment = cx->new_<JSCompartment>(rt);
if (compartment && compartment->init(cx)) {
// Any compartment with the trusted principals -- and there can be
// multiple -- is a system compartment.
compartment->isSystemCompartment = principals && rt->trustedPrincipals() == principals;
if (principals) {
compartment->principals = principals;
JS_HoldPrincipals(principals);
}
// Set up the principals.
JS_SetCompartmentPrincipals(compartment, principals);
compartment->setGCLastBytes(8192, 8192, GC_NORMAL);