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
|
/* 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/. */
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
URILoadingHelper: "resource:///modules/URILoadingHelper.sys.mjs",
AnimationFramePromise: "chrome://remote/content/shared/Sync.sys.mjs",
AppInfo: "chrome://remote/content/shared/AppInfo.sys.mjs",
BrowsingContextListener:
"chrome://remote/content/shared/listeners/BrowsingContextListener.sys.mjs",
DebounceCallback: "chrome://remote/content/marionette/sync.sys.mjs",
error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
EventPromise: "chrome://remote/content/shared/Sync.sys.mjs",
generateUUID: "chrome://remote/content/shared/UUID.sys.mjs",
Log: "chrome://remote/content/shared/Log.sys.mjs",
NavigableManager: "chrome://remote/content/shared/NavigableManager.sys.mjs",
TabManager: "chrome://remote/content/shared/TabManager.sys.mjs",
TimedPromise: "chrome://remote/content/shared/Sync.sys.mjs",
UserContextManager:
"chrome://remote/content/shared/UserContextManager.sys.mjs",
waitForObserverTopic: "chrome://remote/content/marionette/sync.sys.mjs",
});
ChromeUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get());
// Timeout used to abort fullscreen, maximize, and minimize
// commands if no window manager is present.
const TIMEOUT_NO_WINDOW_MANAGER = 5000;
/**
* Provides helpers to interact with Window objects.
*
* @class WindowManager
*/
class WindowManager {
#clientWindowIds;
#contextListener;
constructor() {
// Maps ChromeWindow to uuid: WeakMap.<Object, string>
this._chromeWindowHandles = new WeakMap();
/**
* Keep track of the client window for any registered contexts. When the
* contextDestroyed event is fired, the context is already destroyed so
* we cannot query for the client window at that time.
*/
this.#clientWindowIds = new WeakMap();
this.#contextListener = new lazy.BrowsingContextListener();
this.#contextListener.on("attached", this.#onContextAttached);
this.#contextListener.startListening();
}
get chromeWindowHandles() {
const chromeWindowHandles = [];
for (const win of this.windows) {
chromeWindowHandles.push(this.getIdForWindow(win));
}
return chromeWindowHandles;
}
/**
* Retrieve all the open windows.
*
* @returns {Array<Window>}
* All the open windows. Will return an empty list if no window is open.
*/
get windows() {
const windows = [];
for (const win of Services.wm.getEnumerator(null)) {
if (win.closed) {
continue;
}
windows.push(win);
}
return windows;
}
/**
* Find a specific window matching the provided window handle.
*
* @param {string} handle
* The unique handle of either a chrome window or a content browser, as
* returned by :js:func:`#getIdForBrowser` or :js:func:`#getIdForWindow`.
*
* @returns {object} A window properties object,
* @see :js:func:`GeckoDriver#getWindowProperties`
*/
findWindowByHandle(handle) {
for (const win of this.windows) {
// In case the wanted window is a chrome window, we are done.
const chromeWindowId = this.getIdForWindow(win);
if (chromeWindowId == handle) {
return this.getWindowProperties(win);
}
// Otherwise check if the chrome window has a tab browser, and that it
// contains a tab with the wanted window handle.
const tabBrowser = lazy.TabManager.getTabBrowser(win);
if (tabBrowser && tabBrowser.tabs) {
for (let i = 0; i < tabBrowser.tabs.length; ++i) {
let contentBrowser = lazy.TabManager.getBrowserForTab(
tabBrowser.tabs[i]
);
let contentWindowId =
lazy.NavigableManager.getIdForBrowser(contentBrowser);
if (contentWindowId == handle) {
return this.getWindowProperties(win, { tabIndex: i });
}
}
}
}
return null;
}
/**
* A set of properties describing a window and that should allow to uniquely
* identify it. The described window can either be a Chrome Window or a
* Content Window.
*
* @typedef {object} WindowProperties
* @property {Window} win - The Chrome Window containing the window.
* When describing a Chrome Window, this is the window itself.
* @property {string} id - The unique id of the containing Chrome Window.
* @property {boolean} hasTabBrowser - `true` if the Chrome Window has a
* tabBrowser.
* @property {number} tabIndex - Optional, the index of the specific tab
* within the window.
*/
/**
* Returns a WindowProperties object, that can be used with :js:func:`GeckoDriver#setWindowHandle`.
*
* @param {Window} win
* The Chrome Window for which we want to create a properties object.
* @param {object} options
* @param {number} options.tabIndex
* Tab index of a specific Content Window in the specified Chrome Window.
* @returns {WindowProperties} A window properties object.
*/
getWindowProperties(win, options = {}) {
if (!Window.isInstance(win)) {
throw new TypeError("Invalid argument, expected a Window object");
}
return {
win,
id: this.getIdForWindow(win),
hasTabBrowser: !!lazy.TabManager.getTabBrowser(win),
tabIndex: options.tabIndex,
};
}
/**
* Returns the window ID for a specific browsing context.
*
* @param {BrowsingContext} context
* The browsing context for which we want to retrieve the window ID.
*
* @returns {(string|undefined)}
* The ID of the window associated with the browsing context.
*/
getIdForBrowsingContext(context) {
const window = this.getWindowForBrowsingContext(context);
return window
? this.getIdForWindow(window)
: this.#clientWindowIds.get(context);
}
/**
* Retrieves an id for the given chrome window. The id is a dynamically
* generated uuid associated with the window object.
*
* @param {window} win
* The window object for which we want to retrieve the id.
* @returns {string} The unique id for this chrome window.
*/
getIdForWindow(win) {
if (!this._chromeWindowHandles.has(win)) {
this._chromeWindowHandles.set(win, lazy.generateUUID());
}
return this._chromeWindowHandles.get(win);
}
/**
* Close the specified window.
*
* @param {window} win
* The window to close.
* @returns {Promise}
* A promise which is resolved when the current window has been closed.
*/
async closeWindow(win) {
const destroyed = lazy.waitForObserverTopic("xul-window-destroyed", {
checkFn: () => win && win.closed,
});
win.close();
return destroyed;
}
/**
* Adjusts the window geometry.
*
*@param {window} win
* The browser window to adjust.
* @param {number} x
* The x-coordinate of the window.
* @param {number} y
* The y-coordinate of the window.
* @param {number} width
* The width of the window.
* @param {number} height
* The height of the window.
*
* @returns {Promise}
* A promise that resolves when the window geometry has been adjusted.
*
* @throws {TimeoutError}
* Raised if the operating system fails to honor the requested move or resize.
*/
async adjustWindowGeometry(win, x, y, width, height) {
// we find a matching position on e.g. resize, then resolve, then a geometry
// change comes in, then the window pos listener runs, we might try to
// incorrectly reset the position without this check.
let foundMatch = false;
function geometryMatches() {
lazy.logger.trace(
`Checking window geometry ${win.outerWidth}x${win.outerHeight} @ (${win.screenX}, ${win.screenY})`
);
if (foundMatch) {
lazy.logger.trace(`Already found a previous match for this request`);
return true;
}
let sizeMatches = true;
let posMatches = true;
if (
width !== null &&
height !== null &&
(win.outerWidth !== width || win.outerHeight !== height)
) {
sizeMatches = false;
}
// Wayland doesn't support getting the window position.
if (
x !== null &&
y !== null &&
(win.screenX !== x || win.screenY !== y)
) {
if (lazy.AppInfo.isWayland) {
lazy.logger.info(
`Wayland doesn't support setting the window position`
);
} else {
posMatches = false;
}
}
if (sizeMatches && posMatches) {
lazy.logger.trace(`Requested window geometry matches`);
foundMatch = true;
return true;
}
return false;
}
if (!geometryMatches()) {
// There might be more than one resize or MozUpdateWindowPos event due
// to previous geometry changes, such as from restoreWindow(), so
// wait longer if window geometry does not match.
const options = {
checkFn: geometryMatches,
timeout: 500,
};
const promises = [];
if (width !== null && height !== null) {
promises.push(new lazy.EventPromise(win, "resize", options));
win.resizeTo(width, height);
}
// Wayland doesn't support setting the window position.
if (!lazy.AppInfo.isWayland && x !== null && y !== null) {
promises.push(
new lazy.EventPromise(win.windowRoot, "MozUpdateWindowPos", options)
);
win.moveTo(x, y);
}
try {
await Promise.race(promises);
} catch (e) {
if (e instanceof lazy.error.TimeoutError) {
// The operating system might not honor the move or resize, in which
// case assume that geometry will have been adjusted "as close as
// possible" to that requested. There may be no event received if the
// geometry is already as close as possible.
} else {
throw e;
}
}
}
}
/**
* Focus the specified window.
*
* @param {window} win
* The window to focus.
* @returns {Promise}
* A promise which is resolved when the window has been focused.
*/
async focusWindow(win) {
if (Services.focus.activeWindow != win) {
let activated = new lazy.EventPromise(win, "activate");
let focused = new lazy.EventPromise(win, "focus", { capture: true });
win.focus();
await Promise.all([activated, focused]);
}
}
/**
* Returns the window for a specific browsing context.
*
* @param {BrowsingContext} context
* The browsing context for which we want to retrieve the window.
*
* @returns {ChromeWindow}
* The chrome window associated with the browsing context.
*/
getWindowForBrowsingContext(context) {
return lazy.AppInfo.isAndroid
? context.top.embedderElement?.ownerGlobal
: context.topChromeWindow;
}
/**
* Open a new browser window.
*
* @param {object=} options
* @param {boolean=} options.focus
* If true, the opened window will receive the focus. Defaults to false.
* @param {boolean=} options.isPrivate
* If true, the opened window will be a private window. Defaults to false.
* @param {ChromeWindow=} options.openerWindow
* Use this window as the opener of the new window. Defaults to the
* topmost window.
* @param {string=} options.userContextId
* The id of the user context which should own the initial tab of the new
* window.
* @returns {Promise}
* A promise resolving to the newly created chrome window.
*/
async openBrowserWindow(options = {}) {
let {
focus = false,
isPrivate = false,
openerWindow = null,
userContextId = null,
} = options;
switch (lazy.AppInfo.name) {
case "Firefox": {
if (openerWindow === null) {
// If no opener was provided, fallback to the topmost window.
openerWindow = Services.wm.getMostRecentBrowserWindow();
}
if (!openerWindow) {
throw new lazy.error.UnsupportedOperationError(
`openWindow() could not find a valid opener window`
);
}
// 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.
const browser = await new Promise(resolveOnContentBrowserCreated =>
lazy.URILoadingHelper.openTrustedLinkIn(
openerWindow,
"about:blank",
"window",
{
private: isPrivate,
resolveOnContentBrowserCreated,
userContextId:
lazy.UserContextManager.getInternalIdById(userContextId),
}
)
);
// TODO: Both for WebDriver BiDi and classic, opening a new window
// should not run the focus steps. When focus is false we should avoid
// focusing the new window completely. See Bug 1766329
if (focus) {
// Focus the currently selected tab.
browser.focus();
} else {
// If the new window shouldn't get focused, set the
// focus back to the opening window.
await this.focusWindow(openerWindow);
}
return browser.ownerGlobal;
}
default:
throw new lazy.error.UnsupportedOperationError(
`openWindow() not supported in ${lazy.AppInfo.name}`
);
}
}
supportsWindows() {
return !lazy.AppInfo.isAndroid;
}
/**
* Minimize the specified window.
*
* @param {window} win
* The window to minimize.
*
* @returns {Promise}
* A promise resolved when the window is minimized, or times out if no window manager is present.
*/
async minimizeWindow(win) {
if (WindowState.from(win.windowState) != WindowState.Minimized) {
await waitForWindowState(win, () => win.minimize());
}
}
/**
* Maximize the specified window.
*
* @param {window} win
* The window to maximize.
*
* @returns {Promise}
* A promise resolved when the window is maximized, or times out if no window manager is present.
*/
async maximizeWindow(win) {
if (WindowState.from(win.windowState) != WindowState.Maximized) {
await waitForWindowState(win, () => win.maximize());
}
}
/**
* Restores the specified window to its normal state.
*
* @param {window} win
* The window to restore.
*
* @returns {Promise}
* A promise resolved when the window is restored, or times out if no window manager is present.
*/
async restoreWindow(win) {
if (WindowState.from(win.windowState) !== WindowState.Normal) {
await waitForWindowState(win, () => win.restore());
}
}
/**
* Sets the fullscreen state of the specified window.
*
* @param {window} win
* The target window.
* @param {boolean} enable
* Whether to enter fullscreen (true) or exit fullscreen (false).
*
* @returns {Promise}
* A promise resolved when the window enters or exits fullscreen mode.
*/
async setFullscreen(win, enable) {
const isFullscreen =
WindowState.from(win.windowState) === WindowState.Fullscreen;
if (enable !== isFullscreen) {
await waitForWindowState(win, () => (win.fullScreen = enable));
}
}
/**
* Wait until the initial application window has been opened and loaded.
*
* @returns {Promise<WindowProxy>}
* A promise that resolved to the application window.
*/
waitForInitialApplicationWindowLoaded() {
return new lazy.TimedPromise(
async resolve => {
// This call includes a fallback to "mail:3pane" as well.
const win = Services.wm.getMostRecentBrowserWindow();
const windowLoaded = lazy.waitForObserverTopic(
"browser-delayed-startup-finished",
{
checkFn: subject => (win !== null ? subject == win : true),
}
);
// The current window has already been finished loading.
if (win && win.document.readyState == "complete") {
resolve(win);
return;
}
// Wait for the next browser/mail window to open and finished loading.
const { subject } = await windowLoaded;
resolve(subject);
},
{
errorMessage: "No applicable application window found",
}
);
}
#onContextAttached = (_, data = {}) => {
const { browsingContext } = data;
const window = this.getWindowForBrowsingContext(browsingContext);
if (!window) {
// Short-lived iframes might already be disconnected from their parent
// window.
return;
}
this.#clientWindowIds.set(browsingContext, this.getIdForWindow(window));
};
}
// Expose a shared singleton.
export const windowManager = new WindowManager();
/**
* Representation of the {@link ChromeWindow} window state.
*
* @enum {string}
*/
export const WindowState = {
Maximized: "maximized",
Minimized: "minimized",
Normal: "normal",
Fullscreen: "fullscreen",
/**
* Converts {@link Window.windowState} to WindowState.
*
* @param {number} windowState
* Attribute from {@link Window.windowState}.
*
* @returns {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}`);
}
},
};
/**
* Waits for the window to reach a specific state after invoking a callback.
*
* @param {window} win
* The target window.
* @param {Function} callback
* The function to invoke to change the window state.
*
* @returns {Promise}
* A promise resolved when the window reaches the target state, or times out if no window manager is present.
*/
async function waitForWindowState(win, callback) {
let cb;
// Use a timed promise to abort if no window manager is present
await new lazy.TimedPromise(
resolve => {
cb = new lazy.DebounceCallback(resolve);
win.addEventListener("sizemodechange", cb);
callback();
},
{ throws: null, timeout: TIMEOUT_NO_WINDOW_MANAGER }
);
win.removeEventListener("sizemodechange", cb);
await new lazy.AnimationFramePromise(win);
}
|