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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
|
/* 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 EXPORTED_SYMBOLS = [
"Capabilities",
"PageLoadStrategy",
"Proxy",
"Timeouts",
"UnhandledPromptBehavior",
];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(this, {
Preferences: "resource://gre/modules/Preferences.jsm",
assert: "chrome://marionette/content/assert.js",
error: "chrome://marionette/content/error.js",
Log: "chrome://marionette/content/log.js",
pprint: "chrome://marionette/content/format.js",
});
XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]);
XPCOMUtils.defineLazyGetter(this, "appinfo", () => {
// Enable testing this module, as Services.appinfo.* is not available
// in xpcshell tests.
const appinfo = { name: "<missing>", version: "<missing>" };
try {
appinfo.name = Services.appinfo.name.toLowerCase();
} catch (e) {}
try {
appinfo.version = Services.appinfo.version;
} catch (e) {}
return appinfo;
});
XPCOMUtils.defineLazyGetter(this, "logger", () => Log.get());
XPCOMUtils.defineLazyGetter(this, "remoteAgent", () => {
// The Remote Agent is currently not available on Android, and all
// release channels (bug 1606604),
try {
return Cc["@mozilla.org/remote/agent;1"].createInstance(Ci.nsIRemoteAgent);
} catch (e) {
logger.debug("Remote agent not available for this build and platform");
return null;
}
});
/** Representation of WebDriver session timeouts. */
class Timeouts {
constructor() {
// disabled
this.implicit = 0;
// five mintues
this.pageLoad = 300000;
// 30 seconds
this.script = 30000;
}
toString() {
return "[object Timeouts]";
}
/** Marshals timeout durations to a JSON Object. */
toJSON() {
return {
implicit: this.implicit,
pageLoad: this.pageLoad,
script: this.script,
};
}
static fromJSON(json) {
assert.object(
json,
pprint`Expected "timeouts" to be an object, got ${json}`
);
let t = new Timeouts();
for (let [type, ms] of Object.entries(json)) {
switch (type) {
case "implicit":
t.implicit = assert.positiveInteger(
ms,
pprint`Expected ${type} to be a positive integer, got ${ms}`
);
break;
case "script":
if (ms !== null) {
assert.positiveInteger(
ms,
pprint`Expected ${type} to be a positive integer, got ${ms}`
);
}
t.script = ms;
break;
case "pageLoad":
t.pageLoad = assert.positiveInteger(
ms,
pprint`Expected ${type} to be a positive integer, got ${ms}`
);
break;
default:
throw new error.InvalidArgumentError("Unrecognised timeout: " + type);
}
}
return t;
}
}
/**
* Enum of page loading strategies.
*
* @enum
*/
const PageLoadStrategy = {
/** No page load strategy. Navigation will return immediately. */
None: "none",
/**
* Eager, causing navigation to complete when the document reaches
* the <code>interactive</code> ready state.
*/
Eager: "eager",
/**
* Normal, causing navigation to return when the document reaches the
* <code>complete</code> ready state.
*/
Normal: "normal",
};
/** Proxy configuration object representation. */
class Proxy {
/** @class */
constructor() {
this.proxyType = null;
this.ftpProxy = null;
this.ftpProxyPort = null;
this.httpProxy = null;
this.httpProxyPort = null;
this.noProxy = null;
this.sslProxy = null;
this.sslProxyPort = null;
this.socksProxy = null;
this.socksProxyPort = null;
this.socksVersion = null;
this.proxyAutoconfigUrl = null;
}
/**
* Sets Firefox proxy settings.
*
* @return {boolean}
* True if proxy settings were updated as a result of calling this
* function, or false indicating that this function acted as
* a no-op.
*/
init() {
switch (this.proxyType) {
case "autodetect":
Preferences.set("network.proxy.type", 4);
return true;
case "direct":
Preferences.set("network.proxy.type", 0);
return true;
case "manual":
Preferences.set("network.proxy.type", 1);
if (this.ftpProxy) {
Preferences.set("network.proxy.ftp", this.ftpProxy);
if (Number.isInteger(this.ftpProxyPort)) {
Preferences.set("network.proxy.ftp_port", this.ftpProxyPort);
}
}
if (this.httpProxy) {
Preferences.set("network.proxy.http", this.httpProxy);
if (Number.isInteger(this.httpProxyPort)) {
Preferences.set("network.proxy.http_port", this.httpProxyPort);
}
}
if (this.sslProxy) {
Preferences.set("network.proxy.ssl", this.sslProxy);
if (Number.isInteger(this.sslProxyPort)) {
Preferences.set("network.proxy.ssl_port", this.sslProxyPort);
}
}
if (this.socksProxy) {
Preferences.set("network.proxy.socks", this.socksProxy);
if (Number.isInteger(this.socksProxyPort)) {
Preferences.set("network.proxy.socks_port", this.socksProxyPort);
}
if (this.socksVersion) {
Preferences.set("network.proxy.socks_version", this.socksVersion);
}
}
if (this.noProxy) {
Preferences.set(
"network.proxy.no_proxies_on",
this.noProxy.join(", ")
);
}
return true;
case "pac":
Preferences.set("network.proxy.type", 2);
Preferences.set(
"network.proxy.autoconfig_url",
this.proxyAutoconfigUrl
);
return true;
case "system":
Preferences.set("network.proxy.type", 5);
return true;
default:
return false;
}
}
/**
* @param {Object.<string, ?>} json
* JSON Object to unmarshal.
*
* @throws {InvalidArgumentError}
* When proxy configuration is invalid.
*/
static fromJSON(json) {
function stripBracketsFromIpv6Hostname(hostname) {
return hostname.includes(":")
? hostname.replace(/[\[\]]/g, "")
: hostname;
}
// Parse hostname and optional port from host
function fromHost(scheme, host) {
assert.string(
host,
pprint`Expected proxy "host" to be a string, got ${host}`
);
if (host.includes("://")) {
throw new error.InvalidArgumentError(`${host} contains a scheme`);
}
let url;
try {
// To parse the host a scheme has to be added temporarily.
// If the returned value for the port is an empty string it
// could mean no port or the default port for this scheme was
// specified. In such a case parse again with a different
// scheme to ensure we filter out the default port.
url = new URL("http://" + host);
if (url.port == "") {
url = new URL("https://" + host);
}
} catch (e) {
throw new error.InvalidArgumentError(e.message);
}
let hostname = stripBracketsFromIpv6Hostname(url.hostname);
// If the port hasn't been set, use the default port of
// the selected scheme (except for socks which doesn't have one).
let port = parseInt(url.port);
if (!Number.isInteger(port)) {
if (scheme === "socks") {
port = null;
} else {
port = Services.io.getProtocolHandler(scheme).defaultPort;
}
}
if (
url.username != "" ||
url.password != "" ||
url.pathname != "/" ||
url.search != "" ||
url.hash != ""
) {
throw new error.InvalidArgumentError(
`${host} was not of the form host[:port]`
);
}
return [hostname, port];
}
let p = new Proxy();
if (typeof json == "undefined" || json === null) {
return p;
}
assert.object(json, pprint`Expected "proxy" to be an object, got ${json}`);
assert.in(
"proxyType",
json,
pprint`Expected "proxyType" in "proxy" object, got ${json}`
);
p.proxyType = assert.string(
json.proxyType,
pprint`Expected "proxyType" to be a string, got ${json.proxyType}`
);
switch (p.proxyType) {
case "autodetect":
case "direct":
case "system":
break;
case "pac":
p.proxyAutoconfigUrl = assert.string(
json.proxyAutoconfigUrl,
`Expected "proxyAutoconfigUrl" to be a string, ` +
pprint`got ${json.proxyAutoconfigUrl}`
);
break;
case "manual":
if (typeof json.ftpProxy != "undefined") {
[p.ftpProxy, p.ftpProxyPort] = fromHost("ftp", json.ftpProxy);
}
if (typeof json.httpProxy != "undefined") {
[p.httpProxy, p.httpProxyPort] = fromHost("http", json.httpProxy);
}
if (typeof json.sslProxy != "undefined") {
[p.sslProxy, p.sslProxyPort] = fromHost("https", json.sslProxy);
}
if (typeof json.socksProxy != "undefined") {
[p.socksProxy, p.socksProxyPort] = fromHost("socks", json.socksProxy);
p.socksVersion = assert.positiveInteger(
json.socksVersion,
pprint`Expected "socksVersion" to be a positive integer, got ${json.socksVersion}`
);
}
if (typeof json.noProxy != "undefined") {
let entries = assert.array(
json.noProxy,
pprint`Expected "noProxy" to be an array, got ${json.noProxy}`
);
p.noProxy = entries.map(entry => {
assert.string(
entry,
pprint`Expected "noProxy" entry to be a string, got ${entry}`
);
return stripBracketsFromIpv6Hostname(entry);
});
}
break;
default:
throw new error.InvalidArgumentError(
`Invalid type of proxy: ${p.proxyType}`
);
}
return p;
}
/**
* @return {Object.<string, (number|string)>}
* JSON serialisation of proxy object.
*/
toJSON() {
function addBracketsToIpv6Hostname(hostname) {
return hostname.includes(":") ? `[${hostname}]` : hostname;
}
function toHost(hostname, port) {
if (!hostname) {
return null;
}
// Add brackets around IPv6 addresses
hostname = addBracketsToIpv6Hostname(hostname);
if (port != null) {
return `${hostname}:${port}`;
}
return hostname;
}
let excludes = this.noProxy;
if (excludes) {
excludes = excludes.map(addBracketsToIpv6Hostname);
}
return marshal({
proxyType: this.proxyType,
ftpProxy: toHost(this.ftpProxy, this.ftpProxyPort),
httpProxy: toHost(this.httpProxy, this.httpProxyPort),
noProxy: excludes,
sslProxy: toHost(this.sslProxy, this.sslProxyPort),
socksProxy: toHost(this.socksProxy, this.socksProxyPort),
socksVersion: this.socksVersion,
proxyAutoconfigUrl: this.proxyAutoconfigUrl,
});
}
toString() {
return "[object Proxy]";
}
}
/**
* Enum of unhandled prompt behavior.
*
* @enum
*/
const UnhandledPromptBehavior = {
/** All simple dialogs encountered should be accepted. */
Accept: "accept",
/**
* All simple dialogs encountered should be accepted, and an error
* returned that the dialog was handled.
*/
AcceptAndNotify: "accept and notify",
/** All simple dialogs encountered should be dismissed. */
Dismiss: "dismiss",
/**
* All simple dialogs encountered should be dismissed, and an error
* returned that the dialog was handled.
*/
DismissAndNotify: "dismiss and notify",
/** All simple dialogs encountered should be left to the user to handle. */
Ignore: "ignore",
};
/** WebDriver session capabilities representation. */
class Capabilities extends Map {
/** @class */
constructor() {
super([
// webdriver
["browserName", getWebDriverBrowserName()],
["browserVersion", appinfo.version],
["platformName", getWebDriverPlatformName()],
["platformVersion", Services.sysinfo.getProperty("version")],
["acceptInsecureCerts", false],
["pageLoadStrategy", PageLoadStrategy.Normal],
["proxy", new Proxy()],
["setWindowRect", !Services.androidBridge],
["timeouts", new Timeouts()],
["strictFileInteractability", false],
["unhandledPromptBehavior", UnhandledPromptBehavior.DismissAndNotify],
// features
["rotatable", appinfo.name == "B2G"],
// proprietary
["moz:accessibilityChecks", false],
["moz:buildID", Services.appinfo.appBuildID],
["moz:debuggerAddress", remoteAgent?.debuggerAddress || null],
[
"moz:headless",
Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo).isHeadless,
],
["moz:processID", Services.appinfo.processID],
["moz:profile", maybeProfile()],
[
"moz:shutdownTimeout",
Services.prefs.getIntPref("toolkit.asyncshutdown.crash_timeout"),
],
["moz:useNonSpecCompliantPointerOrigin", false],
["moz:webdriverClick", true],
]);
}
/**
* @param {string} key
* Capability key.
* @param {(string|number|boolean)} value
* JSON-safe capability value.
*/
set(key, value) {
if (key === "timeouts" && !(value instanceof Timeouts)) {
throw new TypeError();
} else if (key === "proxy" && !(value instanceof Proxy)) {
throw new TypeError();
}
return super.set(key, value);
}
toString() {
return "[object Capabilities]";
}
/**
* JSON serialisation of capabilities object.
*
* @return {Object.<string, ?>}
*/
toJSON() {
let marshalled = marshal(this);
marshalled.timeouts = super.get("timeouts");
return marshalled;
}
/**
* Unmarshal a JSON object representation of WebDriver capabilities.
*
* @param {Object.<string, *>=} json
* WebDriver capabilities.
*
* @return {Capabilities}
* Internal representation of WebDriver capabilities.
*/
static fromJSON(json) {
if (typeof json == "undefined" || json === null) {
json = {};
}
assert.object(
json,
pprint`Expected "capabilities" to be an object, got ${json}"`
);
return Capabilities.match_(json);
}
// Matches capabilities as described by WebDriver.
static match_(json = {}) {
let matched = new Capabilities();
for (let [k, v] of Object.entries(json)) {
switch (k) {
case "acceptInsecureCerts":
assert.boolean(v, pprint`Expected ${k} to be a boolean, got ${v}`);
break;
case "pageLoadStrategy":
assert.string(v, pprint`Expected ${k} to be a string, got ${v}`);
if (!Object.values(PageLoadStrategy).includes(v)) {
throw new error.InvalidArgumentError(
"Unknown page load strategy: " + v
);
}
break;
case "proxy":
v = Proxy.fromJSON(v);
break;
case "setWindowRect":
assert.boolean(v, pprint`Expected ${k} to be boolean, got ${v}`);
if (!Services.androidBridge && !v) {
throw new error.InvalidArgumentError(
"setWindowRect cannot be disabled"
);
} else if (Services.androidBridge && v) {
throw new error.InvalidArgumentError(
"setWindowRect is only supported on desktop"
);
}
break;
case "timeouts":
v = Timeouts.fromJSON(v);
break;
case "strictFileInteractability":
v = assert.boolean(v);
break;
case "unhandledPromptBehavior":
assert.string(v, pprint`Expected ${k} to be a string, got ${v}`);
if (!Object.values(UnhandledPromptBehavior).includes(v)) {
throw new error.InvalidArgumentError(
`Unknown unhandled prompt behavior: ${v}`
);
}
break;
case "moz:accessibilityChecks":
assert.boolean(v, pprint`Expected ${k} to be boolean, got ${v}`);
break;
case "moz:useNonSpecCompliantPointerOrigin":
assert.boolean(v, pprint`Expected ${k} to be boolean, got ${v}`);
break;
case "moz:webdriverClick":
assert.boolean(v, pprint`Expected ${k} to be boolean, got ${v}`);
break;
// Don't set the value because it's only used to return the address
// of the Remote Agent's debugger (HTTP server).
case "moz:debuggerAddress":
continue;
}
matched.set(k, v);
}
return matched;
}
}
this.Capabilities = Capabilities;
this.PageLoadStrategy = PageLoadStrategy;
this.Proxy = Proxy;
this.Timeouts = Timeouts;
this.UnhandledPromptBehavior = UnhandledPromptBehavior;
function getWebDriverBrowserName() {
// Similar to chromedriver which reports "chrome" as browser name for all
// WebView apps, we will report "firefox" for all GeckoView apps.
if (Services.androidBridge) {
return "firefox";
}
return appinfo.name;
}
function getWebDriverPlatformName() {
let name = Services.sysinfo.getProperty("name");
if (Services.androidBridge) {
return "android";
}
switch (name) {
case "Windows_NT":
return "windows";
case "Darwin":
return "mac";
default:
return name.toLowerCase();
}
}
// Specialisation of |JSON.stringify| that produces JSON-safe object
// literals, dropping empty objects and entries which values are undefined
// or null. Objects are allowed to produce their own JSON representations
// by implementing a |toJSON| function.
function marshal(obj) {
let rv = Object.create(null);
function* iter(mapOrObject) {
if (mapOrObject instanceof Map) {
for (const [k, v] of mapOrObject) {
yield [k, v];
}
} else {
for (const k of Object.keys(mapOrObject)) {
yield [k, mapOrObject[k]];
}
}
}
for (let [k, v] of iter(obj)) {
// Skip empty values when serialising to JSON.
if (typeof v == "undefined" || v === null) {
continue;
}
// Recursively marshal objects that are able to produce their own
// JSON representation.
if (typeof v.toJSON == "function") {
v = marshal(v.toJSON());
// Or do the same for object literals.
} else if (isObject(v)) {
v = marshal(v);
}
// And finally drop (possibly marshaled) objects which have no
// entries.
if (!isObjectEmpty(v)) {
rv[k] = v;
}
}
return rv;
}
function isObject(obj) {
return Object.prototype.toString.call(obj) == "[object Object]";
}
function isObjectEmpty(obj) {
return isObject(obj) && Object.keys(obj).length === 0;
}
// Services.dirsvc is not accessible from content frame scripts,
// but we should not panic about that.
function maybeProfile() {
try {
return Services.dirsvc.get("ProfD", Ci.nsIFile).path;
} catch (e) {
return "<protected>";
}
}
|