OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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[1] + 'resource_from_sw', | |
40 origins[2] + 'resource_from_sw', | |
41 origins[2] + 'resource', | |
42 origins[3] + 'resource_from_sw', | |
43 origins[3] + 'resource_from_sw', | |
44 origins[4] + 'resource', | |
45 origins[4] + 'another_resource_so_that_the_previous_one_isnt_reused', | |
46 ]; | |
47 var header = encodeURIComponent('{ "types": [ "cookies" ] }'); | |
48 | |
49 // When all resources are fetched, this helper method will report it back | |
50 // to the C++ side by setting the document title. | |
51 var resources_loaded = 0; | |
52 function done() { | |
53 if (++resources_loaded == resource_urls.length) | |
54 document.title = "done"; | |
55 } | |
56 | |
57 // Fetch the resources. | |
58 for (var i = 0; i < resource_urls.length; i++) { | |
59 var img = new Image(); | |
60 document.body.appendChild(img); | |
61 img.onload = img.onerror = done; | |
62 img.src = resource_urls[i] + "?header=" + header; | |
63 } | |
64 } | |
65 | |
66 // Return the code of |response_script_body| as the response. | |
67 event.respondWith(new Response( | |
68 '<html><head></head><body><script>' + | |
69 '(' + response_script_body.toString() + ')("' + url.search + '")' + | |
70 '</script></body></html>', | |
71 { 'headers': { 'Content-Type': 'text/html' } } | |
72 )); | |
falken
2016/10/19 07:14:38
Nicely written test.
msramek
2016/10/19 12:20:12
Thanks again :) *blush*
| |
73 }); | |
OLD | NEW |