Bug 1492700 - Extract AdbRuntime out of adb-scanner.js;r=daisuke
authorJulian Descottes <jdescottes@mozilla.com>
Tue, 27 Nov 2018 12:52:00 +0000
changeset 504697 21f0a93a65c24f34ad47fcf2204c84ac8fd9fba9
parent 504696 bbc5bcb72208a7ec7c5bf4a54a6977101e154993
child 504698 ad28397dfa43589ea5bd814391c26386c5bf751f
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdaisuke
bugs1492700
milestone65.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 1492700 - Extract AdbRuntime out of adb-scanner.js;r=daisuke Depends on D12765 Differential Revision: https://phabricator.services.mozilla.com/D12766
devtools/shared/adb/adb-runtime.js
devtools/shared/adb/adb-scanner.js
devtools/shared/adb/moz.build
copy from devtools/shared/adb/adb-scanner.js
copy to devtools/shared/adb/adb-runtime.js
--- a/devtools/shared/adb/adb-scanner.js
+++ b/devtools/shared/adb/adb-runtime.js
@@ -1,105 +1,16 @@
 /* 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 EventEmitter = require("devtools/shared/event-emitter");
-const { dumpn } = require("devtools/shared/DevToolsUtils");
-const { RuntimeTypes } =
-  require("devtools/client/webide/modules/runtime-types");
+const { RuntimeTypes } = require("devtools/client/webide/modules/runtime-types");
 const { ADB } = require("devtools/shared/adb/adb");
-const { adbDevicesRegistry } = require("devtools/shared/adb/adb-devices-registry");
-
-loader.lazyRequireGetter(this, "AdbDevice", "devtools/shared/adb/adb-device");
-
-class ADBScanner extends EventEmitter {
-  constructor() {
-    super();
-    this._runtimes = [];
-
-    this._onDeviceConnected = this._onDeviceConnected.bind(this);
-    this._onDeviceDisconnected = this._onDeviceDisconnected.bind(this);
-    this._updateRuntimes = this._updateRuntimes.bind(this);
-  }
-
-  enable() {
-    EventEmitter.on(ADB, "device-connected", this._onDeviceConnected);
-    EventEmitter.on(ADB, "device-disconnected", this._onDeviceDisconnected);
-
-    adbDevicesRegistry.on("register", this._updateRuntimes);
-    adbDevicesRegistry.on("unregister", this._updateRuntimes);
-
-    ADB.start().then(() => {
-      ADB.trackDevices();
-    });
-    this._updateRuntimes();
-  }
-
-  disable() {
-    EventEmitter.off(ADB, "device-connected", this._onDeviceConnected);
-    EventEmitter.off(ADB, "device-disconnected", this._onDeviceDisconnected);
-    adbDevicesRegistry.off("register", this._updateRuntimes);
-    adbDevicesRegistry.off("unregister", this._updateRuntimes);
-    this._updateRuntimes();
-  }
-
-  _emitUpdated() {
-    this.emit("runtime-list-updated");
-  }
-
-  _onDeviceConnected(deviceId) {
-    const device = new AdbDevice(deviceId);
-    adbDevicesRegistry.register(deviceId, device);
-  }
-
-  _onDeviceDisconnected(deviceId) {
-    adbDevicesRegistry.unregister(deviceId);
-  }
-
-  _updateRuntimes() {
-    if (this._updatingPromise) {
-      return this._updatingPromise;
-    }
-    this._runtimes = [];
-    const promises = [];
-    for (const id of adbDevicesRegistry.available()) {
-      const device = adbDevicesRegistry.getByName(id);
-      promises.push(this._detectRuntimes(device));
-    }
-    this._updatingPromise = Promise.all(promises);
-    this._updatingPromise.then(() => {
-      this._emitUpdated();
-      this._updatingPromise = null;
-    }, () => {
-      this._updatingPromise = null;
-    });
-    return this._updatingPromise;
-  }
-
-  async _detectRuntimes(adbDevice) {
-    const model = await adbDevice.getModel();
-    const socketPaths = await adbDevice.getRuntimeSocketPaths();
-    for (const socketPath of socketPaths) {
-      const runtime = new AdbRuntime(adbDevice, model, socketPath);
-      dumpn("Found " + runtime.name);
-      this._runtimes.push(runtime);
-    }
-  }
-
-  scan() {
-    return this._updateRuntimes();
-  }
-
-  listRuntimes() {
-    return this._runtimes;
-  }
-}
 
 function AdbRuntime(adbDevice, model, socketPath) {
   this._adbDevice = adbDevice;
   this._model = model;
   this._socketPath = socketPath;
 }
 
 AdbRuntime.prototype = {
@@ -159,9 +70,9 @@ Object.defineProperty(AdbRuntime.prototy
 
 Object.defineProperty(AdbRuntime.prototype, "name", {
   get() {
     const channel = this._channel();
     return "Firefox " + channel + " on Android (" + this.deviceName + ")";
   },
 });
 
-exports.ADBScanner = ADBScanner;
+exports.AdbRuntime = AdbRuntime;
--- a/devtools/shared/adb/adb-scanner.js
+++ b/devtools/shared/adb/adb-scanner.js
@@ -1,20 +1,19 @@
 /* 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 EventEmitter = require("devtools/shared/event-emitter");
 const { dumpn } = require("devtools/shared/DevToolsUtils");
-const { RuntimeTypes } =
-  require("devtools/client/webide/modules/runtime-types");
 const { ADB } = require("devtools/shared/adb/adb");
 const { adbDevicesRegistry } = require("devtools/shared/adb/adb-devices-registry");
+const { AdbRuntime } = require("devtools/shared/adb/adb-runtime");
 
 loader.lazyRequireGetter(this, "AdbDevice", "devtools/shared/adb/adb-device");
 
 class ADBScanner extends EventEmitter {
   constructor() {
     super();
     this._runtimes = [];
 
@@ -91,77 +90,9 @@ class ADBScanner extends EventEmitter {
     return this._updateRuntimes();
   }
 
   listRuntimes() {
     return this._runtimes;
   }
 }
 
-function AdbRuntime(adbDevice, model, socketPath) {
-  this._adbDevice = adbDevice;
-  this._model = model;
-  this._socketPath = socketPath;
-}
-
-AdbRuntime.prototype = {
-  type: RuntimeTypes.USB,
-  connect(connection) {
-    return ADB.prepareTCPConnection(this._socketPath).then(port => {
-      connection.host = "localhost";
-      connection.port = port;
-      connection.connect();
-    });
-  },
-  get id() {
-    return this._adbDevice.id + "|" + this._socketPath;
-  },
-};
-
-AdbRuntime.prototype._channel = function() {
-  const packageName = this._packageName();
-
-  switch (packageName) {
-    case "org.mozilla.firefox":
-      return "";
-    case "org.mozilla.firefox_beta":
-      return "Beta";
-    case "org.mozilla.fennec":
-    case "org.mozilla.fennec_aurora":
-      // This package name is now the one for Firefox Nightly distributed
-      // through the Google Play Store since "dawn project"
-      // cf. https://bugzilla.mozilla.org/show_bug.cgi?id=1357351#c8
-      return "Nightly";
-    default:
-      return "Custom";
-  }
-};
-
-AdbRuntime.prototype._packageName = function() {
-  // If using abstract socket address, it is "@org.mozilla.firefox/..."
-  // If using path base socket, it is "/data/data/<package>...""
-  // Until Fennec 62 only supports path based UNIX domain socket, but
-  // Fennec 63+ supports both path based and abstract socket.
-  return this._socketPath.startsWith("@") ?
-    this._socketPath.substr(1).split("/")[0] :
-    this._socketPath.split("/")[3];
-};
-
-Object.defineProperty(AdbRuntime.prototype, "shortName", {
-  get() {
-    return `Firefox ${this._channel()}`;
-  },
-});
-
-Object.defineProperty(AdbRuntime.prototype, "deviceName", {
-  get() {
-    return this._model || this._adbDevice.id;
-  },
-});
-
-Object.defineProperty(AdbRuntime.prototype, "name", {
-  get() {
-    const channel = this._channel();
-    return "Firefox " + channel + " on Android (" + this.deviceName + ")";
-  },
-});
-
 exports.ADBScanner = ADBScanner;
--- a/devtools/shared/adb/moz.build
+++ b/devtools/shared/adb/moz.build
@@ -4,16 +4,17 @@
 
 DevToolsModules(
     'adb-addon.js',
     'adb-binary.js',
     'adb-client.js',
     'adb-device.js',
     'adb-devices-registry.js',
     'adb-running-checker.js',
+    'adb-runtime.js',
     'adb-scanner.js',
     'adb-socket.js',
     'adb.js',
     'addon-aware-adb-scanner.js',
 )
 
 with Files('**'):
     BUG_COMPONENT = ('DevTools', 'about:debugging')