REGRESSION: Reloading a local file doesn't pick up changes
https://bugs.webkit.org/show_bug.cgi?id=109344

Reviewed by Alexey Proskuryakov.

Source/WebCore:

Test: http/tests/cache/reload-main-resource.php

* loader/cache/CachedResource.cpp:
(WebCore::CachedResource::load):
* loader/cache/CachedResourceLoader.cpp:
(WebCore::CachedResourceLoader::determineRevalidationPolicy):
(WebCore::CachedResourceLoader::cachePolicy): Don't use subresourceCachePolicy()
    for main resources.
* loader/cache/CachedResourceLoader.h:
(CachedResourceLoader):

LayoutTests:

* http/tests/cache/reload-main-resource-expected.txt: Added.
* http/tests/cache/reload-main-resource.php: Added.
* http/tests/cache/resources/reload-main-resource-iframe.php: Added.
* http/tests/misc/favicon-loads-with-images-disabled-expected.txt: This test
    was being loaded from memory cache in spite of being loaded via reload. We
    shouldn't do that.
* http/tests/misc/link-rel-icon-beforeload-expected.txt: This test
    was being loaded from memory cache in spite of being loaded via reload. We
    shouldn't do that.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@142707 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index b1e9584..33f73f2 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,20 @@
+2013-02-12  Nate Chapin  <japhet@chromium.org>
+
+        REGRESSION: Reloading a local file doesn't pick up changes
+        https://bugs.webkit.org/show_bug.cgi?id=109344
+
+        Reviewed by Alexey Proskuryakov.
+
+        * http/tests/cache/reload-main-resource-expected.txt: Added.
+        * http/tests/cache/reload-main-resource.php: Added.
+        * http/tests/cache/resources/reload-main-resource-iframe.php: Added.
+        * http/tests/misc/favicon-loads-with-images-disabled-expected.txt: This test
+            was being loaded from memory cache in spite of being loaded via reload. We
+            shouldn't do that.
+        * http/tests/misc/link-rel-icon-beforeload-expected.txt: This test
+            was being loaded from memory cache in spite of being loaded via reload. We
+            shouldn't do that.
+
 2013-02-12  Dima Gorbik  <dgorbik@apple.com>
 
         Mac rebaseline for r142638.
diff --git a/LayoutTests/http/tests/cache/reload-main-resource-expected.txt b/LayoutTests/http/tests/cache/reload-main-resource-expected.txt
new file mode 100644
index 0000000..69cfc5a
--- /dev/null
+++ b/LayoutTests/http/tests/cache/reload-main-resource-expected.txt
@@ -0,0 +1,2 @@
+PASS
+
diff --git a/LayoutTests/http/tests/cache/reload-main-resource.php b/LayoutTests/http/tests/cache/reload-main-resource.php
new file mode 100644
index 0000000..14cd750
--- /dev/null
+++ b/LayoutTests/http/tests/cache/reload-main-resource.php
@@ -0,0 +1,27 @@
+<?php
+require_once '../resources/portabilityLayer.php';
+clearstatcache();
+if (file_exists(sys_get_temp_dir() . "/reload-main-resource.tmp"))
+    unlink(sys_get_temp_dir() . "/reload-main-resource.tmp");
+?>
+
+<body>
+<div id="result"></div>
+<script>
+if (window.testRunner) {
+    testRunner.dumpAsText();
+    testRunner.waitUntilDone();
+}
+
+function reloadIframe() {
+    window.frames[0].location.reload();
+}
+
+function finish() {
+    document.getElementById("result").innerText = "PASS";
+    if (window.testRunner)
+        testRunner.notifyDone();
+}
+</script>
+<iframe src="resources/reload-main-resource-iframe.php" onload="reloadIframe();"></iframe>
+</body>
diff --git a/LayoutTests/http/tests/cache/resources/reload-main-resource-iframe.php b/LayoutTests/http/tests/cache/resources/reload-main-resource-iframe.php
new file mode 100644
index 0000000..1a04288
--- /dev/null
+++ b/LayoutTests/http/tests/cache/resources/reload-main-resource-iframe.php
@@ -0,0 +1,23 @@
+<?php
+if (file_exists(sys_get_temp_dir() . "/reload-main-resource.tmp")) {
+    unlink(sys_get_temp_dir() . "/reload-main-resource.tmp");
+    echo "<body>";
+    echo "<script>";
+    echo "window.parent.finish();";
+    echo "</script>";
+    echo "</body>";
+    exit();
+}
+
+$tmpFile = fopen(sys_get_temp_dir() . "/reload-main-resource.tmp", 'w');
+fclose($tmpFile);
+
+$max_age = 12 * 31 * 24 * 60 * 60; //one year
+$last_modified = gmdate(DATE_RFC1123, time() + $max_age);
+$expires = gmdate(DATE_RFC1123, time() + $max_age);
+
+header('Cache-Control: public, max-age=' . $max_age);
+header('Expires: ' . $expires);
+header('Content-Type: text/html');
+header('Last-Modified: ' . $last_modified);
+?>
diff --git a/LayoutTests/http/tests/misc/favicon-loads-with-images-disabled-expected.txt b/LayoutTests/http/tests/misc/favicon-loads-with-images-disabled-expected.txt
index c23bbfc..c6c9bc5 100644
--- a/LayoutTests/http/tests/misc/favicon-loads-with-images-disabled-expected.txt
+++ b/LayoutTests/http/tests/misc/favicon-loads-with-images-disabled-expected.txt
@@ -1,7 +1,6 @@
 <unknown> - didFinishLoading
 http://127.0.0.1:8000/misc/favicon-loads-with-images-disabled.html - willSendRequest <NSURLRequest URL http://127.0.0.1:8000/misc/favicon-loads-with-images-disabled.html, main document URL http://127.0.0.1:8000/misc/favicon-loads-with-images-disabled.html, http method GET> redirectResponse (null)
 http://127.0.0.1:8000/misc/favicon-loads-with-images-disabled.html - didReceiveResponse <NSURLResponse http://127.0.0.1:8000/misc/favicon-loads-with-images-disabled.html, http status code 200>
-http://127.0.0.1:8000/misc/favicon-loads-with-images-disabled.html - didFinishLoading
 Radar 6973106 and https://bugs.webkit.org/show_bug.cgi?id=27896 - Favicons still load when automatic image loading is disabled.
 This test uses DRT's resource load delegate callback mode to see if the favicon is loaded even when image loading is off.
 
diff --git a/LayoutTests/http/tests/misc/link-rel-icon-beforeload-expected.txt b/LayoutTests/http/tests/misc/link-rel-icon-beforeload-expected.txt
index b51ac55..9f3ee65 100644
--- a/LayoutTests/http/tests/misc/link-rel-icon-beforeload-expected.txt
+++ b/LayoutTests/http/tests/misc/link-rel-icon-beforeload-expected.txt
@@ -1,6 +1,5 @@
 <unknown> - didFinishLoading
 http://127.0.0.1:8000/misc/link-rel-icon-beforeload.html - willSendRequest <NSURLRequest URL http://127.0.0.1:8000/misc/link-rel-icon-beforeload.html, main document URL http://127.0.0.1:8000/misc/link-rel-icon-beforeload.html, http method GET> redirectResponse (null)
 http://127.0.0.1:8000/misc/link-rel-icon-beforeload.html - didReceiveResponse <NSURLResponse http://127.0.0.1:8000/misc/link-rel-icon-beforeload.html, http status code 200>
-http://127.0.0.1:8000/misc/link-rel-icon-beforeload.html - didFinishLoading
 http://127.0.0.1:8000/favicon.ico - willSendRequest <NSURLRequest URL http://127.0.0.1:8000/favicon.ico, main document URL http://127.0.0.1:8000/misc/link-rel-icon-beforeload.html, http method GET> redirectResponse (null)
 This test should not show a request for the favicon dont-load-this.ico, since the beforeload handler on the favicon link returns false. Therefore, if the resource request list below shows a request for dont-load-this.ico, then this test has failed.
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 23b6183..053bccf 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,21 @@
+2013-02-12  Nate Chapin  <japhet@chromium.org>
+
+        REGRESSION: Reloading a local file doesn't pick up changes
+        https://bugs.webkit.org/show_bug.cgi?id=109344
+
+        Reviewed by Alexey Proskuryakov.
+
+        Test: http/tests/cache/reload-main-resource.php
+
+        * loader/cache/CachedResource.cpp:
+        (WebCore::CachedResource::load):
+        * loader/cache/CachedResourceLoader.cpp:
+        (WebCore::CachedResourceLoader::determineRevalidationPolicy):
+        (WebCore::CachedResourceLoader::cachePolicy): Don't use subresourceCachePolicy()
+            for main resources.
+        * loader/cache/CachedResourceLoader.h:
+        (CachedResourceLoader):
+
 2013-02-12  Ryosuke Niwa  <rniwa@webkit.org>
 
         Turn avoidIntersectionWithNode into Editor member functions to encapsulate delete button controller
diff --git a/Source/WebCore/loader/cache/CachedResource.cpp b/Source/WebCore/loader/cache/CachedResource.cpp
index fd98ba4..98593b2 100644
--- a/Source/WebCore/loader/cache/CachedResource.cpp
+++ b/Source/WebCore/loader/cache/CachedResource.cpp
@@ -325,8 +325,8 @@
         const String& lastModified = resourceToRevalidate->response().httpHeaderField("Last-Modified");
         const String& eTag = resourceToRevalidate->response().httpHeaderField("ETag");
         if (!lastModified.isEmpty() || !eTag.isEmpty()) {
-            ASSERT(cachedResourceLoader->cachePolicy() != CachePolicyReload);
-            if (cachedResourceLoader->cachePolicy() == CachePolicyRevalidate)
+            ASSERT(cachedResourceLoader->cachePolicy(type()) != CachePolicyReload);
+            if (cachedResourceLoader->cachePolicy(type()) == CachePolicyRevalidate)
                 m_resourceRequest.setHTTPHeaderField("Cache-Control", "max-age=0");
             if (!lastModified.isEmpty())
                 m_resourceRequest.setHTTPHeaderField("If-Modified-Since", lastModified);
diff --git a/Source/WebCore/loader/cache/CachedResourceLoader.cpp b/Source/WebCore/loader/cache/CachedResourceLoader.cpp
index 74323f9..81435ac 100644
--- a/Source/WebCore/loader/cache/CachedResourceLoader.cpp
+++ b/Source/WebCore/loader/cache/CachedResourceLoader.cpp
@@ -584,7 +584,7 @@
         return Use;
     
     // CachePolicyHistoryBuffer uses the cache no matter what.
-    if (cachePolicy() == CachePolicyHistoryBuffer)
+    if (cachePolicy(type) == CachePolicyHistoryBuffer)
         return Use;
 
     // Don't reuse resources with Cache-control: no-store.
@@ -609,7 +609,7 @@
         return Use;
 
     // CachePolicyReload always reloads
-    if (cachePolicy() == CachePolicyReload) {
+    if (cachePolicy(type) == CachePolicyReload) {
         LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to CachePolicyReload.");
         return Reload;
     }
@@ -625,7 +625,7 @@
         return Use;
 
     // Check if the cache headers requires us to revalidate (cache expiration for example).
-    if (existingResource->mustRevalidateDueToCacheHeaders(cachePolicy())) {
+    if (existingResource->mustRevalidateDueToCacheHeaders(cachePolicy(type))) {
         // See if the resource has usable ETag or Last-modified headers.
         if (existingResource->canUseCacheValidator())
             return Revalidate;
@@ -701,9 +701,17 @@
     }
 }
 
-CachePolicy CachedResourceLoader::cachePolicy() const
+CachePolicy CachedResourceLoader::cachePolicy(CachedResource::Type type) const
 {
-    return frame() ? frame()->loader()->subresourceCachePolicy() : CachePolicyVerify;
+    if (!frame())
+        return CachePolicyVerify;
+
+    if (type != CachedResource::MainResource)
+        return frame()->loader()->subresourceCachePolicy();
+    
+    if (frame()->loader()->loadType() == FrameLoadTypeReloadFromOrigin || frame()->loader()->loadType() == FrameLoadTypeReload)
+        return CachePolicyReload;
+    return CachePolicyVerify;
 }
 
 void CachedResourceLoader::removeCachedResource(CachedResource* resource) const
diff --git a/Source/WebCore/loader/cache/CachedResourceLoader.h b/Source/WebCore/loader/cache/CachedResourceLoader.h
index 8cf311c..dae51f3 100644
--- a/Source/WebCore/loader/cache/CachedResourceLoader.h
+++ b/Source/WebCore/loader/cache/CachedResourceLoader.h
@@ -112,7 +112,7 @@
 
     bool shouldDeferImageLoad(const KURL&) const;
     
-    CachePolicy cachePolicy() const;
+    CachePolicy cachePolicy(CachedResource::Type) const;
     
     Frame* frame() const; // Can be null
     Document* document() const { return m_document; } // Can be null