summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fenced-frame/resources/background-fetch-sw.js
blob: 44b7d087b574615f6af830203fa5e3a1d1e40540 (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
self.addEventListener('install', e => e.waitUntil(skipWaiting()));
self.addEventListener('activate', e => e.waitUntil(clients.claim()));

self.addEventListener('message', async e => {
  const method = e.data;

  let promise;
  switch (method) {
    case 'fetch':
      promise = self.registration.backgroundFetch.fetch(
          'test-fetch', ['background-fetch-inner.https.html.headers'],
          {title: 'Background Fetch'});
      break;
    case 'get':
      promise = self.registration.backgroundFetch.get('test-fetch')
      break;
    case 'getIds':
      promise = registration.backgroundFetch.getIds();
      break;
    default:
      promise = Promise.resolve();
      break;
  }

  const message =
      await promise
          .then(() => {
            return `[backgroundFetch.${method}] Unexpectedly started`;
          })
          .catch((e) => {
            return `[backgroundFetch.${
                method}] Failed inside fencedframe as expected`;
          });

  e.source.postMessage(message);
});