Bug 824864 - Implement nsContentUtils::GetObjectPrincipal. r=bz

The SSM interface is super awkward.
This commit is contained in:
Bobby Holley 2013-01-16 18:50:25 -08:00
parent 85b3a83b16
commit 1d5c9aeac3
3 changed files with 19 additions and 0 deletions

View File

@ -438,6 +438,13 @@ public:
// be called when nsContentUtils is initialized.
static nsIPrincipal* GetSubjectPrincipal();
// Returns the principal of the given JS object. This should never be null
// for any object in the XPConnect runtime.
//
// In general, being interested in the principal of an object is enough to
// guarantee that the return value is non-null.
static nsIPrincipal* GetObjectPrincipal(JSObject* aObj);
static nsresult GenerateStateKey(nsIContent* aContent,
const nsIDocument* aDocument,
nsACString& aKey);

View File

@ -2330,6 +2330,17 @@ nsContentUtils::GetSubjectPrincipal()
return subject;
}
// static
nsIPrincipal*
nsContentUtils::GetObjectPrincipal(JSObject* aObj)
{
// This is duplicated from nsScriptSecurityManager. We don't call through there
// because the API unnecessarily requires a JSContext for historical reasons.
JSCompartment *compartment = js::GetObjectCompartment(aObj);
JSPrincipals *principals = JS_GetCompartmentPrincipals(compartment);
return nsJSPrincipals::get(principals);
}
// static
nsresult
nsContentUtils::NewURIWithDocumentCharset(nsIURI** aResult,

View File

@ -1061,6 +1061,7 @@ CreateGlobalObject(JSContext *cx, JSClass *clasp, nsIPrincipal *principal)
CheckTypeInference(cx, clasp, principal);
NS_ABORT_IF_FALSE(NS_IsMainThread(), "using a principal off the main thread?");
MOZ_ASSERT(principal);
JSObject *global = JS_NewGlobalObject(cx, clasp, nsJSPrincipals::get(principal));
if (!global)