Skip to content

Commit

Permalink
Bug 1823686 - Share URLExtraData between SVG attribute mapping and st…
Browse files Browse the repository at this point in the history
…yle attribute. r=smaug

I don't think there's ever a way these should differ.

Got some include hell from removing ReferrerInfo.h from Document.h but
hopefully should be straight-forward to review.

Depends on D173154

Differential Revision: https://phabricator.services.mozilla.com/D173155
  • Loading branch information
emilio committed Mar 22, 2023
1 parent 4155719 commit 91e6e4c
Show file tree
Hide file tree
Showing 36 changed files with 101 additions and 82 deletions.
50 changes: 31 additions & 19 deletions dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3939,6 +3939,12 @@ nsresult Document::InitFeaturePolicy(nsIChannel* aChannel) {
return NS_OK;
}

void Document::SetReferrerInfo(nsIReferrerInfo* aReferrerInfo) {
mReferrerInfo = aReferrerInfo;
mCachedReferrerInfoForInternalCSSAndSVGResources = nullptr;
mCachedURLData = nullptr;
}

nsresult Document::InitReferrerInfo(nsIChannel* aChannel) {
MOZ_ASSERT(mReferrerInfo);
MOZ_ASSERT(mPreloadReferrerInfo);
Expand All @@ -3955,7 +3961,7 @@ nsresult Document::InitReferrerInfo(nsIChannel* aChannel) {
? bc->GetEmbedderElement()->OwnerDoc()
: nullptr;
if (parentDoc) {
mReferrerInfo = parentDoc->GetReferrerInfo();
SetReferrerInfo(parentDoc->GetReferrerInfo());
mPreloadReferrerInfo = mReferrerInfo;
return NS_OK;
}
Expand All @@ -3975,17 +3981,17 @@ nsresult Document::InitReferrerInfo(nsIChannel* aChannel) {
return NS_OK;
}

nsCOMPtr<nsIReferrerInfo> referrerInfo = httpChannel->GetReferrerInfo();
if (referrerInfo) {
mReferrerInfo = referrerInfo;
if (nsCOMPtr<nsIReferrerInfo> referrerInfo = httpChannel->GetReferrerInfo()) {
SetReferrerInfo(referrerInfo);
}

// Override policy if we get one from Referrerr-Policy header
mozilla::dom::ReferrerPolicy policy =
nsContentUtils::GetReferrerPolicyFromChannel(aChannel);
mReferrerInfo = static_cast<dom::ReferrerInfo*>(mReferrerInfo.get())
->CloneWithNewPolicy(policy);

nsCOMPtr<nsIReferrerInfo> clone =
static_cast<dom::ReferrerInfo*>(mReferrerInfo.get())
->CloneWithNewPolicy(policy);
SetReferrerInfo(clone);
mPreloadReferrerInfo = mReferrerInfo;
return NS_OK;
}
Expand Down Expand Up @@ -4044,6 +4050,7 @@ void Document::SetDocumentURI(nsIURI* aURI) {
// If changing the document's URI changed the base URI of the document, we
// need to refresh the hrefs of all the links on the page.
if (!equalBases) {
mCachedURLData = nullptr;
RefreshLinkHrefs();
}

Expand Down Expand Up @@ -4177,9 +4184,10 @@ void Document::UpdateReferrerInfoFromMeta(const nsAString& aMetaReferrer,
static_cast<mozilla::dom::ReferrerInfo*>((mPreloadReferrerInfo).get())
->CloneWithNewPolicy(policy);
} else {
mReferrerInfo =
nsCOMPtr<nsIReferrerInfo> clone =
static_cast<mozilla::dom::ReferrerInfo*>((mReferrerInfo).get())
->CloneWithNewPolicy(policy);
SetReferrerInfo(clone);
}
}

Expand All @@ -4198,6 +4206,8 @@ void Document::SetPrincipals(nsIPrincipal* aNewPrincipal,
mNodeInfoManager->SetDocumentPrincipal(aNewPrincipal);
mPartitionedPrincipal = aNewPartitionedPrincipal;

mCachedURLData = nullptr;

mCSSLoader->RegisterInSheetCache();

#ifdef DEBUG
Expand Down Expand Up @@ -6637,6 +6647,7 @@ void Document::SetBaseURI(nsIURI* aURI) {
}

mDocumentBaseURI = aURI;
mCachedURLData = nullptr;
RefreshLinkHrefs();
}

Expand All @@ -6648,19 +6659,20 @@ Result<OwningNonNull<nsIURI>, nsresult> Document::ResolveWithBaseURI(
return OwningNonNull<nsIURI>(std::move(resolvedURI));
}

nsIReferrerInfo* Document::ReferrerInfoForInternalCSSAndSVGResources() {
if (!mCachedReferrerInfoForInternalCSSAndSVGResources) {
mCachedReferrerInfoForInternalCSSAndSVGResources =
ReferrerInfo::CreateForInternalCSSAndSVGResources(this);
}
return mCachedReferrerInfoForInternalCSSAndSVGResources;
}

URLExtraData* Document::DefaultStyleAttrURLData() {
MOZ_ASSERT(NS_IsMainThread());
nsIURI* baseURI = GetDocBaseURI();
nsIPrincipal* principal = NodePrincipal();
bool equals;
if (!mCachedURLData || mCachedURLData->BaseURI() != baseURI ||
mCachedURLData->Principal() != principal || !mCachedReferrerInfo ||
NS_FAILED(mCachedURLData->ReferrerInfo()->Equals(mCachedReferrerInfo,
&equals)) ||
!equals) {
mCachedReferrerInfo =
ReferrerInfo::CreateForInternalCSSAndSVGResources(this);
mCachedURLData = new URLExtraData(baseURI, mCachedReferrerInfo, principal);
if (!mCachedURLData) {
mCachedURLData = new URLExtraData(
GetDocBaseURI(), ReferrerInfoForInternalCSSAndSVGResources(),
NodePrincipal());
}
return mCachedURLData;
}
Expand Down
10 changes: 4 additions & 6 deletions dom/base/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <utility>
#include "ErrorList.h"
#include "MainThreadUtils.h"
#include "ReferrerInfo.h"
#include "Units.h"
#include "imgIRequest.h"
#include "js/RootingAPI.h"
Expand Down Expand Up @@ -873,9 +872,7 @@ class Document : public nsINode,
return mUpgradeInsecureRequests;
}

void SetReferrerInfo(nsIReferrerInfo* aReferrerInfo) {
mReferrerInfo = aReferrerInfo;
}
void SetReferrerInfo(nsIReferrerInfo*);

/*
* Referrer policy from <meta name="referrer" content=`policy`>
Expand Down Expand Up @@ -966,6 +963,7 @@ class Document : public nsINode,
* the document, a new one is created.
*/
URLExtraData* DefaultStyleAttrURLData();
nsIReferrerInfo* ReferrerInfoForInternalCSSAndSVGResources();

/**
* Get/Set the base target of a link in a document.
Expand Down Expand Up @@ -4490,9 +4488,9 @@ class Document : public nsINode,
// The base domain of the document for third-party checks.
nsCString mBaseDomain;

// A lazily-constructed URL data for style system to resolve URL value.
// A lazily-constructed URL data for style system to resolve URL values.
RefPtr<URLExtraData> mCachedURLData;
nsCOMPtr<nsIReferrerInfo> mCachedReferrerInfo;
nsCOMPtr<nsIReferrerInfo> mCachedReferrerInfoForInternalCSSAndSVGResources;

nsWeakPtr mDocumentLoadGroup;

Expand Down
13 changes: 6 additions & 7 deletions dom/base/FragmentOrElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,15 @@ already_AddRefed<URLExtraData> nsIContent::GetURLDataForStyleAttr(
return do_AddRef(data);
}
}
auto* doc = OwnerDoc();
if (aSubjectPrincipal && aSubjectPrincipal != NodePrincipal()) {
// TODO: Cache this?
nsCOMPtr<nsIReferrerInfo> referrerInfo =
ReferrerInfo::CreateForInternalCSSAndSVGResources(OwnerDoc());
return MakeAndAddRef<URLExtraData>(OwnerDoc()->GetDocBaseURI(),
referrerInfo, aSubjectPrincipal);
doc->ReferrerInfoForInternalCSSAndSVGResources();
// TODO: Cache this?
return MakeAndAddRef<URLExtraData>(doc->GetDocBaseURI(), referrerInfo,
aSubjectPrincipal);
}
// This also ignores the case that SVG inside XBL binding.
// But it is probably fine.
return do_AddRef(OwnerDoc()->DefaultStyleAttrURLData());
return do_AddRef(doc->DefaultStyleAttrURLData());
}

void nsIContent::ConstructUbiNode(void* storage) {
Expand Down
1 change: 1 addition & 0 deletions dom/base/LocationBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "nsGlobalWindow.h"
#include "mozilla/NullPrincipal.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "mozilla/dom/WindowContext.h"

namespace mozilla::dom {
Expand Down
25 changes: 11 additions & 14 deletions dom/base/nsAttrValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "mozilla/DebugOnly.h"
#include "mozilla/HashFunctions.h"

#include "mozilla/URLExtraData.h"
#include "nsAttrValue.h"
#include "nsAttrValueInlines.h"
#include "nsAtom.h"
Expand Down Expand Up @@ -1727,16 +1728,14 @@ bool nsAttrValue::ParseIntMarginValue(const nsAString& aString) {
bool nsAttrValue::ParseStyleAttribute(const nsAString& aString,
nsIPrincipal* aMaybeScriptedPrincipal,
nsStyledElement* aElement) {
dom::Document* ownerDoc = aElement->OwnerDoc();
nsHTMLCSSStyleSheet* sheet = ownerDoc->GetInlineStyleSheet();
nsIURI* baseURI = aElement->GetBaseURIForStyleAttr();
nsIURI* docURI = ownerDoc->GetDocumentURI();

NS_ASSERTION(aElement->NodePrincipal() == ownerDoc->NodePrincipal(),
dom::Document* doc = aElement->OwnerDoc();
nsHTMLCSSStyleSheet* sheet = doc->GetInlineStyleSheet();
NS_ASSERTION(aElement->NodePrincipal() == doc->NodePrincipal(),
"This is unexpected");

nsIPrincipal* principal = aMaybeScriptedPrincipal ? aMaybeScriptedPrincipal
: aElement->NodePrincipal();
RefPtr<URLExtraData> data = aElement->GetURLDataForStyleAttr(principal);

// If the (immutable) document URI does not match the element's base URI
// (the common case is that they do match) do not cache the rule. This is
Expand All @@ -1745,8 +1744,9 @@ bool nsAttrValue::ParseStyleAttribute(const nsAString& aString,
// Similarly, if the triggering principal does not match the node principal,
// do not cache the rule, since the principal will be encoded in any parsed
// URLs in the rule.
const bool cachingAllowed =
sheet && baseURI == docURI && principal == aElement->NodePrincipal();
const bool cachingAllowed = sheet &&
doc->GetDocumentURI() == data->BaseURI() &&
principal == aElement->NodePrincipal();
if (cachingAllowed) {
MiscContainer* cont = sheet->LookupStyleAttr(aString);
if (cont) {
Expand All @@ -1757,12 +1757,9 @@ bool nsAttrValue::ParseStyleAttribute(const nsAString& aString,
}
}

nsCOMPtr<nsIReferrerInfo> referrerInfo =
dom::ReferrerInfo::CreateForInternalCSSAndSVGResources(ownerDoc);
auto data = MakeRefPtr<URLExtraData>(baseURI, referrerInfo, principal);
RefPtr<DeclarationBlock> decl = DeclarationBlock::FromCssText(
aString, data, ownerDoc->GetCompatibilityMode(), ownerDoc->CSSLoader(),
StyleCssRuleType::Style);
RefPtr<DeclarationBlock> decl =
DeclarationBlock::FromCssText(aString, data, doc->GetCompatibilityMode(),
doc->CSSLoader(), StyleCssRuleType::Style);
if (!decl) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions dom/base/nsContentSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "mozilla/StaticPrefs_content.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/LinkStyle.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "mozilla/css/Loader.h"
#include "mozilla/dom/MutationObservers.h"
#include "mozilla/dom/SRILogHelper.h"
Expand Down
2 changes: 1 addition & 1 deletion dom/base/nsFocusManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class nsFocusManager final : public nsIFocusManager,
* Returns Nothing() if we end up not trying to focus the element,
* otherwise returns the generated action id.
*/
MOZ_CAN_RUN_SCRIPT Maybe<uint64_t> SetFocusInner(
MOZ_CAN_RUN_SCRIPT mozilla::Maybe<uint64_t> SetFocusInner(
mozilla::dom::Element* aNewContent, int32_t aFlags, bool aFocusChanged,
bool aAdjustWidget);

Expand Down
1 change: 1 addition & 0 deletions dom/base/nsImageLoadingContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "mozilla/dom/HTMLImageElement.h"
#include "mozilla/dom/ImageTextBinding.h"
#include "mozilla/dom/ImageTracker.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/intl/LocaleService.h"
#include "mozilla/intl/Locale.h"
Expand Down
4 changes: 2 additions & 2 deletions dom/base/nsTreeSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1787,8 +1787,8 @@ static void SanitizeStyleSheet(const nsAString& aOriginal,
aSanitized.Truncate();

NS_ConvertUTF16toUTF8 style(aOriginal);
RefPtr<nsIReferrerInfo> referrer =
ReferrerInfo::CreateForInternalCSSAndSVGResources(aDocument);
nsIReferrerInfo* referrer =
aDocument->ReferrerInfoForInternalCSSAndSVGResources();
auto extraData =
MakeRefPtr<URLExtraData>(aBaseURI, referrer, aDocument->NodePrincipal());
RefPtr<RawServoStyleSheetContents> contents =
Expand Down
1 change: 1 addition & 0 deletions dom/fetch/FetchUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "mozilla/dom/DOMException.h"
#include "mozilla/dom/InternalRequest.h"
#include "mozilla/dom/Response.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "mozilla/dom/WorkerRef.h"

namespace mozilla::dom {
Expand Down
1 change: 1 addition & 0 deletions dom/html/HTMLImageElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "nsLayoutUtils.h"

using namespace mozilla::net;
using mozilla::Maybe;

NS_IMPL_NS_NEW_HTML_ELEMENT(Image)

Expand Down
1 change: 1 addition & 0 deletions dom/html/HTMLLinkElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/dom/HTMLLinkElementBinding.h"
#include "mozilla/dom/HTMLDNSPrefetch.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "mozilla/dom/ScriptLoader.h"
#include "nsContentUtils.h"
#include "nsDOMTokenList.h"
Expand Down
1 change: 1 addition & 0 deletions dom/html/HTMLStyleElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "nsGkAtoms.h"
#include "nsStyleConsts.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "nsUnicharUtils.h"
#include "nsThreadUtils.h"
#include "nsContentUtils.h"
Expand Down
8 changes: 3 additions & 5 deletions dom/security/ReferrerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/RefPtr.h"
#include "mozilla/dom/ReferrerPolicyBinding.h"
#include "nsIClassInfoImpl.h"
#include "nsIEffectiveTLDService.h"
Expand Down Expand Up @@ -1284,11 +1285,8 @@ already_AddRefed<nsIReferrerInfo> ReferrerInfo::CreateForExternalCSSResources(
already_AddRefed<nsIReferrerInfo>
ReferrerInfo::CreateForInternalCSSAndSVGResources(Document* aDocument) {
MOZ_ASSERT(aDocument);
nsCOMPtr<nsIReferrerInfo> referrerInfo;

referrerInfo = new ReferrerInfo(aDocument->GetDocumentURI(),
aDocument->GetReferrerPolicy());
return referrerInfo.forget();
return do_AddRef(new ReferrerInfo(aDocument->GetDocumentURI(),
aDocument->GetReferrerPolicy()));
}

nsresult ReferrerInfo::ComputeReferrer(nsIHttpChannel* aChannel) {
Expand Down
2 changes: 0 additions & 2 deletions dom/security/ReferrerInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class nsHttpChannel;
} // namespace net
} // namespace mozilla

using mozilla::Maybe;

namespace mozilla::dom {

/**
Expand Down
6 changes: 2 additions & 4 deletions dom/svg/SVGAnimationElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,8 @@ void SVGAnimationElement::UpdateHrefTarget(const nsAString& aHrefStr) {
nsCOMPtr<nsIURI> targetURI;
nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(targetURI), aHrefStr,
OwnerDoc(), baseURI);
nsCOMPtr<nsIReferrerInfo> referrerInfo =
ReferrerInfo::CreateForInternalCSSAndSVGResources(OwnerDoc());

mHrefTarget.ResetToURIFragmentID(this, targetURI, referrerInfo);
mHrefTarget.ResetToURIFragmentID(
this, targetURI, OwnerDoc()->ReferrerInfoForInternalCSSAndSVGResources());
AnimationTargetChanged();
}

Expand Down
6 changes: 1 addition & 5 deletions dom/svg/SVGElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1146,11 +1146,7 @@ class MOZ_STACK_CLASS MappedAttrParser {

URLExtraData& EnsureExtraData() {
if (!mExtraData) {
nsCOMPtr<nsIReferrerInfo> referrerInfo =
ReferrerInfo::CreateForInternalCSSAndSVGResources(
mElement.OwnerDoc());
mExtraData = MakeRefPtr<URLExtraData>(mElement.GetBaseURI(), referrerInfo,
mElement.NodePrincipal());
mExtraData = mElement.GetURLDataForStyleAttr();
}
return *mExtraData;
}
Expand Down
5 changes: 2 additions & 3 deletions dom/svg/SVGMPathElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "nsDebug.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/SVGObserverUtils.h"
#include "mozilla/dom/ReferrerInfo.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/SVGAnimateMotionElement.h"
#include "mozilla/dom/SVGGeometryElement.h"
#include "nsContentUtils.h"
Expand Down Expand Up @@ -206,8 +206,7 @@ void SVGMPathElement::UpdateHrefTarget(nsIContent* aParent,
// for a call to GetComposedDoc(), and |this| might not have a current
// document yet (if our caller is BindToTree).
nsCOMPtr<nsIReferrerInfo> referrerInfo =
ReferrerInfo::CreateForInternalCSSAndSVGResources(OwnerDoc());

OwnerDoc()->ReferrerInfoForInternalCSSAndSVGResources();
mPathTracker.ResetToURIFragmentID(aParent, targetURI, referrerInfo);
} else {
// if we don't have a parent, then there's no animateMotion element
Expand Down
Loading

0 comments on commit 91e6e4c

Please sign in to comment.