Bug 1776755 - Skip ordering ExpandedPrincipals to speed up creation; r=nika,necko-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D161450
This commit is contained in:
june wilde 2023-02-07 16:47:29 +00:00
Родитель 4dfe07ec96
Коммит 46711bc1dc
2 изменённых файлов: 12 добавлений и 30 удалений

Просмотреть файл

@ -390,6 +390,16 @@ inline bool BasePrincipal::FastEqualsConsideringDomain(nsIPrincipal* aOther) {
return FastEquals(aOther); return FastEquals(aOther);
} }
// Principals of different kinds can't be equal.
if (Kind() != other->Kind()) {
return false;
}
// Only ContentPrincipals should have mHasExplicitDomain set to true, so test
// that we haven't ended up here instead of FastEquals by mistake.
MOZ_ASSERT(IsContentPrincipal(),
"Only content principals can set mHasExplicitDomain");
return Subsumes(aOther, ConsiderDocumentDomain) && return Subsumes(aOther, ConsiderDocumentDomain) &&
other->Subsumes(this, ConsiderDocumentDomain); other->Subsumes(this, ConsiderDocumentDomain);
} }

Просмотреть файл

@ -20,28 +20,6 @@ NS_IMPL_QUERY_INTERFACE_CI(ExpandedPrincipal, nsIPrincipal,
NS_IMPL_CI_INTERFACE_GETTER(ExpandedPrincipal, nsIPrincipal, NS_IMPL_CI_INTERFACE_GETTER(ExpandedPrincipal, nsIPrincipal,
nsIExpandedPrincipal) nsIExpandedPrincipal)
struct OriginComparator {
bool LessThan(nsIPrincipal* a, nsIPrincipal* b) const {
nsAutoCString originA;
DebugOnly<nsresult> rv = a->GetOrigin(originA);
MOZ_ASSERT(NS_SUCCEEDED(rv));
nsAutoCString originB;
rv = b->GetOrigin(originB);
MOZ_ASSERT(NS_SUCCEEDED(rv));
return originA < originB;
}
bool Equals(nsIPrincipal* a, nsIPrincipal* b) const {
nsAutoCString originA;
DebugOnly<nsresult> rv = a->GetOrigin(originA);
MOZ_ASSERT(NS_SUCCEEDED(rv));
nsAutoCString originB;
rv = b->GetOrigin(originB);
MOZ_ASSERT(NS_SUCCEEDED(rv));
return a == b;
}
};
ExpandedPrincipal::ExpandedPrincipal( ExpandedPrincipal::ExpandedPrincipal(
nsTArray<nsCOMPtr<nsIPrincipal>>&& aPrincipals, nsTArray<nsCOMPtr<nsIPrincipal>>&& aPrincipals,
const nsACString& aOriginNoSuffix, const OriginAttributes& aAttrs) const nsACString& aOriginNoSuffix, const OriginAttributes& aAttrs)
@ -53,12 +31,9 @@ ExpandedPrincipal::~ExpandedPrincipal() = default;
already_AddRefed<ExpandedPrincipal> ExpandedPrincipal::Create( already_AddRefed<ExpandedPrincipal> ExpandedPrincipal::Create(
const nsTArray<nsCOMPtr<nsIPrincipal>>& aAllowList, const nsTArray<nsCOMPtr<nsIPrincipal>>& aAllowList,
const OriginAttributes& aAttrs) { const OriginAttributes& aAttrs) {
// We force the principals to be sorted by origin so that ExpandedPrincipal
// origins can have a canonical form.
nsTArray<nsCOMPtr<nsIPrincipal>> principals; nsTArray<nsCOMPtr<nsIPrincipal>> principals;
OriginComparator c;
for (size_t i = 0; i < aAllowList.Length(); ++i) { for (size_t i = 0; i < aAllowList.Length(); ++i) {
principals.InsertElementSorted(aAllowList[i], c); principals.AppendElement(aAllowList[i]);
} }
nsAutoCString origin; nsAutoCString origin;
@ -248,7 +223,6 @@ ExpandedPrincipal::Deserializer::Read(nsIObjectInputStream* aStream) {
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
} }
OriginComparator c;
for (uint32_t i = 0; i < count; ++i) { for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsISupports> read; nsCOMPtr<nsISupports> read;
rv = aStream->ReadObject(true, getter_AddRefs(read)); rv = aStream->ReadObject(true, getter_AddRefs(read));
@ -261,9 +235,7 @@ ExpandedPrincipal::Deserializer::Read(nsIObjectInputStream* aStream) {
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
} }
// Play it safe and InsertElementSorted, in case the sort order principals.AppendElement(std::move(principal));
// changed for some bizarre reason.
principals.InsertElementSorted(std::move(principal), c);
} }
mPrincipal = ExpandedPrincipal::Create(principals, OriginAttributes()); mPrincipal = ExpandedPrincipal::Create(principals, OriginAttributes());