summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/targets/target-actor-registry.sys.mjs
blob: 4cb6d13868442deed12cdf91474fd33ecb81c057 (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
/* 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/. */

// Keep track of all WindowGlobal target actors.
// This is especially used to track the actors using Message manager connector,
// or the ones running in the parent process.
// Top level actors, like tab's top level target or parent process target
// are still using message manager in order to avoid being destroyed on navigation.
// And because of this, these actors aren't using JS Window Actor.
const windowGlobalTargetActors = new Set();
let xpcShellTargetActor = null;

export var TargetActorRegistry = {
  registerTargetActor(targetActor) {
    windowGlobalTargetActors.add(targetActor);
  },

  unregisterTargetActor(targetActor) {
    windowGlobalTargetActors.delete(targetActor);
  },

  registerXpcShellTargetActor(targetActor) {
    xpcShellTargetActor = targetActor;
  },

  unregisterXpcShellTargetActor(targetActor) {
    xpcShellTargetActor = null;
  },

  get xpcShellTargetActor() {
    return xpcShellTargetActor;
  },

  /**
   * Return the target actors matching the passed browser element id.
   * In some scenarios, the registry can have multiple target actors for a given
   * browserId (e.g. the regular DevTools content toolbox + DevTools WebExtensions targets).
   *
   * @param {Object} sessionContext: The Session Context to help know what is debugged.
   *                                 See devtools/server/actors/watcher/session-context.js
   * @param {String} connectionPrefix: DevToolsServerConnection's prefix, in order to select only actor
   *                                   related to the same connection. i.e. the same client.
   * @returns {Array<TargetActor>}
   */
  getTargetActors(sessionContext, connectionPrefix) {
    const actors = [];
    for (const actor of windowGlobalTargetActors) {
      const isMatchingPrefix = actor.actorID.startsWith(connectionPrefix);
      const isMatchingContext =
        sessionContext.type == "all" ||
        (sessionContext.type == "browser-element" &&
          (actor.browserId == sessionContext.browserId ||
            actor.openerBrowserId == sessionContext.browserId)) ||
        (sessionContext.type == "webextension" &&
          actor.addonId == sessionContext.addonId);
      if (isMatchingPrefix && isMatchingContext) {
        actors.push(actor);
      }
    }
    return actors;
  },

  /**
   * Helper for tests to help track the number of targets created for a given tab.
   * (Used by browser_ext_devtools_inspectedWindow.js)
   *
   * @param {Number} browserId: ID for the tab
   *
   * @returns {Number} Number of targets for this tab.
   */

  getTargetActorsCountForBrowserElement(browserId) {
    let count = 0;
    for (const actor of windowGlobalTargetActors) {
      if (actor.browserId == browserId) {
        count++;
      }
    }
    return count;
  },
};