From 01e6c6d4380abe60da99cca4d1aab2cce8cec6fd Mon Sep 17 00:00:00 2001 From: Andreas Tolfsen Date: Sun, 17 Feb 2019 16:57:01 +0000 Subject: [PATCH] bug 1523104: remote: move Targets class to separate module; r=ochameau --- remote/RemoteAgent.js | 67 +------------------------------------- remote/Targets.jsm | 75 +++++++++++++++++++++++++++++++++++++++++++ remote/jar.mn | 1 + 3 files changed, 77 insertions(+), 66 deletions(-) create mode 100644 remote/Targets.jsm diff --git a/remote/RemoteAgent.js b/remote/RemoteAgent.js index 9a3e9b57f888..f8c6ef4eeae8 100644 --- a/remote/RemoteAgent.js +++ b/remote/RemoteAgent.js @@ -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 - 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, diff --git a/remote/Targets.jsm b/remote/Targets.jsm new file mode 100644 index 000000000000..afb5722dce87 --- /dev/null +++ b/remote/Targets.jsm @@ -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 + 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}]`; + } +} diff --git a/remote/jar.mn b/remote/jar.mn index 6d43d3790f74..55506292adb0 100644 --- a/remote/jar.mn +++ b/remote/jar.mn @@ -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