Bug 1492700 - Rename adb.js to adb-process.js and switch to class;r=daisuke
authorJulian Descottes <jdescottes@mozilla.com>
Wed, 05 Dec 2018 20:51:58 +0000
changeset 508739 c804e90f0ce5daec220046b51a387a4edcd59294
parent 508738 271eb6988c586ce2f460768be1680ce904fc1e26
child 508740 738cae90db60f99317bc2418e36a99a5c703eef3
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [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 - Rename adb.js to adb-process.js and switch to class;r=daisuke Depends on D13475 Differential Revision: https://phabricator.services.mozilla.com/D13476
devtools/client/aboutdebugging-new/test/browser/browser_aboutdebugging_connect_toggle_usb_devices.js
devtools/client/aboutdebugging-new/test/browser/browser_aboutdebugging_sidebar_usb_status.js
devtools/client/aboutdebugging-new/test/browser/head.js
devtools/shared/adb/adb-process.js
devtools/shared/adb/adb-scanner.js
devtools/shared/adb/adb.js
devtools/shared/adb/commands/run-command.js
devtools/shared/adb/commands/track-devices.js
devtools/shared/adb/moz.build
devtools/shared/adb/test/test_adb.js
--- a/devtools/client/aboutdebugging-new/test/browser/browser_aboutdebugging_connect_toggle_usb_devices.js
+++ b/devtools/client/aboutdebugging-new/test/browser/browser_aboutdebugging_connect_toggle_usb_devices.js
@@ -1,15 +1,15 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
 const { AddonManager } = require("resource://gre/modules/AddonManager.jsm");
-const { ADB } = require("devtools/shared/adb/adb");
+const { adbProcess } = require("devtools/shared/adb/adb-process");
 
 /**
  * Check that USB Devices scanning can be enabled and disabled from the connect page.
  */
 add_task(async function() {
   await pushPref("devtools.remote.adb.extensionURL",
                  CHROME_URL_ROOT + "resources/test-adb-extension/adb-extension-#OS#.xpi");
 
@@ -43,17 +43,17 @@ add_task(async function() {
   Assert.deepEqual(addon.installTelemetryInfo, { source: "about:debugging" },
     "Got the expected addon.installTelemetryInfo");
 
   // Right now we are resuming as soon as "USB devices enabled" is displayed, but ADB
   // might still be starting up. If we move to uninstall directly, the ADB startup will
   // fail and we will have an unhandled promise rejection.
   // See Bug 1498469.
   info("Wait until ADB has started.");
-  await waitUntil(() => ADB.ready);
+  await waitUntil(() => adbProcess.ready);
 
   info("Click on the toggle button");
   usbToggleButton.click();
 
   info("Wait until the toggle button text is updated");
   await waitUntil(() => usbToggleButton.textContent.includes("Enable"));
   ok(document.querySelector(".js-connect-usb-disabled-message"),
     "The message about enabling USB devices is rendered again");
--- a/devtools/client/aboutdebugging-new/test/browser/browser_aboutdebugging_sidebar_usb_status.js
+++ b/devtools/client/aboutdebugging-new/test/browser/browser_aboutdebugging_sidebar_usb_status.js
@@ -1,15 +1,15 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
 const { adbAddon } = require("devtools/shared/adb/adb-addon");
-const { ADB } = require("devtools/shared/adb/adb");
+const { adbProcess } = require("devtools/shared/adb/adb-process");
 
 /**
  * This test asserts that the sidebar shows a message describing the status of the USB
  * devices scanning.
  */
 add_task(async function() {
   await pushPref("devtools.remote.adb.extensionURL",
                  CHROME_URL_ROOT + "resources/test-adb-extension/adb-extension-#OS#.xpi");
@@ -26,16 +26,16 @@ add_task(async function() {
   adbAddon.install("internal");
   await waitUntil(() => usbStatusElement.textContent.includes("USB devices enabled"));
 
   // Right now we are resuming as soon as "USB devices enabled" is displayed, but ADB
   // might still be starting up. If we move to uninstall directly, the ADB startup will
   // fail and we will have an unhandled promise rejection.
   // See Bug 1498469.
   info("Wait until ADB has started.");
-  await waitUntil(() => ADB.ready);
+  await waitUntil(() => adbProcess.ready);
 
   info("Uninstall the adb extension and wait for the message to udpate");
   adbAddon.uninstall();
   await waitUntil(() => usbStatusElement.textContent.includes("USB devices disabled"));
 
   await removeTab(tab);
 });
--- a/devtools/client/aboutdebugging-new/test/browser/head.js
+++ b/devtools/client/aboutdebugging-new/test/browser/head.js
@@ -25,18 +25,18 @@ Services.scriptloader.loadSubScript(
 // Make sure the ADB addon is removed and ADB is stopped when the test ends.
 registerCleanupFunction(async function() {
   try {
     const { adbAddon } = require("devtools/shared/adb/adb-addon");
     await adbAddon.uninstall();
   } catch (e) {
     // Will throw if the addon is already uninstalled, ignore exceptions here.
   }
-  const { ADB } = require("devtools/shared/adb/adb");
-  await ADB.kill();
+  const { adbProcess } = require("devtools/shared/adb/adb-process");
+  await adbProcess.kill();
 
   const { remoteClientManager } =
     require("devtools/client/shared/remote-debugging/remote-client-manager");
   await remoteClientManager.removeAllClients();
 });
 
 /**
  * Enable the new about:debugging panel.
rename from devtools/shared/adb/adb.js
rename to devtools/shared/adb/adb-process.js
--- a/devtools/shared/adb/adb.js
+++ b/devtools/shared/adb/adb-process.js
@@ -1,146 +1,137 @@
 /* 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/. */
 
-// Wrapper around the ADB utility.
-
 "use strict";
 
 const { Cc, Ci } = require("chrome");
 const { dumpn } = require("devtools/shared/DevToolsUtils");
 const { getFileForBinary } = require("./adb-binary");
 const { setTimeout } = require("resource://gre/modules/Timer.jsm");
 const { Services } = require("resource://gre/modules/Services.jsm");
-const { runCommand } = require("./commands/index");
-loader.lazyRequireGetter(this, "check",
-                         "devtools/shared/adb/adb-running-checker", true);
 
-let ready = false;
-let didRunInitially = false;
+loader.lazyRequireGetter(this, "runCommand", "devtools/shared/adb/commands/index", true);
+loader.lazyRequireGetter(this, "check", "devtools/shared/adb/adb-running-checker", true);
 
-const ADB = {
-  get didRunInitially() {
-    return didRunInitially;
-  },
-  set didRunInitially(newVal) {
-    didRunInitially = newVal;
-  },
+// Waits until a predicate returns true or re-tries the predicate calls
+// |retry| times, we wait for 100ms between each calls.
+async function waitUntil(predicate, retry = 20) {
+  let count = 0;
+  while (count++ < retry) {
+    if (await predicate()) {
+      return true;
+    }
+    // Wait for 100 milliseconds.
+    await new Promise(resolve => setTimeout(resolve, 100));
+  }
+  // Timed out after trying too many times.
+  return false;
+}
+
+// Class representing the ADB process.
+class AdbProcess {
+  constructor() {
+    this._ready = false;
+    this._didRunInitially = false;
+  }
 
   get ready() {
-    return ready;
-  },
-  set ready(newVal) {
-    ready = newVal;
-  },
+    return this._ready;
+  }
 
-  get adbFilePromise() {
+  _getAdbFile() {
     if (this._adbFilePromise) {
       return this._adbFilePromise;
     }
     this._adbFilePromise = getFileForBinary();
     return this._adbFilePromise;
-  },
+  }
 
   async _runProcess(process, params) {
     return new Promise((resolve, reject) => {
       process.runAsync(params, params.length, {
         observe(subject, topic, data) {
           switch (topic) {
             case "process-finished":
               resolve();
               break;
             case "process-failed":
               reject();
               break;
           }
         },
       }, false);
     });
-  },
-
-  // Waits until a predicate returns true or re-tries the predicate calls
-  // |retry| times, we wait for 100ms between each calls.
-  async _waitUntil(predicate, retry = 20) {
-    let count = 0;
-    while (count++ < retry) {
-      if (await predicate()) {
-        return true;
-      }
-      // Wait for 100 milliseconds.
-      await new Promise(resolve => setTimeout(resolve, 100));
-    }
-    // Timed out after trying too many times.
-    return false;
-  },
+  }
 
   // We startup by launching adb in server mode, and setting
   // the tcp socket preference to |true|
   async start() {
     return new Promise(async (resolve, reject) => {
       const onSuccessfulStart = () => {
         Services.obs.notifyObservers(null, "adb-ready");
-        this.ready = true;
+        this._ready = true;
         resolve();
       };
 
       const isAdbRunning = await check();
       if (isAdbRunning) {
         dumpn("Found ADB process running, not restarting");
         onSuccessfulStart();
         return;
       }
       dumpn("Didn't find ADB process running, restarting");
 
-      this.didRunInitially = true;
-      const process = Cc["@mozilla.org/process/util;1"]
-                      .createInstance(Ci.nsIProcess);
+      this._didRunInitially = true;
+      const process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
+
       // FIXME: Bug 1481691 - We should avoid extracting files every time.
-      const adbFile = await this.adbFilePromise;
+      const adbFile = await this._getAdbFile();
       process.init(adbFile);
       // Hide command prompt window on Windows
       process.startHidden = true;
       process.noShell = true;
       const params = ["start-server"];
       let isStarted = false;
       try {
         await this._runProcess(process, params);
-        isStarted = await this._waitUntil(check);
+        isStarted = await waitUntil(check);
       } catch (e) {
       }
 
       if (isStarted) {
         onSuccessfulStart();
       } else {
-        this.ready = false;
+        this._ready = false;
         reject();
       }
     });
-  },
+  }
 
   /**
    * Stop the ADB server, but only if we started it.  If it was started before
    * us, we return immediately.
    */
   async stop() {
-    if (!this.didRunInitially) {
+    if (!this._didRunInitially) {
       return; // We didn't start the server, nothing to do
     }
     await this.kill();
-  },
+  }
 
   /**
    * Kill the ADB server.
    */
   async kill() {
     try {
       await runCommand("host:kill");
     } catch (e) {
       dumpn("Failed to send host:kill command");
     }
     dumpn("adb server was terminated by host:kill");
-    this.ready = false;
-    this.didRunInitially = false;
-  },
-};
+    this._ready = false;
+    this._didRunInitially = false;
+  }
+}
 
-exports.ADB = ADB;
+exports.adbProcess = new AdbProcess();
--- a/devtools/shared/adb/adb-scanner.js
+++ b/devtools/shared/adb/adb-scanner.js
@@ -1,17 +1,17 @@
 /* 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 { ADB } = require("devtools/shared/adb/adb");
+const { adbProcess } = require("devtools/shared/adb/adb-process");
 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() {
@@ -26,17 +26,17 @@ class ADBScanner extends EventEmitter {
 
   enable() {
     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(() => {
+    adbProcess.start().then(() => {
       this._trackDevicesCommand.run();
     });
     this._updateRuntimes();
   }
 
   disable() {
     this._trackDevicesCommand.off("device-connected", this._onDeviceConnected);
     this._trackDevicesCommand.off("device-disconnected", this._onDeviceDisconnected);
--- a/devtools/shared/adb/commands/run-command.js
+++ b/devtools/shared/adb/commands/run-command.js
@@ -3,28 +3,28 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 // Wrapper around the ADB utility.
 
 "use strict";
 
 const { dumpn } = require("devtools/shared/DevToolsUtils");
 const { setTimeout } = require("resource://gre/modules/Timer.jsm");
-const { ADB } = require("../adb");
+const { adbProcess } = require("../adb-process");
 const client = require("../adb-client");
 
 const OKAY = 0x59414b4f;
 
 // Asynchronously runs an adb command.
 // @param command The command as documented in
 // http://androidxref.com/4.0.4/xref/system/core/adb/SERVICES.TXT
 const runCommand = function(command) {
   dumpn("runCommand " + command);
   return new Promise((resolve, reject) => {
-    if (!ADB.ready) {
+    if (!adbProcess.ready) {
       setTimeout(function() {
         reject("ADB_NOT_READY");
       });
       return;
     }
 
     const socket = client.connect();
 
--- a/devtools/shared/adb/commands/track-devices.js
+++ b/devtools/shared/adb/commands/track-devices.js
@@ -5,17 +5,17 @@
 // Wrapper around the ADB utility.
 
 "use strict";
 
 const EventEmitter = require("devtools/shared/event-emitter");
 const { dumpn } = require("devtools/shared/DevToolsUtils");
 const { setTimeout } = require("resource://gre/modules/Timer.jsm");
 
-const { ADB } = require("../adb");
+const { adbProcess } = require("../adb-process");
 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.
 class TrackDevicesCommand extends EventEmitter {
   run() {
@@ -61,19 +61,19 @@ class TrackDevicesCommand extends EventE
 
     // 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.
 
     // 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) {
+      if (adbProcess.ready) {
         // try to connect to the new local adb server or spawn a new one
-        ADB.start().then(() => {
+        adbProcess.start().then(() => {
           // Re-track devices
           this.run();
         });
       }
     }, 2000);
   }
 
   _onData(event) {
--- a/devtools/shared/adb/moz.build
+++ b/devtools/shared/adb/moz.build
@@ -7,20 +7,20 @@ DIRS += [
 ]
 
 DevToolsModules(
     'adb-addon.js',
     'adb-binary.js',
     'adb-client.js',
     'adb-device.js',
     'adb-devices-registry.js',
+    'adb-process.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')
 
 XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell.ini']
--- a/devtools/shared/adb/test/test_adb.js
+++ b/devtools/shared/adb/test/test_adb.js
@@ -3,17 +3,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 { adbProcess } = require("devtools/shared/adb/adb-process");
 const { TrackDevicesCommand } = require("devtools/shared/adb/commands/index");
 
 const ADB_JSON = {
   "Linux": {
     "x86": [
       "linux/adb",
     ],
     "x86_64": [
@@ -172,32 +172,32 @@ add_task({
       "win32/AdbWinApi.dll": "dummy",
       "win32/AdbWinUsbApi.dll": "dummy",
     },
   });
 
   await extension.startup();
 
   // Call start() once and call stop() afterwards.
-  await ADB.start();
-  ok(ADB.ready);
+  await adbProcess.start();
+  ok(adbProcess.ready);
   ok(await check(), "adb is now running");
 
-  await ADB.stop();
-  ok(!ADB.ready);
+  await adbProcess.stop();
+  ok(!adbProcess.ready);
   ok(!(await check()), "adb is no longer running");
 
   // Call start() twice and call stop() afterwards.
-  await ADB.start();
-  await ADB.start();
-  ok(ADB.ready);
+  await adbProcess.start();
+  await adbProcess.start();
+  ok(adbProcess.ready);
   ok(await check(), "adb is now running");
 
-  await ADB.stop();
-  ok(!ADB.ready);
+  await adbProcess.stop();
+  ok(!adbProcess.ready);
   ok(!(await check()), "adb is no longer running");
 
   await extension.unload();
 });
 
 add_task({
   skip_if: () => mozinfo.os == "win", // bug 1482008
 }, async function testTrackDevices() {
@@ -216,29 +216,29 @@ add_task({
       "win32/adb.exe": adbMock,
       "win32/AdbWinApi.dll": "dummy",
       "win32/AdbWinUsbApi.dll": "dummy",
     },
   });
 
   await extension.startup();
 
-  await ADB.start();
-  ok(ADB.ready);
+  await adbProcess.start();
+  ok(adbProcess.ready);
 
   ok(await check(), "adb is now running");
 
   const receivedDeviceId = await new Promise(resolve => {
     const trackDevicesCommand = new TrackDevicesCommand();
     trackDevicesCommand.on("device-connected", deviceId => {
       resolve(deviceId);
     });
     trackDevicesCommand.run();
   });
 
   equal(receivedDeviceId, "1234567890");
 
-  await ADB.stop();
-  ok(!ADB.ready);
+  await adbProcess.stop();
+  ok(!adbProcess.ready);
 
   await extension.unload();
 });