2019-02-17 19:57:01 +03:00
|
|
|
/* 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"];
|
|
|
|
|
2019-03-08 01:10:02 +03:00
|
|
|
const {EventEmitter} = ChromeUtils.import("resource://gre/modules/EventEmitter.jsm");
|
2019-02-21 16:54:57 +03:00
|
|
|
const {MessagePromise} = ChromeUtils.import("chrome://remote/content/Sync.jsm");
|
2019-03-10 15:51:09 +03:00
|
|
|
const {TabTarget} = ChromeUtils.import("chrome://remote/content/targets/TabTarget.jsm");
|
2019-03-10 15:51:49 +03:00
|
|
|
const {MainProcessTarget} = ChromeUtils.import("chrome://remote/content/targets/MainProcessTarget.jsm");
|
2019-02-17 19:57:01 +03:00
|
|
|
|
|
|
|
class Targets {
|
|
|
|
constructor() {
|
|
|
|
// browser context ID -> Target<XULElement>
|
|
|
|
this._targets = new Map();
|
2019-03-08 01:10:02 +03:00
|
|
|
|
|
|
|
EventEmitter.decorate(this);
|
2019-02-17 19:57:01 +03:00
|
|
|
}
|
|
|
|
|
2019-03-08 01:10:02 +03:00
|
|
|
/** @param {BrowserElement} browser */
|
2019-02-17 19:57:01 +03:00
|
|
|
async connect(browser) {
|
|
|
|
// 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) {
|
2019-02-21 16:54:57 +03:00
|
|
|
await new MessagePromise(browser.messageManager, "Browser:Init");
|
2019-02-17 19:57:01 +03:00
|
|
|
}
|
|
|
|
|
2019-03-10 15:51:09 +03:00
|
|
|
const target = new TabTarget(this, browser);
|
2019-02-17 19:57:01 +03:00
|
|
|
target.connect();
|
|
|
|
this._targets.set(target.id, target);
|
2019-03-08 01:10:02 +03:00
|
|
|
this.emit("connect", target);
|
2019-02-17 19:57:01 +03:00
|
|
|
}
|
|
|
|
|
2019-03-08 01:10:02 +03:00
|
|
|
/** @param {BrowserElement} browser */
|
2019-02-17 19:57:01 +03:00
|
|
|
disconnect(browser) {
|
|
|
|
// Ignore the browsers that haven't had time to initialize.
|
|
|
|
if (!browser.browsingContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:54:57 +03:00
|
|
|
const target = this._targets.get(browser.browsingContext.id);
|
2019-02-17 19:57:01 +03:00
|
|
|
if (target) {
|
2019-03-08 01:10:02 +03:00
|
|
|
this.emit("disconnect", target);
|
2019-02-17 19:57:01 +03:00
|
|
|
target.disconnect();
|
|
|
|
this._targets.delete(target.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
for (const target of this) {
|
|
|
|
this.disconnect(target.browser);
|
|
|
|
}
|
2019-03-10 15:51:49 +03:00
|
|
|
if (this.mainProcessTarget) {
|
|
|
|
this.mainProcessTarget.disconnect();
|
|
|
|
this.mainProcessTarget = null;
|
|
|
|
}
|
2019-02-17 19:57:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get size() {
|
|
|
|
return this._targets.size;
|
|
|
|
}
|
|
|
|
|
2019-03-10 15:51:49 +03:00
|
|
|
/**
|
|
|
|
* 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.emit("connect", this.mainProcessTarget);
|
|
|
|
}
|
|
|
|
return this.mainProcessTarget;
|
|
|
|
}
|
|
|
|
|
2019-02-17 19:57:01 +03:00
|
|
|
* [Symbol.iterator]() {
|
|
|
|
for (const target of this._targets.values()) {
|
|
|
|
yield target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return [...this];
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return `[object Targets ${this.size}]`;
|
|
|
|
}
|
|
|
|
}
|