summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/partitioned-service-worker-third-party-iframe-getRegistrations.html
blob: 747c05894602cf97b26685c5eeba344d5dddc643 (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
<!DOCTYPE html>
<title>Service Worker: 3P iframe for partitioned service workers</title>
<script src="./test-helpers.sub.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="./partitioned-utils.js"></script>

<body>
  This iframe will register a service worker when it loads and then will use
  getRegistrations to get a handle to the SW. It will then postMessage to the
  SW to retrieve the SW's ID. This iframe will then forward that message up,
  eventually, to the test.
  <script>

    async function onLoad() {
      const scope = './partitioned-'
      const absoluteScope = new URL(scope, window.location).href;

      await setupServiceWorker();

      // Once the SW sends us its ID, forward it up to the window.
      navigator.serviceWorker.addEventListener('message', evt => {
        window.parent.postMessage(evt.data, '*');
      });

      // Now get the SW with getRegistrations.
      const retrieved_registrations =
        await navigator.serviceWorker.getRegistrations();

      // It's possible that other tests have left behind other service workers.
      // This steps filters those other SWs out.
      const filtered_registrations =
        retrieved_registrations.filter(reg => reg.scope == absoluteScope);

      filtered_registrations[0].active.postMessage({type: "get-id"});

    }

    self.addEventListener('load', onLoad);
  </script>
</body>