Commit 8c8325fc authored by Ayu Ishii's avatar Ayu Ishii Committed by Commit Bot

Set net::LOAD_VALIDATE_CACHE flag instead when requesting service worker...

Set net::LOAD_VALIDATE_CACHE flag instead when requesting service worker scripts with viaCache value

Bug: 925249
Change-Id: I45578d8c0f61676b0958cfab9a7057796c0d2d0d
Reviewed-on: https://chromium-review.googlesource.com/c/1474386
Commit-Queue: Ayu Ishii <ayui@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636576}
parent b8229f45
...@@ -96,7 +96,7 @@ ServiceWorkerNewScriptLoader::ServiceWorkerNewScriptLoader( ...@@ -96,7 +96,7 @@ ServiceWorkerNewScriptLoader::ServiceWorkerNewScriptLoader(
resource_request.headers.SetHeader("Service-Worker", "script"); resource_request.headers.SetHeader("Service-Worker", "script");
} }
// Bypass the browser cache if needed, e.g., updateViaCache demands it or 24 // Validate the browser cache if needed, e.g., updateViaCache demands it or 24
// hours passed since the last update check that hit network. // hours passed since the last update check that hit network.
base::TimeDelta time_since_last_check = base::TimeDelta time_since_last_check =
base::Time::Now() - registration->last_update_check(); base::Time::Now() - registration->last_update_check();
...@@ -104,7 +104,7 @@ ServiceWorkerNewScriptLoader::ServiceWorkerNewScriptLoader( ...@@ -104,7 +104,7 @@ ServiceWorkerNewScriptLoader::ServiceWorkerNewScriptLoader(
is_main_script, registration->update_via_cache()) || is_main_script, registration->update_via_cache()) ||
time_since_last_check > kServiceWorkerScriptMaxCacheAge || time_since_last_check > kServiceWorkerScriptMaxCacheAge ||
version_->force_bypass_cache_for_scripts()) { version_->force_bypass_cache_for_scripts()) {
resource_request.load_flags |= net::LOAD_BYPASS_CACHE; resource_request.load_flags |= net::LOAD_VALIDATE_CACHE;
} }
ServiceWorkerStorage* storage = version_->context()->storage(); ServiceWorkerStorage* storage = version_->context()->storage();
......
...@@ -667,7 +667,7 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, Update_IdenticalScript) { ...@@ -667,7 +667,7 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, Update_IdenticalScript) {
EXPECT_EQ(0UL, version_->script_cache_map()->size()); EXPECT_EQ(0UL, version_->script_cache_map()->size());
} }
// Tests cache bypassing behavior when updateViaCache is 'all'. // Tests cache validation behavior when updateViaCache is 'all'.
TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_All) { TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_All) {
std::unique_ptr<network::TestURLLoaderClient> client; std::unique_ptr<network::TestURLLoaderClient> client;
std::unique_ptr<ServiceWorkerNewScriptLoader> loader; std::unique_ptr<ServiceWorkerNewScriptLoader> loader;
...@@ -680,37 +680,37 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_All) { ...@@ -680,37 +680,37 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_All) {
options.update_via_cache = blink::mojom::ServiceWorkerUpdateViaCache::kAll; options.update_via_cache = blink::mojom::ServiceWorkerUpdateViaCache::kAll;
SetUpRegistrationWithOptions(kScriptURL, options); SetUpRegistrationWithOptions(kScriptURL, options);
// Install the main script and imported script. The cache should be bypassed // Install the main script and imported script. The cache should validate
// since last update time is null. // since last update time is null.
DoRequest(kScriptURL, &client, &loader); DoRequest(kScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
network::ResourceRequest request = mock_url_loader_factory_->last_request(); network::ResourceRequest request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
DoRequest(kImportedScriptURL, &client, &loader); DoRequest(kImportedScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
// Promote to active and prepare to update. // Promote to active and prepare to update.
ActivateVersion(); ActivateVersion();
registration_->set_last_update_check(base::Time::Now()); registration_->set_last_update_check(base::Time::Now());
// Attempt to update. The requests should not bypass cache since the last // Attempt to update. The requests should not validate the cache since the
// update was recent. // last update was recent.
SetUpVersion(kScriptURL); SetUpVersion(kScriptURL);
DoRequest(kScriptURL, &client, &loader); DoRequest(kScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_FALSE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_FALSE(request.load_flags & net::LOAD_VALIDATE_CACHE);
DoRequest(kImportedScriptURL, &client, &loader); DoRequest(kImportedScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_FALSE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_FALSE(request.load_flags & net::LOAD_VALIDATE_CACHE);
// Set update check to far in the past and repeat. The requests should bypass // Set update check to far in the past and repeat. The requests should
// cache. // validate the cache.
registration_->set_last_update_check(base::Time::Now() - registration_->set_last_update_check(base::Time::Now() -
base::TimeDelta::FromHours(24)); base::TimeDelta::FromHours(24));
...@@ -718,15 +718,15 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_All) { ...@@ -718,15 +718,15 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_All) {
DoRequest(kScriptURL, &client, &loader); DoRequest(kScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
DoRequest(kImportedScriptURL, &client, &loader); DoRequest(kImportedScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
} }
// Tests cache bypassing behavior when updateViaCache is 'imports'. // Tests cache validation behavior when updateViaCache is 'imports'.
TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_Imports) { TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_Imports) {
std::unique_ptr<network::TestURLLoaderClient> client; std::unique_ptr<network::TestURLLoaderClient> client;
std::unique_ptr<ServiceWorkerNewScriptLoader> loader; std::unique_ptr<ServiceWorkerNewScriptLoader> loader;
...@@ -740,36 +740,36 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_Imports) { ...@@ -740,36 +740,36 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_Imports) {
blink::mojom::ServiceWorkerUpdateViaCache::kImports; blink::mojom::ServiceWorkerUpdateViaCache::kImports;
SetUpRegistrationWithOptions(kScriptURL, options); SetUpRegistrationWithOptions(kScriptURL, options);
// Install the main script and imported script. The cache should be bypassed // Install the main script and imported script. The cache should be validated
// since last update time is null. // since last update time is null.
DoRequest(kScriptURL, &client, &loader); DoRequest(kScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
network::ResourceRequest request = mock_url_loader_factory_->last_request(); network::ResourceRequest request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
DoRequest(kImportedScriptURL, &client, &loader); DoRequest(kImportedScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
// Promote to active and prepare to update. // Promote to active and prepare to update.
ActivateVersion(); ActivateVersion();
registration_->set_last_update_check(base::Time::Now()); registration_->set_last_update_check(base::Time::Now());
// Attempt to update. Only the imported script should bypass cache because // Attempt to update. Only the imported script should validate the cache
// kImports. // because kImports.
SetUpVersion(kScriptURL); SetUpVersion(kScriptURL);
DoRequest(kScriptURL, &client, &loader); DoRequest(kScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
DoRequest(kImportedScriptURL, &client, &loader); DoRequest(kImportedScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_FALSE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_FALSE(request.load_flags & net::LOAD_VALIDATE_CACHE);
// Set the time to far in the past and repeat. The requests should bypass // Set the time to far in the past and repeat. The requests should validate
// cache. // cache.
registration_->set_last_update_check(base::Time::Now() - registration_->set_last_update_check(base::Time::Now() -
base::TimeDelta::FromHours(24)); base::TimeDelta::FromHours(24));
...@@ -778,15 +778,15 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_Imports) { ...@@ -778,15 +778,15 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_Imports) {
DoRequest(kScriptURL, &client, &loader); DoRequest(kScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
DoRequest(kImportedScriptURL, &client, &loader); DoRequest(kImportedScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
} }
// Tests cache bypassing behavior when updateViaCache is 'none'. // Tests cache validation behavior when updateViaCache is 'none'.
TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_None) { TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_None) {
const GURL kScriptURL(kNormalScriptURL); const GURL kScriptURL(kNormalScriptURL);
const GURL kImportedScriptURL(kNormalImportedScriptURL); const GURL kImportedScriptURL(kNormalImportedScriptURL);
...@@ -799,33 +799,33 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_None) { ...@@ -799,33 +799,33 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, UpdateViaCache_None) {
options.update_via_cache = blink::mojom::ServiceWorkerUpdateViaCache::kNone; options.update_via_cache = blink::mojom::ServiceWorkerUpdateViaCache::kNone;
SetUpRegistrationWithOptions(kScriptURL, options); SetUpRegistrationWithOptions(kScriptURL, options);
// Install the main script and imported script. The cache should be bypassed // Install the main script and imported script. The cache should be validated
// since kNone (and the last update time is null anyway). // since kNone (and the last update time is null anyway).
DoRequest(kScriptURL, &client, &loader); DoRequest(kScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
network::ResourceRequest request = mock_url_loader_factory_->last_request(); network::ResourceRequest request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
DoRequest(kImportedScriptURL, &client, &loader); DoRequest(kImportedScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
// Promote to active and prepare to update. // Promote to active and prepare to update.
ActivateVersion(); ActivateVersion();
registration_->set_last_update_check(base::Time::Now()); registration_->set_last_update_check(base::Time::Now());
// Attempt to update. The requests should bypass cache because KNone. // Attempt to update. The requests should validate the cache because KNone.
SetUpVersion(kScriptURL); SetUpVersion(kScriptURL);
DoRequest(kScriptURL, &client, &loader); DoRequest(kScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
DoRequest(kImportedScriptURL, &client, &loader); DoRequest(kImportedScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
} }
// Tests respecting ServiceWorkerVersion's |force_bypass_cache_for_scripts| // Tests respecting ServiceWorkerVersion's |force_bypass_cache_for_scripts|
...@@ -843,27 +843,27 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, ForceBypassCache) { ...@@ -843,27 +843,27 @@ TEST_F(ServiceWorkerNewScriptLoaderTest, ForceBypassCache) {
// should win. // should win.
options.update_via_cache = blink::mojom::ServiceWorkerUpdateViaCache::kAll; options.update_via_cache = blink::mojom::ServiceWorkerUpdateViaCache::kAll;
SetUpRegistrationWithOptions(kScriptURL, options); SetUpRegistrationWithOptions(kScriptURL, options);
// Also set last_update_time to a recent time, so the 24 hour bypass doesn't // Also set last_update_time to a recent time, so the 24 hour validation
// kick in. // doesn't kick in.
registration_->set_last_update_check(base::Time::Now()); registration_->set_last_update_check(base::Time::Now());
version_->set_force_bypass_cache_for_scripts(true); version_->set_force_bypass_cache_for_scripts(true);
// Install the main script and imported script. The cache should be bypassed. // Install the main script and imported script. The cache should be validated.
DoRequest(kScriptURL, &client, &loader); DoRequest(kScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
network::ResourceRequest request = mock_url_loader_factory_->last_request(); network::ResourceRequest request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
DoRequest(kImportedScriptURL, &client, &loader); DoRequest(kImportedScriptURL, &client, &loader);
client->RunUntilComplete(); client->RunUntilComplete();
request = mock_url_loader_factory_->last_request(); request = mock_url_loader_factory_->last_request();
EXPECT_TRUE(request.load_flags & net::LOAD_BYPASS_CACHE); EXPECT_TRUE(request.load_flags & net::LOAD_VALIDATE_CACHE);
} }
// Tests that EmbeddedWorkerInstance's |network_accessed_for_script_| flag is // Tests that EmbeddedWorkerInstance's |network_accessed_for_script_| flag is
// set when the script loader accesses network. This flag is used to enforce the // set when the script loader accesses network. This flag is used to enforce the
// 24 hour cache bypass. // 24 hour cache validation.
TEST_F(ServiceWorkerNewScriptLoaderTest, AccessedNetwork) { TEST_F(ServiceWorkerNewScriptLoaderTest, AccessedNetwork) {
const GURL kScriptURL(kNormalScriptURL); const GURL kScriptURL(kNormalScriptURL);
const GURL kImportedScriptURL(kNormalImportedScriptURL); const GURL kImportedScriptURL(kNormalImportedScriptURL);
......
<!DOCTYPE html>
<meta charset="utf-8">
<title>Test that cache is being bypassed/validated in no-cache mode on update</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
// Tests a service worker script fetch during an update check which
// bypasses/validates the browser cache. The fetch should have the
// 'if-none-match' request header.
//
// This tests the Update step:
// "Set request’s cache mode to "no-cache" if any of the following are true..."
// https://w3c.github.io/ServiceWorker/#update-algorithm
//
// The test works by registering a service worker with |updateViaCache|
// set to "none". It then does an update. The test server responds with
// an updated worker script that remembers the http request headers.
// The updated worker reports back these headers to the test page.
promise_test(async (t) => {
const script = "resources/test-request-headers-worker.py";
const scope = "resources/";
// Register the service worker.
await service_worker_unregister(t, scope);
const registration = await navigator.serviceWorker.register(
script, {scope, updateViaCache: 'none'});
await wait_for_state(t, registration.installing, 'activated');
// Do an update.
await registration.update();
// Ask the new worker what the request headers were.
const newWorker = registration.installing;
const sawMessage = new Promise((resolve) => {
navigator.serviceWorker.onmessage = (event) => {
resolve(event.data);
};
});
newWorker.postMessage('getHeaders');
const result = await sawMessage;
// Test the result.
assert_equals(result['service-worker'], 'script');
assert_equals(result['if-none-match'], 'etag');
}, 'headers in no-cache mode');
</script>
// The server injects the request headers here as a JSON string.
const headersAsJson = `%HEADERS%`;
const headers = JSON.parse(headersAsJson);
self.addEventListener('message', async (e) => {
e.source.postMessage(headers);
});
import json
import os
def main(request, response):
path = os.path.join(os.path.dirname(__file__),
"test-request-headers-worker.js")
body = open(path, "rb").read()
data = {key:request.headers[key] for key,value in request.headers.iteritems()}
body = body.replace("%HEADERS%", json.dumps(data))
headers = []
headers.append(("ETag", "etag"))
headers.append(("Content-Type", 'text/javascript'))
return headers, body
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment