OLD | NEW |
(Empty) | |
| 1 function match_query(query_string) { |
| 2 return self.location.search.substr(1) == query_string; |
| 3 } |
| 4 |
| 5 function navigate_test(e) { |
| 6 var port = e.data.port; |
| 7 var url = e.data.url; |
| 8 |
| 9 return clients.matchAll({ includeUncontrolled : true }) |
| 10 .then(function(client_list) { |
| 11 for (var i = 0; i < client_list.length; i++) { |
| 12 var client = client_list[i]; |
| 13 if (client.frameType == 'nested') { |
| 14 return client.navigate(url); |
| 15 } |
| 16 } |
| 17 port.postMessage('Could not locate window client.'); |
| 18 }) |
| 19 .then(function(new_client) { |
| 20 if (new_client === null) |
| 21 port.postMessage(new_client); |
| 22 else |
| 23 port.postMessage(new_client.url); |
| 24 }) |
| 25 .catch(function(error) { |
| 26 port.postMessage(error.name); |
| 27 }); |
| 28 } |
| 29 |
| 30 function getTestClient() { |
| 31 return clients.matchAll({ includeUncontrolled: true }) |
| 32 .then(function(client_list) { |
| 33 for (var i = 0; i < client_list.length; i++) { |
| 34 var client = client_list[i]; |
| 35 |
| 36 if (/windowclient-navigate\.https\.html/.test(client.url)) { |
| 37 return client; |
| 38 } |
| 39 } |
| 40 |
| 41 throw new Error('Service worker was unable to locate test client.'); |
| 42 }); |
| 43 } |
| 44 |
| 45 function waitForMessage(client) { |
| 46 var channel = new MessageChannel(); |
| 47 client.postMessage({ port: channel.port2 }, [channel.port2]); |
| 48 |
| 49 return new Promise(function(resolve) { |
| 50 channel.port1.onmessage = resolve; |
| 51 }); |
| 52 } |
| 53 |
| 54 // The worker must remain in the "installing" state for the duration of some |
| 55 // sub-tests. In order to achieve this coordination without relying on global |
| 56 // state, the worker must create a message channel with the client from within |
| 57 // the "install" event handler. |
| 58 if (match_query('installing')) { |
| 59 self.addEventListener('install', function(e) { |
| 60 e.waitUntil(getTestClient().then(waitForMessage)); |
| 61 }); |
| 62 } |
| 63 |
| 64 self.addEventListener('message', function(e) { |
| 65 e.waitUntil(navigate_test(e)); |
| 66 }); |
OLD | NEW |