Bug 1646019 - Make PreloadHashKey don't check referrer for equality for style. r=mayhemer
authorEmilio Cobos Álvarez <emilio@crisal.io>
Tue, 16 Jun 2020 11:41:18 +0000
changeset 535842 071ebe8caaf199e1ed2ec7066048f85693e919a3
parent 535841 c36c8243afb8117b01288cd9f9956d34633d41fd
child 535843 73c8aa87664eb00bd8596896fe2c7c25767ceeeb
push id37511
push userabutkovits@mozilla.com
push dateTue, 16 Jun 2020 15:49:59 +0000
treeherdermozilla-central@89a54069f124 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmayhemer
bugs1646019, 1642591, 1599160
milestone79.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1646019 - Make PreloadHashKey don't check referrer for equality for style. r=mayhemer We probably should remove the referrer policy check altogether in bug 1642591, but the preload code still uses the whole referrer as the key for css, which the css loader stopped doing in bug 1599160. So this should be an uncontroversial simplification. Differential Revision: https://phabricator.services.mozilla.com/D79810
uriloader/preload/PreloadHashKey.cpp
uriloader/preload/PreloadHashKey.h
uriloader/preload/PreloadService.cpp
uriloader/preload/PreloadService.h
--- a/uriloader/preload/PreloadHashKey.cpp
+++ b/uriloader/preload/PreloadHashKey.cpp
@@ -1,16 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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 "PreloadHashKey.h"
 
 #include "mozilla/dom/Element.h"  // StringToCORSMode
 #include "mozilla/css/SheetLoadData.h"
+#include "mozilla/dom/ReferrerPolicyBinding.h"
 #include "nsIPrincipal.h"
 #include "nsIReferrerInfo.h"
 
 #define IGNORE_REFERRER_POLICY_FOR_PRELOAD_COALESCING true
 
 namespace mozilla {
 
 PreloadHashKey::PreloadHashKey(const nsIURI* aKey, ResourceType aAs)
@@ -100,33 +101,33 @@ PreloadHashKey PreloadHashKey::CreateAsS
   }
   CORSMode crossOrigin = dom::Element::StringToCORSMode(aCrossOrigin);
 
   return CreateAsScript(aURI, crossOrigin, scriptKind, aReferrerPolicy);
 }
 
 // static
 PreloadHashKey PreloadHashKey::CreateAsStyle(
-    nsIURI* aURI, nsIPrincipal* aPrincipal, nsIReferrerInfo* aReferrerInfo,
+    nsIURI* aURI, nsIPrincipal* aPrincipal, dom::ReferrerPolicy aReferrerPolicy,
     CORSMode aCORSMode, css::SheetParsingMode aParsingMode) {
   PreloadHashKey key(aURI, ResourceType::STYLE);
+  key.mReferrerPolicy = aReferrerPolicy;
   key.mCORSMode = aCORSMode;
   key.mPrincipal = aPrincipal;
 
   key.mStyle.mParsingMode = aParsingMode;
-  key.mStyle.mReferrerInfo = aReferrerInfo;
 
   return key;
 }
 
 // static
 PreloadHashKey PreloadHashKey::CreateAsStyle(
     css::SheetLoadData& aSheetLoadData) {
   return CreateAsStyle(aSheetLoadData.mURI, aSheetLoadData.mTriggeringPrincipal,
-                       aSheetLoadData.ReferrerInfo(),
+                       aSheetLoadData.ReferrerInfo()->ReferrerPolicy(),
                        aSheetLoadData.mSheet->GetCORSMode(),
                        aSheetLoadData.mSheet->ParsingMode());
 }
 
 // static
 PreloadHashKey PreloadHashKey::CreateAsImage(
     nsIURI* aURI, nsIPrincipal* aPrincipal, CORSMode aCORSMode,
     dom::ReferrerPolicy const& aReferrerPolicy) {
@@ -197,22 +198,16 @@ bool PreloadHashKey::KeyEquals(KeyTypePo
       if (mScript.mScriptKind != aOther->mScript.mScriptKind) {
         return false;
       }
       break;
     case ResourceType::STYLE: {
       if (mStyle.mParsingMode != aOther->mStyle.mParsingMode) {
         return false;
       }
-      bool eq;
-      if (NS_FAILED(mStyle.mReferrerInfo->Equals(aOther->mStyle.mReferrerInfo,
-                                                 &eq)) ||
-          !eq) {
-        return false;
-      }
       break;
     }
     case ResourceType::IMAGE:
       // No more checks needed.  The image cache key consists of the document
       // (which we are scoped into), origin attributes (which we compare as part
       // of the principal check) and the URL.  imgLoader::ValidateEntry compares
       // CORS, referrer info and principal, which we do by default.
       break;
--- a/uriloader/preload/PreloadHashKey.h
+++ b/uriloader/preload/PreloadHashKey.h
@@ -7,17 +7,16 @@
 
 #include "mozilla/CORSMode.h"
 #include "mozilla/css/SheetParsingMode.h"
 #include "mozilla/dom/ReferrerPolicyBinding.h"
 #include "mozilla/dom/ScriptKind.h"
 #include "nsURIHashKey.h"
 
 class nsIPrincipal;
-class nsIReferrerInfo;
 
 namespace mozilla {
 
 namespace css {
 class SheetLoadData;
 }
 
 /**
@@ -50,17 +49,17 @@ class PreloadHashKey : public nsURIHashK
       const dom::ScriptKind& aScriptKind,
       const dom::ReferrerPolicy& aReferrerPolicy);
   static PreloadHashKey CreateAsScript(
       nsIURI* aURI, const nsAString& aCrossOrigin, const nsAString& aType,
       const dom::ReferrerPolicy& aReferrerPolicy);
 
   // Construct key for "style"
   static PreloadHashKey CreateAsStyle(nsIURI* aURI, nsIPrincipal* aPrincipal,
-                                      nsIReferrerInfo* aReferrerInfo,
+                                      dom::ReferrerPolicy aReferrerPolicy,
                                       CORSMode aCORSMode,
                                       css::SheetParsingMode aParsingMode);
   static PreloadHashKey CreateAsStyle(css::SheetLoadData&);
 
   // Construct key for "image"
   static PreloadHashKey CreateAsImage(
       nsIURI* aURI, nsIPrincipal* aPrincipal, CORSMode aCORSMode,
       dom::ReferrerPolicy const& aReferrerPolicy);
@@ -102,16 +101,15 @@ class PreloadHashKey : public nsURIHashK
   enum dom::ReferrerPolicy mReferrerPolicy = dom::ReferrerPolicy::_empty;
   nsCOMPtr<nsIPrincipal> mPrincipal;
 
   struct {
     dom::ScriptKind mScriptKind = dom::ScriptKind::eClassic;
   } mScript;
 
   struct {
-    nsCOMPtr<nsIReferrerInfo> mReferrerInfo;
     css::SheetParsingMode mParsingMode = css::eAuthorSheetFeatures;
   } mStyle;
 };
 
 }  // namespace mozilla
 
 #endif
--- a/uriloader/preload/PreloadService.cpp
+++ b/uriloader/preload/PreloadService.cpp
@@ -80,26 +80,23 @@ already_AddRefed<PreloaderBase> PreloadS
   aLinkElement->GetCharset(charset);
   aLinkElement->GetImageSrcset(srcset);
   aLinkElement->GetImageSizes(sizes);
   aLinkElement->GetHref(url);
   aLinkElement->GetCrossOrigin(crossOrigin);
   aLinkElement->GetIntegrity(integrity);
   aLinkElement->GetReferrerPolicy(referrerPolicyAttr);
   auto referrerPolicy = PreloadReferrerPolicy(referrerPolicyAttr);
-  nsCOMPtr<nsIReferrerInfo> referrerInfo =
-      dom::ReferrerInfo::CreateFromDocumentAndPolicyOverride(mDocument,
-                                                             referrerPolicy);
   dom::DOMString domType;
   aLinkElement->GetType(domType);
   domType.ToString(type);
 
   RefPtr<PreloaderBase> preload = PreloadOrCoalesce(
       uri, url, aPolicyType, as, type, charset, srcset, sizes, integrity,
-      crossOrigin, referrerPolicy, referrerPolicyAttr, referrerInfo);
+      crossOrigin, referrerPolicy, referrerPolicyAttr);
 
   if (!preload) {
     NotifyNodeEvent(aLinkElement, false);
     return nullptr;
   }
 
   preload->AddLinkPreloadNode(aLinkElement);
   return preload.forget();
@@ -120,36 +117,35 @@ already_AddRefed<PreloaderBase> PreloadS
 
   if (aPolicyType == nsIContentPolicy::TYPE_INVALID) {
     return nullptr;
   }
 
   auto referrerPolicy = PreloadReferrerPolicy(aReferrerPolicy);
   return PreloadOrCoalesce(aURI, aURL, aPolicyType, aAs, aType, EmptyString(),
                            aSrcset, aSizes, aIntegrity, aCORS, referrerPolicy,
-                           aReferrerPolicy, aReferrerInfo);
+                           aReferrerPolicy);
 }
 
 already_AddRefed<PreloaderBase> PreloadService::PreloadOrCoalesce(
     nsIURI* aURI, const nsAString& aURL, nsContentPolicyType aPolicyType,
     const nsAString& aAs, const nsAString& aType, const nsAString& aCharset,
     const nsAString& aSrcset, const nsAString& aSizes,
     const nsAString& aIntegrity, const nsAString& aCORS,
-    dom::ReferrerPolicy aReferrerPolicy, const nsAString& aReferrerPolicyAttr,
-    nsIReferrerInfo* aReferrerInfo) {
+    dom::ReferrerPolicy aReferrerPolicy, const nsAString& aReferrerPolicyAttr) {
   bool isImgSet = false;
   PreloadHashKey preloadKey;
   nsCOMPtr<nsIURI> uri = aURI;
 
   if (aAs.LowerCaseEqualsASCII("script")) {
     preloadKey =
         PreloadHashKey::CreateAsScript(uri, aCORS, aType, aReferrerPolicy);
   } else if (aAs.LowerCaseEqualsASCII("style")) {
     preloadKey = PreloadHashKey::CreateAsStyle(
-        uri, mDocument->NodePrincipal(), aReferrerInfo,
+        uri, mDocument->NodePrincipal(), aReferrerPolicy,
         dom::Element::StringToCORSMode(aCORS),
         css::eAuthorSheetFeatures /* see Loader::LoadSheet */);
   } else if (aAs.LowerCaseEqualsASCII("image")) {
     uri = mDocument->ResolvePreloadImage(BaseURIForPreload(), aURL, aSrcset,
                                          aSizes, &isImgSet);
     if (!uri) {
       return nullptr;
     }
--- a/uriloader/preload/PreloadService.h
+++ b/uriloader/preload/PreloadService.h
@@ -94,18 +94,18 @@ class PreloadService {
   bool CheckReferrerURIScheme(nsIReferrerInfo* aReferrerInfo);
   nsIURI* BaseURIForPreload();
 
   already_AddRefed<PreloaderBase> PreloadOrCoalesce(
       nsIURI* aURI, const nsAString& aURL, nsContentPolicyType aPolicyType,
       const nsAString& aAs, const nsAString& aType, const nsAString& aCharset,
       const nsAString& aSrcset, const nsAString& aSizes,
       const nsAString& aIntegrity, const nsAString& aCORS,
-      dom::ReferrerPolicy aReferrerPolicy, const nsAString& aReferrerPolicyAttr,
-      nsIReferrerInfo* aReferrerInfo);
+      dom::ReferrerPolicy aReferrerPolicy,
+      const nsAString& aReferrerPolicyAttr);
 
  private:
   nsRefPtrHashtable<PreloadHashKey, PreloaderBase> mPreloads;
 
   // Raw pointer only, we are intended to be a direct member of dom::Document
   dom::Document* mDocument;
 
   // Set by `nsHtml5TreeOpExecutor::SetSpeculationBase`.