summaryrefslogtreecommitdiffstats
path: root/devtools/client/fronts/watcher.js
blob: 239d3047a07d4a51a800d5aa85c051b8f3a6604e (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
/* 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";

const { watcherSpec } = require("devtools/shared/specs/watcher");
const {
  FrontClassWithSpec,
  registerFront,
} = require("devtools/shared/protocol");

loader.lazyRequireGetter(
  this,
  "BrowsingContextTargetFront",
  "devtools/client/fronts/targets/browsing-context",
  true
);
loader.lazyRequireGetter(
  this,
  "ContentProcessTargetFront",
  "devtools/client/fronts/targets/content-process",
  true
);
loader.lazyRequireGetter(
  this,
  "WorkerTargetFront",
  "devtools/client/fronts/targets/worker",
  true
);

class WatcherFront extends FrontClassWithSpec(watcherSpec) {
  constructor(client, targetFront, parentFront) {
    super(client, targetFront, parentFront);

    this._onTargetAvailable = this._onTargetAvailable.bind(this);
    this._onTargetDestroyed = this._onTargetDestroyed.bind(this);

    // Convert form, which is just JSON object to Fronts for these two events
    this.on("target-available-form", this._onTargetAvailable);
    this.on("target-destroyed-form", this._onTargetDestroyed);
  }

  form(json) {
    this.actorID = json.actor;
    this.traits = json.traits;
  }

  _onTargetAvailable(form) {
    let front;
    if (form.actor.includes("/contentProcessTarget")) {
      front = new ContentProcessTargetFront(this.conn, null, this);
    } else if (form.actor.includes("/workerTarget")) {
      front = new WorkerTargetFront(this.conn, null, this);
    } else {
      front = new BrowsingContextTargetFront(this.conn, null, this);
    }
    front.actorID = form.actor;
    front.form(form);
    this.manage(front);
    this.emit("target-available", front);
  }

  _onTargetDestroyed(form) {
    const front = this.getActorByID(form.actor);
    this.emit("target-destroyed", front);
  }

  /**
   * Retrieve the already existing BrowsingContextTargetFront for the parent
   * BrowsingContext of the given BrowsingContext ID.
   */
  async getParentBrowsingContextTarget(browsingContextID) {
    const id = await this.getParentBrowsingContextID(browsingContextID);
    if (!id) {
      return null;
    }
    return this.getBrowsingContextTarget(id);
  }

  /**
   * For a given BrowsingContext ID, return the already existing BrowsingContextTargetFront
   */
  async getBrowsingContextTarget(id) {
    // First scan the watcher children as the watcher manages all the targets
    for (const front of this.poolChildren()) {
      if (front.browsingContextID == id) {
        return front;
      }
    }
    // But the top level target will be created by the Descriptor.getTarget() method
    // and so be hosted in the Descriptor's pool.
    // The parent front of the WatcherActor happens to be the Descriptor Actor.
    // This code could go away or be simplified if the Descriptor starts fetch all
    // the targets, including the top level one via the Watcher. i.e. drop Descriptor.getTarget().
    const topLevelTarget = await this.parentFront.getTarget();
    if (topLevelTarget.browsingContextID == id) {
      return topLevelTarget;
    }

    // If we could not find a browsing context target for the provided id, the
    // browsing context might not be the topmost browsing context of a given
    // process. For now we only create targets for the top browsing context of
    // each process, so we recursively check the parent browsing context ids
    // until we find a valid target.
    const parentBrowsingContextID = await this.getParentBrowsingContextID(id);
    if (parentBrowsingContextID && parentBrowsingContextID !== id) {
      return this.getBrowsingContextTarget(parentBrowsingContextID);
    }

    return null;
  }
}
registerFront(WatcherFront);