summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/targets/target-actor-registry.jsm
blob: f3ba0da908b8480412b0c0cca840f60c8934250b (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
/* 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 EXPORTED_SYMBOLS = ["TargetActorRegistry"];

// Keep track of all BrowsingContext 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 browsingContextTargetActors = new Set();

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

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

  /**
   * Return the first target actor matching the passed browser element id. Returns null if
   * no matching target actors could be found.
   *
   * @param {Integer} browserId: The browserId to retrieve targets for. Pass null to
   *                             retrieve the parent process targets.
   * @param {String} connectionPrefix: Optional prefix, in order to select only actor
   *                                   related to the same connection. i.e. the same client.
   * @returns {TargetActor|null}
   */
  getTargetActor(browserId, connectionPrefix) {
    return this.getTargetActors(browserId, connectionPrefix)[0] || null;
  },

  /**
   * 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 {Integer} browserId: The browserId to retrieve targets for. Pass null to
   *                             retrieve the parent process targets.
   * @param {String} connectionPrefix: Optional prefix, in order to select only actor
   *                                   related to the same connection. i.e. the same client.
   * @returns {Array<TargetActor>}
   */
  getTargetActors(browserId, connectionPrefix) {
    const actors = [];
    for (const actor of browsingContextTargetActors) {
      if (
        ((!connectionPrefix || actor.actorID.startsWith(connectionPrefix)) &&
          actor.browserId == browserId) ||
        (browserId === null && actor.typeName === "parentProcessTarget")
      ) {
        actors.push(actor);
      }
    }
    return actors;
  },

  /**
   * Return the parent process target actor. Returns null if it couldn't be found.
   *
   * @returns {TargetActor|null}
   */
  getParentProcessTargetActor() {
    for (const actor of browsingContextTargetActors) {
      if (actor.typeName === "parentProcessTarget") {
        return actor;
      }
    }

    return null;
  },
};