tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 4ea5ebeaa0215c1d4c5facab82428d41e7bebf0c
parent f7c56c1d096a4d8916ee11a3bd7625c26ad30988
Author: Emilio Cobos Álvarez <emilio@crisal.io>
Date:   Wed, 22 Oct 2025 11:22:07 +0000

Bug 1995282 - Remove StyleSheet::mSheetURI. r=smaug

We don't need it:

 * For built-in sheets (ContentParent etc) the original URI is the same.
 * For Document / nsStyleSheetService / LinkStyle, the original URI is
   what you want to avoid registering / loading the same URI multiple
   times.

Differential Revision: https://phabricator.services.mozilla.com/D269423

Diffstat:
Mdom/base/Document.cpp | 9+++++----
Mdom/base/LinkStyle.cpp | 2+-
Mdom/ipc/ContentParent.cpp | 8++++----
Mlayout/style/FontFaceSetImpl.cpp | 15++-------------
Mlayout/style/GeckoBindings.cpp | 3+--
Mlayout/style/GlobalStyleSheetCache.cpp | 3+--
Mlayout/style/Loader.cpp | 12++++--------
Mlayout/style/StyleSheet.cpp | 16+++++++---------
Mlayout/style/StyleSheet.h | 5+----
Mlayout/style/nsStyleSheetService.cpp | 11+++++------
10 files changed, 31 insertions(+), 53 deletions(-)

diff --git a/dom/base/Document.cpp b/dom/base/Document.cpp @@ -8078,10 +8078,10 @@ static int32_t FindSheet(const nsTArray<RefPtr<StyleSheet>>& aSheets, nsIURI* aSheetURI) { for (int32_t i = aSheets.Length() - 1; i >= 0; i--) { bool bEqual; - nsIURI* uri = aSheets[i]->GetSheetURI(); - - if (uri && NS_SUCCEEDED(uri->Equals(aSheetURI, &bEqual)) && bEqual) + nsIURI* uri = aSheets[i]->GetOriginalURI(); + if (uri && NS_SUCCEEDED(uri->Equals(aSheetURI, &bEqual)) && bEqual) { return i; + } } return -1; @@ -8092,8 +8092,9 @@ nsresult Document::LoadAdditionalStyleSheet(additionalSheetType aType, MOZ_ASSERT(aSheetURI, "null arg"); // Checking if we have loaded this one already. - if (FindSheet(mAdditionalSheets[aType], aSheetURI) >= 0) + if (FindSheet(mAdditionalSheets[aType], aSheetURI) >= 0) { return NS_ERROR_INVALID_ARG; + } // Loading the sheet sync. RefPtr<css::Loader> loader = new css::Loader(GetDocGroup()); diff --git a/dom/base/LinkStyle.cpp b/dom/base/LinkStyle.cpp @@ -275,7 +275,7 @@ Result<LinkStyle::Update, nsresult> LinkStyle::DoUpdateStyleSheet( Maybe<SheetInfo> info = GetStyleSheetInfo(); if (aForceUpdate == ForceUpdate::No && mStyleSheet && info && !info->mIsInline && info->mURI) { - if (nsIURI* oldURI = mStyleSheet->GetSheetURI()) { + if (nsIURI* oldURI = mStyleSheet->GetOriginalURI()) { bool equal; nsresult rv = oldURI->Equals(info->mURI, &equal); if (NS_SUCCEEDED(rv) && equal) { diff --git a/dom/ipc/ContentParent.cpp b/dom/ipc/ContentParent.cpp @@ -2779,7 +2779,7 @@ bool ContentParent::InitInternal(ProcessPriority aInitialPriority) { // send the file URL instead. auto* sheetCache = GlobalStyleSheetCache::Singleton(); if (StyleSheet* ucs = sheetCache->GetUserContentSheet()) { - xpcomInit.userContentSheetURL() = ucs->GetSheetURI(); + xpcomInit.userContentSheetURL() = ucs->GetOriginalURI(); } else { xpcomInit.userContentSheetURL() = nullptr; } @@ -2942,17 +2942,17 @@ bool ContentParent::InitInternal(ProcessPriority aInitialPriority) { // shouldn't matter which we look at. for (StyleSheet* sheet : *sheetService->AgentStyleSheets()) { - (void)SendLoadAndRegisterSheet(sheet->GetSheetURI(), + (void)SendLoadAndRegisterSheet(sheet->GetOriginalURI(), nsIStyleSheetService::AGENT_SHEET); } for (StyleSheet* sheet : *sheetService->UserStyleSheets()) { - (void)SendLoadAndRegisterSheet(sheet->GetSheetURI(), + (void)SendLoadAndRegisterSheet(sheet->GetOriginalURI(), nsIStyleSheetService::USER_SHEET); } for (StyleSheet* sheet : *sheetService->AuthorStyleSheets()) { - (void)SendLoadAndRegisterSheet(sheet->GetSheetURI(), + (void)SendLoadAndRegisterSheet(sheet->GetOriginalURI(), nsIStyleSheetService::AUTHOR_SHEET); } } diff --git a/layout/style/FontFaceSetImpl.cpp b/layout/style/FontFaceSetImpl.cpp @@ -643,19 +643,8 @@ nsresult FontFaceSetImpl::LogMessage(gfxUserFontEntry* aUserFontEntry, if (rule) { Servo_FontFaceRule_GetSourceLocation(rule, &line, &column); // FIXME We need to figure out an approach to get the style sheet - // of this raw rule. See bug 1450903. -#if 0 - StyleSheet* sheet = rule->GetStyleSheet(); - // if the style sheet is removed while the font is loading can be null - if (sheet) { - nsCString spec = sheet->GetSheetURI()->GetSpecOrDefault(); - CopyUTF8toUTF16(spec, href); - } else { - NS_WARNING("null parent stylesheet for @font-face rule"); - href.AssignLiteral("unknown"); - } -#endif - // Leave href empty if we don't know how to get the correct sheet. + // of this raw rule. See bug 1450903. Leave href empty if we don't know how + // to get the correct sheet. } nsresult rv; diff --git a/layout/style/GeckoBindings.cpp b/layout/style/GeckoBindings.cpp @@ -1396,8 +1396,7 @@ static already_AddRefed<StyleSheet> LoadImportSheet( } nsCOMPtr<nsIReferrerInfo> referrerInfo = ReferrerInfo::CreateForExternalCSSResources(emptySheet, uri); - emptySheet->SetURIs(uri, uri, uri, referrerInfo, - aURL.ExtraData().Principal()); + emptySheet->SetURIs(uri, uri, referrerInfo, aURL.ExtraData().Principal()); emptySheet->SetComplete(); aParent->AppendStyleSheet(*emptySheet); return emptySheet.forget(); diff --git a/layout/style/GlobalStyleSheetCache.cpp b/layout/style/GlobalStyleSheetCache.cpp @@ -313,8 +313,7 @@ void GlobalStyleSheetCache::LoadSheetFromSharedMemory( nsCOMPtr<nsIReferrerInfo> referrerInfo = dom::ReferrerInfo::CreateForExternalCSSResources(sheet, uri); - sheet->SetURIs(uri, uri, uri, referrerInfo, - nsContentUtils::GetSystemPrincipal()); + sheet->SetURIs(uri, uri, referrerInfo, nsContentUtils::GetSystemPrincipal()); sheet->SetSharedContents(aHeader->mSheets[i]); sheet->SetComplete(); URLExtraData::sShared[i] = sheet->URLData(); diff --git a/layout/style/Loader.cpp b/layout/style/Loader.cpp @@ -667,7 +667,7 @@ void SheetLoadData::OnStartRequest(nsIRequest* aRequest) { ReferrerInfo::CreateForExternalCSSResources( mSheet, finalURI, nsContentUtils::GetReferrerPolicyFromChannel(channel)); - mSheet->SetURIs(finalURI, originalURI, finalURI, referrerInfo, principal); + mSheet->SetURIs(originalURI, finalURI, referrerInfo, principal); mSheet->SetOriginClean([&] { if (mParentData && !mParentData->mSheet->IsOriginClean()) { return false; @@ -755,7 +755,7 @@ nsresult SheetLoadData::VerifySheetReadyToParse(nsresult aStatus, const auto errorMessage = flag == nsIScriptError::errorFlag ? "MimeNotCss"_ns : "MimeNotCssWarn"_ns; - NS_ConvertUTF8toUTF16 sheetUri(mSheet->GetSheetURI()->GetSpecOrDefault()); + NS_ConvertUTF8toUTF16 sheetUri(mURI->GetSpecOrDefault()); NS_ConvertUTF8toUTF16 contentType16(contentType); nsAutoCString referrerSpec; @@ -962,7 +962,7 @@ Loader::CreateSheet(nsIURI* aURI, nsIContent* aLinkingContent, ReferrerInfo::CreateForExternalCSSResources(sheet, aURI); // NOTE: If the sheet is loaded, then SetURIs gets called again with the right // principal / final uri / etc. - sheet->SetURIs(aURI, aURI, aURI, referrerInfo, LoaderPrincipal()); + sheet->SetURIs(aURI, aURI, referrerInfo, LoaderPrincipal()); // Load errors should be origin-dirty, even if same-origin. sheet->SetOriginClean(false); LOG((" Needs parser")); @@ -1798,9 +1798,6 @@ Result<Loader::LoadSheetResult, nsresult> Loader::LoadInlineStyle( // Use the document's base URL so that @import in the inline sheet picks up // the right base. nsIURI* baseURI = aInfo.mContent->GetBaseURI(); - nsIURI* sheetURI = aInfo.mContent->OwnerDoc()->GetDocumentURI(); - nsIURI* originalURI = nullptr; - MOZ_ASSERT(aInfo.mIntegrity.IsEmpty()); nsIPrincipal* loadingPrincipal = LoaderPrincipal(); nsIPrincipal* principal = aInfo.mTriggeringPrincipal @@ -1828,8 +1825,7 @@ Result<Loader::LoadSheetResult, nsresult> Loader::LoadInlineStyle( SRIMetadata{}); nsIReferrerInfo* referrerInfo = aInfo.mContent->OwnerDoc()->ReferrerInfoForInternalCSSAndSVGResources(); - sheet->SetURIs(sheetURI, originalURI, baseURI, referrerInfo, - sheetPrincipal); + sheet->SetURIs(nullptr, baseURI, referrerInfo, sheetPrincipal); // If an extension creates an inline stylesheet, we don't want to consider // it same-origin with the page. // FIXME(emilio): That's rather odd. diff --git a/layout/style/StyleSheet.cpp b/layout/style/StyleSheet.cpp @@ -54,7 +54,6 @@ StyleSheet::StyleSheet(const StyleSheet& aCopy, StyleSheet* aParentSheetToUse, mTitle(aCopy.mTitle), mDocumentOrShadowRoot(aDocOrShadowRootToUse), mURLData(aCopy.mURLData), - mSheetURI(aCopy.mSheetURI), mOriginalSheetURI(aCopy.mOriginalSheetURI), mParsingMode(aCopy.mParsingMode), mState(aCopy.mState), @@ -991,7 +990,9 @@ void StyleSheet::List(FILE* aOut, int32_t aIndent) { line.AppendLiteral("/* "); nsCString url; - GetSheetURI()->GetSpec(url); + if (auto* uri = GetOriginalURI()) { + uri->GetSpec(url); + } if (url.IsEmpty()) { line.AppendLiteral("(no URL)"); } else { @@ -1158,9 +1159,7 @@ already_AddRefed<StyleSheet> StyleSheet::CreateConstructedSheet( } auto referrerInfo = MakeRefPtr<ReferrerInfo>(aConstructorDocument); - nsIURI* sheetURI = aConstructorDocument.GetDocumentURI(); - nsIURI* originalURI = nullptr; - sheet->SetURIs(sheetURI, originalURI, baseURI, referrerInfo, + sheet->SetURIs(nullptr, baseURI, referrerInfo, aConstructorDocument.NodePrincipal()); sheet->mConstructorDocument = &aConstructorDocument; @@ -1259,15 +1258,13 @@ const StyleUseCounters* StyleSheet::UseCounters() const { return Servo_StyleSheet_UseCounters(RawContents()); } -void StyleSheet::SetURIs(nsIURI* aSheetURI, nsIURI* aOriginalSheetURI, - nsIURI* aBaseURI, nsIReferrerInfo* aReferrerInfo, +void StyleSheet::SetURIs(nsIURI* aOriginalSheetURI, nsIURI* aBaseURI, + nsIReferrerInfo* aReferrerInfo, nsIPrincipal* aPrincipal) { - MOZ_ASSERT(aSheetURI); MOZ_ASSERT(aBaseURI); MOZ_ASSERT(aPrincipal); MOZ_ASSERT(aReferrerInfo); mURLData = MakeAndAddRef<URLExtraData>(aBaseURI, aReferrerInfo, aPrincipal); - mSheetURI = aSheetURI; mOriginalSheetURI = aOriginalSheetURI; } @@ -1534,3 +1531,4 @@ bool StyleSheet::IsReadOnly() const { } } // namespace mozilla +// diff --git a/layout/style/StyleSheet.h b/layout/style/StyleSheet.h @@ -196,15 +196,13 @@ class StyleSheet final : public nsICSSLoaderObserver, public nsWrapperCache { // Whether the sheet is for an inline <style> element. bool IsInline() const { return !GetOriginalURI(); } - - nsIURI* GetSheetURI() const { return mSheetURI; } /** * Get the URI this sheet was originally loaded from, if any. Can return null. */ nsIURI* GetOriginalURI() const { return mOriginalSheetURI; } nsIURI* GetBaseURI() const; - void SetURIs(nsIURI* aSheetURI, nsIURI* aOriginalSheetURI, nsIURI* aBaseURI, + void SetURIs(nsIURI* aOriginalSheetURI, nsIURI* aBaseURI, nsIReferrerInfo* aReferrerInfo, nsIPrincipal* aPrincipal); void SetOriginClean(bool aValue) { Inner().mOriginClean = aValue; } @@ -597,7 +595,6 @@ class StyleSheet final : public nsICSSLoaderObserver, public nsWrapperCache { RefPtr<dom::MediaList> mMedia; RefPtr<URLExtraData> mURLData; - RefPtr<nsIURI> mSheetURI; RefPtr<nsIURI> mOriginalSheetURI; // mParsingMode controls access to nonstandard style constructs that diff --git a/layout/style/nsStyleSheetService.cpp b/layout/style/nsStyleSheetService.cpp @@ -53,11 +53,11 @@ nsStyleSheetService::~nsStyleSheetService() { NS_IMPL_ISUPPORTS(nsStyleSheetService, nsIStyleSheetService, nsIMemoryReporter) -static bool SheetHasURI(StyleSheet* aSheet, nsIURI* aSheetURI) { +static bool SheetHasOriginalURI(StyleSheet* aSheet, nsIURI* aSheetURI) { MOZ_ASSERT(aSheetURI); bool result; - nsIURI* uri = aSheet->GetSheetURI(); + nsIURI* uri = aSheet->GetOriginalURI(); return uri && NS_SUCCEEDED(uri->Equals(aSheetURI, &result)) && result; } @@ -65,7 +65,7 @@ int32_t nsStyleSheetService::FindSheetByURI(uint32_t aSheetType, nsIURI* aSheetURI) { SheetArray& sheets = mSheets[aSheetType]; for (int32_t i = sheets.Length() - 1; i >= 0; i--) { - if (SheetHasURI(sheets[i], aSheetURI)) { + if (SheetHasOriginalURI(sheets[i], aSheetURI)) { return i; } } @@ -174,8 +174,7 @@ nsStyleSheetService::SheetRegistered(nsIURI* sheetURI, uint32_t aSheetType, MOZ_ASSERT(_retval, "Null out param"); // Check to see if we have the sheet. - *_retval = (FindSheetByURI(aSheetType, sheetURI) >= 0); - + *_retval = FindSheetByURI(aSheetType, sheetURI) >= 0; return NS_OK; } @@ -315,7 +314,7 @@ nsStyleSheetService::CollectReports(nsIHandleReportCallback* aHandleReport, size_t nsStyleSheetService::SizeOfIncludingThis( mozilla::MallocSizeOf aMallocSizeOf) const { size_t n = aMallocSizeOf(this); - for (auto& sheetArray : mSheets) { + for (const auto& sheetArray : mSheets) { n += sheetArray.ShallowSizeOfExcludingThis(aMallocSizeOf); for (StyleSheet* sheet : sheetArray) { if (sheet) {