summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/clients-get-other-origin.html
blob: cbd3dcc6f3b13484663e2ab9d6832e5570ce5d4d (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
60
61
62
63
64
<!DOCTYPE html>
<script src="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();
            });
        });
    });

function send_result(result) {
  window.parent.postMessage(
      {result: result},
      host_info['HTTPS_ORIGIN']);
}

window.addEventListener("message", on_message, false);

function on_message(e) {
  if (e.origin != host_info['HTTPS_ORIGIN']) {
    console.error('invalid origin: ' + e.origin);
    return;
  }
  if (e.data.message == 'get_client_id') {
    var otherOriginClientId = e.data.clientId;
    wait_for_worker_promise
      .then(function() {
          return with_iframe(SCOPE);
        })
      .then(function(iframe) {
          var channel = new MessageChannel();
          channel.port1.onmessage = function(e) {
            navigator.serviceWorker.getRegistration(SCOPE)
              .then(function(reg) {
                  reg.unregister();
                  send_result(e.data);
                });
          };
          iframe.contentWindow.navigator.serviceWorker.controller.postMessage(
              {port:channel.port2, clientId: otherOriginClientId,
               message: 'get_other_client_id'}, [channel.port2]);
        })
  }
}
</script>