Skip to content

Commit

Permalink
Bug 1340710 - Part 8: Add a fast path for nsIPrincipal::EqualsConside…
Browse files Browse the repository at this point in the history
…ringDomain() and nsIPrincipal::SubsumesConsideringDomain(); r=bholley
  • Loading branch information
ehsan committed Mar 7, 2017
1 parent 1331e34 commit aac03a4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
58 changes: 49 additions & 9 deletions caps/BasePrincipal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ OriginAttributes::IsPrivateBrowsing(const nsACString& aOrigin)

BasePrincipal::BasePrincipal(PrincipalKind aKind)
: mKind(aKind)
, mDomainSet(false)
{}

BasePrincipal::~BasePrincipal()
Expand Down Expand Up @@ -381,16 +382,39 @@ NS_IMETHODIMP
BasePrincipal::EqualsConsideringDomain(nsIPrincipal *aOther, bool *aResult)
{
NS_ENSURE_TRUE(aOther, NS_ERROR_INVALID_ARG);

// If neither of the principals have document.domain set, we use the fast path
// in Equals(). Otherwise, we fall back to the slow path below.
auto other = Cast(aOther);
if (!mDomainSet && !other->mDomainSet) {
*aResult = FastEquals(aOther);
return NS_OK;
}

*aResult = Subsumes(aOther, ConsiderDocumentDomain) &&
Cast(aOther)->Subsumes(this, ConsiderDocumentDomain);
other->Subsumes(this, ConsiderDocumentDomain);
return NS_OK;
}

NS_IMETHODIMP
BasePrincipal::Subsumes(nsIPrincipal *aOther, bool *aResult)
bool
BasePrincipal::EqualsIgnoringAddonId(nsIPrincipal *aOther)
{
NS_ENSURE_TRUE(aOther, NS_ERROR_INVALID_ARG);
MOZ_ASSERT(aOther);

// Note that this will not work for expanded principals, nor is it intended
// to.
if (!dom::ChromeUtils::IsOriginAttributesEqualIgnoringAddonId(
OriginAttributesRef(), Cast(aOther)->OriginAttributesRef())) {
return false;
}

return SubsumesInternal(aOther, DontConsiderDocumentDomain) &&
Cast(aOther)->SubsumesInternal(this, DontConsiderDocumentDomain);
}

bool
BasePrincipal::FastSubsumes(nsIPrincipal* aOther)
{
// If two principals are equal, then they both subsume each other.
// We deal with two special cases first:
// Null principals only subsume each other if they are equal, and are only
Expand All @@ -400,23 +424,39 @@ BasePrincipal::Subsumes(nsIPrincipal *aOther, bool *aResult)
// infinite recursion.
auto other = Cast(aOther);
if (Kind() == eNullPrincipal && other->Kind() == eNullPrincipal) {
*aResult = (this == other);
return NS_OK;
return this == other;
}
if (mOriginNoSuffix && FastEquals(aOther)) {
*aResult = true;
return NS_OK;
return true;
}

// Otherwise, fall back to the slow path.
*aResult = Subsumes(aOther, DontConsiderDocumentDomain);
return Subsumes(aOther, DontConsiderDocumentDomain);
}

NS_IMETHODIMP
BasePrincipal::Subsumes(nsIPrincipal *aOther, bool *aResult)
{
NS_ENSURE_TRUE(aOther, NS_ERROR_INVALID_ARG);

*aResult = FastSubsumes(aOther);

return NS_OK;
}

NS_IMETHODIMP
BasePrincipal::SubsumesConsideringDomain(nsIPrincipal *aOther, bool *aResult)
{
NS_ENSURE_TRUE(aOther, NS_ERROR_INVALID_ARG);

// If neither of the principals have document.domain set, we hand off to
// FastSubsumes() which has fast paths for some special cases. Otherwise, we fall
// back to the slow path below.
if (!mDomainSet && !Cast(aOther)->mDomainSet) {
*aResult = FastSubsumes(aOther);
return NS_OK;
}

*aResult = Subsumes(aOther, ConsiderDocumentDomain);
return NS_OK;
}
Expand Down
3 changes: 3 additions & 0 deletions caps/BasePrincipal.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ class BasePrincipal : public nsJSPrincipals

// Call this instead of Equals to avoid the cost of virtual dispatch.
bool FastEquals(nsIPrincipal* aOther);
// Call this instead of Subsumes to avoid the const of virtual dispatch.
bool FastSubsumes(nsIPrincipal* aOther);

protected:
virtual ~BasePrincipal();
Expand All @@ -293,6 +295,7 @@ class BasePrincipal : public nsJSPrincipals
nsCOMPtr<nsIAtom> mOriginSuffix;
OriginAttributes mOriginAttributes;
PrincipalKind mKind;
bool mDomainSet;
};

} // namespace mozilla
Expand Down
1 change: 1 addition & 0 deletions caps/nsPrincipal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ nsPrincipal::SetDomain(nsIURI* aDomain)
{
mDomain = NS_TryToMakeImmutable(aDomain);
mDomainImmutable = URIIsImmutable(mDomain);
mDomainSet = true;

// Recompute all wrappers between compartments using this principal and other
// non-chrome compartments.
Expand Down

0 comments on commit aac03a4

Please sign in to comment.