Bug 1492700 - Migrate adb/commands/track-devices to a Class;r=daisuke
authorJulian Descottes <jdescottes@mozilla.com>
Wed, 05 Dec 2018 20:49:24 +0000
changeset 449503 41384e20308d690d1bd65288a1b1af9da24851d9
parent 449502 9d8c5a3fc14e0df0dde204aaaa6c933df43ef57b
child 449504 271eb6988c586ce2f460768be1680ce904fc1e26
push id35169
push userdvarga@mozilla.com
push dateThu, 06 Dec 2018 21:40:09 +0000
treeherdermozilla-central@25282bb38b69 [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 - Migrate adb/commands/track-devices to a Class;r=daisuke Depends on D13473 Differential Revision: https://phabricator.services.mozilla.com/D13474
devtools/shared/adb/adb-scanner.js
devtools/shared/adb/commands/index.js
devtools/shared/adb/commands/track-devices.js
devtools/shared/adb/test/test_adb.js
--- a/devtools/shared/adb/adb-scanner.js
+++ b/devtools/shared/adb/adb-scanner.js
@@ -2,48 +2,49 @@
  * 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 { ADB } = require("devtools/shared/adb/adb");
-const { trackDevices } = require("devtools/shared/adb/commands/index");
+const { TrackDevicesCommand } = require("devtools/shared/adb/commands/index");
 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 = [];
+    this._trackDevicesCommand = new TrackDevicesCommand();
 
     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);
+    this._trackDevicesCommand.on("device-connected", this._onDeviceConnected);
+    this._trackDevicesCommand.on("device-disconnected", this._onDeviceDisconnected);
 
     adbDevicesRegistry.on("register", this._updateRuntimes);
     adbDevicesRegistry.on("unregister", this._updateRuntimes);
 
     ADB.start().then(() => {
-      trackDevices();
+      this._trackDevicesCommand.run();
     });
     this._updateRuntimes();
   }
 
   disable() {
-    EventEmitter.off(ADB, "device-connected", this._onDeviceConnected);
-    EventEmitter.off(ADB, "device-disconnected", this._onDeviceDisconnected);
+    this._trackDevicesCommand.off("device-connected", this._onDeviceConnected);
+    this._trackDevicesCommand.off("device-disconnected", this._onDeviceDisconnected);
     adbDevicesRegistry.off("register", this._updateRuntimes);
     adbDevicesRegistry.off("unregister", this._updateRuntimes);
     this._updateRuntimes();
   }
 
   _emitUpdated() {
     this.emit("runtime-list-updated");
   }
--- a/devtools/shared/adb/commands/index.js
+++ b/devtools/shared/adb/commands/index.js
@@ -3,17 +3,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 const { listDevices } = require("./list-devices");
 const { prepareTCPConnection } = require("./prepare-tcp-connection");
 const { runCommand } = require("./run-command");
 const { shell } = require("./shell");
-const { trackDevices } = require("./track-devices");
+const { TrackDevicesCommand } = require("./track-devices");
 
 module.exports = {
   listDevices,
   prepareTCPConnection,
   runCommand,
   shell,
-  trackDevices,
+  TrackDevicesCommand,
 };
--- a/devtools/shared/adb/commands/track-devices.js
+++ b/devtools/shared/adb/commands/track-devices.js
@@ -13,102 +13,122 @@ const { Services } = require("resource:/
 
 const { ADB } = require("../adb");
 const client = require("../adb-client");
 
 const OKAY = 0x59414b4f;
 
 // Start tracking devices connecting and disconnecting from the host.
 // We can't reuse runCommand here because we keep the socket alive.
-// @return The socket used.
-const trackDevices = function() {
-  dumpn("trackDevices");
-  const socket = client.connect();
-  let waitForFirst = true;
-  const devices = {};
+class TrackDevicesCommand extends EventEmitter {
+  run() {
+    this._waitForFirst = true;
+    this._devices = {};
+    this._socket = client.connect();
+
+    this._socket.s.onopen = this._onOpen.bind(this);
+    this._socket.s.onerror = this._onError.bind(this);
+    this._socket.s.onclose = this._onClose.bind(this);
+    this._socket.s.ondata = this._onData.bind(this);
+  }
 
-  socket.s.onopen = function() {
+  stop() {
+    if (this._socket) {
+      this._socket.close();
+
+      this._socket.s.onopen = null;
+      this._socket.s.onerror = null;
+      this._socket.s.onclose = null;
+      this._socket.s.ondata = null;
+    }
+  }
+
+  _onOpen() {
     dumpn("trackDevices onopen");
     Services.obs.notifyObservers(null, "adb-track-devices-start");
     const req = client.createRequest("host:track-devices");
-    socket.send(req);
-  };
+    this._socket.send(req);
+  }
 
-  socket.s.onerror = function(event) {
+  _onError(event) {
     dumpn("trackDevices onerror: " + event);
     Services.obs.notifyObservers(null, "adb-track-devices-stop");
-  };
+  }
 
-  socket.s.onclose = function() {
+  _onClose() {
     dumpn("trackDevices onclose");
 
     // Report all devices as disconnected
-    for (const dev in devices) {
-      devices[dev] = false;
-      EventEmitter.emit(ADB, "device-disconnected", dev);
+    for (const dev in this._devices) {
+      this._devices[dev] = false;
+      this.emit("device-disconnected", dev);
     }
 
     Services.obs.notifyObservers(null, "adb-track-devices-stop");
 
     // When we lose connection to the server,
     // and the adb is still on, we most likely got our server killed
     // by local adb. So we do try to reconnect to it.
-    setTimeout(function() { // Give some time to the new adb to start
-      if (ADB.ready) { // Only try to reconnect/restart if the add-on is still enabled
-        ADB.start().then(function() { // try to connect to the new local adb server
-                                       // or, spawn a new one
-          trackDevices(); // Re-track devices
+
+    // Give some time to the new adb to start
+    setTimeout(() => {
+      // Only try to reconnect/restart if the add-on is still enabled
+      if (ADB.ready) {
+        // try to connect to the new local adb server or spawn a new one
+        ADB.start().then(() => {
+          // Re-track devices
+          this.run();
         });
       }
     }, 2000);
-  };
+  }
 
-  socket.s.ondata = function(event) {
+  _onData(event) {
     dumpn("trackDevices ondata");
     const data = event.data;
     dumpn("length=" + data.byteLength);
     const dec = new TextDecoder();
     dumpn(dec.decode(new Uint8Array(data)).trim());
 
     // check the OKAY or FAIL on first packet.
-    if (waitForFirst) {
+    if (this._waitForFirst) {
       if (!client.checkResponse(data, OKAY)) {
-        socket.close();
+        this._socket.close();
         return;
       }
     }
 
-    const packet = client.unpackPacket(data, !waitForFirst);
-    waitForFirst = false;
+    const packet = client.unpackPacket(data, !this._waitForFirst);
+    this._waitForFirst = false;
 
     if (packet.data == "") {
       // All devices got disconnected.
-      for (const dev in devices) {
-        devices[dev] = false;
-        EventEmitter.emit(ADB, "device-disconnected", dev);
+      for (const dev in this._devices) {
+        this._devices[dev] = false;
+        this.emit("device-disconnected", dev);
       }
     } else {
       // One line per device, each line being $DEVICE\t(offline|device)
       const lines = packet.data.split("\n");
       const newDev = {};
       lines.forEach(function(line) {
         if (line.length == 0) {
           return;
         }
 
         const [dev, status] = line.split("\t");
         newDev[dev] = status !== "offline";
       });
       // Check which device changed state.
       for (const dev in newDev) {
-        if (devices[dev] != newDev[dev]) {
-          if (dev in devices || newDev[dev]) {
+        if (this._devices[dev] != newDev[dev]) {
+          if (dev in this._devices || newDev[dev]) {
             const topic = newDev[dev] ? "device-connected"
                                       : "device-disconnected";
-            EventEmitter.emit(ADB, topic, dev);
+            this.emit(topic, dev);
           }
-          devices[dev] = newDev[dev];
+          this._devices[dev] = newDev[dev];
         }
       }
     }
-  };
-};
-exports.trackDevices = trackDevices;
+  }
+}
+exports.TrackDevicesCommand = TrackDevicesCommand;
--- a/devtools/shared/adb/test/test_adb.js
+++ b/devtools/shared/adb/test/test_adb.js
@@ -4,17 +4,17 @@
 "use strict";
 
 const EventEmitter = require("devtools/shared/event-emitter");
 const { ExtensionTestUtils } = ChromeUtils.import("resource://testing-common/ExtensionXPCShellUtils.jsm", {});
 const { NetUtil } = require("resource://gre/modules/NetUtil.jsm");
 const { getFileForBinary } = require("devtools/shared/adb/adb-binary");
 const { check } = require("devtools/shared/adb/adb-running-checker");
 const { ADB } = require("devtools/shared/adb/adb");
-const { trackDevices } = require("devtools/shared/adb/commands/index");
+const { TrackDevicesCommand } = require("devtools/shared/adb/commands/index");
 
 const ADB_JSON = {
   "Linux": {
     "x86": [
       "linux/adb",
     ],
     "x86_64": [
       "linux64/adb",
@@ -222,20 +222,21 @@ add_task({
   await extension.startup();
 
   await ADB.start();
   ok(ADB.ready);
 
   ok(await check(), "adb is now running");
 
   const receivedDeviceId = await new Promise(resolve => {
-    EventEmitter.on(ADB, "device-connected", deviceId => {
+    const trackDevicesCommand = new TrackDevicesCommand();
+    trackDevicesCommand.on("device-connected", deviceId => {
       resolve(deviceId);
     });
-    trackDevices();
+    trackDevicesCommand.run();
   });
 
   equal(receivedDeviceId, "1234567890");
 
   await ADB.stop();
   ok(!ADB.ready);
 
   await extension.unload();