Bug 1493104 - Convert ADBScanner from a singleton to a class;r=ladybenko,daisuke
authorJulian Descottes <jdescottes@mozilla.com>
Fri, 05 Oct 2018 11:17:22 +0200
changeset 498959 6d25a11674ba1e78587b08c570995e7980c83648
parent 498958 857fb59a720e8f04e5d4f6ba60d4c9428f459751
child 498960 a56d01a749647c9bb733ee790bc2111e61a7591d
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersladybenko, daisuke
bugs1493104
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1493104 - Convert ADBScanner from a singleton to a class;r=ladybenko,daisuke
devtools/client/aboutdebugging-new/src/modules/usb-runtimes.js
devtools/client/webide/content/addons.js
devtools/shared/adb/adb-scanner.js
--- a/devtools/client/aboutdebugging-new/src/modules/usb-runtimes.js
+++ b/devtools/client/aboutdebugging-new/src/modules/usb-runtimes.js
@@ -1,43 +1,46 @@
 /* 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 { ADBScanner } = require("devtools/shared/adb/adb-scanner");
 loader.lazyRequireGetter(this, "adbAddon", "devtools/shared/adb/adb-addon", true);
 loader.lazyRequireGetter(this, "ADB_ADDON_STATES", "devtools/shared/adb/adb-addon", true);
+loader.lazyGetter(this, "adbScanner", () => {
+  const { ADBScanner } = require("devtools/shared/adb/adb-scanner");
+  return new ADBScanner();
+});
 
 /**
  * This module provides a collection of helper methods to detect USB runtimes whom Firefox
  * is running on.
  */
 function addUSBRuntimesObserver(listener) {
-  ADBScanner.on("runtime-list-updated", listener);
+  adbScanner.on("runtime-list-updated", listener);
 }
 exports.addUSBRuntimesObserver = addUSBRuntimesObserver;
 
 function disableUSBRuntimes() {
-  ADBScanner.disable();
+  adbScanner.disable();
 }
 exports.disableUSBRuntimes = disableUSBRuntimes;
 
 async function enableUSBRuntimes() {
   if (adbAddon.status !== ADB_ADDON_STATES.INSTALLED) {
     console.error("ADB extension is not installed");
     return;
   }
 
-  ADBScanner.enable();
+  adbScanner.enable();
 }
 exports.enableUSBRuntimes = enableUSBRuntimes;
 
 function getUSBRuntimes() {
-  return ADBScanner.listRuntimes();
+  return adbScanner.listRuntimes();
 }
 exports.getUSBRuntimes = getUSBRuntimes;
 
 function removeUSBRuntimesObserver(listener) {
-  ADBScanner.off("runtime-list-updated", listener);
+  adbScanner.off("runtime-list-updated", listener);
 }
 exports.removeUSBRuntimesObserver = removeUSBRuntimesObserver;
--- a/devtools/client/webide/content/addons.js
+++ b/devtools/client/webide/content/addons.js
@@ -3,18 +3,21 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 const {loader, require} = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {});
 
 const Services = require("Services");
 const Strings = Services.strings.createBundle("chrome://devtools/locale/webide.properties");
 
 const {gDevTools} = require("devtools/client/framework/devtools");
-const {ADBScanner} = require("devtools/shared/adb/adb-scanner");
 const {RuntimeScanners} = require("devtools/client/webide/modules/runtimes");
+loader.lazyGetter(this, "adbScanner", () => {
+  const { ADBScanner } = require("devtools/shared/adb/adb-scanner");
+  return new ADBScanner();
+});
 
 loader.lazyRequireGetter(this, "adbAddon", "devtools/shared/adb/adb-addon", true);
 loader.lazyRequireGetter(this, "ADB_ADDON_STATES", "devtools/shared/adb/adb-addon", true);
 
 window.addEventListener("load", function() {
   document.querySelector("#aboutaddons").onclick = function() {
     const browserWin = Services.wm.getMostRecentWindow(gDevTools.chromeWindowType);
     if (browserWin && browserWin.BrowserOpenAddonsMgr) {
@@ -30,19 +33,19 @@ function CloseUI() {
 }
 
 function BuildUI() {
   function onAddonUpdate(arg) {
     progress.removeAttribute("value");
     li.setAttribute("status", adbAddon.status);
     status.textContent = Strings.GetStringFromName("addons_status_" + adbAddon.status);
     if (adbAddon.status == ADB_ADDON_STATES.INSTALLED) {
-      RuntimeScanners.add(ADBScanner);
+      RuntimeScanners.add(adbScanner);
     } else if (adbAddon.status == ADB_ADDON_STATES.UNINSTALLED) {
-      RuntimeScanners.remove(ADBScanner);
+      RuntimeScanners.remove(adbScanner);
     }
   }
 
   function onAddonFailure(arg) {
     window.parent.UI.reportError("error_operationFail", arg);
   }
 
   function onAddonProgress(arg) {
--- a/devtools/shared/adb/adb-scanner.js
+++ b/devtools/shared/adb/adb-scanner.js
@@ -7,57 +7,60 @@
 const EventEmitter = require("devtools/shared/event-emitter");
 const { Devices } = require("devtools/shared/apps/Devices.jsm");
 const { dumpn } = require("devtools/shared/DevToolsUtils");
 const { RuntimeTypes } =
   require("devtools/client/webide/modules/runtime-types");
 const { ADB } = require("devtools/shared/adb/adb");
 loader.lazyRequireGetter(this, "Device", "devtools/shared/adb/adb-device");
 
-const ADBScanner = {
+class ADBScanner extends EventEmitter {
+  constructor() {
+    super();
+    this._runtimes = [];
 
-  _runtimes: [],
+    this._onDeviceConnected = this._onDeviceConnected.bind(this);
+    this._onDeviceDisconnected = this._onDeviceDisconnected.bind(this);
+    this._updateRuntimes = this._updateRuntimes.bind(this);
+  }
 
   enable() {
-    this._onDeviceConnected = this._onDeviceConnected.bind(this);
-    this._onDeviceDisconnected = this._onDeviceDisconnected.bind(this);
     EventEmitter.on(ADB, "device-connected", this._onDeviceConnected);
     EventEmitter.on(ADB, "device-disconnected", this._onDeviceDisconnected);
 
-    this._updateRuntimes = this._updateRuntimes.bind(this);
     Devices.on("register", this._updateRuntimes);
     Devices.on("unregister", this._updateRuntimes);
     Devices.on("addon-status-updated", this._updateRuntimes);
 
     ADB.start().then(() => {
       ADB.trackDevices();
     });
     this._updateRuntimes();
-  },
+  }
 
   disable() {
     EventEmitter.off(ADB, "device-connected", this._onDeviceConnected);
     EventEmitter.off(ADB, "device-disconnected", this._onDeviceDisconnected);
     Devices.off("register", this._updateRuntimes);
     Devices.off("unregister", this._updateRuntimes);
     Devices.off("addon-status-updated", this._updateRuntimes);
-  },
+  }
 
   _emitUpdated() {
     this.emit("runtime-list-updated");
-  },
+  }
 
   _onDeviceConnected(deviceId) {
     const device = new Device(deviceId);
     Devices.register(deviceId, device);
-  },
+  }
 
   _onDeviceDisconnected(deviceId) {
     Devices.unregister(deviceId);
-  },
+  }
 
   _updateRuntimes() {
     if (this._updatingPromise) {
       return this._updatingPromise;
     }
     this._runtimes = [];
     const promises = [];
     for (const id of Devices.available()) {
@@ -67,36 +70,33 @@ const ADBScanner = {
     this._updatingPromise = Promise.all(promises);
     this._updatingPromise.then(() => {
       this._emitUpdated();
       this._updatingPromise = null;
     }, () => {
       this._updatingPromise = null;
     });
     return this._updatingPromise;
-  },
+  }
 
-  _detectRuntimes: async function(device) {
+  async _detectRuntimes(device) {
     const model = await device.getModel();
     const detectedRuntimes =
       await FirefoxOnAndroidRuntime.detect(device, model);
     this._runtimes.push(...detectedRuntimes);
-  },
+  }
 
   scan() {
     return this._updateRuntimes();
-  },
+  }
 
   listRuntimes() {
     return this._runtimes;
   }
-
-};
-
-EventEmitter.decorate(ADBScanner);
+}
 
 function Runtime(device, model, socketPath) {
   this.device = device;
   this._model = model;
   this._socketPath = socketPath;
 }
 
 Runtime.prototype = {