Re-set referrer on issuing a validation request for a cached resource.

If a cached resource needs to be revalidated, then supply the referrer
of the document making the request.

R=japhet@chromium.org
BUG=344335

Review URL: https://codereview.chromium.org/201973002

git-svn-id: svn://svn.chromium.org/blink/trunk@169392 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/LayoutTests/http/tests/cache/resources/referrer-echo.php b/LayoutTests/http/tests/cache/resources/referrer-echo.php
new file mode 100644
index 0000000..0be4235
--- /dev/null
+++ b/LayoutTests/http/tests/cache/resources/referrer-echo.php
@@ -0,0 +1,5 @@
+<?php
+header("Last-Modified: " . gmdate(DATE_RFC1123, time()));
+header("Content-Type: text/javascript");
+echo "var referrer = '" . (isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "none") . "';";
+?>
diff --git a/LayoutTests/http/tests/cache/resources/subresource-revalidation-referrer-iframe.html b/LayoutTests/http/tests/cache/resources/subresource-revalidation-referrer-iframe.html
new file mode 100644
index 0000000..6a7993e
--- /dev/null
+++ b/LayoutTests/http/tests/cache/resources/subresource-revalidation-referrer-iframe.html
@@ -0,0 +1,9 @@
+<!DOCTYPE HTML>
+<script src="referrer-echo.php" type="text/javascript"></script>
+<script>
+window.onload = function () {
+    if (window.parent)
+        window.parent.iframeLoaded(referrer);
+};
+</script>
+<a href="subresource-revalidation-referrer-iframe.html">Click</a>
diff --git a/LayoutTests/http/tests/cache/subresource-revalidation-referrer-expected.txt b/LayoutTests/http/tests/cache/subresource-revalidation-referrer-expected.txt
new file mode 100644
index 0000000..25280c7
--- /dev/null
+++ b/LayoutTests/http/tests/cache/subresource-revalidation-referrer-expected.txt
@@ -0,0 +1,11 @@
+Check that Referrer: for validation requests are correctly set.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS referrer.indexOf("subresource-revalidation-referrer.html") >= 0 is true
+PASS referrer.indexOf("subresource-revalidation-referrer-iframe.html") >= 0 is true
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/http/tests/cache/subresource-revalidation-referrer.html b/LayoutTests/http/tests/cache/subresource-revalidation-referrer.html
new file mode 100644
index 0000000..8b83911
--- /dev/null
+++ b/LayoutTests/http/tests/cache/subresource-revalidation-referrer.html
@@ -0,0 +1,25 @@
+<!DOCTYPE HTML>
+<script src="/js-test-resources/js-test.js"></script>
+<script src="resources/referrer-echo.php" type="text/javascript"></script>
+<script>
+description("Check that Referrer: for validation requests are correctly set.");
+window.jsTestIsAsync = true;
+
+// The above script adds the 'referrer' global.
+shouldBeTrue('referrer.indexOf("subresource-revalidation-referrer.html") >= 0');
+
+var first = true;
+function iframeLoaded(ref) {
+    if (first) {
+        // Navigating the iframe-embedded anchor will use the iframe's
+        // URL as referrer.
+        first = false;
+        window.frames[0].document.getElementsByTagName("a")[0].click();
+    } else {
+        referrer = ref;
+        shouldBeTrue('referrer.indexOf("subresource-revalidation-referrer-iframe.html") >= 0');
+        finishJSTest();
+    }
+}
+</script>
+<iframe src="resources/subresource-revalidation-referrer-iframe.html"></iframe>
diff --git a/Source/core/fetch/ResourceFetcher.cpp b/Source/core/fetch/ResourceFetcher.cpp
index 6b6e2d2..ce1e184 100644
--- a/Source/core/fetch/ResourceFetcher.cpp
+++ b/Source/core/fetch/ResourceFetcher.cpp
@@ -780,6 +780,7 @@
     ASSERT(!resource->resourceToRevalidate());
 
     ResourceRequest revalidatingRequest(resource->resourceRequest());
+    revalidatingRequest.clearHTTPReferrer();
     addAdditionalRequestHeaders(revalidatingRequest, resource->type());
 
     const AtomicString& lastModified = resource->response().httpHeaderField("Last-Modified");