summaryrefslogtreecommitdiffstats
path: root/dom/quota/test/modules/system/WorkerDriver.sys.mjs
blob: 60e67907608d59896b0595608aaedc8a659fa6ae (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

export async function runTestInWorker(script, base, listener) {
  return new Promise(function (resolve) {
    const globalHeadUrl = new URL(
      "resource://testing-common/dom/quota/test/modules/worker/head.js"
    );

    let modules = {};

    const worker = new Worker(globalHeadUrl.href);

    worker.onmessage = async function (event) {
      const data = event.data;
      const moduleName = data.moduleName;
      const objectName = data.objectName;

      if (moduleName && objectName) {
        if (!modules[moduleName]) {
          modules[moduleName] = ChromeUtils.importESModule(
            "resource://testing-common/dom/quota/test/modules/" +
              moduleName +
              ".sys.mjs"
          );
        }
        await modules[moduleName][objectName].OnMessageReceived(worker, data);
        return;
      }

      switch (data.op) {
        case "ok":
          listener.onOk(data.value, data.message);
          break;

        case "is":
          listener.onIs(data.a, data.b, data.message);
          break;

        case "info":
          listener.onInfo(data.message);
          break;

        case "finish":
          resolve();
          break;

        case "failure":
          listener.onOk(false, "Worker had a failure: " + data.message);
          resolve();
          break;
      }
    };

    worker.onerror = function (event) {
      listener.onOk(false, "Worker had an error: " + event.data);
      resolve();
    };

    const scriptUrl = new URL(script, base);

    const localHeadUrl = new URL("head.js", scriptUrl);

    worker.postMessage([localHeadUrl.href, scriptUrl.href]);
  });
}