summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/data/TestWorkerWatcherChild.sys.mjs
blob: 907631dec1c5b5c6516bf620765c964e691f3fb8 (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
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

const lazy = {};

XPCOMUtils.defineLazyServiceGetter(
  lazy,
  "wdm",
  "@mozilla.org/dom/workers/workerdebuggermanager;1",
  "nsIWorkerDebuggerManager"
);

export class TestWorkerWatcherChild extends JSProcessActorChild {
  async receiveMessage(msg) {
    switch (msg.name) {
      case "Test:StartWatchingWorkers":
        this.startWatchingWorkers();
        break;
      case "Test:StopWatchingWorkers":
        this.stopWatchingWorkers();
        break;
      default:
        // Ensure the test case will fail if this JSProcessActorChild does receive
        // unexpected messages.
        return Promise.reject(
          new Error(`Unexpected message received: ${msg.name}`)
        );
    }
  }

  startWatchingWorkers() {
    if (!this._workerDebuggerListener) {
      const actor = this;
      this._workerDebuggerListener = {
        onRegister(dbg) {
          actor.sendAsyncMessage("Test:WorkerSpawned", {
            workerType: dbg.type,
            workerUrl: dbg.url,
          });
        },
        onUnregister(dbg) {
          actor.sendAsyncMessage("Test:WorkerTerminated", {
            workerType: dbg.type,
            workerUrl: dbg.url,
          });
        },
      };

      lazy.wdm.addListener(this._workerDebuggerListener);
    }
  }

  stopWatchingWorkers() {
    if (this._workerDebuggerListener) {
      lazy.wdm.removeListener(this._workerDebuggerListener);
      this._workerDebuggerListener = null;
    }
  }

  willDestroy() {
    this.stopWatchingWorkers();
  }
}