Bug 1335890 - Factor out nsContentUtils::SubjectPrincipal version that takes JSContext r=bz

MozReview-Commit-ID: CGRipgKUm7g

--HG--
extra : rebase_source : 33c2dbd7eaad80a3e523113aa8217ba1e086572a
This commit is contained in:
Tomislav Jovanovic 2017-02-05 23:19:26 +01:00
parent c1706f8fa9
commit 22c41867ed
2 changed files with 25 additions and 17 deletions

View File

@ -2084,11 +2084,8 @@ nsContentUtils::CallerHasPermission(JSContext* aCx, const nsAString& aPerm)
return true;
}
JSCompartment* c = js::GetContextCompartment(aCx);
nsIPrincipal* p = nsJSPrincipals::get(JS_GetCompartmentPrincipals(c));
// Otherwise, only allow if caller is an addon with the permission.
return BasePrincipal::Cast(p)->AddonHasPermission(aPerm);
return BasePrincipal::Cast(SubjectPrincipal(aCx))->AddonHasPermission(aPerm);
}
//static
@ -2174,16 +2171,8 @@ nsContentUtils::IsCallerContentXBL()
bool
nsContentUtils::IsSystemCaller(JSContext* aCx)
{
MOZ_ASSERT(NS_IsMainThread());
// This is similar to what SubjectPrincipal() does, except we do in fact
// assume that we're in a compartment here; anyone who calls this function in
// situations where that's not the case is doing it wrong.
JSCompartment *compartment = js::GetContextCompartment(aCx);
MOZ_ASSERT(compartment);
JSPrincipals *principals = JS_GetCompartmentPrincipals(compartment);
return nsJSPrincipals::get(principals) == sSystemPrincipal;
// Note that SubjectPrincipal() assumes we are in a compartment here.
return SubjectPrincipal(aCx) == sSystemPrincipal;
}
bool
@ -2777,6 +2766,22 @@ nsContentUtils::GenerateStateKey(nsIContent* aContent,
return NS_OK;
}
// static
nsIPrincipal*
nsContentUtils::SubjectPrincipal(JSContext* aCx)
{
MOZ_ASSERT(NS_IsMainThread());
// As opposed to SubjectPrincipal(), we do in fact assume that
// we're in a compartment here; anyone who calls this function
// in situations where that's not the case is doing it wrong.
JSCompartment* compartment = js::GetContextCompartment(aCx);
MOZ_ASSERT(compartment);
JSPrincipals* principals = JS_GetCompartmentPrincipals(compartment);
return nsJSPrincipals::get(principals);
}
// static
nsIPrincipal*
nsContentUtils::SubjectPrincipal()
@ -2803,7 +2808,7 @@ nsContentUtils::SubjectPrincipal()
// The natural thing to return is a null principal. Ideally, we'd return a
// different null principal each time, to avoid any unexpected interactions
// when the principal accidentally gets inherited somewhere. But
// GetSubjectPrincipal doesn't return strong references, so there's no way to
// SubjectPrincipal doesn't return strong references, so there's no way to
// sanely manage the lifetime of multiple null principals.
//
// So we use a singleton null principal. To avoid it being accidentally
@ -2814,8 +2819,7 @@ nsContentUtils::SubjectPrincipal()
return sNullSubjectPrincipal;
}
JSPrincipals *principals = JS_GetCompartmentPrincipals(compartment);
return nsJSPrincipals::get(principals);
return SubjectPrincipal(cx);
}
// static

View File

@ -545,6 +545,10 @@ public:
return sSecurityManager;
}
// Returns the subject principal from the JSContext. May only be called
// from the main thread and assumes an existing compartment.
static nsIPrincipal* SubjectPrincipal(JSContext* aCx);
// Returns the subject principal. Guaranteed to return non-null. May only
// be called when nsContentUtils is initialized.
static nsIPrincipal* SubjectPrincipal();