mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-25 03:05:34 +00:00
111854b5a5
* TabObserver is rather an helper class of Targets rather than RemoteAgent. Targets is the class which holds all the targets and reports about their creation and destructor. It feels legitimate to have it directly integrate with TabObserver. * To better sort of the files. i.e. avoid having "random files" in /remote/ I'm renaming and moving TabObserver according to its usage. * We were emitting "connect" and "disconnect" event when a target was created or destroyed. But this is misleading as there is no connection to anything being made. Only later, a CDP client might connect to a target HTTP endpoint and initiate a connection. These events are making this hard to understand that the connection actually happens when Target.handle is called. Differential Revision: https://phabricator.services.mozilla.com/D37043 --HG-- rename : remote/WindowManager.jsm => remote/targets/TabObserver.jsm extra : moz-landing-system : lando
163 lines
4.2 KiB
JavaScript
163 lines
4.2 KiB
JavaScript
/* 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 = ["Targets"];
|
|
|
|
const { EventEmitter } = ChromeUtils.import(
|
|
"resource://gre/modules/EventEmitter.jsm"
|
|
);
|
|
const { MessagePromise } = ChromeUtils.import(
|
|
"chrome://remote/content/Sync.jsm"
|
|
);
|
|
const { TabTarget } = ChromeUtils.import(
|
|
"chrome://remote/content/targets/TabTarget.jsm"
|
|
);
|
|
const { MainProcessTarget } = ChromeUtils.import(
|
|
"chrome://remote/content/targets/MainProcessTarget.jsm"
|
|
);
|
|
const { TabObserver } = ChromeUtils.import(
|
|
"chrome://remote/content/targets/TabObserver.jsm"
|
|
);
|
|
|
|
class Targets {
|
|
constructor() {
|
|
// Target ID -> Target
|
|
this._targets = new Map();
|
|
|
|
EventEmitter.decorate(this);
|
|
}
|
|
|
|
/**
|
|
* Start listing and listening for all the debuggable targets
|
|
*/
|
|
async watchForTargets() {
|
|
await this.watchForTabs();
|
|
}
|
|
|
|
unwatchForTargets() {
|
|
this.unwatchForTabs();
|
|
}
|
|
|
|
/**
|
|
* Watch for all existing and new tabs being opened.
|
|
* So that we can create the related TabTarget instance for
|
|
* each of them.
|
|
*/
|
|
async watchForTabs() {
|
|
if (this.tabObserver) {
|
|
throw new Error("Targets is already watching for new tabs");
|
|
}
|
|
this.tabObserver = new TabObserver({ registerExisting: true });
|
|
this.tabObserver.on("open", async (eventName, tab) => {
|
|
const browser = tab.linkedBrowser;
|
|
// The tab may just have been created and not fully initialized yet.
|
|
// Target class expects BrowserElement.browsingContext to be defined
|
|
// whereas it is asynchronously set by the custom element class.
|
|
// At least ensure that this property is set before instantiating the target.
|
|
if (!browser.browsingContext) {
|
|
await new MessagePromise(browser.messageManager, "Browser:Init");
|
|
}
|
|
const target = new TabTarget(this, browser);
|
|
this.registerTarget(target);
|
|
});
|
|
this.tabObserver.on("close", (eventName, tab) => {
|
|
const browser = tab.linkedBrowser;
|
|
// Ignore the browsers that haven't had time to initialize.
|
|
if (!browser.browsingContext) {
|
|
return;
|
|
}
|
|
const target = this.getById(browser.browsingContext.id);
|
|
if (target) {
|
|
this.destroyTarget(target);
|
|
}
|
|
});
|
|
await this.tabObserver.start();
|
|
}
|
|
|
|
unwatchForTabs() {
|
|
if (this.tabObserver) {
|
|
this.tabObserver.stop();
|
|
this.tabObserver = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* To be called right after instantiating a new Target instance.
|
|
* This will hold the new instance in the list and notify about
|
|
* its creation.
|
|
*/
|
|
registerTarget(target) {
|
|
this._targets.set(target.id, target);
|
|
this.emit("target-created", target);
|
|
}
|
|
|
|
/**
|
|
* To be called when the debuggable target has been destroy.
|
|
* So that we can notify it no longer exists and disconnect
|
|
* all connecting made to debug it.
|
|
*/
|
|
destroyTarget(target) {
|
|
target.destructor();
|
|
this._targets.delete(target.id);
|
|
this.emit("target-destroyed", target);
|
|
}
|
|
|
|
/**
|
|
* Destroy all the registered target of all kinds.
|
|
* This will end up dropping all connections made to debug any of them.
|
|
*/
|
|
destructor() {
|
|
for (const target of this) {
|
|
this.destroyTarget(target);
|
|
}
|
|
this._targets.clear();
|
|
if (this.mainProcessTarget) {
|
|
this.mainProcessTarget = null;
|
|
}
|
|
|
|
this.unwatchForTargets();
|
|
}
|
|
|
|
get size() {
|
|
return this._targets.size;
|
|
}
|
|
|
|
/**
|
|
* Get Target instance by target id
|
|
*
|
|
* @param int id Target id
|
|
*/
|
|
getById(id) {
|
|
return this._targets.get(id);
|
|
}
|
|
|
|
/**
|
|
* Get the Target instance for the main process.
|
|
* This target is a singleton and only exposes a subset of domains.
|
|
*/
|
|
getMainProcessTarget() {
|
|
if (!this.mainProcessTarget) {
|
|
this.mainProcessTarget = new MainProcessTarget(this);
|
|
this.registerTarget(this.mainProcessTarget);
|
|
}
|
|
return this.mainProcessTarget;
|
|
}
|
|
|
|
*[Symbol.iterator]() {
|
|
for (const target of this._targets.values()) {
|
|
yield target;
|
|
}
|
|
}
|
|
|
|
toJSON() {
|
|
return [...this];
|
|
}
|
|
|
|
toString() {
|
|
return `[object Targets ${this.size}]`;
|
|
}
|
|
}
|