summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_partitionedSharedWorkers.js
blob: 337d36b6e4a50b68273d9b4871ac103d25863540 (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
65
66
67
68
69
70
71
72
73
74
PartitionedStorageHelper.runTestInNormalAndPrivateMode(
  "SharedWorkers",
  async (win3rdParty, win1stParty, allowed) => {
    // This test fails if run with an HTTPS 3rd-party URL because the shared worker
    // which would start from the window opened from 3rdPartyStorage.html will become
    // secure context and per step 11.4.3 of
    // https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker attempting
    // to run the SharedWorker constructor would emit an error event.
    is(
      win3rdParty.location.protocol,
      "http:",
      "Our 3rd party URL shouldn't be HTTPS"
    );

    let sh1 = new win1stParty.SharedWorker("sharedWorker.js");
    await new Promise(resolve => {
      sh1.port.onmessage = e => {
        is(e.data, 1, "We expected 1 connection");
        resolve();
      };
      sh1.port.postMessage("count");
    });

    let sh3 = new win3rdParty.SharedWorker("sharedWorker.js");
    await new Promise(resolve => {
      sh3.port.onmessage = e => {
        is(e.data, 1, `We expected 1 connection for 3rd party SharedWorker`);
        resolve();
      };
      sh3.onerror = _ => {
        ok(false, "We should not be here");
        resolve();
      };
      sh3.port.postMessage("count");
    });

    sh1.port.postMessage("close");
    sh3.port.postMessage("close");
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value =>
        resolve()
      );
    });
  }
);

PartitionedStorageHelper.runPartitioningTestInNormalAndPrivateMode(
  "Partitioned tabs - SharedWorker",
  "sharedworker",

  // getDataCallback
  async win => {
    win.sh = new win.SharedWorker("partitionedSharedWorker.js");
    return new Promise(resolve => {
      win.sh.port.onmessage = e => {
        resolve(e.data);
      };
      win.sh.port.postMessage({ what: "get" });
    });
  },

  // addDataCallback
  async (win, value) => {
    win.sh = new win.SharedWorker("partitionedSharedWorker.js");
    win.sh.port.postMessage({ what: "put", value });
    return true;
  },

  // cleanup
  async _ => {}
);