-
Notifications
You must be signed in to change notification settings - Fork 339
/
firefox-desktop.js
290 lines (252 loc) · 7.9 KB
/
firefox-desktop.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* @flow */
/**
* This module provide an ExtensionRunner subclass that manage an extension executed
* in a Firefox for Desktop instance.
*/
// Import flow types from npm dependencies.
import type FirefoxProfile from 'firefox-profile';
import {
MultiExtensionsReloadError,
RemoteTempInstallNotSupported,
WebExtError,
} from '../errors';
import * as defaultFirefoxApp from '../firefox';
import {
connectWithMaxRetries as defaultFirefoxConnector,
} from '../firefox/remote';
import {createLogger} from '../util/logger';
// Import flow types from project files.
import type {
FirefoxRDPResponseAddon,
RemoteFirefox,
} from '../firefox/remote';
import type {
ExtensionRunnerParams,
ExtensionRunnerReloadResult,
} from './base';
import type {FirefoxPreferences} from '../firefox/preferences';
import type {FirefoxInfo} from '../firefox/index'; // eslint-disable-line import/named
type FirefoxDesktopSpecificRunnerParams = {|
customPrefs?: FirefoxPreferences,
browserConsole: boolean,
firefoxBinary: string,
preInstall: boolean,
// Firefox desktop injected dependencies.
firefoxApp: typeof defaultFirefoxApp,
firefoxClient: typeof defaultFirefoxConnector,
|};
export type FirefoxDesktopExtensionRunnerParams = {|
...ExtensionRunnerParams,
// Firefox desktop CLI params.
...FirefoxDesktopSpecificRunnerParams,
|};
const log = createLogger(__filename);
/**
* Implements an IExtensionRunner which manages a Firefox Desktop instance.
*/
export class FirefoxDesktopExtensionRunner {
cleanupCallbacks: Set<Function>;
params: FirefoxDesktopExtensionRunnerParams;
profile: FirefoxProfile;
// Map extensions sourceDir to their related addon ids.
reloadableExtensions: Map<string, string>;
remoteFirefox: RemoteFirefox;
runningInfo: FirefoxInfo;
constructor(params: FirefoxDesktopExtensionRunnerParams) {
this.params = params;
this.reloadableExtensions = new Map();
this.cleanupCallbacks = new Set();
}
// Method exported from the IExtensionRunner interface.
/**
* Returns the runner name.
*/
getName(): string {
return 'Firefox Desktop';
}
/**
* Setup the Firefox Profile and run a Firefox Desktop instance.
*/
async run(): Promise<void> {
// Get a firefox profile with the custom Prefs set (a new or a cloned one).
// Pre-install extensions as proxy if needed (and disable auto-reload if you do)
await this.setupProfileDir();
// (if reload is enabled):
// - Connect to the firefox instance on RDP
// - Install any extension if needed (if not installed as proxy)
// - Keep track of the extension id assigned in a map with the sourceDir as a key
await this.startFirefoxInstance();
}
/**
* Reloads all the extensions, collect any reload error and resolves to
* an array composed by a single ExtensionRunnerReloadResult object.
*/
async reloadAllExtensions(): Promise<Array<ExtensionRunnerReloadResult>> {
const runnerName = this.getName();
const reloadErrors = new Map();
for (const {sourceDir} of this.params.extensions) {
const [res] = await this.reloadExtensionBySourceDir(sourceDir);
if (res.reloadError instanceof Error) {
reloadErrors.set(sourceDir, res.reloadError);
}
}
if (reloadErrors.size > 0) {
return [{
runnerName,
reloadError: new MultiExtensionsReloadError(reloadErrors),
}];
}
return [{runnerName}];
}
/**
* Reloads a single extension, collect any reload error and resolves to
* an array composed by a single ExtensionRunnerReloadResult object.
*/
async reloadExtensionBySourceDir(
extensionSourceDir: string
): Promise<Array<ExtensionRunnerReloadResult>> {
const runnerName = this.getName();
const addonId = this.reloadableExtensions.get(extensionSourceDir);
if (!addonId) {
return [{
sourceDir: extensionSourceDir,
reloadError: new WebExtError(
'Extension not reloadable: ' +
`no addonId has been mapped to "${extensionSourceDir}"`
),
runnerName,
}];
}
try {
await this.remoteFirefox.reloadAddon(addonId);
} catch (error) {
return [{
sourceDir: extensionSourceDir,
reloadError: error,
runnerName,
}];
}
return [{runnerName, sourceDir: extensionSourceDir}];
}
/**
* Register a callback to be called when the runner has been exited
* (e.g. the Firefox instance exits or the user has requested web-ext
* to exit).
*/
registerCleanup(fn: Function): void {
this.cleanupCallbacks.add(fn);
}
/**
* Exits the runner, by closing the managed Firefox instance.
*/
async exit(): Promise<void> {
if (!this.runningInfo || !this.runningInfo.firefox) {
throw new WebExtError('No firefox instance is currently running');
}
this.runningInfo.firefox.kill();
}
// Private helper methods.
async setupProfileDir() {
const {
customPrefs,
extensions,
keepProfileChanges,
preInstall,
profilePath,
firefoxApp,
} = this.params;
if (profilePath) {
if (keepProfileChanges) {
log.debug(`Using Firefox profile from ${profilePath}`);
this.profile = await firefoxApp.useProfile(profilePath, {customPrefs});
} else {
log.debug(`Copying Firefox profile from ${profilePath}`);
this.profile = await firefoxApp.copyProfile(profilePath, {customPrefs});
}
} else {
log.debug('Creating new Firefox profile');
this.profile = await firefoxApp.createProfile({customPrefs});
}
// preInstall the extensions if needed.
if (preInstall) {
for (const extension of extensions) {
await firefoxApp.installExtension({
asProxy: true,
extensionPath: extension.sourceDir,
manifestData: extension.manifestData,
profile: this.profile,
});
}
}
}
async startFirefoxInstance() {
const {
browserConsole,
extensions,
firefoxBinary,
preInstall,
startUrl,
firefoxApp,
firefoxClient,
args,
} = this.params;
const binaryArgs = [];
if (browserConsole) {
binaryArgs.push('-jsconsole');
}
if (startUrl) {
const urls = Array.isArray(startUrl) ? startUrl : [startUrl];
for (const url of urls) {
binaryArgs.push('--url', url);
}
}
if (args) {
binaryArgs.push(...args);
}
this.runningInfo = await firefoxApp.run(this.profile, {
firefoxBinary, binaryArgs,
});
this.runningInfo.firefox.on('close', () => {
for (const cleanupCb of this.cleanupCallbacks) {
try {
cleanupCb();
} catch (error) {
log.error(`Exception on executing cleanup callback: ${error}`);
}
}
});
if (!preInstall) {
const remoteFirefox = this.remoteFirefox = await firefoxClient({
port: this.runningInfo.debuggerPort,
});
// Install all the temporary addons.
for (const extension of extensions) {
try {
const addonId = await (
remoteFirefox.installTemporaryAddon(extension.sourceDir)
.then((installResult: FirefoxRDPResponseAddon) => {
return installResult.addon.id;
})
);
if (!addonId) {
throw new WebExtError(
'Unexpected missing addonId in the installAsTemporaryAddon result'
);
}
this.reloadableExtensions.set(extension.sourceDir, addonId);
} catch (error) {
if (error instanceof RemoteTempInstallNotSupported) {
log.debug(`Caught: ${error}`);
throw new WebExtError(
'Temporary add-on installation is not supported in this version' +
' of Firefox (you need Firefox 49 or higher). For older Firefox' +
' versions, use --pre-install'
);
} else {
throw error;
}
}
}
}
}
}