1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/* 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 } = ChromeUtils.importESModule(
"resource://gre/modules/AddonManager.sys.mjs",
// AddonManager is a singleton, never create two instances of it.
{ global: "shared" }
);
const EventEmitter = require("resource://devtools/shared/event-emitter.js");
const PREF_ADB_EXTENSION_URL = "devtools.remote.adb.extensionURL";
const PREF_ADB_EXTENSION_ID = "devtools.remote.adb.extensionID";
const ADB_ADDON_STATES = {
DOWNLOADING: "downloading",
INSTALLED: "installed",
INSTALLING: "installing",
PREPARING: "preparing",
UNINSTALLED: "uninstalled",
UNKNOWN: "unknown",
};
exports.ADB_ADDON_STATES = ADB_ADDON_STATES;
/**
* Wrapper around the ADB Extension providing ADB binaries for devtools remote debugging.
* Fires the following events:
* - "update": the status of the addon was updated
* - "failure": addon installation failed
* - "progress": addon download in progress
*
* AdbAddon::state can take any of the values from ADB_ADDON_STATES.
*/
class ADBAddon extends EventEmitter {
constructor() {
super();
this._status = ADB_ADDON_STATES.UNKNOWN;
const addonsListener = {};
addonsListener.onEnabled =
addonsListener.onDisabled =
addonsListener.onInstalled =
addonsListener.onUninstalled =
() => this.updateInstallStatus();
AddonManager.addAddonListener(addonsListener);
this.updateInstallStatus();
}
set status(value) {
if (this._status != value) {
this._status = value;
this.emit("update");
}
}
get status() {
return this._status;
}
async _getAddon() {
const addonId = Services.prefs.getCharPref(PREF_ADB_EXTENSION_ID);
return AddonManager.getAddonByID(addonId);
}
async updateInstallStatus() {
const addon = await this._getAddon();
if (addon && !addon.userDisabled) {
this.status = ADB_ADDON_STATES.INSTALLED;
} else {
this.status = ADB_ADDON_STATES.UNINSTALLED;
}
}
/**
* Returns the platform specific download link for the ADB extension.
*/
_getXpiLink() {
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";
}
}
const xpiLink = Services.prefs.getCharPref(PREF_ADB_EXTENSION_URL);
return xpiLink.replace(/#OS#/g, OS);
}
/**
* Install and enable the adb extension. Returns a promise that resolves when ADB is
* enabled.
*
* @param {String} source
* String passed to the AddonManager for telemetry.
*/
async install(source) {
if (!source) {
throw new Error(
"Missing mandatory `source` parameter for adb-addon.install"
);
}
const addon = await this._getAddon();
if (addon && !addon.userDisabled) {
this.status = ADB_ADDON_STATES.INSTALLED;
return;
}
this.status = ADB_ADDON_STATES.PREPARING;
if (addon?.userDisabled) {
await addon.enable();
} else {
const install = await AddonManager.getInstallForURL(this._getXpiLink(), {
telemetryInfo: { source },
});
install.addListener(this);
install.install();
}
}
async uninstall() {
const addon = await this._getAddon();
addon.uninstall();
}
installFailureHandler(install, message) {
this.status = ADB_ADDON_STATES.UNINSTALLED;
this.emit("failure", message);
}
// Expected AddonManager install listener.
onDownloadStarted() {
this.status = ADB_ADDON_STATES.DOWNLOADING;
}
// Expected AddonManager install listener.
onDownloadProgress(install) {
if (install.maxProgress == -1) {
this.emit("progress", -1);
} else {
this.emit("progress", install.progress / install.maxProgress);
}
}
// Expected AddonManager install listener.
onDownloadCancelled(install) {
this.installFailureHandler(install, "Download cancelled");
}
// Expected AddonManager install listener.
onDownloadFailed(install) {
this.installFailureHandler(install, "Download failed");
}
// Expected AddonManager install listener.
onInstallStarted() {
this.status = ADB_ADDON_STATES.INSTALLING;
}
// Expected AddonManager install listener.
onInstallCancelled(install) {
this.installFailureHandler(install, "Install cancelled");
}
// Expected AddonManager install listener.
onInstallFailed(install) {
this.installFailureHandler(install, "Install failed");
}
// Expected AddonManager install listener.
onInstallEnded({ addon }) {
addon.enable();
}
}
exports.adbAddon = new ADBAddon();
|