Index: content/test/data/browsing_data/worker.js |
diff --git a/content/test/data/browsing_data/worker.js b/content/test/data/browsing_data/worker.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7f402280cbb9aac7db6b8952c361077fcc785534 |
--- /dev/null |
+++ b/content/test/data/browsing_data/worker.js |
@@ -0,0 +1,73 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Service worker used in ClearSiteDataThrottleBrowserTest. |
+ |
+// Handle all resource requests. |
+self.addEventListener('fetch', function(event) { |
+ var url = new URL(event.request.url); |
+ |
+ // If this is a request for 'resource_from_sw', serve that resource |
+ // with a Clear-Site-Data header. |
+ if (url.pathname.match('resource_from_sw')) { |
+ event.respondWith(new Response( |
+ 'Response content is not important, only the header is.', { |
+ 'headers': { 'Clear-Site-Data': '{ "types" : [ "cookies" ] }' } |
+ })); |
+ return; |
+ } |
+ |
+ // If this is a request for 'resource', let it through. It will be responded |
+ // to by the test server. |
+ if (url.pathname.match('resource')) |
+ return; |
+ |
+ // Otherwise, serve the default response - a simple HTML page that will |
+ // execute the following function: |
+ var response_script_body = function(url_search) { |
+ // Parse the origin[1234] |url| parameters. |
+ var origins = {}; |
+ for (var i = 1; i <= 4; i++) { |
+ var origin_param_regex = new RegExp('origin' + i + '=([^&=?]+)'); |
+ origins[i] = decodeURIComponent(url_search.match(origin_param_regex)[1]); |
+ } |
+ |
+ // Prepare the test cases. |
+ var resource_urls = [ |
+ origins[1] + 'resource', |
+ origins[1] + 'resource_from_sw', |
+ origins[2] + 'resource_from_sw', |
+ origins[2] + 'resource', |
+ origins[3] + 'resource_from_sw', |
+ origins[3] + 'resource_from_sw', |
+ origins[4] + 'resource', |
+ origins[4] + 'another_resource_so_that_the_previous_one_isnt_reused', |
+ ]; |
+ var header = encodeURIComponent('{ "types": [ "cookies" ] }'); |
+ |
+ // When all resources are fetched, this helper method will report it back |
+ // to the C++ side by setting the document title. |
+ var resources_loaded = 0; |
+ function done() { |
+ if (++resources_loaded == resource_urls.length) |
+ document.title = "done"; |
+ } |
+ |
+ // Fetch the resources. |
+ for (var i = 0; i < resource_urls.length; i++) { |
+ var img = new Image(); |
+ document.body.appendChild(img); |
+ img.onload = img.onerror = done; |
+ img.src = resource_urls[i] + "?header=" + header; |
+ } |
+ } |
+ |
+ // Return the code of |response_script_body| as the response. |
+ event.respondWith(new Response( |
+ '<html><head></head><body><script>' + |
+ '(' + response_script_body.toString() + ')("' + url.search + '")' + |
+ '</script></body></html>', |
+ { 'headers': { 'Content-Type': 'text/html' } } |
+ )); |
+}); |