File: shared-worker-in-data-url-context.window.js

package info (click to toggle)
thunderbird 1%3A91.13.0-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,953,400 kB
  • sloc: cpp: 6,084,049; javascript: 4,790,441; ansic: 3,341,496; python: 862,958; asm: 366,542; xml: 204,277; java: 152,477; sh: 111,436; makefile: 21,388; perl: 15,312; yacc: 4,583; objc: 3,026; lex: 1,720; exp: 762; pascal: 635; awk: 564; sql: 453; php: 436; lisp: 432; ruby: 99; sed: 69; csh: 45
file content (63 lines) | stat: -rw-r--r-- 2,734 bytes parent folder | download | duplicates (8)
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
// META: title=data URL shared worker in data URL context
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
const mimeType = 'application/javascript';

// Tests creating a data URL shared worker in a data URL iframe.
promise_test(async t => {
  const nestedWorkerScriptURL =
      new URL('/workers/support/post-message-on-load-worker.js', location.href);

  // This code will be executed in a data URL iframe. The iframe tries to create
  // a shared worker from |nestedWorkerScriptURL|, but that should result in a
  // failure. This is because the data URL iframe has an opaque origin, and
  // script fetch is handled as a cross-origin request.
  const frameCode = `
      <script>
      try {
        const worker = new SharedWorker('${nestedWorkerScriptURL}');
        worker.port.onmessage = e => {
          window.parent.postMessage(
              'SharedWorker construction unexpectedly succeeded', '*');
        };
        worker.onerror = e => window.parent.postMessage('PASS', '*');
      } catch (e) {
        // Cross-origin request should asynchronously fail during worker script
        // fetch because its request mode is 'same-origin'.
        window.parent.postMessage(
            'SharedWorker construction unexpectedly synchronously failed', '*');
      }
      </script>
  `;

  const frame = await with_iframe(`data:text/html;base64,${btoa(frameCode)}`);
  const result = await new Promise(r => window.onmessage = e => r(e.data));
  assert_equals(result, 'PASS');
}, 'Create a shared worker in a data url frame');

// Tests creating a data URL shared worker in a data URL iframe.
promise_test(async t => {
  const workerCode = `onconnect = e => e.ports[0].postMessage("PASS");`;

  // This code will be executed in a data URL iframe. The iframe tries to create
  // a data URL shared worker. Fetching a data URL from the data URL shared
  // worker is allowed, so the worker construction should succeed. The worker
  // posts the result to the parent frame.
  const frameCode = `
      <script>
      try {
        const worker = new SharedWorker('data:${mimeType},${workerCode};');
        worker.port.onmessage = e => window.parent.postMessage(e.data, '*');
        worker.onerror = e => {
          window.parent.postMessage('FAIL: ' + e.message, '*');
        };
      } catch (e) {
        window.parent.postMessage(
            'SharedWorker construction unexpectedly synchronously failed', '*');
      }
      </script>
  `;

  const frame = await with_iframe(`data:text/html;base64,${btoa(frameCode)}`);
  const result = await new Promise(r => window.onmessage = e => r(e.data));
  assert_equals(result, 'PASS');
}, 'Create a data url shared worker in a data url frame');