Skip to content

Instantly share code, notes, and snippets.

@juliandescottes
Created October 2, 2018 08:05
Show Gist options
  • Save juliandescottes/43b10989ead35e4cbbc30cd50d6a1434 to your computer and use it in GitHub Desktop.
Save juliandescottes/43b10989ead35e4cbbc30cd50d6a1434 to your computer and use it in GitHub Desktop.
static helper adb extension
# HG changeset patch
# User Julian Descottes <[email protected]>
# Date 1538465251 -7200
# Tue Oct 02 09:27:31 2018 +0200
# Node ID 4edce7cd93d3d1d2d3020d26427945b837a84dd7
# Parent ceeb1c7d62517f4ac960f1adaf75b2a506c1183a
add static helper WIP WIP WIP
diff --git a/devtools/client/webide/modules/addons.js b/devtools/client/webide/modules/addons.js
--- a/devtools/client/webide/modules/addons.js
+++ b/devtools/client/webide/modules/addons.js
@@ -1,39 +1,25 @@
/* 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 {
+ enableADBExtension,
+ isADBExtensionEnabled,
+ uninstallADBExtension,
+ uninstallOldADBExtension,
+} = require("devtools/shared/adb/adb-extension");
+
const {AddonManager} = require("resource://gre/modules/AddonManager.jsm");
const {Devices} = require("resource://devtools/shared/apps/Devices.jsm");
-const Services = require("Services");
const EventEmitter = require("devtools/shared/event-emitter");
-var ADB_LINK = Services.prefs.getCharPref("devtools.remote.adb.extensionURL");
-var ADB_ADDON_ID = Services.prefs.getCharPref("devtools.remote.adb.extensionID");
-
-// Extension ID for adb helper extension that might be installed on Firefox 63 or older.
-const OLD_ADB_ADDON_ID = "[email protected]";
-
-var platform = Services.appShell.hiddenDOMWindow.navigator.platform;
-var 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";
- }
-}
-
var addonsListener = {};
addonsListener.onEnabled =
addonsListener.onDisabled =
addonsListener.onInstalled =
addonsListener.onUninstalled = (updatedAddon) => {
getADBAddon().updateInstallStatus();
};
AddonManager.addAddonListener(addonsListener);
@@ -48,22 +34,18 @@ var getADBAddon = exports.getADBAddon =
exports.forgetADBAddon = function() {
adbAddon = null;
};
function ADBAddon() {
EventEmitter.decorate(this);
- // This addon uses the string "linux" for "linux32"
- const fixedOS = OS == "linux32" ? "linux" : OS;
- this.xpiLink = ADB_LINK.replace(/#OS#/g, fixedOS);
-
// Uninstall old version of the extension that might be installed on this profile.
- this.uninstallOldExtension();
+ uninstallOldADBExtension();
this.updateInstallStatus();
}
ADBAddon.prototype = {
_status: "unknown",
set status(value) {
Devices.adbExtensionInstalled = (value == "installed");
@@ -72,51 +54,37 @@ ADBAddon.prototype = {
this.emit("update");
}
},
get status() {
return this._status;
},
updateInstallStatus: async function() {
- const addon = await AddonManager.getAddonByID(ADB_ADDON_ID);
- if (addon && !addon.userDisabled) {
+ const isEnabled = await isADBExtensionEnabled();
+ if (isEnabled) {
this.status = "installed";
} else {
this.status = "uninstalled";
}
},
install: async function() {
- const addon = await AddonManager.getAddonByID(ADB_ADDON_ID);
- if (addon && !addon.userDisabled) {
- this.status = "installed";
- return;
+ const isEnabled = await isADBExtensionEnabled();
+
+ if (!isEnabled) {
+ this.status = "preparing";
+ await enableADBExtension("webide", this);
}
- this.status = "preparing";
- if (addon && addon.userDisabled) {
- await addon.enable();
- } else {
- const install = await AddonManager.getInstallForURL(this.xpiLink, "application/x-xpinstall", null,
- null, null, null, null, {source: "webide"});
- install.addListener(this);
- install.install();
- }
+
+ this.status = "installed";
},
uninstall: async function() {
- const addon = await AddonManager.getAddonByID(ADB_ADDON_ID);
- addon.uninstall();
- },
-
- uninstallOldExtension: async function() {
- const oldAddon = await AddonManager.getAddonByID(OLD_ADB_ADDON_ID);
- if (oldAddon) {
- oldAddon.uninstall();
- }
+ await uninstallADBExtension();
},
installFailureHandler: function(install, message) {
this.status = "uninstalled";
this.emit("failure", message);
},
onDownloadStarted: function() {
@@ -130,20 +98,16 @@ ADBAddon.prototype = {
onDownloadProgress: function(install) {
if (install.maxProgress == -1) {
this.emit("progress", -1);
} else {
this.emit("progress", install.progress / install.maxProgress);
}
},
- onInstallEnded: function({addon}) {
- addon.enable();
- },
-
onDownloadCancelled: function(install) {
this.installFailureHandler(install, "Download cancelled");
},
onDownloadFailed: function(install) {
this.installFailureHandler(install, "Download failed");
},
onInstallCancelled: function(install) {
this.installFailureHandler(install, "Install cancelled");
diff --git a/devtools/shared/adb/adb-extension.js b/devtools/shared/adb/adb-extension.js
new file mode 100644
--- /dev/null
+++ b/devtools/shared/adb/adb-extension.js
@@ -0,0 +1,83 @@
+/* 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 { AddonManager } = require("resource://gre/modules/AddonManager.jsm");
+const Services = require("Services");
+
+const ADB_LINK = Services.prefs.getCharPref("devtools.remote.adb.extensionURL");
+const ADB_ADDON_ID = Services.prefs.getCharPref("devtools.remote.adb.extensionID");
+
+// Extension ID for adb helper extension that might be installed on Firefox 63 or older.
+const OLD_ADB_ADDON_ID = "[email protected]";
+
+function _getADBExtensionXPILink() {
+ 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 = "linux";
+ }
+ }
+ return ADB_LINK.replace(/#OS#/g, OS);
+}
+
+async function _uninstallExtension(id) {
+ const addon = await AddonManager.getAddonByID(id);
+ if (addon) {
+ addon.uninstall();
+ }
+}
+
+async function getADBExtension() {
+ const addon = await AddonManager.getAddonByID(ADB_ADDON_ID);
+ return addon;
+}
+exports.getADBExtension = getADBExtension;
+
+async function installADBExtension(source, installListener) {
+ const xpiLink = _getADBExtensionXPILink();
+ const install = await AddonManager.getInstallForURL(
+ xpiLink,
+ "application/x-xpinstall",
+ null, null, null, null, null,
+ { source }
+ );
+ install.addListener(installListener);
+ await install.install();
+ return install.addon;
+}
+
+async function enableADBExtension(source, installListener) {
+ let addon = await getADBExtension();
+ if (!addon) {
+ addon = await installADBExtension(source, installListener);
+ }
+
+ await addon.enable();
+}
+exports.enableADBExtension = enableADBExtension;
+
+async function isADBExtensionEnabled() {
+ const addon = await getADBExtension();
+ return addon && !addon.userDisabled;
+}
+exports.isADBExtensionEnabled = isADBExtensionEnabled;
+
+async function uninstallADBExtension() {
+ await _uninstallExtension(ADB_ADDON_ID);
+}
+exports.uninstallADBExtension = uninstallADBExtension;
+
+async function uninstallOldADBExtension() {
+ await _uninstallExtension(OLD_ADB_ADDON_ID);
+}
+exports.uninstallOldADBExtension = uninstallOldADBExtension;
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.js',
'adb-running-checker.js',
'adb-scanner.js',
'adb-socket.js',
'adb.js',
)
with Files('**'):
BUG_COMPONENT = ('DevTools', 'about:debugging')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment