OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Service worker used in ClearSiteDataThrottleBrowserTest. |
| 6 |
| 7 // Handle all resource requests. |
| 8 self.addEventListener('fetch', function(event) { |
| 9 var url = new URL(event.request.url); |
| 10 |
| 11 // If this is a request for 'resource_from_sw', serve that resource |
| 12 // with a Clear-Site-Data header. |
| 13 if (url.pathname.match('resource_from_sw')) { |
| 14 event.respondWith(new Response( |
| 15 'Response content is not important, only the header is.', { |
| 16 'headers': { 'Clear-Site-Data': '{ "types" : [ "cookies" ] }' } |
| 17 })); |
| 18 return; |
| 19 } |
| 20 |
| 21 // If this is a request for 'resource', let it through. It will be responded |
| 22 // to by the test server. |
| 23 if (url.pathname.match('resource')) |
| 24 return; |
| 25 |
| 26 // Otherwise, serve the default response - a simple HTML page that will |
| 27 // execute the following function: |
| 28 var response_script_body = function(url_search) { |
| 29 // Parse the origin[1234] |url| parameters. |
| 30 var origins = {}; |
| 31 for (var i = 1; i <= 4; i++) { |
| 32 var origin_param_regex = new RegExp('origin' + i + '=([^&=?]+)'); |
| 33 origins[i] = decodeURIComponent(url_search.match(origin_param_regex)[1]); |
| 34 } |
| 35 |
| 36 // Prepare the test cases. |
| 37 var resource_urls = [ |
| 38 origins[1] + 'resource', |
| 39 origins[2] + 'resource_from_sw', |
| 40 origins[3] + 'resource_from_sw', |
| 41 origins[4] + 'resource', |
| 42 origins[1] + 'resource_from_sw', |
| 43 origins[2] + 'resource', |
| 44 origins[3] + 'resource_from_sw', |
| 45 origins[4] + 'another_resource_so_that_the_previous_one_isnt_reused', |
| 46 ]; |
| 47 var header = encodeURIComponent('{ "types": [ "cookies" ] }'); |
| 48 |
| 49 // Fetch all resources and report back to the C++ side by setting |
| 50 // the document title. |
| 51 var fetchResource = function(index) { |
| 52 var img = new Image(); |
| 53 document.body.appendChild(img); |
| 54 |
| 55 img.onload = img.onerror = function() { |
| 56 if (index + 1 == resource_urls.length) |
| 57 document.title = "done"; |
| 58 else |
| 59 fetchResource(index + 1); |
| 60 } |
| 61 |
| 62 img.src = resource_urls[index] + "?header=" + header; |
| 63 } |
| 64 fetchResource(0); |
| 65 } |
| 66 |
| 67 // Return the code of |response_script_body| as the response. |
| 68 event.respondWith(new Response( |
| 69 '<html><head></head><body><script>' + |
| 70 '(' + response_script_body.toString() + ')("' + url.search + '")' + |
| 71 '</script></body></html>', |
| 72 { 'headers': { 'Content-Type': 'text/html' } } |
| 73 )); |
| 74 }); |
OLD | NEW |