summaryrefslogtreecommitdiffstats
path: root/devtools/server/connectors/js-process-actor/DevToolsProcessChild.sys.mjs
blob: 9e8ad64eea74c2d34b6df0dd98c2c62b53eb4b85 (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
/* 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 { EventEmitter } from "resource://gre/modules/EventEmitter.sys.mjs";

const lazy = {};
ChromeUtils.defineESModuleGetters(
  lazy,
  {
    releaseDistinctSystemPrincipalLoader:
      "resource://devtools/shared/loader/DistinctSystemPrincipalLoader.sys.mjs",
    useDistinctSystemPrincipalLoader:
      "resource://devtools/shared/loader/DistinctSystemPrincipalLoader.sys.mjs",
  },
  { global: "contextual" }
);

// Name of the attribute into which we save data in `sharedData` object.
const SHARED_DATA_KEY_NAME = "DevTools:watchedPerWatcher";

// If true, log info about DOMProcess's being created.
const DEBUG = false;

/**
 * Print information about operation being done against each content process.
 *
 * @param {nsIDOMProcessChild} domProcessChild
 *        The process for which we should log a message.
 * @param {String} message
 *        Message to log.
 */
function logDOMProcess(domProcessChild, message) {
  if (!DEBUG) {
    return;
  }
  dump(" [pid:" + domProcessChild + "] " + message + "\n");
}

export class DevToolsProcessChild extends JSProcessActorChild {
  constructor() {
    super();

    // The map is indexed by the Watcher Actor ID.
    // The values are objects containing the following properties:
    // - connection: the DevToolsServerConnection itself
    // - actor: the ContentProcessTargetActor instance
    this._connections = new Map();

    this._onConnectionChange = this._onConnectionChange.bind(this);
    EventEmitter.decorate(this);
  }

  instantiate() {
    const { sharedData } = Services.cpmm;
    const watchedDataByWatcherActor = sharedData.get(SHARED_DATA_KEY_NAME);
    if (!watchedDataByWatcherActor) {
      throw new Error(
        "Request to instantiate the target(s) for the process, but `sharedData` is empty about watched targets"
      );
    }

    // Create one Target actor for each prefix/client which listen to processes
    for (const [watcherActorID, sessionData] of watchedDataByWatcherActor) {
      const { connectionPrefix } = sessionData;

      if (sessionData.targets?.includes("process")) {
        this._createTargetActor(watcherActorID, connectionPrefix, sessionData);
      }
    }
  }

  /**
   * Instantiate a new ProcessTarget for the given connection.
   *
   * @param String watcherActorID
   *        The ID of the WatcherActor who requested to observe and create these target actors.
   * @param String parentConnectionPrefix
   *        The prefix of the DevToolsServerConnection of the Watcher Actor.
   *        This is used to compute a unique ID for the target actor.
   * @param Object sessionData
   *        All data managed by the Watcher Actor and WatcherRegistry.sys.mjs, containing
   *        target types, resources types to be listened as well as breakpoints and any
   *        other data meant to be shared across processes and threads.
   */
  _createTargetActor(watcherActorID, parentConnectionPrefix, sessionData) {
    // This method will be concurrently called from `observe()` and `DevToolsProcessParent:instantiate-already-available`
    // When the JSprocessActor initializes itself and when the watcher want to force instantiating existing targets.
    // Simply ignore the second call as there is nothing to return, neither to wait for as this method is synchronous.
    if (this._connections.has(watcherActorID)) {
      return;
    }

    // Compute a unique prefix, just for this DOM Process,
    // which will be used to create a JSWindowActorTransport pair between content and parent processes.
    // This is slightly hacky as we typicaly compute Prefix and Actor ID via `DevToolsServerConnection.allocID()`,
    // but here, we can't have access to any DevTools connection as we are really early in the content process startup
    // XXX: nsIDOMProcessChild's childID should be unique across processes, I think. So that should be safe?
    // (this.manager == nsIDOMProcessChild interface)
    // Ensure appending a final slash, otherwise the prefix may be the same between childID 1 and 10...
    const forwardingPrefix =
      parentConnectionPrefix + "contentProcess" + this.manager.childID + "/";

    logDOMProcess(
      this.manager,
      "Instantiate ContentProcessTarget with prefix: " + forwardingPrefix
    );

    const { connection, targetActor } = this._createConnectionAndActor(
      watcherActorID,
      forwardingPrefix,
      sessionData
    );
    this._connections.set(watcherActorID, {
      connection,
      actor: targetActor,
    });

    // Immediately queue a message for the parent process,
    // in order to ensure that the JSWindowActorTransport is instantiated
    // before any packet is sent from the content process.
    // As the order of messages is guaranteed to be delivered in the order they
    // were queued, we don't have to wait for anything around this sendAsyncMessage call.
    // In theory, the ContentProcessTargetActor may emit events in its constructor.
    // If it does, such RDP packets may be lost. But in practice, no events
    // are emitted during its construction. Instead the frontend will start
    // the communication first.
    this.sendAsyncMessage("DevToolsProcessChild:connectFromContent", {
      watcherActorID,
      forwardingPrefix,
      actor: targetActor.form(),
    });

    // Pass initialization data to the target actor
    for (const type in sessionData) {
      // `sessionData` will also contain `browserId` as well as entries with empty arrays,
      // which shouldn't be processed.
      const entries = sessionData[type];
      if (!Array.isArray(entries) || !entries.length) {
        continue;
      }
      targetActor.addOrSetSessionDataEntry(
        type,
        sessionData[type],
        false,
        "set"
      );
    }
  }

  _destroyTargetActor(watcherActorID, isModeSwitching) {
    const connectionInfo = this._connections.get(watcherActorID);
    // This connection has already been cleaned?
    if (!connectionInfo) {
      throw new Error(
        `Trying to destroy a target actor that doesn't exists, or has already been destroyed. Watcher Actor ID:${watcherActorID}`
      );
    }
    connectionInfo.connection.close({ isModeSwitching });
    this._connections.delete(watcherActorID);
    if (this._connections.size == 0) {
      this.didDestroy({ isModeSwitching });
    }
  }

  _createConnectionAndActor(watcherActorID, forwardingPrefix, sessionData) {
    if (!this.loader) {
      this.loader = lazy.useDistinctSystemPrincipalLoader(this);
    }
    const { DevToolsServer } = this.loader.require(
      "devtools/server/devtools-server"
    );

    const { ContentProcessTargetActor } = this.loader.require(
      "devtools/server/actors/targets/content-process"
    );

    DevToolsServer.init();

    // For browser content toolbox, we do need a regular root actor and all tab
    // actors, but don't need all the "browser actors" that are only useful when
    // debugging the parent process via the browser toolbox.
    DevToolsServer.registerActors({ target: true });
    DevToolsServer.on("connectionchange", this._onConnectionChange);

    const connection = DevToolsServer.connectToParentWindowActor(
      this,
      forwardingPrefix,
      "DevToolsProcessChild:packet"
    );

    // Create the actual target actor.
    const targetActor = new ContentProcessTargetActor(connection, {
      sessionContext: sessionData.sessionContext,
    });
    // There is no root actor in content processes and so
    // the target actor can't be managed by it, but we do have to manage
    // the actor to have it working and be registered in the DevToolsServerConnection.
    // We make it manage itself and become a top level actor.
    targetActor.manage(targetActor);

    const form = targetActor.form();
    targetActor.once("destroyed", options => {
      // This will destroy the content process one
      this._destroyTargetActor(watcherActorID, options.isModeSwitching);
      // And this will destroy the parent process one
      try {
        this.sendAsyncMessage("DevToolsProcessChild:destroy", {
          actors: [
            {
              watcherActorID,
              form,
            },
          ],
          options,
        });
      } catch (e) {
        // Ignore exception when the JSProcessActorChild has already been destroyed.
        // We often try to emit this message while the process is being destroyed,
        // but sendAsyncMessage doesn't have time to complete and throws.
        if (
          !e.message.includes("JSProcessActorChild cannot send at the moment")
        ) {
          throw e;
        }
      }
    });

    return { connection, targetActor };
  }

  /**
   * Destroy the server once its last connection closes. Note that multiple
   * frame scripts may be running in parallel and reuse the same server.
   */
  _onConnectionChange() {
    if (this._destroyed) {
      return;
    }
    this._destroyed = true;

    const { DevToolsServer } = this.loader.require(
      "devtools/server/devtools-server"
    );

    // Only destroy the server if there is no more connections to it. It may be
    // used to debug another tab running in the same process.
    if (DevToolsServer.hasConnection() || DevToolsServer.keepAlive) {
      return;
    }

    DevToolsServer.off("connectionchange", this._onConnectionChange);
    DevToolsServer.destroy();
  }

  /**
   * Supported Queries
   */

  sendPacket(packet, prefix) {
    this.sendAsyncMessage("DevToolsProcessChild:packet", { packet, prefix });
  }

  /**
   * JsWindowActor API
   */

  async sendQuery(msg, args) {
    try {
      const res = await super.sendQuery(msg, args);
      return res;
    } catch (e) {
      console.error("Failed to sendQuery in DevToolsProcessChild", msg);
      console.error(e.toString());
      throw e;
    }
  }

  receiveMessage(message) {
    switch (message.name) {
      case "DevToolsProcessParent:instantiate-already-available": {
        const { watcherActorID, connectionPrefix, sessionData } = message.data;
        return this._createTargetActor(
          watcherActorID,
          connectionPrefix,
          sessionData
        );
      }
      case "DevToolsProcessParent:destroy": {
        const { watcherActorID, isModeSwitching } = message.data;
        return this._destroyTargetActor(watcherActorID, isModeSwitching);
      }
      case "DevToolsProcessParent:addOrSetSessionDataEntry": {
        const { watcherActorID, type, entries, updateType } = message.data;
        return this._addOrSetSessionDataEntry(
          watcherActorID,
          type,
          entries,
          updateType
        );
      }
      case "DevToolsProcessParent:removeSessionDataEntry": {
        const { watcherActorID, type, entries } = message.data;
        return this._removeSessionDataEntry(watcherActorID, type, entries);
      }
      case "DevToolsProcessParent:packet":
        return this.emit("packet-received", message);
      default:
        throw new Error(
          "Unsupported message in DevToolsProcessParent: " + message.name
        );
    }
  }

  _getTargetActorForWatcherActorID(watcherActorID) {
    const connectionInfo = this._connections.get(watcherActorID);
    return connectionInfo?.actor;
  }

  _addOrSetSessionDataEntry(watcherActorID, type, entries, updateType) {
    const targetActor = this._getTargetActorForWatcherActorID(watcherActorID);
    if (!targetActor) {
      throw new Error(
        `No target actor for this Watcher Actor ID:"${watcherActorID}"`
      );
    }
    return targetActor.addOrSetSessionDataEntry(
      type,
      entries,
      false,
      updateType
    );
  }

  _removeSessionDataEntry(watcherActorID, type, entries) {
    const targetActor = this._getTargetActorForWatcherActorID(watcherActorID);
    // By the time we are calling this, the target may already have been destroyed.
    if (!targetActor) {
      return null;
    }
    return targetActor.removeSessionDataEntry(type, entries);
  }

  observe(subject, topic) {
    if (topic === "init-devtools-content-process-actor") {
      // This is triggered by the process actor registration and some code in process-helper.js
      // which defines a unique topic to be observed
      this.instantiate();
    }
  }

  didDestroy(options) {
    for (const { connection } of this._connections.values()) {
      connection.close(options);
    }
    this._connections.clear();
    if (this.loader) {
      lazy.releaseDistinctSystemPrincipalLoader(this);
      this.loader = null;
    }
  }
}