summaryrefslogtreecommitdiffstats
path: root/devtools/server/connectors/js-process-actor/target-watchers/worker.sys.mjs
blob: 0b67e8b03813f41b679b94d2743783aa31c4c473 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* 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/. */

import { ContentProcessWatcherRegistry } from "resource://devtools/server/connectors/js-process-actor/ContentProcessWatcherRegistry.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

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

const { TYPE_DEDICATED, TYPE_SERVICE, TYPE_SHARED } = Ci.nsIWorkerDebugger;

export class WorkerTargetWatcherClass {
  constructor(workerTargetType = "worker") {
    this.#workerTargetType = workerTargetType;
    this.#workerDebuggerListener = {
      onRegister: this.#onWorkerRegister.bind(this),
      onUnregister: this.#onWorkerUnregister.bind(this),
    };
  }

  // {String}
  #workerTargetType;
  // {nsIWorkerDebuggerListener}
  #workerDebuggerListener;

  watch() {
    lazy.wdm.addListener(this.#workerDebuggerListener);
  }

  unwatch() {
    lazy.wdm.removeListener(this.#workerDebuggerListener);
  }

  createTargetsForWatcher(watcherDataObject) {
    const { sessionData } = watcherDataObject;
    for (const dbg of lazy.wdm.getWorkerDebuggerEnumerator()) {
      if (!this.shouldHandleWorker(sessionData, dbg, this.#workerTargetType)) {
        continue;
      }
      this.createWorkerTargetActor(watcherDataObject, dbg);
    }
  }

  async addOrSetSessionDataEntry(watcherDataObject, type, entries, updateType) {
    // Collect the SessionData update into `pendingWorkers` in order to notify
    // about the updates to workers which are still in process of being hooked by devtools.
    for (const concurrentSessionUpdates of watcherDataObject.pendingWorkers) {
      concurrentSessionUpdates.push({
        type,
        entries,
        updateType,
      });
    }

    const promises = [];
    for (const {
      dbg,
      workerThreadServerForwardingPrefix,
    } of watcherDataObject.workers) {
      promises.push(
        addOrSetSessionDataEntryInWorkerTarget({
          dbg,
          workerThreadServerForwardingPrefix,
          type,
          entries,
          updateType,
        })
      );
    }
    await Promise.all(promises);
  }

  /**
   * Called whenever a new Worker is instantiated in the current process
   *
   * @param {WorkerDebugger} dbg
   */
  #onWorkerRegister(dbg) {
    // Create a Target Actor for each watcher currently watching for Workers
    for (const watcherDataObject of ContentProcessWatcherRegistry.getAllWatchersDataObjects(
      this.#workerTargetType
    )) {
      const { sessionData } = watcherDataObject;
      if (this.shouldHandleWorker(sessionData, dbg, this.#workerTargetType)) {
        this.createWorkerTargetActor(watcherDataObject, dbg);
      }
    }
  }

  /**
   * Called whenever a Worker is destroyed in the current process
   *
   * @param {WorkerDebugger} dbg
   */
  #onWorkerUnregister(dbg) {
    for (const watcherDataObject of ContentProcessWatcherRegistry.getAllWatchersDataObjects(
      this.#workerTargetType
    )) {
      const { watcherActorID, workers } = watcherDataObject;
      // Check if the worker registration was handled for this watcherActorID.
      const unregisteredActorIndex = workers.findIndex(worker => {
        try {
          // Accessing the WorkerDebugger id might throw (NS_ERROR_UNEXPECTED).
          return worker.dbg.id === dbg.id;
        } catch (e) {
          return false;
        }
      });
      if (unregisteredActorIndex === -1) {
        continue;
      }

      const { workerTargetForm, transport } = workers[unregisteredActorIndex];
      // Close the transport made to the worker thread
      transport.close();

      try {
        watcherDataObject.jsProcessActor.sendAsyncMessage(
          "DevToolsProcessChild:targetDestroyed",
          {
            actors: [
              {
                watcherActorID,
                targetActorForm: workerTargetForm,
              },
            ],
            options: {},
          }
        );
      } catch (e) {
        // This often throws as the JSActor is being destroyed when DevTools closes
        // and we are trying to notify about the destroyed targets.
      }

      workers.splice(unregisteredActorIndex, 1);
    }
  }

  /**
   * Instantiate a worker target actor related to a given WorkerDebugger object
   * and for a given watcher actor.
   *
   * @param {Object} watcherDataObject
   * @param {WorkerDebugger} dbg
   */
  async createWorkerTargetActor(watcherDataObject, dbg) {
    // Prevent the debuggee from executing in this worker until the client has
    // finished attaching to it. This call will throw if the debugger is already "registered"
    // (i.e. if this is called outside of the register listener)
    // See https://searchfox.org/mozilla-central/rev/84922363f4014eae684aabc4f1d06380066494c5/dom/workers/nsIWorkerDebugger.idl#55-66
    try {
      dbg.setDebuggerReady(false);
    } catch (e) {
      if (!e.message.startsWith("Component returned failure code")) {
        throw e;
      }
    }

    const { watcherActorID } = watcherDataObject;
    const { connection, loader } =
      ContentProcessWatcherRegistry.getOrCreateConnectionForWatcher(
        watcherActorID
      );

    // Compute a unique prefix for the bridge made between this content process main thread
    // and the worker thread.
    const workerThreadServerForwardingPrefix =
      connection.allocID("workerTarget");

    const { connectToWorker } = loader.require(
      "resource://devtools/server/connectors/worker-connector.js"
    );

    // Create the actual worker target actor, in the worker thread.
    const { sessionData, sessionContext } = watcherDataObject;
    const onConnectToWorker = connectToWorker(
      connection,
      dbg,
      workerThreadServerForwardingPrefix,
      {
        sessionData,
        sessionContext,
      }
    );

    // Only add data to the connection if we successfully send the
    // workerTargetAvailable message.
    const workerInfo = {
      dbg,
      workerThreadServerForwardingPrefix,
    };
    watcherDataObject.workers.push(workerInfo);

    // The onConnectToWorker is async and we may receive new Session Data (e.g breakpoints)
    // while we are instantiating the worker targets.
    // Let cache the pending session data and flush it after the targets are being instantiated.
    const concurrentSessionUpdates = [];
    watcherDataObject.pendingWorkers.add(concurrentSessionUpdates);

    try {
      await onConnectToWorker;
    } catch (e) {
      // connectToWorker is supposed to call setDebuggerReady(true) to release the worker execution.
      // But if anything goes wrong and an exception is thrown, ensure releasing its execution,
      // otherwise if devtools is broken, it will freeze the worker indefinitely.
      //
      // onConnectToWorker can reject if the Worker Debugger is closed; so we only want to
      // resume the debugger if it is not closed (otherwise it can cause crashes).
      if (!dbg.isClosed) {
        dbg.setDebuggerReady(true);
      }
      // Also unregister the worker
      watcherDataObject.workers.splice(
        watcherDataObject.workers.indexOf(workerInfo),
        1
      );
      watcherDataObject.pendingWorkers.delete(concurrentSessionUpdates);
      return;
    }
    watcherDataObject.pendingWorkers.delete(concurrentSessionUpdates);

    const { workerTargetForm, transport } = await onConnectToWorker;
    workerInfo.workerTargetForm = workerTargetForm;
    workerInfo.transport = transport;

    const { forwardingPrefix } = watcherDataObject;
    // Immediately queue a message for the parent process, before applying any SessionData
    // as it may start emitting RDP events on the target actor and be lost if the client
    // didn't get notified about the target actor first
    try {
      watcherDataObject.jsProcessActor.sendAsyncMessage(
        "DevToolsProcessChild:targetAvailable",
        {
          watcherActorID,
          forwardingPrefix,
          targetActorForm: workerTargetForm,
        }
      );
    } catch (e) {
      // If there was an error while sending the message, we are not going to use this
      // connection to communicate with the worker.
      transport.close();
      // Also unregister the worker
      watcherDataObject.workers.splice(
        watcherDataObject.workers.indexOf(workerInfo),
        1
      );
      return;
    }

    // Dispatch to the worker thread any SessionData updates which may have been notified
    // while we were waiting for onConnectToWorker to resolve.
    const promises = [];
    for (const { type, entries, updateType } of concurrentSessionUpdates) {
      promises.push(
        addOrSetSessionDataEntryInWorkerTarget({
          dbg,
          workerThreadServerForwardingPrefix,
          type,
          entries,
          updateType,
        })
      );
    }
    await Promise.all(promises);
  }

  destroyTargetsForWatcher(watcherDataObject) {
    // Notify to all worker threads to destroy their target actor running in them
    for (const {
      dbg,
      workerThreadServerForwardingPrefix,
      transport,
    } of watcherDataObject.workers) {
      if (isWorkerDebuggerAlive(dbg)) {
        try {
          dbg.postMessage(
            JSON.stringify({
              type: "disconnect",
              forwardingPrefix: workerThreadServerForwardingPrefix,
            })
          );
        } catch (e) {}
      }
      // Also cleanup the DevToolsTransport created in the main thread to bridge RDP to the worker thread
      if (transport) {
        transport.close();
      }
    }
    // Wipe all workers info
    watcherDataObject.workers = [];
  }

  /**
   * Indicates whether or not we should handle the worker debugger
   *
   * @param {Object} sessionData
   *        The session data for a given watcher, which includes metadata
   *        about the debugged context.
   * @param {WorkerDebugger} dbg
   *        The worker debugger we want to check.
   * @param {String} targetType
   *        The expected worker target type.
   * @returns {Boolean}
   */
  shouldHandleWorker(sessionData, dbg, targetType) {
    if (!isWorkerDebuggerAlive(dbg)) {
      return false;
    }

    if (
      (dbg.type === TYPE_DEDICATED && targetType != "worker") ||
      (dbg.type === TYPE_SERVICE && targetType != "service_worker") ||
      (dbg.type === TYPE_SHARED && targetType != "shared_worker")
    ) {
      return false;
    }

    const { type: sessionContextType } = sessionData.sessionContext;
    if (sessionContextType == "all") {
      return true;
    }
    if (sessionContextType == "content-process") {
      throw new Error(
        "Content process session type shouldn't try to spawn workers"
      );
    }
    if (sessionContextType == "worker") {
      throw new Error(
        "worker session type should spawn only one target via the WorkerDescriptor"
      );
    }

    if (dbg.type === TYPE_DEDICATED) {
      // Assume that all dedicated workers executes in the same process as the debugged document.
      const browsingContext = BrowsingContext.getCurrentTopByBrowserId(
        sessionData.sessionContext.browserId
      );
      // If we aren't executing in the same process as the worker and its BrowsingContext,
      // it will be undefined.
      if (!browsingContext) {
        return false;
      }
      for (const subBrowsingContext of browsingContext.getAllBrowsingContextsInSubtree()) {
        if (
          subBrowsingContext.currentWindowContext &&
          dbg.windowIDs.includes(
            subBrowsingContext.currentWindowContext.innerWindowId
          )
        ) {
          return true;
        }
      }
      return false;
    }

    if (dbg.type === TYPE_SERVICE) {
      // Accessing `nsIPrincipal.host` may easily throw on non-http URLs.
      // Ignore all non-HTTP as they most likely don't have any valid host name.
      if (!dbg.principal.scheme.startsWith("http")) {
        return false;
      }

      const workerHost = dbg.principal.hostPort;
      return workerHost == sessionData["browser-element-host"][0];
    }

    if (dbg.type === TYPE_SHARED) {
      // We still don't fully support instantiating targets for shared workers from the server side
      throw new Error(
        "Server side listening for shared workers isn't supported"
      );
    }

    return false;
  }
}

/**
 * Communicate the type and entries to the Worker Target actor, via the WorkerDebugger.
 *
 * @param {WorkerDebugger} dbg
 * @param {String} workerThreadServerForwardingPrefix
 * @param {String} type
 *        Session data type name
 * @param {Array} entries
 *        Session data entries to add or set.
 * @param {String} updateType
 *        Either "add" or "set", to control if we should only add some items,
 *        or replace the whole data set with the new entries.
 * @returns {Promise} Returns a Promise that resolves once the data entry were handled
 *                    by the worker target.
 */
function addOrSetSessionDataEntryInWorkerTarget({
  dbg,
  workerThreadServerForwardingPrefix,
  type,
  entries,
  updateType,
}) {
  if (!isWorkerDebuggerAlive(dbg)) {
    return Promise.resolve();
  }

  return new Promise(resolve => {
    // Wait until we're notified by the worker that the resources are watched.
    // This is important so we know existing resources were handled.
    const listener = {
      onMessage: message => {
        message = JSON.parse(message);
        if (message.type === "session-data-entry-added-or-set") {
          dbg.removeListener(listener);
          resolve();
        }
      },
      // Resolve if the worker is being destroyed so we don't have a dangling promise.
      onClose: () => {
        dbg.removeListener(listener);
        resolve();
      },
    };

    dbg.addListener(listener);

    dbg.postMessage(
      JSON.stringify({
        type: "add-or-set-session-data-entry",
        forwardingPrefix: workerThreadServerForwardingPrefix,
        dataEntryType: type,
        entries,
        updateType,
      })
    );
  });
}

function isWorkerDebuggerAlive(dbg) {
  if (dbg.isClosed) {
    return false;
  }
  // Some workers are zombies. `isClosed` is false, but nothing works.
  // `postMessage` is a noop, `addListener`'s `onClosed` doesn't work.
  return (
    dbg.window?.docShell ||
    // consider dbg without `window` as being alive, as they aren't related
    // to any docShell and probably do not suffer from this issue
    !dbg.window
  );
}

export const WorkerTargetWatcher = new WorkerTargetWatcherClass();