summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/clients-get-cross-origin-frame.html
blob: e16bb1116dc810d275073e363fa0d7b4ffa46629 (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="test-helpers.sub.js"></script>
<script>
var host_info = get_host_info();
var scope = 'blank.html?clients-get';
var script = 'clients-get-worker.js';

var registration;
var worker;
var wait_for_worker_promise = navigator.serviceWorker.getRegistration(scope)
  .then(function(reg) {
      if (reg)
        return reg.unregister();
    })
  .then(function() {
      return navigator.serviceWorker.register(script, {scope: scope});
    })
  .then(function(reg) {
      registration = reg;
      worker = reg.installing;
      return new Promise(function(resolve) {
          worker.addEventListener('statechange', function() {
              if (worker.state == 'activated')
                resolve();
            });
        });
    });

window.addEventListener('message', function(e) {
  var cross_origin_client_ids = [];
  cross_origin_client_ids.push(e.data.clientId);
  wait_for_worker_promise
    .then(function() {
        return with_iframe(scope);
      })
    .then(function(iframe) {
        add_completion_callback(function() { iframe.remove(); });
        navigator.serviceWorker.onmessage = function(e) {
          registration.unregister();
          window.parent.postMessage(
            { type: 'clientId', value: e.data }, host_info['HTTPS_ORIGIN']
          );
        };
        registration.active.postMessage({clientIds: cross_origin_client_ids});
      });
});
</script>