summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/targets/worker.js
blob: 7604b5be6eeb56315ec0880d29bd64edaa3dd31b (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  workerTargetSpec,
} = require("resource://devtools/shared/specs/targets/worker.js");

const {
  WebConsoleActor,
} = require("resource://devtools/server/actors/webconsole.js");
const { ThreadActor } = require("resource://devtools/server/actors/thread.js");
const { TracerActor } = require("resource://devtools/server/actors/tracer.js");
const {
  ObjectsManagerActor,
} = require("resource://devtools/server/actors/objects-manager.js");

const Targets = require("resource://devtools/server/actors/targets/index.js");

const makeDebuggerUtil = require("resource://devtools/server/actors/utils/make-debugger.js");
const {
  SourcesManager,
} = require("resource://devtools/server/actors/utils/sources-manager.js");

const {
  BaseTargetActor,
} = require("resource://devtools/server/actors/targets/base-target-actor.js");

class WorkerTargetActor extends BaseTargetActor {
  /**
   * Target actor for a worker in the content process.
   *
   * @param {DevToolsServerConnection} conn: The connection to the client.
   * @param {WorkerGlobalScope} workerGlobal: The worker global.
   * @param {Object} workerDebuggerData: The worker debugger information
   * @param {String} workerDebuggerData.id: The worker debugger id
   * @param {String} workerDebuggerData.url: The worker debugger url
   * @param {String} workerDebuggerData.type: The worker debugger type
   * @param {Boolean} workerDebuggerData.workerConsoleApiMessagesDispatchedToMainThread:
   *                  Value of the dom.worker.console.dispatch_events_to_main_thread pref
   * @param {Object} sessionContext: The Session Context to help know what is debugged.
   *                                 See devtools/server/actors/watcher/session-context.js
   */
  constructor(conn, workerGlobal, workerDebuggerData, sessionContext) {
    super(conn, Targets.TYPES.WORKER, workerTargetSpec);

    // workerGlobal is needed by the console actor for evaluations.
    this.workerGlobal = workerGlobal;
    this.sessionContext = sessionContext;

    // We don't have access to Ci from worker thread
    // 2 == nsIWorkerDebugger.TYPE_SERVICE
    // 1 == nsIWorkerDebugger.TYPE_SHARED
    if (workerDebuggerData.type == 2) {
      this.targetType = Targets.TYPES.SERVICE_WORKER;
    } else if (workerDebuggerData.type == 1) {
      this.targetType = Targets.TYPES.SHARED_WORKER;
    }

    this._workerDebuggerData = workerDebuggerData;
    this._sourcesManager = null;
    this.workerConsoleApiMessagesDispatchedToMainThread =
      workerDebuggerData.workerConsoleApiMessagesDispatchedToMainThread;

    this.makeDebugger = makeDebuggerUtil.bind(null, {
      findDebuggees: () => {
        return [workerGlobal];
      },
      shouldAddNewGlobalAsDebuggee: () => true,
    });

    // needed by the console actor
    this.threadActor = new ThreadActor(this);

    // needed by the thread actor to communicate with the console when evaluating logpoints.
    this._consoleActor = new WebConsoleActor(this.conn, this);

    this.tracerActor = new TracerActor(this.conn, this);
    this.objectsManagerActor = new ObjectsManagerActor(this.conn, this);

    this.manage(this.threadActor);
    this.manage(this._consoleActor);
    this.manage(this.tracerActor);
    this.manage(this.objectsManagerActor);
  }

  // Expose the worker URL to the thread actor.
  // so that it can easily know what is the base URL of all worker scripts.
  get workerUrl() {
    return this._workerDebuggerData.url;
  }

  form() {
    return {
      actor: this.actorID,

      consoleActor: this._consoleActor?.actorID,
      threadActor: this.threadActor?.actorID,
      tracerActor: this.tracerActor?.actorID,
      objectsManagerActor: this.objectsManagerActor?.actorID,

      id: this._workerDebuggerData.id,
      type: this._workerDebuggerData.type,
      url: this._workerDebuggerData.url,
      traits: {
        // See trait description in browsing-context.js
        supportsTopLevelTargetFlag: false,
      },
    };
  }

  get dbg() {
    if (!this._dbg) {
      this._dbg = this.makeDebugger();
    }
    return this._dbg;
  }

  get sourcesManager() {
    if (this._sourcesManager === null) {
      this._sourcesManager = new SourcesManager(this.threadActor);
    }

    return this._sourcesManager;
  }

  destroy() {
    super.destroy();

    if (this._sourcesManager) {
      this._sourcesManager.destroy();
      this._sourcesManager = null;
    }

    this.workerGlobal = null;
    this._dbg = null;
    this._consoleActor = null;
    this.threadActor = null;
  }
}
exports.WorkerTargetActor = WorkerTargetActor;