Commit 60524744 authored by Julian Descottes's avatar Julian Descottes
Browse files

Bug 1493104 - Convert ADBScanner from a singleton to a class;r=ladybenko,daisuke

This allows to cleanly disable the scanner when you have several users.
Without this if you start both WebIDE and about:debugging, the first listeners
set in enable() can never be removed.

Differential Revision: https://phabricator.services.mozilla.com/D7872

--HG--
extra : moz-landing-system : lando
parent 0a8491c6
......@@ -4,21 +4,24 @@
"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;
......@@ -28,16 +31,16 @@ async function enableUSBRuntimes() {
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;
......@@ -8,8 +8,11 @@ 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);
......@@ -35,9 +38,9 @@ function BuildUI() {
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);
}
}
......
......@@ -12,17 +12,20 @@ const { RuntimeTypes } =
const { ADB } = require("devtools/shared/adb/adb");
loader.lazyRequireGetter(this, "Device", "devtools/shared/adb/adb-device");
const ADBScanner = {
_runtimes: [],
class ADBScanner extends EventEmitter {
constructor() {
super();
this._runtimes = [];
enable() {
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._updateRuntimes = this._updateRuntimes.bind(this);
Devices.on("register", this._updateRuntimes);
Devices.on("unregister", this._updateRuntimes);
Devices.on("addon-status-updated", this._updateRuntimes);
......@@ -31,7 +34,7 @@ const ADBScanner = {
ADB.trackDevices();
});
this._updateRuntimes();
},
}
disable() {
EventEmitter.off(ADB, "device-connected", this._onDeviceConnected);
......@@ -39,20 +42,20 @@ const ADBScanner = {
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) {
......@@ -72,26 +75,23 @@ const ADBScanner = {
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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment