2016-05-10 15:29:21 +03:00
|
|
|
/* 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";
|
2017-06-28 21:04:14 +03:00
|
|
|
/* global frame */
|
2016-05-10 15:29:21 +03:00
|
|
|
|
2019-02-18 13:37:21 +03:00
|
|
|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const {WebElementEventTarget} = ChromeUtils.import("chrome://marionette/content/dom.js");
|
|
|
|
const {element} = ChromeUtils.import("chrome://marionette/content/element.js");
|
2017-06-28 21:01:49 +03:00
|
|
|
const {
|
|
|
|
NoSuchWindowError,
|
|
|
|
UnsupportedOperationError,
|
2019-01-17 21:18:31 +03:00
|
|
|
} = ChromeUtils.import("chrome://marionette/content/error.js");
|
2019-02-18 13:37:21 +03:00
|
|
|
const {Log} = ChromeUtils.import("chrome://marionette/content/log.js");
|
2018-04-17 11:43:27 +03:00
|
|
|
const {
|
2018-12-20 19:07:02 +03:00
|
|
|
MessageManagerDestroyedPromise,
|
2019-01-10 13:10:47 +03:00
|
|
|
waitForEvent,
|
2019-01-10 13:14:24 +03:00
|
|
|
waitForObserverTopic,
|
2019-01-17 21:18:31 +03:00
|
|
|
} = ChromeUtils.import("chrome://marionette/content/sync.js");
|
2016-05-10 15:29:21 +03:00
|
|
|
|
2019-02-18 13:37:21 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "logger", Log.get);
|
|
|
|
|
2017-10-16 19:47:35 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["browser", "Context", "WindowState"];
|
2016-05-10 15:29:21 +03:00
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/** @namespace */
|
2016-05-10 15:29:21 +03:00
|
|
|
this.browser = {};
|
|
|
|
|
|
|
|
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
|
|
|
|
2017-10-16 19:47:35 +03:00
|
|
|
/**
|
|
|
|
* Variations of Marionette contexts.
|
|
|
|
*
|
|
|
|
* Choosing a context through the <tt>Marionette:SetContext</tt>
|
|
|
|
* command directs all subsequent browsing context scoped commands
|
|
|
|
* to that context.
|
|
|
|
*/
|
2018-01-09 19:45:06 +03:00
|
|
|
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;
|
2017-10-16 19:47:35 +03:00
|
|
|
|
2018-01-09 19:45:06 +03:00
|
|
|
case "content":
|
|
|
|
return Context.Content;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new TypeError(`Unknown context: ${s}`);
|
|
|
|
}
|
2017-10-16 19:47:35 +03:00
|
|
|
}
|
2018-01-09 19:45:06 +03:00
|
|
|
}
|
|
|
|
Context.Chrome = "chrome";
|
|
|
|
Context.Content = "content";
|
|
|
|
this.Context = Context;
|
2017-10-16 19:47:35 +03:00
|
|
|
|
2017-01-26 18:42:35 +03:00
|
|
|
/**
|
2017-07-26 15:11:53 +03:00
|
|
|
* Get the <code><xul:browser></code> for the specified tab.
|
2017-01-26 18:42:35 +03:00
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @param {Tab} tab
|
2017-01-26 18:42:35 +03:00
|
|
|
* The tab whose browser needs to be returned.
|
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @return {Browser}
|
2017-01-30 17:35:16 +03:00
|
|
|
* The linked browser for the tab or null if no browser can be found.
|
2017-01-26 18:42:35 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
browser.getBrowserForTab = function(tab) {
|
|
|
|
// Fennec
|
2019-01-10 13:14:24 +03:00
|
|
|
if (tab && "browser" in tab) {
|
2017-01-26 18:42:35 +03:00
|
|
|
return tab.browser;
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
// Firefox
|
2019-01-10 13:14:24 +03:00
|
|
|
} else if (tab && "linkedBrowser" in tab) {
|
2017-01-26 18:42:35 +03:00
|
|
|
return tab.linkedBrowser;
|
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
|
|
|
|
return null;
|
2017-01-26 18:42:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the tab browser for the specified chrome window.
|
|
|
|
*
|
2017-08-21 20:52:18 +03:00
|
|
|
* @param {ChromeWindow} win
|
|
|
|
* Window whose <code>tabbrowser</code> needs to be accessed.
|
2017-01-26 18:42:35 +03:00
|
|
|
*
|
2017-07-26 15:11:53 +03:00
|
|
|
* @return {Tab}
|
2017-01-26 18:42:35 +03:00
|
|
|
* Tab browser or null if it's not a browser window.
|
|
|
|
*/
|
2017-08-21 20:52:18 +03:00
|
|
|
browser.getTabBrowser = function(window) {
|
2017-06-30 02:40:24 +03:00
|
|
|
// Fennec
|
2017-08-21 20:52:18 +03:00
|
|
|
if ("BrowserApp" in window) {
|
|
|
|
return window.BrowserApp;
|
2017-01-26 18:42:35 +03:00
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
// Firefox
|
2017-08-21 20:52:18 +03:00
|
|
|
} else if ("gBrowser" in window) {
|
|
|
|
return window.gBrowser;
|
2018-11-23 00:38:19 +03:00
|
|
|
|
|
|
|
// Thunderbird
|
|
|
|
} else if (window.document.getElementById("tabmail")) {
|
|
|
|
return window.document.getElementById("tabmail");
|
2017-01-26 18:42:35 +03:00
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
|
|
|
|
return null;
|
2017-01-26 18:42:35 +03:00
|
|
|
};
|
|
|
|
|
2016-05-10 15:29:21 +03:00
|
|
|
/**
|
|
|
|
* Creates a browsing context wrapper.
|
|
|
|
*
|
|
|
|
* Browsing contexts handle interactions with the browser, according to
|
2017-05-04 12:45:08 +03:00
|
|
|
* the current environment (Firefox, Fennec).
|
2016-05-10 15:29:21 +03:00
|
|
|
*/
|
|
|
|
browser.Context = class {
|
2017-01-04 22:37:13 +03:00
|
|
|
/**
|
2017-07-06 19:02:19 +03:00
|
|
|
* @param {ChromeWindow} win
|
|
|
|
* ChromeWindow that contains the top-level browsing context.
|
2017-01-04 22:37:13 +03:00
|
|
|
* @param {GeckoDriver} driver
|
|
|
|
* Reference to driver instance.
|
|
|
|
*/
|
2017-08-21 20:52:18 +03:00
|
|
|
constructor(window, driver) {
|
|
|
|
this.window = window;
|
2016-05-10 15:29:21 +03:00
|
|
|
this.driver = driver;
|
2017-01-04 22:37:13 +03:00
|
|
|
|
|
|
|
// In Firefox this is <xul:tabbrowser> (not <xul:browser>!)
|
|
|
|
// and BrowserApp in Fennec
|
2017-08-21 20:52:18 +03:00
|
|
|
this.tabBrowser = browser.getTabBrowser(this.window);
|
2017-01-04 22:37:13 +03:00
|
|
|
|
2016-05-10 15:29:21 +03:00
|
|
|
this.knownFrames = [];
|
2017-01-04 22:37:13 +03:00
|
|
|
|
|
|
|
// Used to set curFrameId upon new session
|
2016-05-10 15:29:21 +03:00
|
|
|
this.newSession = true;
|
2017-01-04 22:37:13 +03:00
|
|
|
|
2016-05-20 18:49:19 +03:00
|
|
|
this.seenEls = new element.Store();
|
2016-05-10 15:29:21 +03:00
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
// 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.
|
2016-05-10 15:29:21 +03:00
|
|
|
this.tab = null;
|
2017-07-06 19:02:19 +03:00
|
|
|
|
2018-11-06 15:08:55 +03:00
|
|
|
// 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.
|
2016-05-10 15:29:21 +03:00
|
|
|
this.pendingCommands = [];
|
2017-07-06 19:02:19 +03:00
|
|
|
this._needsFlushPendingCommands = false;
|
2016-05-10 15:29:21 +03:00
|
|
|
|
|
|
|
this.frameRegsPending = 0;
|
|
|
|
|
|
|
|
this.getIdForBrowser = driver.getIdForBrowser.bind(driver);
|
|
|
|
this.updateIdForBrowser = driver.updateIdForBrowser.bind(driver);
|
|
|
|
}
|
|
|
|
|
2017-03-27 12:02:36 +03:00
|
|
|
/**
|
|
|
|
* 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);
|
2017-06-30 02:40:24 +03:00
|
|
|
} else if (this.tabBrowser &&
|
|
|
|
this.driver.isReftestBrowser(this.tabBrowser)) {
|
2017-05-09 21:05:49 +03:00
|
|
|
return this.tabBrowser;
|
2017-03-27 12:02:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-09-15 19:07:41 +03:00
|
|
|
get messageManager() {
|
2018-04-17 11:43:27 +03:00
|
|
|
if (this.contentBrowser) {
|
|
|
|
return this.contentBrowser.messageManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2017-09-15 19:07:41 +03:00
|
|
|
}
|
|
|
|
|
2018-01-12 17:25:30 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2016-05-10 15:29:21 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
2017-06-30 02:40:24 +03:00
|
|
|
if (this.tab || this.driver.isReftestBrowser(this.contentBrowser)) {
|
2017-03-27 12:02:36 +03:00
|
|
|
rv = this.getIdForBrowser(this.contentBrowser);
|
2016-05-10 15:29:21 +03:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-07-02 14:31:18 +03:00
|
|
|
/**
|
|
|
|
* 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");
|
|
|
|
}
|
|
|
|
|
2017-05-31 17:49:56 +03:00
|
|
|
/**
|
2017-06-13 16:46:59 +03:00
|
|
|
* 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.
|
2017-05-31 17:49:56 +03:00
|
|
|
*/
|
2017-06-13 16:46:59 +03:00
|
|
|
get currentURI() {
|
2017-05-31 17:49:56 +03:00
|
|
|
// Bug 1363368 - contentBrowser could be null until we wait for its
|
|
|
|
// initialization been finished
|
|
|
|
if (this.contentBrowser) {
|
2017-06-13 16:46:59 +03:00
|
|
|
return this.contentBrowser.currentURI;
|
2017-05-31 17:49:56 +03:00
|
|
|
}
|
2017-06-30 02:40:24 +03:00
|
|
|
throw new NoSuchWindowError(
|
|
|
|
"Current window does not have a content browser");
|
2017-05-31 17:49:56 +03:00
|
|
|
}
|
|
|
|
|
Bug 1364319 - Make setWindowRect deterministic; r=automatedtester,maja_zf
The Marionette setWindowRect command is meant to provide a blocking API
for resizing and moving the window. This currently has an intermittent
rate of ~0.254 with the WPT conformance test.
The main issue in providing a blocking API is that the DOM resize
event fires at a high rate and needs to be throttled. We are able to
throttle this successfully with requestAnimationFrame, and before that,
a hard-coded 60 fps setTimeout delay. To the naked eye, this appears to
synchronise window resizing before returning the resposne to the client.
However, occasionally the response contains the wrong window size.
window.outerWidth and window.outerHeight do not appear to be
deterministic as DOM properties are not synchronously populated.
Calls to ChromeWindow.outerWidth and ChromeWindow.outerHeight sometimes
also returns inconsistent values. Tests, document in the bug, have
shown that somtimes, the returned window size from the setWindowRect
command is correct, and that the size from the subsequent getWindowRect
command is not.
By using a combination of Services.tm.mainThread.idleDispatch and a
blocking promise on not returning a response until the window size has
changed, we are able to reduce the intermittent rate significantly (by
over an order of magnitude).
As one would expect, delaying computation allows DOM property values to
populate, and as such does not address the underlying problem or make it
inconceivable that the race described above could re-appear, but it does
seem to be more reliable than the current approach.
MozReview-Commit-ID: Lf2yeeXH082
--HG--
extra : rebase_source : e27912fdcb6edbf825bf3168f3542ff2b4551b8b
2017-06-21 19:13:51 +03:00
|
|
|
/**
|
2018-09-24 19:05:23 +03:00
|
|
|
* Gets the position and dimensions of the top-level browsing context.
|
Bug 1364319 - Make setWindowRect deterministic; r=automatedtester,maja_zf
The Marionette setWindowRect command is meant to provide a blocking API
for resizing and moving the window. This currently has an intermittent
rate of ~0.254 with the WPT conformance test.
The main issue in providing a blocking API is that the DOM resize
event fires at a high rate and needs to be throttled. We are able to
throttle this successfully with requestAnimationFrame, and before that,
a hard-coded 60 fps setTimeout delay. To the naked eye, this appears to
synchronise window resizing before returning the resposne to the client.
However, occasionally the response contains the wrong window size.
window.outerWidth and window.outerHeight do not appear to be
deterministic as DOM properties are not synchronously populated.
Calls to ChromeWindow.outerWidth and ChromeWindow.outerHeight sometimes
also returns inconsistent values. Tests, document in the bug, have
shown that somtimes, the returned window size from the setWindowRect
command is correct, and that the size from the subsequent getWindowRect
command is not.
By using a combination of Services.tm.mainThread.idleDispatch and a
blocking promise on not returning a response until the window size has
changed, we are able to reduce the intermittent rate significantly (by
over an order of magnitude).
As one would expect, delaying computation allows DOM property values to
populate, and as such does not address the underlying problem or make it
inconceivable that the race described above could re-appear, but it does
seem to be more reliable than the current approach.
MozReview-Commit-ID: Lf2yeeXH082
--HG--
extra : rebase_source : e27912fdcb6edbf825bf3168f3542ff2b4551b8b
2017-06-21 19:13:51 +03:00
|
|
|
*
|
|
|
|
* @return {Map.<string, number>}
|
2018-09-24 19:05:23 +03:00
|
|
|
* Object with |x|, |y|, |width|, and |height| properties.
|
Bug 1364319 - Make setWindowRect deterministic; r=automatedtester,maja_zf
The Marionette setWindowRect command is meant to provide a blocking API
for resizing and moving the window. This currently has an intermittent
rate of ~0.254 with the WPT conformance test.
The main issue in providing a blocking API is that the DOM resize
event fires at a high rate and needs to be throttled. We are able to
throttle this successfully with requestAnimationFrame, and before that,
a hard-coded 60 fps setTimeout delay. To the naked eye, this appears to
synchronise window resizing before returning the resposne to the client.
However, occasionally the response contains the wrong window size.
window.outerWidth and window.outerHeight do not appear to be
deterministic as DOM properties are not synchronously populated.
Calls to ChromeWindow.outerWidth and ChromeWindow.outerHeight sometimes
also returns inconsistent values. Tests, document in the bug, have
shown that somtimes, the returned window size from the setWindowRect
command is correct, and that the size from the subsequent getWindowRect
command is not.
By using a combination of Services.tm.mainThread.idleDispatch and a
blocking promise on not returning a response until the window size has
changed, we are able to reduce the intermittent rate significantly (by
over an order of magnitude).
As one would expect, delaying computation allows DOM property values to
populate, and as such does not address the underlying problem or make it
inconceivable that the race described above could re-appear, but it does
seem to be more reliable than the current approach.
MozReview-Commit-ID: Lf2yeeXH082
--HG--
extra : rebase_source : e27912fdcb6edbf825bf3168f3542ff2b4551b8b
2017-06-21 19:13:51 +03:00
|
|
|
*/
|
|
|
|
get rect() {
|
|
|
|
return {
|
|
|
|
x: this.window.screenX,
|
|
|
|
y: this.window.screenY,
|
2018-09-24 19:05:23 +03:00
|
|
|
width: this.window.outerWidth,
|
|
|
|
height: this.window.outerHeight,
|
Bug 1364319 - Make setWindowRect deterministic; r=automatedtester,maja_zf
The Marionette setWindowRect command is meant to provide a blocking API
for resizing and moving the window. This currently has an intermittent
rate of ~0.254 with the WPT conformance test.
The main issue in providing a blocking API is that the DOM resize
event fires at a high rate and needs to be throttled. We are able to
throttle this successfully with requestAnimationFrame, and before that,
a hard-coded 60 fps setTimeout delay. To the naked eye, this appears to
synchronise window resizing before returning the resposne to the client.
However, occasionally the response contains the wrong window size.
window.outerWidth and window.outerHeight do not appear to be
deterministic as DOM properties are not synchronously populated.
Calls to ChromeWindow.outerWidth and ChromeWindow.outerHeight sometimes
also returns inconsistent values. Tests, document in the bug, have
shown that somtimes, the returned window size from the setWindowRect
command is correct, and that the size from the subsequent getWindowRect
command is not.
By using a combination of Services.tm.mainThread.idleDispatch and a
blocking promise on not returning a response until the window size has
changed, we are able to reduce the intermittent rate significantly (by
over an order of magnitude).
As one would expect, delaying computation allows DOM property values to
populate, and as such does not address the underlying problem or make it
inconceivable that the race described above could re-appear, but it does
seem to be more reliable than the current approach.
MozReview-Commit-ID: Lf2yeeXH082
--HG--
extra : rebase_source : e27912fdcb6edbf825bf3168f3542ff2b4551b8b
2017-06-21 19:13:51 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-10 15:29:21 +03:00
|
|
|
/**
|
|
|
|
* Retrieves the current tabmodal UI object. According to the browser
|
|
|
|
* associated with the currently selected tab.
|
|
|
|
*/
|
2019-02-16 03:03:11 +03:00
|
|
|
getTabModal() {
|
2017-03-27 12:02:36 +03:00
|
|
|
let br = this.contentBrowser;
|
2016-05-10 15:29:21 +03:00
|
|
|
if (!br.hasAttribute("tabmodalPromptShowing")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The modal is a direct sibling of the browser element.
|
|
|
|
// See tabbrowser.xml's getTabModalPromptBox.
|
2019-01-04 22:29:34 +03:00
|
|
|
let modalElements = br.parentNode.getElementsByTagNameNS(
|
2016-05-10 15:29:21 +03:00
|
|
|
XUL_NS, "tabmodalprompt");
|
2019-01-04 22:29:34 +03:00
|
|
|
|
2019-02-16 03:03:11 +03:00
|
|
|
return br.tabModalPromptBox.prompts.get(modalElements[0]);
|
2016-05-10 15:29:21 +03:00
|
|
|
}
|
|
|
|
|
2017-01-10 18:36:49 +03:00
|
|
|
/**
|
|
|
|
* Close the current window.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* A promise which is resolved when the current window has been closed.
|
|
|
|
*/
|
|
|
|
closeWindow() {
|
2019-01-10 13:10:47 +03:00
|
|
|
let destroyed = new MessageManagerDestroyedPromise(
|
|
|
|
this.window.messageManager);
|
|
|
|
let unloaded = waitForEvent(this.window, "unload");
|
|
|
|
|
|
|
|
this.window.close();
|
|
|
|
|
|
|
|
return Promise.all([destroyed, unloaded]);
|
2019-01-09 21:27:28 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 13:14:24 +03:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
// Bug 1509380 - Missing focus/activate event when Firefox is not
|
|
|
|
// the top-most application. As such wait for the next tick, and
|
|
|
|
// manually focus the newly opened window.
|
|
|
|
win.setTimeout(() => win.focus(), 0);
|
|
|
|
|
|
|
|
await Promise.all([activated, focused, startup]);
|
|
|
|
|
2019-03-14 18:14:17 +03:00
|
|
|
// The new window shouldn't get focused. As such set the
|
|
|
|
// focus back to the opening window if needed.
|
|
|
|
if (!focus && Services.focus.activeWindow != this.window) {
|
2019-01-10 13:14:24 +03:00
|
|
|
activated = waitForEvent(this.window, "activate");
|
|
|
|
focused = waitForEvent(this.window, "focus", {capture: true});
|
|
|
|
|
|
|
|
this.window.focus();
|
|
|
|
|
|
|
|
await Promise.all([activated, focused]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return win;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new UnsupportedOperationError(
|
|
|
|
`openWindow() not supported in ${this.driver.appName}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 18:36:49 +03:00
|
|
|
/**
|
|
|
|
* Close the current tab.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* A promise which is resolved when the current tab has been closed.
|
2017-01-26 18:42:35 +03:00
|
|
|
*
|
|
|
|
* @throws UnsupportedOperationError
|
|
|
|
* If tab handling for the current application isn't supported.
|
2017-01-10 18:36:49 +03:00
|
|
|
*/
|
2016-05-10 15:29:21 +03:00
|
|
|
closeTab() {
|
2017-01-10 18:36:49 +03:00
|
|
|
// 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.
|
2017-06-30 02:40:24 +03:00
|
|
|
if (!this.tabBrowser ||
|
|
|
|
!this.tabBrowser.tabs ||
|
|
|
|
this.tabBrowser.tabs.length === 1 ||
|
|
|
|
!this.tab) {
|
2017-01-10 18:36:49 +03:00
|
|
|
return this.closeWindow();
|
2016-05-10 15:29:21 +03:00
|
|
|
}
|
2017-01-10 18:36:49 +03:00
|
|
|
|
2019-01-10 13:10:47 +03:00
|
|
|
let destroyed = new MessageManagerDestroyedPromise(this.messageManager);
|
|
|
|
let tabClosed;
|
|
|
|
|
2019-01-10 13:14:24 +03:00
|
|
|
switch (this.driver.appName) {
|
|
|
|
case "fennec":
|
|
|
|
// Fennec
|
|
|
|
tabClosed = waitForEvent(this.tabBrowser.deck, "TabClose");
|
|
|
|
this.tabBrowser.closeTab(this.tab);
|
|
|
|
break;
|
2019-01-10 13:10:47 +03:00
|
|
|
|
2019-01-10 13:14:24 +03:00
|
|
|
case "firefox":
|
|
|
|
tabClosed = waitForEvent(this.tab, "TabClose");
|
|
|
|
this.tabBrowser.removeTab(this.tab);
|
|
|
|
break;
|
2019-01-10 13:10:47 +03:00
|
|
|
|
2019-01-10 13:14:24 +03:00
|
|
|
default:
|
|
|
|
throw new UnsupportedOperationError(
|
|
|
|
`closeTab() not supported in ${this.driver.appName}`);
|
2019-01-10 13:10:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all([destroyed, tabClosed]);
|
2016-05-10 15:29:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-10 13:14:24 +03:00
|
|
|
* Open a new tab in the currently selected chrome window.
|
2016-05-10 15:29:21 +03:00
|
|
|
*/
|
2019-01-10 13:14:24 +03:00
|
|
|
async openTab(focus = false) {
|
|
|
|
let tab = null;
|
|
|
|
let tabOpened = waitForEvent(this.window, "TabOpen");
|
|
|
|
|
|
|
|
switch (this.driver.appName) {
|
|
|
|
case "fennec":
|
2019-03-14 22:53:09 +03:00
|
|
|
tab = this.tabBrowser.addTab(null);
|
|
|
|
this.tabBrowser.selectTab(focus ? tab : this.tab);
|
2019-01-10 13:14:24 +03:00
|
|
|
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;
|
2016-05-10 15:29:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-06 19:02:19 +03:00
|
|
|
* Set the current tab.
|
2016-05-10 15:29:21 +03:00
|
|
|
*
|
2017-01-26 18:42:35 +03:00
|
|
|
* @param {number=} index
|
|
|
|
* Tab index to switch to. If the parameter is undefined,
|
|
|
|
* the currently selected tab will be used.
|
2017-08-21 20:52:18 +03:00
|
|
|
* @param {ChromeWindow=} window
|
2017-01-26 18:42:35 +03:00
|
|
|
* Switch to this window before selecting the tab.
|
2017-01-30 17:35:16 +03:00
|
|
|
* @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.
|
2016-05-10 15:29:21 +03:00
|
|
|
*/
|
2017-08-21 20:52:18 +03:00
|
|
|
switchToTab(index, window = undefined, focus = true) {
|
|
|
|
if (window) {
|
|
|
|
this.window = window;
|
|
|
|
this.tabBrowser = browser.getTabBrowser(this.window);
|
2017-01-26 18:42:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.tabBrowser) {
|
|
|
|
return;
|
2016-05-10 15:29:21 +03:00
|
|
|
}
|
2017-01-26 18:42:35 +03:00
|
|
|
|
|
|
|
if (typeof index == "undefined") {
|
|
|
|
this.tab = this.tabBrowser.selectedTab;
|
|
|
|
} else {
|
|
|
|
this.tab = this.tabBrowser.tabs[index];
|
|
|
|
|
2017-01-30 17:35:16 +03:00
|
|
|
if (focus) {
|
2019-02-04 21:15:52 +03:00
|
|
|
if ("selectTab" in this.tabBrowser) {
|
|
|
|
this.tabBrowser.selectTab(this.tab);
|
|
|
|
} else if ("selectedTab" in this.tabBrowser) {
|
|
|
|
this.tabBrowser.selectedTab = this.tab;
|
|
|
|
} else {
|
|
|
|
throw new UnsupportedOperationError(
|
|
|
|
`switchToTab() not supported in ${this.driver.appName}`);
|
2017-01-30 17:35:16 +03:00
|
|
|
}
|
2017-01-26 18:42:35 +03:00
|
|
|
}
|
2016-07-01 00:03:41 +03:00
|
|
|
}
|
2017-09-15 19:07:41 +03:00
|
|
|
|
|
|
|
// 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);
|
2016-05-10 15:29:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-07-06 19:02:19 +03:00
|
|
|
* @param {xul:browser} target
|
|
|
|
* The <xul:browser> that was the target of the originating message.
|
2016-05-10 15:29:21 +03:00
|
|
|
*/
|
|
|
|
register(uid, target) {
|
2017-07-06 19:02:19 +03:00
|
|
|
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();
|
|
|
|
}
|
2016-05-10 15:29:21 +03:00
|
|
|
|
2017-07-06 19:02:19 +03:00
|
|
|
if (target === this.contentBrowser) {
|
|
|
|
this.updateIdForBrowser(this.contentBrowser, uid);
|
|
|
|
this._needsFlushPendingCommands = true;
|
2016-05-10 15:29:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// used to delete sessions
|
|
|
|
this.knownFrames.push(uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-06 15:08:55 +03:00
|
|
|
* Flush any queued pending commands.
|
|
|
|
*
|
|
|
|
* Needs to be run after a process change for the frame script.
|
2016-05-10 15:29:21 +03:00
|
|
|
*/
|
|
|
|
flushPendingCommands() {
|
2017-07-06 19:02:19 +03:00
|
|
|
if (!this._needsFlushPendingCommands) {
|
2016-05-10 15:29:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pendingCommands.forEach(cb => cb());
|
2017-07-06 19:02:19 +03:00
|
|
|
this._needsFlushPendingCommands = false;
|
2016-05-10 15:29:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function intercepts commands interacting with content and queues
|
|
|
|
* or executes them as needed.
|
|
|
|
*
|
|
|
|
* No commands interacting with content are safe to process until
|
2018-11-06 15:08:55 +03:00
|
|
|
* the new listener script is loaded and registered itself.
|
2016-05-10 15:29:21 +03:00
|
|
|
* This occurs when a command whose effect is asynchronous (such
|
2018-11-06 15:08:55 +03:00
|
|
|
* as goBack) results in process change of the frame script and new
|
|
|
|
* commands are subsequently posted to the server.
|
2016-05-10 15:29:21 +03:00
|
|
|
*/
|
|
|
|
executeWhenReady(cb) {
|
2017-07-06 19:02:19 +03:00
|
|
|
if (this._needsFlushPendingCommands) {
|
2016-05-10 15:29:21 +03:00
|
|
|
this.pendingCommands.push(cb);
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-05-23 12:21:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}
|
2017-06-28 21:12:33 +03:00
|
|
|
* Saved window object.
|
|
|
|
*
|
|
|
|
* @throws {RangeError}
|
|
|
|
* If |id| is not in the store.
|
2016-05-23 12:21:15 +03:00
|
|
|
*/
|
|
|
|
get(id) {
|
|
|
|
let wref = super.get(id);
|
2017-06-28 21:12:33 +03:00
|
|
|
if (!wref) {
|
|
|
|
throw new RangeError();
|
2016-05-23 12:21:15 +03:00
|
|
|
}
|
2017-06-28 21:12:33 +03:00
|
|
|
return wref.get();
|
2016-05-23 12:21:15 +03:00
|
|
|
}
|
|
|
|
};
|
2018-01-17 20:56:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|