summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/utils/actor-registry.js
blob: ae88c38c6bc071d11febdeb8625c654f0f888868 (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
/* 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";

var gRegisteredModules = Object.create(null);

const ActorRegistry = {
  // Map of global actor names to actor constructors.
  globalActorFactories: {},
  // Map of target-scoped actor names to actor constructors.
  targetScopedActorFactories: {},
  init(connections) {
    this._connections = connections;
  },

  /**
   * Register a CommonJS module with the devtools server.
   * @param id string
   *        The ID of a CommonJS module.
   *        The actor is going to be registered immediately, but loaded only
   *        when a client starts sending packets to an actor with the same id.
   *
   * @param options object
   *        An object with 3 mandatory attributes:
   *        - prefix (string):
   *          The prefix of an actor is used to compute:
   *          - the `actorID` of each new actor instance (ex: prefix1). (See Pool.manage)
   *          - the actor name in the listTabs request. Sending a listTabs
   *            request to the root actor returns actor IDs. IDs are in
   *            dictionaries, with actor names as keys and actor IDs as values.
   *            The actor name is the prefix to which the "Actor" string is
   *            appended. So for an actor with the `console` prefix, the actor
   *            name will be `consoleActor`.
   *        - constructor (string):
   *          the name of the exported symbol to be used as the actor
   *          constructor.
   *        - type (a dictionary of booleans with following attribute names):
   *          - "global"
   *            registers a global actor instance, if true.
   *            A global actor has the root actor as its parent.
   *          - "target"
   *            registers a target-scoped actor instance, if true.
   *            A new actor will be created for each target, such as a tab.
   */
  registerModule(id, options) {
    if (id in gRegisteredModules) {
      return;
    }

    if (!options) {
      throw new Error(
        "ActorRegistry.registerModule requires an options argument"
      );
    }
    const { prefix, constructor, type } = options;
    if (typeof prefix !== "string") {
      throw new Error(
        `Lazy actor definition for '${id}' requires a string ` +
          `'prefix' option.`
      );
    }
    if (typeof constructor !== "string") {
      throw new Error(
        `Lazy actor definition for '${id}' requires a string ` +
          `'constructor' option.`
      );
    }
    if (!("global" in type) && !("target" in type)) {
      throw new Error(
        `Lazy actor definition for '${id}' requires a dictionary ` +
          `'type' option whose attributes can be 'global' or 'target'.`
      );
    }
    const name = prefix + "Actor";
    const mod = {
      id,
      prefix,
      constructorName: constructor,
      type,
      globalActor: type.global,
      targetScopedActor: type.target,
    };
    gRegisteredModules[id] = mod;
    if (mod.targetScopedActor) {
      this.addTargetScopedActor(mod, name);
    }
    if (mod.globalActor) {
      this.addGlobalActor(mod, name);
    }
  },

  /**
   * Unregister a previously-loaded CommonJS module from the devtools server.
   */
  unregisterModule(id) {
    const mod = gRegisteredModules[id];
    if (!mod) {
      throw new Error(
        "Tried to unregister a module that was not previously registered."
      );
    }

    // Lazy actors
    if (mod.targetScopedActor) {
      this.removeTargetScopedActor(mod);
    }
    if (mod.globalActor) {
      this.removeGlobalActor(mod);
    }

    delete gRegisteredModules[id];
  },

  /**
   * Install Firefox-specific actors.
   *
   * /!\ Be careful when adding a new actor, especially global actors.
   * Any new global actor will be exposed and returned by the root actor.
   */
  addBrowserActors() {
    this.registerModule("devtools/server/actors/preference", {
      prefix: "preference",
      constructor: "PreferenceActor",
      type: { global: true },
    });
    this.registerModule("devtools/server/actors/addon/addons", {
      prefix: "addons",
      constructor: "AddonsActor",
      type: { global: true },
    });
    this.registerModule("devtools/server/actors/device", {
      prefix: "device",
      constructor: "DeviceActor",
      type: { global: true },
    });
    this.registerModule("devtools/server/actors/heap-snapshot-file", {
      prefix: "heapSnapshotFile",
      constructor: "HeapSnapshotFileActor",
      type: { global: true },
    });
    // Always register this as a global module, even while there is a pref turning
    // on and off the other performance actor. This actor shouldn't conflict with
    // the other one. These are also lazily loaded so there shouldn't be a performance
    // impact.
    this.registerModule("devtools/server/actors/perf", {
      prefix: "perf",
      constructor: "PerfActor",
      type: { global: true },
    });
    /**
     * Always register parent accessibility actor as a global module. This
     * actor is responsible for all top level accessibility actor functionality
     * that relies on the parent process.
     */
    this.registerModule(
      "devtools/server/actors/accessibility/parent-accessibility",
      {
        prefix: "parentAccessibility",
        constructor: "ParentAccessibilityActor",
        type: { global: true },
      }
    );

    this.registerModule("devtools/server/actors/screenshot", {
      prefix: "screenshot",
      constructor: "ScreenshotActor",
      type: { global: true },
    });
  },

  /**
   * Install target-scoped actors.
   */
  addTargetScopedActors() {
    this.registerModule("devtools/server/actors/webconsole", {
      prefix: "console",
      constructor: "WebConsoleActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/inspector/inspector", {
      prefix: "inspector",
      constructor: "InspectorActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/style-sheets", {
      prefix: "styleSheets",
      constructor: "StyleSheetsActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/memory", {
      prefix: "memory",
      constructor: "MemoryActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/reflow", {
      prefix: "reflow",
      constructor: "ReflowActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/css-properties", {
      prefix: "cssProperties",
      constructor: "CssPropertiesActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/animation", {
      prefix: "animations",
      constructor: "AnimationsActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/emulation/responsive", {
      prefix: "responsive",
      constructor: "ResponsiveActor",
      type: { target: true },
    });
    this.registerModule(
      "devtools/server/actors/addon/webextension-inspected-window",
      {
        prefix: "webExtensionInspectedWindow",
        constructor: "WebExtensionInspectedWindowActor",
        type: { target: true },
      }
    );
    this.registerModule("devtools/server/actors/accessibility/accessibility", {
      prefix: "accessibility",
      constructor: "AccessibilityActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/changes", {
      prefix: "changes",
      constructor: "ChangesActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/manifest", {
      prefix: "manifest",
      constructor: "ManifestActor",
      type: { target: true },
    });
    this.registerModule(
      "devtools/server/actors/network-monitor/network-content",
      {
        prefix: "networkContent",
        constructor: "NetworkContentActor",
        type: { target: true },
      }
    );
    this.registerModule("devtools/server/actors/screenshot-content", {
      prefix: "screenshotContent",
      constructor: "ScreenshotContentActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/tracer", {
      prefix: "tracer",
      constructor: "TracerActor",
      type: { target: true },
    });
    this.registerModule("devtools/server/actors/objects-manager", {
      prefix: "objectsManager",
      constructor: "ObjectsManagerActor",
      type: { target: true },
    });
  },

  /**
   * Registers handlers for new target-scoped request types defined dynamically.
   *
   * Note that the name of the request type is not allowed to clash with existing protocol
   * packet properties, like 'title', 'url' or 'actor', since that would break the protocol.
   *
   * @param options object
   *        - constructorName: (required)
   *          name of actor constructor, which is also used when removing the actor.
   *        One of the following:
   *          - id:
   *            module ID that contains the actor
   *          - constructorFun:
   *            a function to construct the actor
   * @param name string
   *        The name of the new request type.
   */
  addTargetScopedActor(options, name) {
    if (!name) {
      throw Error("addTargetScopedActor requires the `name` argument");
    }
    if (["title", "url", "actor"].includes(name)) {
      throw Error(name + " is not allowed");
    }
    if (this.targetScopedActorFactories.hasOwnProperty(name)) {
      throw Error(name + " already exists");
    }
    this.targetScopedActorFactories[name] = { options, name };
  },

  /**
   * Unregisters the handler for the specified target-scoped request type.
   *
   * When unregistering an existing target-scoped actor, we remove the actor factory as
   * well as all existing instances of the actor.
   *
   * @param actor object, string
   *        In case of object:
   *          The `actor` object being given to related addTargetScopedActor call.
   *        In case of string:
   *          The `name` string being given to related addTargetScopedActor call.
   */
  removeTargetScopedActor(actorOrName) {
    let name;
    if (typeof actorOrName == "string") {
      name = actorOrName;
    } else {
      const actor = actorOrName;
      for (const factoryName in this.targetScopedActorFactories) {
        const handler = this.targetScopedActorFactories[factoryName];
        if (
          handler.options.constructorName == actor.name ||
          handler.options.id == actor.id
        ) {
          name = factoryName;
          break;
        }
      }
    }
    if (!name) {
      return;
    }
    delete this.targetScopedActorFactories[name];
    for (const connID of Object.getOwnPropertyNames(this._connections)) {
      // DevToolsServerConnection in child process don't have rootActor
      if (this._connections[connID].rootActor) {
        this._connections[connID].rootActor.removeActorByName(name);
      }
    }
  },

  /**
   * Registers handlers for new browser-scoped request types defined dynamically.
   *
   * Note that the name of the request type is not allowed to clash with existing protocol
   * packet properties, like 'from', 'tabs' or 'selected', since that would break the protocol.
   *
   * @param options object
   *        - constructorName: (required)
   *          name of actor constructor, which is also used when removing the actor.
   *        One of the following:
   *          - id:
   *            module ID that contains the actor
   *          - constructorFun:
   *            a function to construct the actor
   * @param name string
   *        The name of the new request type.
   */
  addGlobalActor(options, name) {
    if (!name) {
      throw Error("addGlobalActor requires the `name` argument");
    }
    if (["from", "tabs", "selected"].includes(name)) {
      throw Error(name + " is not allowed");
    }
    if (this.globalActorFactories.hasOwnProperty(name)) {
      throw Error(name + " already exists");
    }
    this.globalActorFactories[name] = { options, name };
  },

  /**
   * Unregisters the handler for the specified browser-scoped request type.
   *
   * When unregistering an existing global actor, we remove the actor factory as well as
   * all existing instances of the actor.
   *
   * @param actor object, string
   *        In case of object:
   *          The `actor` object being given to related addGlobalActor call.
   *        In case of string:
   *          The `name` string being given to related addGlobalActor call.
   */
  removeGlobalActor(actorOrName) {
    let name;
    if (typeof actorOrName == "string") {
      name = actorOrName;
    } else {
      const actor = actorOrName;
      for (const factoryName in this.globalActorFactories) {
        const handler = this.globalActorFactories[factoryName];
        if (
          handler.options.constructorName == actor.name ||
          handler.options.id == actor.id
        ) {
          name = factoryName;
          break;
        }
      }
    }
    if (!name) {
      return;
    }
    delete this.globalActorFactories[name];
    for (const connID of Object.getOwnPropertyNames(this._connections)) {
      // DevToolsServerConnection in child process don't have rootActor
      if (this._connections[connID].rootActor) {
        this._connections[connID].rootActor.removeActorByName(name);
      }
    }
  },

  destroy() {
    for (const id of Object.getOwnPropertyNames(gRegisteredModules)) {
      this.unregisterModule(id);
    }
    gRegisteredModules = Object.create(null);

    this.globalActorFactories = {};
    this.targetScopedActorFactories = {};
  },
};

exports.ActorRegistry = ActorRegistry;