forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.js
670 lines (583 loc) · 17.5 KB
/
browser.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
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
/* 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";
/* global frame */
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const { WebElementEventTarget } = ChromeUtils.import(
"chrome://marionette/content/dom.js"
);
const { element } = ChromeUtils.import(
"chrome://marionette/content/element.js"
);
const { NoSuchWindowError, UnsupportedOperationError } = ChromeUtils.import(
"chrome://marionette/content/error.js"
);
const { Log } = ChromeUtils.import("chrome://marionette/content/log.js");
const {
MessageManagerDestroyedPromise,
waitForEvent,
waitForObserverTopic,
} = ChromeUtils.import("chrome://marionette/content/sync.js");
XPCOMUtils.defineLazyGetter(this, "logger", Log.get);
this.EXPORTED_SYMBOLS = ["browser", "Context", "WindowState"];
/** @namespace */
this.browser = {};
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
/**
* Variations of Marionette contexts.
*
* Choosing a context through the <tt>Marionette:SetContext</tt>
* command directs all subsequent browsing context scoped commands
* to that context.
*/
class Context {
/**
* Gets the correct context from a string.
*
* @param {string} s
* Context string serialisation.
*
* @return {Context}
* Context.
*
* @throws {TypeError}
* If <var>s</var> is not a context.
*/
static fromString(s) {
switch (s) {
case "chrome":
return Context.Chrome;
case "content":
return Context.Content;
default:
throw new TypeError(`Unknown context: ${s}`);
}
}
}
Context.Chrome = "chrome";
Context.Content = "content";
this.Context = Context;
/**
* Get the <code><xul:browser></code> for the specified tab.
*
* @param {Tab} tab
* The tab whose browser needs to be returned.
*
* @return {Browser}
* The linked browser for the tab or null if no browser can be found.
*/
browser.getBrowserForTab = function(tab) {
// Fennec
if (tab && "browser" in tab) {
return tab.browser;
// Firefox
} else if (tab && "linkedBrowser" in tab) {
return tab.linkedBrowser;
}
return null;
};
/**
* Return the tab browser for the specified chrome window.
*
* @param {ChromeWindow} win
* Window whose <code>tabbrowser</code> needs to be accessed.
*
* @return {Tab}
* Tab browser or null if it's not a browser window.
*/
browser.getTabBrowser = function(window) {
// Fennec
if ("BrowserApp" in window) {
return window.BrowserApp;
// Firefox
} else if ("gBrowser" in window) {
return window.gBrowser;
// Thunderbird
} else if (window.document.getElementById("tabmail")) {
return window.document.getElementById("tabmail");
}
return null;
};
/**
* Creates a browsing context wrapper.
*
* Browsing contexts handle interactions with the browser, according to
* the current environment (Firefox, Fennec).
*/
browser.Context = class {
/**
* @param {ChromeWindow} win
* ChromeWindow that contains the top-level browsing context.
* @param {GeckoDriver} driver
* Reference to driver instance.
*/
constructor(window, driver) {
this.window = window;
this.driver = driver;
// In Firefox this is <xul:tabbrowser> (not <xul:browser>!)
// and BrowserApp in Fennec
this.tabBrowser = browser.getTabBrowser(this.window);
this.knownFrames = [];
// Used to set curFrameId upon new session
this.newSession = true;
this.seenEls = new element.Store();
// A reference to the tab corresponding to the current window handle,
// if any. Specifically, this.tab refers to the last tab that Marionette
// switched to in this browser window. Note that this may not equal the
// currently selected tab. For example, if Marionette switches to tab
// A, and then clicks on a button that opens a new tab B in the same
// browser window, this.tab will still point to tab A, despite tab B
// being the currently selected tab.
this.tab = null;
// Commands which trigger a navigation can cause the frame script to be
// moved to a different process. To not loose the currently active
// command, or any other already pushed following command, store them as
// long as they haven't been fully processed. The commands get flushed
// after a new browser has been registered.
this.pendingCommands = [];
this._needsFlushPendingCommands = false;
this.frameRegsPending = 0;
this.getIdForBrowser = driver.getIdForBrowser.bind(driver);
this.updateIdForBrowser = driver.updateIdForBrowser.bind(driver);
}
/**
* Returns the content browser for the currently selected tab.
* If there is no tab selected, null will be returned.
*/
get contentBrowser() {
if (this.tab) {
return browser.getBrowserForTab(this.tab);
} else if (
this.tabBrowser &&
this.driver.isReftestBrowser(this.tabBrowser)
) {
return this.tabBrowser;
}
return null;
}
get messageManager() {
if (this.contentBrowser) {
return this.contentBrowser.messageManager;
}
return null;
}
/**
* Checks if the browsing context has been discarded.
*
* The browsing context will have been discarded if the content
* browser, represented by the <code><xul:browser></code>,
* has been detached.
*
* @return {boolean}
* True if browsing context has been discarded, false otherwise.
*/
get closed() {
return this.contentBrowser === null;
}
/**
* The current frame ID is managed per browser element on desktop in
* case the ID needs to be refreshed. The currently selected window is
* identified by a tab.
*/
get curFrameId() {
let rv = null;
if (this.tab || this.driver.isReftestBrowser(this.contentBrowser)) {
rv = this.getIdForBrowser(this.contentBrowser);
}
return rv;
}
/**
* Returns the current title of the content browser.
*
* @return {string}
* Read-only property containing the current title.
*
* @throws {NoSuchWindowError}
* If the current ChromeWindow does not have a content browser.
*/
get currentTitle() {
// Bug 1363368 - contentBrowser could be null until we wait for its
// initialization been finished
if (this.contentBrowser) {
return this.contentBrowser.contentTitle;
}
throw new NoSuchWindowError(
"Current window does not have a content browser"
);
}
/**
* Returns the current URI of the content browser.
*
* @return {nsIURI}
* Read-only property containing the currently loaded URL.
*
* @throws {NoSuchWindowError}
* If the current ChromeWindow does not have a content browser.
*/
get currentURI() {
// Bug 1363368 - contentBrowser could be null until we wait for its
// initialization been finished
if (this.contentBrowser) {
return this.contentBrowser.currentURI;
}
throw new NoSuchWindowError(
"Current window does not have a content browser"
);
}
/**
* Gets the position and dimensions of the top-level browsing context.
*
* @return {Map.<string, number>}
* Object with |x|, |y|, |width|, and |height| properties.
*/
get rect() {
return {
x: this.window.screenX,
y: this.window.screenY,
width: this.window.outerWidth,
height: this.window.outerHeight,
};
}
/**
* Retrieves the current tabmodal UI object. According to the browser
* associated with the currently selected tab.
*/
getTabModal() {
let br = this.contentBrowser;
if (!br.hasAttribute("tabmodalPromptShowing")) {
return null;
}
// The modal is a direct sibling of the browser element.
// See tabbrowser.xml's getTabModalPromptBox.
let modalElements = br.parentNode.getElementsByTagNameNS(
XUL_NS,
"tabmodalprompt"
);
return br.tabModalPromptBox.prompts.get(modalElements[0]);
}
/**
* Close the current window.
*
* @return {Promise}
* A promise which is resolved when the current window has been closed.
*/
closeWindow() {
let destroyed = new MessageManagerDestroyedPromise(
this.window.messageManager
);
let unloaded = waitForEvent(this.window, "unload");
this.window.close();
return Promise.all([destroyed, unloaded]);
}
/**
* Focus the current window.
*
* @return {Promise}
* A promise which is resolved when the current window has been focused.
*/
async focusWindow() {
if (Services.focus.activeWindow != this.window) {
let activated = waitForEvent(this.window, "activate");
let focused = waitForEvent(this.window, "focus", { capture: true });
this.window.focus();
await Promise.all([activated, focused]);
}
}
/**
* Open a new browser window.
*
* @return {Promise}
* A promise resolving to the newly created chrome window.
*/
async openBrowserWindow(focus = false) {
switch (this.driver.appName) {
case "firefox":
// Open new browser window, and wait until it is fully loaded.
// Also wait for the window to be focused and activated to prevent a
// race condition when promptly focusing to the original window again.
let win = this.window.OpenBrowserWindow();
let activated = waitForEvent(win, "activate");
let focused = waitForEvent(win, "focus", { capture: true });
let startup = waitForObserverTopic(
"browser-delayed-startup-finished",
subject => subject == win
);
win.focus();
await Promise.all([activated, focused, startup]);
// The new window shouldn't get focused. As such set the
// focus back to the opening window.
if (!focus) {
await this.focusWindow();
}
return win;
default:
throw new UnsupportedOperationError(
`openWindow() not supported in ${this.driver.appName}`
);
}
}
/**
* Close the current tab.
*
* @return {Promise}
* A promise which is resolved when the current tab has been closed.
*
* @throws UnsupportedOperationError
* If tab handling for the current application isn't supported.
*/
closeTab() {
// If the current window is not a browser then close it directly. Do the
// same if only one remaining tab is open, or no tab selected at all.
if (
!this.tabBrowser ||
!this.tabBrowser.tabs ||
this.tabBrowser.tabs.length === 1 ||
!this.tab
) {
return this.closeWindow();
}
let destroyed = new MessageManagerDestroyedPromise(this.messageManager);
let tabClosed;
switch (this.driver.appName) {
case "fennec":
// Fennec
tabClosed = waitForEvent(this.tabBrowser.deck, "TabClose");
this.tabBrowser.closeTab(this.tab);
break;
case "firefox":
tabClosed = waitForEvent(this.tab, "TabClose");
this.tabBrowser.removeTab(this.tab);
break;
default:
throw new UnsupportedOperationError(
`closeTab() not supported in ${this.driver.appName}`
);
}
return Promise.all([destroyed, tabClosed]);
}
/**
* Open a new tab in the currently selected chrome window.
*/
async openTab(focus = false) {
let tab = null;
let tabOpened = waitForEvent(this.window, "TabOpen");
switch (this.driver.appName) {
case "fennec":
tab = this.tabBrowser.addTab(null);
this.tabBrowser.selectTab(focus ? tab : this.tab);
break;
case "firefox":
this.window.BrowserOpenTab();
tab = this.tabBrowser.selectedTab;
// The new tab is always selected by default. If focus is not wanted,
// the previously tab needs to be selected again.
if (!focus) {
this.tabBrowser.selectedTab = this.tab;
}
break;
default:
throw new UnsupportedOperationError(
`openTab() not supported in ${this.driver.appName}`
);
}
await tabOpened;
return tab;
}
/**
* Set the current tab.
*
* @param {number=} index
* Tab index to switch to. If the parameter is undefined,
* the currently selected tab will be used.
* @param {ChromeWindow=} window
* Switch to this window before selecting the tab.
* @param {boolean=} focus
* A boolean value which determins whether to focus
* the window. Defaults to true.
*
* @throws UnsupportedOperationError
* If tab handling for the current application isn't supported.
*/
async switchToTab(index, window = undefined, focus = true) {
let currentTab = this.tabBrowser.selectedTab;
if (window) {
this.window = window;
this.tabBrowser = browser.getTabBrowser(this.window);
}
if (!this.tabBrowser) {
return;
}
if (typeof index == "undefined") {
this.tab = this.tabBrowser.selectedTab;
} else {
this.tab = this.tabBrowser.tabs[index];
}
if (focus && this.tab != currentTab) {
let tabSelected = waitForEvent(this.window, "TabSelect");
switch (this.driver.appName) {
case "fennec":
this.tabBrowser.selectTab(this.tab);
await tabSelected;
break;
case "firefox":
this.tabBrowser.selectedTab = this.tab;
await tabSelected;
break;
default:
throw new UnsupportedOperationError(
`switchToTab() not supported in ${this.driver.appName}`
);
}
}
// TODO(ato): Currently tied to curBrowser, but should be moved to
// WebElement when introduced by https://bugzil.la/1400256.
this.eventObserver = new WebElementEventTarget(this.messageManager);
}
/**
* Registers a new frame, and sets its current frame id to this frame
* if it is not already assigned, and if a) we already have a session
* or b) we're starting a new session and it is the right start frame.
*
* @param {string} uid
* Frame uid for use by Marionette.
* @param {xul:browser} target
* The <xul:browser> that was the target of the originating message.
*/
register(uid, target) {
if (this.tabBrowser) {
// If we're setting up a new session on Firefox, we only process the
// registration for this frame if it belongs to the current tab.
if (!this.tab) {
this.switchToTab();
}
if (target === this.contentBrowser) {
this.updateIdForBrowser(this.contentBrowser, uid);
this._needsFlushPendingCommands = true;
}
}
// used to delete sessions
this.knownFrames.push(uid);
}
/**
* Flush any queued pending commands.
*
* Needs to be run after a process change for the frame script.
*/
flushPendingCommands() {
if (!this._needsFlushPendingCommands) {
return;
}
this.pendingCommands.forEach(cb => cb());
this._needsFlushPendingCommands = false;
}
/**
* This function intercepts commands interacting with content and queues
* or executes them as needed.
*
* No commands interacting with content are safe to process until
* the new listener script is loaded and registered itself.
* This occurs when a command whose effect is asynchronous (such
* as goBack) results in process change of the frame script and new
* commands are subsequently posted to the server.
*/
executeWhenReady(cb) {
if (this._needsFlushPendingCommands) {
this.pendingCommands.push(cb);
} else {
cb();
}
}
};
/**
* The window storage is used to save outer window IDs mapped to weak
* references of Window objects.
*
* Usage:
*
* let wins = new browser.Windows();
* wins.set(browser.outerWindowID, window);
*
* ...
*
* let win = wins.get(browser.outerWindowID);
*
*/
browser.Windows = class extends Map {
/**
* Save a weak reference to the Window object.
*
* @param {string} id
* Outer window ID.
* @param {Window} win
* Window object to save.
*
* @return {browser.Windows}
* Instance of self.
*/
set(id, win) {
let wref = Cu.getWeakReference(win);
super.set(id, wref);
return this;
}
/**
* Get the window object stored by provided |id|.
*
* @param {string} id
* Outer window ID.
*
* @return {Window}
* Saved window object.
*
* @throws {RangeError}
* If |id| is not in the store.
*/
get(id) {
let wref = super.get(id);
if (!wref) {
throw new RangeError();
}
return wref.get();
}
};
/**
* Marionette representation of the {@link ChromeWindow} window state.
*
* @enum {string}
*/
const WindowState = {
Maximized: "maximized",
Minimized: "minimized",
Normal: "normal",
Fullscreen: "fullscreen",
/**
* Converts {@link nsIDOMChromeWindow.windowState} to WindowState.
*
* @param {number} windowState
* Attribute from {@link nsIDOMChromeWindow.windowState}.
*
* @return {WindowState}
* JSON representation.
*
* @throws {TypeError}
* If <var>windowState</var> was unknown.
*/
from(windowState) {
switch (windowState) {
case 1:
return WindowState.Maximized;
case 2:
return WindowState.Minimized;
case 3:
return WindowState.Normal;
case 4:
return WindowState.Fullscreen;
default:
throw new TypeError(`Unknown window state: ${windowState}`);
}
},
};
this.WindowState = WindowState;