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";
|
|
|
|
|
2020-09-08 21:19:32 +03:00
|
|
|
const EXPORTED_SYMBOLS = ["browser", "Context", "WindowState"];
|
|
|
|
|
2019-02-18 13:37:21 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2019-07-05 12:01:24 +03:00
|
|
|
|
2020-09-08 21:22:43 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
2021-06-07 13:08:24 +03:00
|
|
|
AppInfo: "chrome://remote/content/marionette/appinfo.js",
|
2021-06-21 15:53:48 +03:00
|
|
|
error: "chrome://remote/content/shared/webdriver/Errors.jsm",
|
2021-06-07 13:08:24 +03:00
|
|
|
MessageManagerDestroyedPromise: "chrome://remote/content/marionette/sync.js",
|
|
|
|
waitForEvent: "chrome://remote/content/marionette/sync.js",
|
|
|
|
WebElementEventTarget: "chrome://remote/content/marionette/dom.js",
|
2021-07-09 16:18:31 +03:00
|
|
|
windowManager: "chrome://remote/content/shared/WindowManager.jsm",
|
2020-09-08 21:22:43 +03:00
|
|
|
});
|
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 = {};
|
|
|
|
|
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.
|
2021-04-20 23:49:48 +03:00
|
|
|
*
|
|
|
|
* @class Marionette.Context
|
2017-10-16 19:47:35 +03:00
|
|
|
*/
|
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
|
|
|
|
2020-04-15 01:58:33 +03:00
|
|
|
// GeckoView shim for Desktop's gBrowser
|
|
|
|
class MobileTabBrowser {
|
|
|
|
constructor(window) {
|
|
|
|
this.window = window;
|
|
|
|
}
|
|
|
|
|
|
|
|
get tabs() {
|
|
|
|
return [this.window.tab];
|
|
|
|
}
|
|
|
|
|
|
|
|
get selectedTab() {
|
|
|
|
return this.window.tab;
|
|
|
|
}
|
2020-08-27 23:24:41 +03:00
|
|
|
|
|
|
|
set selectedTab(tab) {
|
|
|
|
if (tab != this.selectedTab) {
|
|
|
|
throw new Error("GeckoView only supports a single tab");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synthesize a custom TabSelect event to indicate that a tab has been
|
|
|
|
// selected even when we don't change it.
|
|
|
|
const event = this.window.CustomEvent("TabSelect", {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: false,
|
|
|
|
detail: {
|
|
|
|
previousTab: this.selectedTab,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
this.window.document.dispatchEvent(event);
|
|
|
|
}
|
2020-12-11 18:41:50 +03:00
|
|
|
|
|
|
|
get selectedBrowser() {
|
|
|
|
return this.selectedTab.linkedBrowser;
|
|
|
|
}
|
2021-05-11 16:34:05 +03:00
|
|
|
|
|
|
|
addEventListener() {
|
|
|
|
this.window.addEventListener(...arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEventListener() {
|
|
|
|
this.window.removeEventListener(...arguments);
|
|
|
|
}
|
2020-04-15 01:58:33 +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) {
|
2020-04-15 01:58:33 +03:00
|
|
|
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) {
|
2020-08-27 23:24:41 +03:00
|
|
|
// GeckoView
|
2021-03-18 22:02:38 +03:00
|
|
|
if (AppInfo.isAndroid) {
|
2020-04-15 01:58:33 +03:00
|
|
|
return new MobileTabBrowser(window);
|
2020-08-27 23:24:41 +03:00
|
|
|
// Firefox
|
|
|
|
} 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
|
2020-04-15 01:58:33 +03:00
|
|
|
* the current environment.
|
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>!)
|
2020-04-15 01:58:33 +03:00
|
|
|
// and MobileTabBrowser in GeckoView.
|
2017-08-21 20:52:18 +03:00
|
|
|
this.tabBrowser = browser.getTabBrowser(this.window);
|
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
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
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.
|
2020-11-08 06:52:32 +03:00
|
|
|
let modalElements = br.parentNode.getElementsByTagName("tabmodalprompt");
|
2019-01-04 22:29:34 +03:00
|
|
|
|
2020-04-16 17:44:56 +03:00
|
|
|
return br.tabModalPromptBox.getPrompt(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.
|
|
|
|
*/
|
2020-09-24 22:02:26 +03:00
|
|
|
async closeWindow() {
|
2021-04-26 16:01:07 +03:00
|
|
|
return windowManager.closeWindow(this.window);
|
2019-01-09 21:27:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 23:42:28 +03:00
|
|
|
/**
|
|
|
|
* Focus the current window.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* A promise which is resolved when the current window has been focused.
|
|
|
|
*/
|
|
|
|
async focusWindow() {
|
2021-04-26 16:01:07 +03:00
|
|
|
return windowManager.focusWindow(this.window);
|
2019-06-04 23:42: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.
|
|
|
|
*/
|
2020-02-13 22:47:10 +03:00
|
|
|
async openBrowserWindow(focus = false, isPrivate = false) {
|
2021-04-26 16:01:07 +03:00
|
|
|
return windowManager.openBrowserWindow(this.window, focus, isPrivate);
|
2019-01-10 13:14:24 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2021-03-18 22:02:38 +03:00
|
|
|
switch (AppInfo.name) {
|
|
|
|
case "Firefox":
|
2019-01-10 13:14:24 +03:00
|
|
|
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:
|
2020-08-26 11:42:14 +03:00
|
|
|
throw new error.UnsupportedOperationError(
|
2021-03-18 22:02:38 +03:00
|
|
|
`closeTab() not supported in ${AppInfo.name}`
|
2019-01-10 13:14:24 +03:00
|
|
|
);
|
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;
|
|
|
|
|
2021-03-18 22:02:38 +03:00
|
|
|
switch (AppInfo.name) {
|
|
|
|
case "Firefox":
|
2020-11-10 23:23:06 +03:00
|
|
|
const opened = waitForEvent(this.window, "TabOpen");
|
2020-11-07 20:07:19 +03:00
|
|
|
this.window.BrowserOpenTab();
|
2020-11-10 23:23:06 +03:00
|
|
|
await opened;
|
2020-07-17 19:19:17 +03:00
|
|
|
|
2020-11-10 23:23:06 +03:00
|
|
|
tab = this.tabBrowser.selectedTab;
|
2020-11-07 20:07:19 +03:00
|
|
|
|
2019-01-10 13:14:24 +03:00
|
|
|
// 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:
|
2020-08-26 11:42:14 +03:00
|
|
|
throw new error.UnsupportedOperationError(
|
2021-03-18 22:02:38 +03:00
|
|
|
`openTab() not supported in ${AppInfo.name}`
|
2019-01-10 13:14:24 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
2020-08-28 08:09:18 +03:00
|
|
|
* @return {Tab}
|
|
|
|
* The selected tab.
|
|
|
|
*
|
2017-01-30 17:35:16 +03:00
|
|
|
* @throws UnsupportedOperationError
|
|
|
|
* If tab handling for the current application isn't supported.
|
2016-05-10 15:29:21 +03:00
|
|
|
*/
|
2019-06-04 23:42:28 +03:00
|
|
|
async switchToTab(index, window = undefined, focus = true) {
|
|
|
|
let currentTab = this.tabBrowser.selectedTab;
|
|
|
|
|
2017-08-21 20:52:18 +03:00
|
|
|
if (window) {
|
|
|
|
this.window = window;
|
|
|
|
this.tabBrowser = browser.getTabBrowser(this.window);
|
2017-01-26 18:42:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.tabBrowser) {
|
2020-08-28 08:09:18 +03:00
|
|
|
return null;
|
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];
|
2019-06-04 23:42:28 +03:00
|
|
|
}
|
2017-01-26 18:42:35 +03:00
|
|
|
|
2019-06-04 23:42:28 +03:00
|
|
|
if (focus && this.tab != currentTab) {
|
2020-08-27 23:24:41 +03:00
|
|
|
const tabSelected = waitForEvent(this.window, "TabSelect");
|
|
|
|
this.tabBrowser.selectedTab = this.tab;
|
|
|
|
await tabSelected;
|
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);
|
2020-08-28 08:09:18 +03:00
|
|
|
|
|
|
|
return this.tab;
|
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.
|
|
|
|
*
|
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
|
|
|
*/
|
2020-12-11 18:40:53 +03:00
|
|
|
register(target) {
|
2020-12-11 18:40:56 +03:00
|
|
|
if (!this.tabBrowser) {
|
|
|
|
return;
|
2016-05-10 15:29:21 +03:00
|
|
|
}
|
|
|
|
|
2020-12-11 18:40:56 +03:00
|
|
|
// 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
|
|
|
}
|
|
|
|
};
|
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;
|