Skip to content

Commit

Permalink
Bug 1776755 - Skip ordering ExpandedPrincipals to speed up creation; …
Browse files Browse the repository at this point in the history
…r=nika,necko-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D161450
  • Loading branch information
june wilde committed Feb 7, 2023
1 parent 4dfe07e commit 46711bc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 deletions.
10 changes: 10 additions & 0 deletions caps/BasePrincipal.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,16 @@ inline bool BasePrincipal::FastEqualsConsideringDomain(nsIPrincipal* 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) &&
other->Subsumes(this, ConsiderDocumentDomain);
}
Expand Down
32 changes: 2 additions & 30 deletions caps/ExpandedPrincipal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,6 @@ NS_IMPL_QUERY_INTERFACE_CI(ExpandedPrincipal, nsIPrincipal,
NS_IMPL_CI_INTERFACE_GETTER(ExpandedPrincipal, nsIPrincipal,
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(
nsTArray<nsCOMPtr<nsIPrincipal>>&& aPrincipals,
const nsACString& aOriginNoSuffix, const OriginAttributes& aAttrs)
Expand All @@ -53,12 +31,9 @@ ExpandedPrincipal::~ExpandedPrincipal() = default;
already_AddRefed<ExpandedPrincipal> ExpandedPrincipal::Create(
const nsTArray<nsCOMPtr<nsIPrincipal>>& aAllowList,
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;
OriginComparator c;
for (size_t i = 0; i < aAllowList.Length(); ++i) {
principals.InsertElementSorted(aAllowList[i], c);
principals.AppendElement(aAllowList[i]);
}

nsAutoCString origin;
Expand Down Expand Up @@ -248,7 +223,6 @@ ExpandedPrincipal::Deserializer::Read(nsIObjectInputStream* aStream) {
return NS_ERROR_OUT_OF_MEMORY;
}

OriginComparator c;
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsISupports> read;
rv = aStream->ReadObject(true, getter_AddRefs(read));
Expand All @@ -261,9 +235,7 @@ ExpandedPrincipal::Deserializer::Read(nsIObjectInputStream* aStream) {
return NS_ERROR_UNEXPECTED;
}

// Play it safe and InsertElementSorted, in case the sort order
// changed for some bizarre reason.
principals.InsertElementSorted(std::move(principal), c);
principals.AppendElement(std::move(principal));
}

mPrincipal = ExpandedPrincipal::Create(principals, OriginAttributes());
Expand Down

0 comments on commit 46711bc

Please sign in to comment.