summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/download_canceled/page_download_canceled.html
blob: dd677090045e9bdba441739f522cce10d7dae5ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
</head>
<body>

<script src="../utils.js"></script>
<script type="text/javascript">
function wait_until_controlled() {
  return new Promise(function(resolve) {
    if (navigator.serviceWorker.controller) {
      resolve('controlled');
      return;
    }
    navigator.serviceWorker.addEventListener('controllerchange', function onController() {
      if (navigator.serviceWorker.controller) {
        navigator.serviceWorker.removeEventListener('controllerchange', onController);
        resolve('controlled');
      }
    });
  });
}
addEventListener('load', async function(event) {
  window.controlled = wait_until_controlled();
  window.registration =
    await navigator.serviceWorker.register('sw_download_canceled.js');
  let sw = registration.installing || registration.waiting ||
           registration.active;
  await waitForState(sw, 'activated');
  sw.postMessage('claim');
});

// Place to hold promises for stream closures reported by the SW.
window.streamClosed = {};

// The ServiceWorker will postMessage to this BroadcastChannel when the streams
// are closed.  (Alternately, the SW could have used the clients API to post at
// us, but the mechanism by which that operates would be different when this
// test is uplifted, and it's desirable to avoid timing changes.)
//
// The browser test will use this promise to wait on stream shutdown.
window.swStreamChannel = new BroadcastChannel("stream-closed");
function trackStreamClosure(path) {
  let resolve;
  const promise = new Promise(r => { resolve = r });
  window.streamClosed[path] = { promise, resolve };
}
window.swStreamChannel.onmessage = ({ data }) => {
  window.streamClosed[data.what].resolve(data);
};
</script>

</body>
</html>