summaryrefslogtreecommitdiffstats
path: root/dom/security/test/general/file_block_script_wrong_mime_sw.js
blob: 4d8d667af439723f9d96c1f4e5d66da61720e23c (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
/**
 * Service Worker that runs in 2 modes: 1) direct pass-through via
 * fetch(event.request) and 2) indirect pass-through via
 * fetch(event.request.url).
 *
 * Because this is updating a pre-existing mochitest that didn't use a SW and
 * used a single test document, we use a SW idiom where the SW claims the
 * existing window client.  And because we operate in two modes and we
 * parameterize via URL, we also ensure that we skipWaiting.
 **/

/* eslint-env serviceworker */

// We are parameterized by "mode".
const params = new URLSearchParams(location.search);
const fetchMode = params.get("fetchMode");

// When activating on initial install, claim the existing window client.
// For synchronziation, also message the controlled document to report our mode.
self.addEventListener("activate", event => {
  event.waitUntil(
    (async () => {
      await clients.claim();
      const allClients = await clients.matchAll();
      for (const client of allClients) {
        client.postMessage({
          fetchMode,
        });
      }
    })()
  );
});

// When updating the SW to change our mode of operation, skipWaiting so we
// advance directly to activating without waiting for the test window client
// to stop being controlled by our previous configuration.
self.addEventListener("install", () => {
  self.skipWaiting();
});

self.addEventListener("fetch", event => {
  switch (fetchMode) {
    case "direct":
      event.respondWith(fetch(event.request));
      break;

    case "indirect":
      event.respondWith(fetch(event.request.url));
      break;
  }
});