Skip to content

Instantly share code, notes, and snippets.

@juliandescottes
Created September 24, 2018 10:33
Show Gist options
  • Save juliandescottes/39a1f94e5ba50c639eaa37a9fe88cee1 to your computer and use it in GitHub Desktop.
Save juliandescottes/39a1f94e5ba50c639eaa37a9fe88cee1 to your computer and use it in GitHub Desktop.
WIP WIP WIP ADB EXTENSION HELPER
# HG changeset patch
# User Julian Descottes <[email protected]>
# Date 1537770943 -7200
# Mon Sep 24 08:35:43 2018 +0200
# Node ID e5d16a78efe5dc96a0540cc0d54db18ba01e455a
# Parent 3e0166df6ac292dd863b1bc62672b27a7a1f1dfa
WIP WIP WIP ADB EXTENSION HELPER
diff --git a/devtools/client/aboutdebugging-new/src/modules/usb-runtimes.js b/devtools/client/aboutdebugging-new/src/modules/usb-runtimes.js
--- a/devtools/client/aboutdebugging-new/src/modules/usb-runtimes.js
+++ b/devtools/client/aboutdebugging-new/src/modules/usb-runtimes.js
@@ -1,39 +1,31 @@
/* 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 { check } = require("devtools/shared/adb/adb-running-checker");
const { ADBScanner } = require("devtools/shared/adb/adb-scanner");
-const { GetAvailableAddons } = require("devtools/client/webide/modules/addons");
/**
* 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);
}
exports.addUSBRuntimesObserver = addUSBRuntimesObserver;
function disableUSBRuntimes() {
ADBScanner.disable();
}
exports.disableUSBRuntimes = disableUSBRuntimes;
async function enableUSBRuntimes() {
- const { adb } = GetAvailableAddons();
- if (adb.status === "uninstalled" || !(await check())) {
- console.error("ADB extension is not installed");
- return;
- }
-
ADBScanner.enable();
}
exports.enableUSBRuntimes = enableUSBRuntimes;
function getUSBRuntimes() {
return ADBScanner.listRuntimes();
}
exports.getUSBRuntimes = getUSBRuntimes;
diff --git a/devtools/shared/adb/adb-extension-helper.js b/devtools/shared/adb/adb-extension-helper.js
new file mode 100644
--- /dev/null
+++ b/devtools/shared/adb/adb-extension-helper.js
@@ -0,0 +1,91 @@
+/* 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 { Services } = require("resource://gre/modules/Services.jsm");
+const { AddonManager } = require("resource://gre/modules/AddonManager.jsm");
+loader.lazyGetter(this, "EXTENSION_ID", () => {
+ return Services.prefs.getCharPref("devtools.remote.adb.extensionID");
+});
+loader.lazyGetter(this, "EXTENSION_URL", () => {
+ return Services.prefs.getCharPref("devtools.remote.adb.extensionURL");
+});
+
+const ADBExtensionHelper = {
+ getExtensionUrl: function() {
+ const platform = Services.appShell.hiddenDOMWindow.navigator.platform;
+ let OS = "";
+ if (platform.includes("Win")) {
+ OS = "win32";
+ } else if (platform.includes("Mac")) {
+ OS = "mac64";
+ } else if (platform.includes("Linux")) {
+ if (platform.includes("x86_64")) {
+ OS = "linux64";
+ } else {
+ OS = "linux32";
+ }
+ }
+ const fixedOS = OS == "linux32" ? "linux" : OS;
+ return EXTENSION_URL.replace(/#OS#/g, fixedOS);
+ },
+
+ getExtension: function() {
+ return AddonManager.getAddonByID(EXTENSION_ID);
+ },
+
+ isInstalled: async function() {
+ const addon = await this.getExtension();
+ return !!addon;
+ },
+
+ isEnabled: async function() {
+ const addon = await this.getExtension();
+ return addon && !addon.userDisabled;
+ },
+
+ install: async function(source) {
+ const url = this.getExtensionUrl();
+ const install = await AddonManager.getInstallForURL(
+ url, "application/x-xpinstall", null, null, null, null, null, { source }
+ );
+ install.addListener(this);
+ install.install();
+ },
+
+ enable: async function() {
+ const addon = await this.getExtension();
+ if (addon && addon.userDisabled) {
+ await addon.enable();
+ }
+ },
+
+ uninstall: function() {
+
+ },
+
+ disable: async function() {
+ const addon = await this.getExtension();
+ if (addon && !addon.userDisabled) {
+ await addon.disable();
+ }
+ }
+};
+
+EventEmitter.decorate(ADBExtensionHelper);
+
+const addonsListener = {};
+addonsListener.onEnabled =
+addonsListener.onDisabled =
+addonsListener.onInstalled =
+addonsListener.onUninstalled = (updatedAddon) => {
+ if (updatedAddon.id === EXTENSION_ID) {
+ ADBExtensionHelper.emit("adb-extension-status-updated");
+ }
+};
+AddonManager.addAddonListener(addonsListener);
+
+exports.ADBExtensionHelper = ADBExtensionHelper;
diff --git a/devtools/shared/adb/adb-scanner.js b/devtools/shared/adb/adb-scanner.js
--- a/devtools/shared/adb/adb-scanner.js
+++ b/devtools/shared/adb/adb-scanner.js
@@ -8,22 +8,29 @@ const EventEmitter = require("devtools/s
const { ConnectionManager } =
require("devtools/shared/client/connection-manager");
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");
+loader.lazyRequireGetter(this, "ADBExtensionHelper", "devtools/shared/adb/adb-extension-helper");
const ADBScanner = {
_runtimes: [],
- enable() {
+ async enable() {
+ const isExtensionEnabled = await ADBExtensionHelper.isEnabled();
+ if ()
+ this._startListening();
+ },
+
+ _startListening() {
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);
diff --git a/devtools/shared/adb/moz.build b/devtools/shared/adb/moz.build
--- a/devtools/shared/adb/moz.build
+++ b/devtools/shared/adb/moz.build
@@ -1,16 +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/.
DevToolsModules(
'adb-binary.js',
'adb-client.js',
'adb-device.js',
+ 'adb-extension-helper.js',
'adb-running-checker.js',
'adb-scanner.js',
'adb-socket.js',
'adb.js',
)
with Files('**'):
BUG_COMPONENT = ('DevTools', 'about:debugging')
diff --git a/devtools/shared/preferences/devtools-shared.js b/devtools/shared/preferences/devtools-shared.js
--- a/devtools/shared/preferences/devtools-shared.js
+++ b/devtools/shared/preferences/devtools-shared.js
@@ -54,13 +54,16 @@ pref("devtools.dump.emit", false);
// Disable device discovery logging
pref("devtools.discovery.log", false);
// Whether to scan for DevTools devices via WiFi
pref("devtools.remote.wifi.scan", true);
// Client must complete TLS handshake within this window (ms)
pref("devtools.remote.tls-handshake-timeout", 10000);
-// The extension ID for devtools-adb-extension
+// The extension ID for devtools adb-extension
pref("devtools.remote.adb.extensionID", "[email protected]");
+// The extension URL for devtools adb-extension
+pref("devtools.remote.adb.extensionURL", "https://ftp.mozilla.org/pub/mozilla.org/labs/devtools/adb-extension/#OS#/adb-extension-latest-#OS#.xpi");
+
// URL of the remote JSON catalog used for device simulation
pref("devtools.devices.url", "https://code.cdn.mozilla.net/devices/devices.json");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment