OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <script src="get-host-info.sub.js"></script> |
| 3 <script src="test-helpers.sub.js"></script> |
| 4 <script> |
| 5 var host_info = get_host_info(); |
| 6 var SCOPE = 'blank.html?clients-get'; |
| 7 var SCRIPT = 'clients-get-worker.js'; |
| 8 |
| 9 var registration; |
| 10 var worker; |
| 11 var wait_for_worker_promise = navigator.serviceWorker.getRegistration(SCOPE) |
| 12 .then(function(reg) { |
| 13 if (reg) |
| 14 return reg.unregister(); |
| 15 }) |
| 16 .then(function() { |
| 17 return navigator.serviceWorker.register(SCRIPT, {scope: SCOPE}); |
| 18 }) |
| 19 .then(function(reg) { |
| 20 registration = reg; |
| 21 worker = reg.installing; |
| 22 return new Promise(function(resolve) { |
| 23 worker.addEventListener('statechange', function() { |
| 24 if (worker.state == 'activated') |
| 25 resolve(); |
| 26 }); |
| 27 }); |
| 28 }); |
| 29 |
| 30 function send_result(result) { |
| 31 window.parent.postMessage( |
| 32 {result: result}, |
| 33 host_info['HTTPS_ORIGIN']); |
| 34 } |
| 35 |
| 36 window.addEventListener("message", on_message, false); |
| 37 |
| 38 function on_message(e) { |
| 39 if (e.origin != host_info['HTTPS_ORIGIN']) { |
| 40 console.error('invalid origin: ' + e.origin); |
| 41 return; |
| 42 } |
| 43 if (e.data.message == 'get_client_id') { |
| 44 var otherOriginClientId = e.data.clientId; |
| 45 wait_for_worker_promise |
| 46 .then(function() { |
| 47 return with_iframe(SCOPE); |
| 48 }) |
| 49 .then(function(iframe) { |
| 50 var channel = new MessageChannel(); |
| 51 channel.port1.onmessage = function(e) { |
| 52 navigator.serviceWorker.getRegistration(SCOPE) |
| 53 .then(function(reg) { |
| 54 reg.unregister(); |
| 55 send_result(e.data); |
| 56 }); |
| 57 }; |
| 58 iframe.contentWindow.navigator.serviceWorker.controller.postMessage( |
| 59 {port:channel.port2, clientId: otherOriginClientId, |
| 60 message: 'get_other_client_id'}, [channel.port2]); |
| 61 }) |
| 62 } |
| 63 } |
| 64 </script> |
OLD | NEW |