Bug 704320 - content/dom changes for meta referrer support. (r=jst)
authorSid Stamm <sstamm@mozilla.com>
Tue, 18 Nov 2014 08:47:03 -0500
changeset 240601 5f0f56a36128be35208c853416ebbed793f08d06
parent 240600 3511a31f2cd316c86564127f6d544d3fc6a7d73a
child 240602 d76316a3c1dd07d5d6a3412ac8a0e7169ab1320f
push id4311
push userraliiev@mozilla.com
push dateMon, 12 Jan 2015 19:37:41 +0000
treeherdermozilla-beta@150c9fed433b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjst
bugs704320
milestone36.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 704320 - content/dom changes for meta referrer support. (r=jst) This enables referrer policies for: - EventSource (content/base/src/EventSource) - XMLHttpRequest (content/base/src/nsXMLHttpRequest) - HTML media elements (content/html/content/src/HTMLMediaElement) - window.open (embedding/components/windowwatcher) - window.location (dom/base/nsLocation)
dom/base/EventSource.cpp
dom/base/EventSource.h
dom/base/nsLocation.cpp
dom/base/nsXMLHttpRequest.cpp
dom/html/HTMLMediaElement.cpp
embedding/components/windowwatcher/nsWindowWatcher.cpp
--- a/dom/base/EventSource.cpp
+++ b/dom/base/EventSource.cpp
@@ -695,16 +695,27 @@ EventSource::GetBaseURI(nsIURI **aBaseUR
   }
 
   NS_ENSURE_STATE(baseURI);
 
   baseURI.forget(aBaseURI);
   return NS_OK;
 }
 
+net::ReferrerPolicy
+EventSource::GetReferrerPolicy()
+{
+  nsresult rv;
+  nsIScriptContext* sc = GetContextForEventHandlers(&rv);
+  NS_ENSURE_SUCCESS(rv, mozilla::net::RP_Default);
+
+  nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromScriptContext(sc);
+  return doc ? doc->GetReferrerPolicy() : mozilla::net::RP_Default;
+}
+
 nsresult
 EventSource::SetupHttpChannel()
 {
   mHttpChannel->SetRequestMethod(NS_LITERAL_CSTRING("GET"));
 
   /* set the http request headers */
 
   mHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
@@ -715,17 +726,17 @@ EventSource::SetupHttpChannel()
   if (!mLastEventID.IsEmpty()) {
     mHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Last-Event-ID"),
       NS_ConvertUTF16toUTF8(mLastEventID), false);
   }
 
   nsCOMPtr<nsIURI> codebase;
   nsresult rv = GetBaseURI(getter_AddRefs(codebase));
   if (NS_SUCCEEDED(rv)) {
-    rv = mHttpChannel->SetReferrer(codebase);
+    rv = mHttpChannel->SetReferrerWithPolicy(codebase, this->GetReferrerPolicy());
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   return NS_OK;
 }
 
 nsresult
 EventSource::InitChannelAndRequestEventSource()
--- a/dom/base/EventSource.h
+++ b/dom/base/EventSource.h
@@ -104,16 +104,18 @@ protected:
   virtual ~EventSource();
 
   nsresult Init(nsISupports* aOwner,
                 const nsAString& aURL,
                 bool aWithCredentials);
 
   nsresult GetBaseURI(nsIURI **aBaseURI);
 
+  net::ReferrerPolicy GetReferrerPolicy();
+
   nsresult SetupHttpChannel();
   nsresult InitChannelAndRequestEventSource();
   nsresult ResetConnection();
   nsresult DispatchFailConnection();
   nsresult SetReconnectionTimeout();
 
   void AnnounceConnection();
   void DispatchAllMessageEvents();
--- a/dom/base/nsLocation.cpp
+++ b/dom/base/nsLocation.cpp
@@ -109,16 +109,17 @@ nsLocation::CheckURL(nsIURI* aURI, nsIDo
 {
   *aLoadInfo = nullptr;
 
   nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
   NS_ENSURE_TRUE(docShell, NS_ERROR_NOT_AVAILABLE);
 
   nsCOMPtr<nsISupports> owner;
   nsCOMPtr<nsIURI> sourceURI;
+  net::ReferrerPolicy referrerPolicy = net::RP_Default;
 
   if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
     // No cx means that there's no JS running, or at least no JS that
     // was run through code that properly pushed a context onto the
     // context stack (as all code that runs JS off of web pages
     // does). We won't bother with security checks in this case, but
     // we need to create the loadinfo etc.
 
@@ -144,16 +145,17 @@ nsLocation::CheckURL(nsIURI* aURI, nsIDo
     if (incumbent) {
       doc = incumbent->GetDoc();
     }
     if (doc) {
       docOriginalURI = doc->GetOriginalURI();
       docCurrentURI = doc->GetDocumentURI();
       rv = doc->NodePrincipal()->GetURI(getter_AddRefs(principalURI));
       NS_ENSURE_SUCCESS(rv, rv);
+      referrerPolicy = doc->GetReferrerPolicy();
     }
 
     bool urisEqual = false;
     if (docOriginalURI && docCurrentURI && principalURI) {
       principalURI->Equals(docOriginalURI, &urisEqual);
     }
 
     if (urisEqual) {
@@ -181,16 +183,17 @@ nsLocation::CheckURL(nsIURI* aURI, nsIDo
   nsCOMPtr<nsIDocShellLoadInfo> loadInfo;
   docShell->CreateLoadInfo(getter_AddRefs(loadInfo));
   NS_ENSURE_TRUE(loadInfo, NS_ERROR_FAILURE);
 
   loadInfo->SetOwner(owner);
 
   if (sourceURI) {
     loadInfo->SetReferrer(sourceURI);
+    loadInfo->SetReferrerPolicy(referrerPolicy);
   }
 
   loadInfo.swap(*aLoadInfo);
 
   return NS_OK;
 }
 
 nsresult
--- a/dom/base/nsXMLHttpRequest.cpp
+++ b/dom/base/nsXMLHttpRequest.cpp
@@ -2717,35 +2717,38 @@ nsXMLHttpRequest::Send(nsIVariant* aVari
 
       nsIScriptContext* sc = GetContextForEventHandlers(&rv);
       NS_ENSURE_SUCCESS(rv, rv);
       nsCOMPtr<nsIDocument> doc =
         nsContentUtils::GetDocumentFromScriptContext(sc);
 
       nsCOMPtr<nsIURI> docCurURI;
       nsCOMPtr<nsIURI> docOrigURI;
+      net::ReferrerPolicy referrerPolicy = net::RP_Default;
+
       if (doc) {
         docCurURI = doc->GetDocumentURI();
         docOrigURI = doc->GetOriginalURI();
+        referrerPolicy = doc->GetReferrerPolicy();
       }
 
       nsCOMPtr<nsIURI> referrerURI;
 
       if (principalURI && docCurURI && docOrigURI) {
         bool equal = false;
         principalURI->Equals(docOrigURI, &equal);
         if (equal) {
           referrerURI = docCurURI;
         }
       }
 
       if (!referrerURI)
         referrerURI = principalURI;
 
-      httpChannel->SetReferrer(referrerURI);
+      httpChannel->SetReferrerWithPolicy(referrerURI, referrerPolicy);
     }
 
     // Some extensions override the http protocol handler and provide their own
     // implementation. The channels returned from that implementation doesn't
     // seem to always implement the nsIUploadChannel2 interface, presumably
     // because it's a new interface.
     // Eventually we should remove this and simply require that http channels
     // implement the new interface.
--- a/dom/html/HTMLMediaElement.cpp
+++ b/dom/html/HTMLMediaElement.cpp
@@ -3714,17 +3714,18 @@ void HTMLMediaElement::SetRequestHeaders
   // which prevents us from estimating the video length (if explicit Content-Duration
   // and a length spec in the container are not present either) and from seeking.
   // So, disable the standard "Accept-Encoding: gzip,deflate" that we usually send.
   // See bug 614760.
   aChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept-Encoding"),
                              EmptyCString(), false);
 
   // Set the Referer header
-  aChannel->SetReferrer(OwnerDoc()->GetDocumentURI());
+  aChannel->SetReferrerWithPolicy(OwnerDoc()->GetDocumentURI(),
+                                  OwnerDoc()->GetReferrerPolicy());
 }
 
 void HTMLMediaElement::FireTimeUpdate(bool aPeriodic)
 {
   NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
 
   TimeStamp now = TimeStamp::Now();
   double time = CurrentTime();
--- a/embedding/components/windowwatcher/nsWindowWatcher.cpp
+++ b/embedding/components/windowwatcher/nsWindowWatcher.cpp
@@ -903,16 +903,17 @@ nsWindowWatcher::OpenWindowInternal(nsID
          it has no better answer, and we only care about a real document.
          Also using GetDocument to force document creation seems to
          screw up focus in the hidden window; see bug 36016.
       */
       nsCOMPtr<nsIDocument> doc = referrerWindow->GetExtantDoc();
       if (doc) {
         // Set the referrer
         loadInfo->SetReferrer(doc->GetDocumentURI());
+        loadInfo->SetReferrerPolicy(doc->GetReferrerPolicy());
       }
     }
   }
 
   if (isNewToplevelWindow) {
     // Notify observers that the window is open and ready.
     // The window has not yet started to load a document.
     nsCOMPtr<nsIObserverService> obsSvc =