bug 1523104: remote: move Targets class to separate module; r=ochameau

This commit is contained in:
Andreas Tolfsen 2019-02-17 16:57:01 +00:00
Родитель b85cdb8aa9
Коммит 01e6c6d438
3 изменённых файлов: 77 добавлений и 66 удалений

Просмотреть файл

@ -18,6 +18,7 @@ XPCOMUtils.defineLazyModuleGetters(this, {
RecommendedPreferences: "chrome://remote/content/Prefs.jsm",
TabObserver: "chrome://remote/content/WindowManager.jsm",
Target: "chrome://remote/content/Target.jsm",
Targets: "chrome://remote/content/Targets.jsm",
TargetListHandler: "chrome://remote/content/Handler.jsm",
});
XPCOMUtils.defineLazyGetter(this, "log", Log.get);
@ -194,72 +195,6 @@ class ParentRemoteAgent {
}
}
class Targets {
constructor() {
// browser context ID -> Target<XULElement>
this._targets = new Map();
}
/** @param BrowserElement browser */
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) {
await new Promise(resolve => {
const onInit = () => {
browser.messageManager.removeMessageListener("Browser:Init", onInit);
resolve();
};
browser.messageManager.addMessageListener("Browser:Init", onInit);
});
}
const target = new Target(browser);
target.connect();
this._targets.set(target.id, target);
}
/** @param BrowserElement browser */
disconnect(browser) {
// Ignore the browsers that haven't had time to initialize.
if (!browser.browsingContext) {
return;
}
let target = this._targets.get(browser.browsingContext.id);
if (target) {
target.disconnect();
this._targets.delete(target.id);
}
}
clear() {
for (const target of this) {
this.disconnect(target.browser);
}
}
get size() {
return this._targets.size;
}
* [Symbol.iterator]() {
for (const target of this._targets.values()) {
yield target;
}
}
toJSON() {
return [...this];
}
toString() {
return `[object Targets ${this.size}]`;
}
}
const RemoteAgentFactory = {
instance_: null,

75
remote/Targets.jsm Normal file
Просмотреть файл

@ -0,0 +1,75 @@
/* 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 {Target} = ChromeUtils.import("chrome://remote/content/Target.jsm");
class Targets {
constructor() {
// browser context ID -> Target<XULElement>
this._targets = new Map();
}
/** @param BrowserElement browser */
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) {
await new Promise(resolve => {
const onInit = () => {
browser.messageManager.removeMessageListener("Browser:Init", onInit);
resolve();
};
browser.messageManager.addMessageListener("Browser:Init", onInit);
});
}
const target = new Target(browser);
target.connect();
this._targets.set(target.id, target);
}
/** @param BrowserElement browser */
disconnect(browser) {
// Ignore the browsers that haven't had time to initialize.
if (!browser.browsingContext) {
return;
}
let target = this._targets.get(browser.browsingContext.id);
if (target) {
target.disconnect();
this._targets.delete(target.id);
}
}
clear() {
for (const target of this) {
this.disconnect(target.browser);
}
}
get size() {
return this._targets.size;
}
* [Symbol.iterator]() {
for (const target of this._targets.values()) {
yield target;
}
}
toJSON() {
return [...this];
}
toString() {
return `[object Targets ${this.size}]`;
}
}

Просмотреть файл

@ -19,6 +19,7 @@ remote.jar:
content/Sync.jsm (Sync.jsm)
content/Target.jsm (Target.jsm)
content/TargetListener.jsm (TargetListener.jsm)
content/Targets.jsm (Targets.jsm)
content/WindowManager.jsm (WindowManager.jsm)
# Frame scripts