Bug 1330487 - Part 2: Propagate referrer policy from doc/sheet to URLValueData draft
authorThomas Nguyen <tnguyen@mozilla.com>
Tue, 16 Jan 2018 15:31:27 +0800
changeset 721905 5c2a600ddcc8e7a54c9060132bc0e358717d8cd3
parent 721904 f5e7f91c3a2311dd0ec0405b302f508ba0c106ce
child 721906 2e9830ab5d585b95451ca43159a2ac13526426d9
push id95995
push userbmo:tnguyen@mozilla.com
push dateThu, 18 Jan 2018 02:41:54 +0000
bugs1330487
milestone59.0a1
Bug 1330487 - Part 2: Propagate referrer policy from doc/sheet to URLValueData MozReview-Commit-ID: LXzTPpS90vg
dom/base/nsAttrValue.cpp
dom/base/nsDocument.cpp
dom/canvas/CanvasRenderingContext2D.cpp
dom/html/nsGenericHTMLElement.cpp
dom/svg/SVGUseElement.cpp
dom/svg/nsSVGElement.cpp
layout/style/ServoCSSParser.cpp
layout/style/ServoStyleSheet.cpp
layout/style/URLExtraData.h
layout/style/nsCSSParser.cpp
layout/style/nsCSSValue.cpp
layout/style/nsCSSValue.h
--- a/dom/base/nsAttrValue.cpp
+++ b/dom/base/nsAttrValue.cpp
@@ -1738,17 +1738,18 @@ nsAttrValue::ParseStyleAttribute(const n
       SetPtrValueAndType(cont, eOtherBase);
       return true;
     }
   }
 
   RefPtr<DeclarationBlock> decl;
   if (ownerDoc->GetStyleBackendType() == StyleBackendType::Servo) {
     RefPtr<URLExtraData> data = new URLExtraData(baseURI, docURI,
-                                                 principal);
+                                                 principal,
+                                                 ownerDoc->GetReferrerPolicy());
     decl = ServoDeclarationBlock::FromCssText(aString, data,
                                               ownerDoc->GetCompatibilityMode(),
                                               ownerDoc->CSSLoader());
   } else {
     css::Loader* cssLoader = ownerDoc->CSSLoader();
     nsCSSParser cssParser(cssLoader);
     decl = cssParser.ParseStyleAttribute(aString, docURI, baseURI,
                                          principal);
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -3808,21 +3808,22 @@ nsDocument::SetBaseURI(nsIURI* aURI)
 URLExtraData*
 nsIDocument::DefaultStyleAttrURLData()
 {
 #ifdef MOZ_STYLO
   MOZ_ASSERT(NS_IsMainThread());
   nsIURI* baseURI = GetDocBaseURI();
   nsIURI* docURI = GetDocumentURI();
   nsIPrincipal* principal = NodePrincipal();
+  mozilla::net::ReferrerPolicy policy = GetReferrerPolicy();
   if (!mCachedURLData ||
       mCachedURLData->BaseURI() != baseURI ||
       mCachedURLData->GetReferrer() != docURI ||
       mCachedURLData->GetPrincipal() != principal) {
-    mCachedURLData = new URLExtraData(baseURI, docURI, principal);
+    mCachedURLData = new URLExtraData(baseURI, docURI, principal, policy);
   }
   return mCachedURLData;
 #else
   MOZ_CRASH("Should not be called for non-stylo build");
   return nullptr;
 #endif
 }
 
--- a/dom/canvas/CanvasRenderingContext2D.cpp
+++ b/dom/canvas/CanvasRenderingContext2D.cpp
@@ -2823,17 +2823,18 @@ GetFontStyleContext(Element* aElement, c
 static already_AddRefed<RawServoDeclarationBlock>
 CreateDeclarationForServo(nsCSSPropertyID aProperty,
                           const nsAString& aPropertyValue,
                           nsIDocument* aDocument)
 {
   RefPtr<URLExtraData> data =
     new URLExtraData(aDocument->GetDocBaseURI(),
                      aDocument->GetDocumentURI(),
-                     aDocument->NodePrincipal());
+                     aDocument->NodePrincipal(),
+                     aDocument->GetReferrerPolicy());
 
   ServoCSSParser::ParsingEnvironment env(data,
                                          aDocument->GetCompatibilityMode(),
                                          aDocument->CSSLoader());
   RefPtr<RawServoDeclarationBlock> servoDeclarations =
     ServoCSSParser::ParseProperty(aProperty, aPropertyValue, env);
 
   if (!servoDeclarations) {
--- a/dom/html/nsGenericHTMLElement.cpp
+++ b/dom/html/nsGenericHTMLElement.cpp
@@ -986,16 +986,17 @@ nsGenericHTMLElement::ParseBackgroundAtt
     nsresult rv = nsContentUtils::NewURIWithDocumentCharset(
         getter_AddRefs(uri), aValue, doc, baseURI);
     if (NS_FAILED(rv)) {
       return false;
     }
 
     mozilla::css::URLValue *url =
       new mozilla::css::URLValue(uri, aValue, baseURI, doc->GetDocumentURI(),
+                                 doc->GetReferrerPolicy(),
                                  NodePrincipal());
     aResult.SetTo(url, &aValue);
     return true;
   }
 
   return false;
 }
 
--- a/dom/svg/SVGUseElement.cpp
+++ b/dom/svg/SVGUseElement.cpp
@@ -277,17 +277,18 @@ SVGUseElement::CreateAnonymousContent()
 
   // Store the base URI
   nsCOMPtr<nsIURI> baseURI = targetContent->GetBaseURI();
   if (!baseURI) {
     return nullptr;
   }
   mContentURLData = new URLExtraData(baseURI.forget(),
                                      do_AddRef(OwnerDoc()->GetDocumentURI()),
-                                     do_AddRef(NodePrincipal()));
+                                     do_AddRef(NodePrincipal()),
+                                     OwnerDoc()->GetReferrerPolicy());
 
   targetContent->AddMutationObserver(this);
 
 #ifdef DEBUG
   // Our anonymous clone can get restyled by various things
   // (e.g. SMIL).  Reconstructing its frame is OK, though, because
   // it's going to be our _only_ child in the frame tree, so can't get
   // mis-ordered with anything.
--- a/dom/svg/nsSVGElement.cpp
+++ b/dom/svg/nsSVGElement.cpp
@@ -1257,18 +1257,19 @@ MappedAttrParser::ParseMappedAttrValue(n
   if (propertyID != eCSSProperty_UNKNOWN) {
     bool changed = false; // outparam for ParseProperty.
     if (mBackend == StyleBackendType::Gecko) {
       mParser.ParseProperty(propertyID, aMappedAttrValue, mDocURI, mBaseURI,
                             mElement->NodePrincipal(), mDecl->AsGecko(), &changed, false, true);
     } else {
       NS_ConvertUTF16toUTF8 value(aMappedAttrValue);
       // FIXME (bug 1343964): Figure out a better solution for sending the base uri to servo
-      RefPtr<URLExtraData> data = new URLExtraData(mBaseURI, mDocURI,
-                                                   mElement->NodePrincipal());
+      RefPtr<URLExtraData> data =
+        new URLExtraData(mBaseURI, mDocURI, mElement->NodePrincipal(),
+                         mElement->OwnerDoc()->GetReferrerPolicy());
       changed = Servo_DeclarationBlock_SetPropertyById(
         mDecl->AsServo()->Raw(), propertyID, &value, false, data,
         ParsingMode::AllowUnitlessLength, mElement->OwnerDoc()->GetCompatibilityMode(), mLoader);
     }
 
     if (changed) {
       // The normal reporting of use counters by the nsCSSParser won't happen
       // since it doesn't have a sheet.
--- a/layout/style/ServoCSSParser.cpp
+++ b/layout/style/ServoCSSParser.cpp
@@ -115,17 +115,18 @@ ServoCSSParser::ParseFontShorthandForMat
 /* static */ already_AddRefed<URLExtraData>
 ServoCSSParser::GetURLExtraData(nsIDocument* aDocument)
 {
   MOZ_ASSERT(aDocument);
 
   // FIXME this is using the wrong base uri (bug 1343919)
   RefPtr<URLExtraData> url = new URLExtraData(aDocument->GetDocumentURI(),
                                               aDocument->GetDocumentURI(),
-                                              aDocument->NodePrincipal());
+                                              aDocument->NodePrincipal(),
+                                              aDocument->GetReferrerPolicy());
   return url.forget();
 }
 
 /* static */ ServoCSSParser::ParsingEnvironment
 ServoCSSParser::GetParsingEnvironment(nsIDocument* aDocument)
 {
   return ParsingEnvironment(GetURLExtraData(aDocument),
                             aDocument->GetCompatibilityMode(),
--- a/layout/style/ServoStyleSheet.cpp
+++ b/layout/style/ServoStyleSheet.cpp
@@ -202,17 +202,17 @@ ServoStyleSheet::ParseSheet(css::Loader*
                             nsIURI* aBaseURI,
                             nsIPrincipal* aSheetPrincipal,
                             uint32_t aLineNumber,
                             nsCompatibility aCompatMode,
                             css::LoaderReusableStyleSheets* aReusableSheets)
 {
   MOZ_ASSERT(!mMedia || mMedia->IsServo());
   RefPtr<URLExtraData> extraData =
-    new URLExtraData(aBaseURI, aSheetURI, aSheetPrincipal);
+    new URLExtraData(aBaseURI, aSheetURI, aSheetPrincipal, mInner->mReferrerPolicy);
 
   Inner()->mContents = Servo_StyleSheet_FromUTF8Bytes(aLoader,
                                                       this,
                                                       aInput.Elements(),
                                                       aInput.Length(),
                                                       mParsingMode,
                                                       extraData,
                                                       aLineNumber,
--- a/layout/style/URLExtraData.h
+++ b/layout/style/URLExtraData.h
@@ -7,61 +7,69 @@
 /* thread-safe container of information for resolving url values */
 
 #ifndef mozilla_URLExtraData_h
 #define mozilla_URLExtraData_h
 
 #include "mozilla/dom/URL.h"
 #include "mozilla/Move.h"
 #include "mozilla/StaticPtr.h"
+#include "mozilla/net/ReferrerPolicy.h"
 
 #include "nsCOMPtr.h"
 #include "nsIPrincipal.h"
 #include "nsIURI.h"
 
 namespace mozilla {
 
 struct URLExtraData
 {
   URLExtraData(already_AddRefed<nsIURI> aBaseURI,
                already_AddRefed<nsIURI> aReferrer,
-               already_AddRefed<nsIPrincipal> aPrincipal)
+               already_AddRefed<nsIPrincipal> aPrincipal,
+               net::ReferrerPolicy aReferrerPolicy = net::RP_Unset)
     : mBaseURI(Move(aBaseURI))
     , mReferrer(Move(aReferrer))
+    , mReferrerPolicy(aReferrerPolicy)
     , mPrincipal(Move(aPrincipal))
       // When we hold the URI data of a style sheet, mReferrer is always
       // equal to the sheet URI.
     , mIsChrome(mReferrer ? dom::IsChromeURI(mReferrer) : false)
   {
     MOZ_ASSERT(mBaseURI);
   }
 
-  URLExtraData(nsIURI* aBaseURI, nsIURI* aReferrer, nsIPrincipal* aPrincipal)
+  URLExtraData(nsIURI* aBaseURI, nsIURI* aReferrer,
+               nsIPrincipal* aPrincipal,
+               net::ReferrerPolicy aReferrerPolicy = net::RP_Unset)
     : URLExtraData(do_AddRef(aBaseURI),
                    do_AddRef(aReferrer),
-                   do_AddRef(aPrincipal)) {}
+                   do_AddRef(aPrincipal),
+                   aReferrerPolicy) {}
 
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(URLExtraData)
 
   nsIURI* BaseURI() const { return mBaseURI; }
   nsIURI* GetReferrer() const { return mReferrer; }
+  net::ReferrerPolicy GetReferrerPolicy() const { return mReferrerPolicy;}
   nsIPrincipal* GetPrincipal() const { return mPrincipal; }
 
   static URLExtraData* Dummy() {
     MOZ_ASSERT(sDummy);
     return sDummy;
   }
   static void InitDummy();
   static void ReleaseDummy();
 
 private:
   ~URLExtraData();
 
   nsCOMPtr<nsIURI> mBaseURI;
   nsCOMPtr<nsIURI> mReferrer;
+  net::ReferrerPolicy mReferrerPolicy;
   nsCOMPtr<nsIPrincipal> mPrincipal;
 
   // True if mReferrer is a chrome:// URI.
   const bool mIsChrome;
 
   static StaticRefPtr<URLExtraData> sDummy;
 };
 
--- a/layout/style/nsCSSParser.cpp
+++ b/layout/style/nsCSSParser.cpp
@@ -8139,18 +8139,21 @@ CSSParserImpl::SetValueToURL(nsCSSValue&
       return true;
     }
 
     NS_NOTREACHED("Codepaths that expect to parse URLs MUST pass in an "
                   "origin principal");
     return false;
   }
 
+  mozilla::net::ReferrerPolicy policy = mSheet ? mSheet->GetReferrerPolicy() :
+                                                 mozilla::net::RP_Unset;
   mozilla::css::URLValue *urlVal =
-    new mozilla::css::URLValue(aURL, mBaseURI, mSheetURI, mSheetPrincipal);
+    new mozilla::css::URLValue(aURL, mBaseURI, mSheetURI, policy, mSheetPrincipal);
+
   aValue.SetURLValue(urlVal);
   return true;
 }
 
 /**
  * Parse the image-orientation property, which has the grammar:
  * <angle> flip? | flip | from-image
  */
--- a/layout/style/nsCSSValue.cpp
+++ b/layout/style/nsCSSValue.cpp
@@ -3074,29 +3074,30 @@ css::URLValueData::SizeOfExcludingThis(m
   // Measurement of the following members may be added later if DMD finds it
   // is worthwhile:
   // - mURI
   // - mExtraData
   return n;
 }
 
 URLValue::URLValue(const nsAString& aString, nsIURI* aBaseURI, nsIURI* aReferrer,
-                   nsIPrincipal* aOriginPrincipal)
+                   net::ReferrerPolicy aPolicy, nsIPrincipal* aOriginPrincipal)
   : URLValueData(aString, do_AddRef(new URLExtraData(aBaseURI, aReferrer,
-                                                     aOriginPrincipal)))
+                                                     aOriginPrincipal, aPolicy)))
 {
   MOZ_ASSERT(NS_IsMainThread());
 }
 
 URLValue::URLValue(nsIURI* aURI, const nsAString& aString, nsIURI* aBaseURI,
-                   nsIURI* aReferrer, nsIPrincipal* aOriginPrincipal)
+                   nsIURI* aReferrer, net::ReferrerPolicy aPolicy,
+                   nsIPrincipal* aOriginPrincipal)
   : URLValueData(do_AddRef(new PtrHolder<nsIURI>("URLValueData::mURI", aURI)),
                  aString,
                  do_AddRef(new URLExtraData(aBaseURI, aReferrer,
-                                            aOriginPrincipal)))
+                                            aOriginPrincipal, aPolicy)))
 {
   MOZ_ASSERT(NS_IsMainThread());
 }
 
 size_t
 css::URLValue::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
 {
   // Only measure it if it's unshared, to avoid double-counting.
@@ -3161,17 +3162,19 @@ css::ImageValue::Initialize(nsIDocument*
   nsIDocument* loadingDoc = aDocument->GetOriginalDocument();
   if (!loadingDoc) {
     loadingDoc = aDocument;
   }
 
   if (!mLoadedImage) {
     loadingDoc->StyleImageLoader()->LoadImage(GetURI(),
                                               mExtraData->GetPrincipal(),
-                                              mExtraData->GetReferrer(), this);
+                                              mExtraData->GetReferrer(),
+                                              mExtraData->GetReferrerPolicy(),
+                                              this);
 
      mLoadedImage = true;
   }
 
   aDocument->StyleImageLoader()->MaybeRegisterCSSImage(this);
 }
 
 css::ImageValue::~ImageValue()
--- a/layout/style/nsCSSValue.h
+++ b/layout/style/nsCSSValue.h
@@ -11,16 +11,17 @@
 
 #include "mozilla/Attributes.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/ServoTypes.h"
 #include "mozilla/SheetType.h"
 #include "mozilla/StyleComplexColor.h"
 #include "mozilla/URLExtraData.h"
 #include "mozilla/UniquePtr.h"
+#include "mozilla/net/ReferrerPolicy.h"
 
 #include "nsCSSKeywords.h"
 #include "nsCSSPropertyID.h"
 #include "nsCSSProps.h"
 #include "nsColor.h"
 #include "nsCoord.h"
 #include "nsProxyRelease.h"
 #include "nsRefPtrHashtable.h"
@@ -215,19 +216,21 @@ private:
 
   friend struct ImageValue;
 };
 
 struct URLValue final : public URLValueData
 {
   // These two constructors are safe to call only on the main thread.
   URLValue(const nsAString& aString, nsIURI* aBaseURI, nsIURI* aReferrer,
+           mozilla::net::ReferrerPolicy aPolicy,
            nsIPrincipal* aOriginPrincipal);
   URLValue(nsIURI* aURI, const nsAString& aString, nsIURI* aBaseURI,
-           nsIURI* aReferrer, nsIPrincipal* aOriginPrincipal);
+           nsIURI* aReferrer, mozilla::net::ReferrerPolicy aPolicy,
+           nsIPrincipal* aOriginPrincipal);
 
   // This constructor is safe to call from any thread.
   URLValue(const nsAString& aString,
            already_AddRefed<URLExtraData> aExtraData)
     : URLValueData(aString, Move(aExtraData)) {}
 
   URLValue(const URLValue&) = delete;
   URLValue& operator=(const URLValue&) = delete;