BrowserWindowTracker
BrowserWindowTracker, in browser/modules/BrowserWindowTracker.sys.mjs, keeps
the list of open browser windows in the order the user last activated them, and
opens new ones. Chrome code that runs outside a browser window uses it to find a
window to act on:
let win =
lazy.BrowserWindowTracker.getTopWindow() ??
(await lazy.BrowserWindowTracker.promiseOpenWindow());
win.openTrustedLinkIn(url, "tab");
Every window that loads browser.xhtml is tracked from its DOMContentLoaded,
through the browser-window-domcontentloaded category entry described in
Browser Startup, until it unloads. Other chrome windows, such as the
Library, are not tracked.
Finding a window
getTopWindow() returns the most recently activated browser window that is fit
to open a tab in. It skips some windows by default and takes options to let them
back in:
Window |
By default |
Option |
|---|---|---|
Popup, i.e. a window opened without a toolbar |
skipped |
|
Taskbar tab |
skipped |
|
On another virtual desktop, on Windows |
skipped |
|
Mini window |
skipped |
none |
Minimized |
returned only if no window that isn’t minimized qualifies, but ahead of a window on another desktop |
none |
The private option works the other way and narrows the search: true returns
only a private window and false only a non-private one. Leave it out to accept
either. Under permanent private browsing the option is ignored, so
private: false returns a private window, since every window is one.
A window from another virtual desktop is returned only when no window on the
current desktop qualifies, even with allowFromInactiveWorkspace, so that
opening a tab doesn’t switch desktops. Without that option, a Windows user with
every window on another desktop gets null. Setting
widget.prefer_windows_on_current_virtual_desktop to false turns the desktop
check off.
getTopWindow() returns null whenever no window passes the filters, which
includes every macOS session with all its windows closed. Handle null.
Why not Services.wm.getMostRecentWindow
Services.wm.getMostRecentWindow("navigator:browser") looks equivalent and is
not. The window mediator stamps a window when it registers and each time it is
shown or hidden, never when it is activated, so “most recent” means most
recently opened, not the window the user is in. The mediator also applies none
of the filters above, and returns a popup as readily as a normal window. Its
z-order enumerator returns no windows at all
(bug 1214585).
BrowserWindowTracker keeps its own order by listening for activate, which is
what makes it right. Use getTopWindow() in new code;
bug 1455349 migrates the
existing callers.
Listing windows
getOrderedWindows(), and the orderedWindows getter that calls it with no
options, return every tracked window, most recently activated first. The JSDoc
calls this z-order, but it is activation order, and the two diverge whenever the
user moves a window without activating it.
The list applies none of getTopWindow()’s filters, so popups and taskbar tabs
are in it. Minimized windows are moved to the back when the list is read, not
when the order is recorded: macOS fires activate while a window is still
minimized (bug 2007691),
so a filter applied on write would misplace the window being restored.
getOrderedWindows({ private }) filters by privateness. Under permanent private
browsing private: true returns every window and private: false returns none.
windowCount counts every tracked window, popups included.
Opening a window
openWindow() opens a browser window and returns it at once.
promiseOpenWindow() takes the same options and resolves once the window has
fired browser-delayed-startup-finished. Prefer it: the window openWindow()
returns has no gBrowser until its document has loaded, and has not started
loading its tabs until delayed startup.
Option |
Effect |
|---|---|
|
Opens a private window, or a normal one where private browsing is disabled. Defaults to |
|
What the new window loads; see below. Defaults to the home page, or |
|
The window to open from. |
|
Window features appended to the defaults, as a comma-separated string. |
|
Whether the default features include |
|
Override the profile’s default for the window. |
|
Opens an AI window. Left out, a non-private window opened from an AI window is one too. |
|
For an AI window that session restore reopens, the URL of its selected tab. |
A window that is still opening
A window is tracked from its DOMContentLoaded, so until then getTopWindow()
cannot see it. Code that runs early in startup, such as a command-line handler,
can find no top window while the first window is on its way, and open a second.
getPendingWindow() closes that gap. It returns a promise for a window that
openWindow() has opened and that is not tracked yet, resolving once the window
finishes delayed startup, or null when there is no such window:
let win = lazy.BrowserWindowTracker.getTopWindow();
if (!win) {
let pending = lazy.BrowserWindowTracker.getPendingWindow();
win = pending
? await pending
: await lazy.BrowserWindowTracker.promiseOpenWindow();
}
A window that closes before it finishes delayed startup resolves the promise
with the closed window, so check win.closed. A window opened some other way
is pending only if its opener passed it to registerOpeningWindow().
The two calls leave a window in between: from DOMContentLoaded until delayed
startup finishes, getTopWindow() returns a window whose tabs have not started
loading. Where that matters, wait for the window’s delayedStartupPromise.
window.arguments
openWindow() hands args to the new window as window.arguments, where
gBrowserInit reads them. An nsIArray is spread across the indexes, and
anything else becomes index 0, so several URLs take an nsIArray with another
nsIArray as its first element. For an AI window, pass an nsIMutableArray
or leave args out, since its options are added at index 1.
Index |
Holds |
|---|---|
0 |
The URL to load, as a string or an |
1 |
An |
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
An |
With fewer than three arguments, a string at index 0 may hold several URLs
separated by |, as the home page does. With three or more it is a single URL,
and with an nsIArray only indexes 5 and 8 to 10 apply.
A tab, split view or tab group at index 0 is how gBrowser.replaceTabWithWindow
moves one into a window of its own. The new window adopts it in place of its
initial tab, and no URL is loaded. gBrowserInit.getTabToAdopt() returns it
and clears index 0, so later readers find null there.
What the tracker does besides
The tracker tells the network stack which browser is selected in the most
recently activated window that isn’t minimized, with the
net:current-browser-id notification. The network stack gives that tab’s
requests priority over background tabs’.
getAllVisibleTabs() and getBrowserById() have no callers in product
JavaScript. They implement nsIBrowserWindowTracker for C++: WebRTC lists the
tabs a user can share with the first, and Places reads the search engine and
sponsored-link attributes a browser holds for a visit with the second. Both skip
hidden tabs and lazy tabs.
API reference
Generated from the JSDoc in BrowserWindowTracker.sys.mjs.
- static BrowserWindowTracker.getTopWindow(options)
Get the most recent browser window. Note that with the default options this may return null on Windows if there are no open windows in the current virtual desktop. To prevent this, set
options.allowFromInactiveWorkspaceto true.- Arguments:
options (object) – An object accepting the arguments for the search.
options.private (boolean) – true to only search for private windows. false to restrict the search to non-private windows. If the property is not provided, search for either. If permanent private browsing is enabled this option will be ignored!
options.allowPopups (boolean) – true if popup windows are permitted.
options.allowTaskbarTabs (boolean) – true if taskbar tab windows are permitted.
options.allowFromInactiveWorkspace (boolean) – true if window is allowed to be from a different virtual desktop (what Windows calls workspaces). Only has an effect on Windows.
- Returns:
Window|null – The current top/selected window. Can return null on MacOS when there is no open window.
- static BrowserWindowTracker.getOrderedWindows(options)
Array of browser windows, most recently activated first, with minimized windows moved to the end.
- Arguments:
options (object)
options.private (boolean) – If set, returns only windows with the specified privateness, i.e.
truereturns only private windows. If omitted, returns all windows.
- Returns:
Array.<BrowserWindow>
- static BrowserWindowTracker.openWindow(options)
A standard function for opening a new browser window.
- Arguments:
options (object) – Options for the new window.
options.openerWindow (Window) – An existing browser window to open the new one from.
options.private (boolean) – True to make the window a private browsing window.
options.aiWindow (boolean) – True to make the window an AI browsing window.
options.restoreSessionURL (string) – For an AI window that session restore is reopening, the URL of its selected tab. The window then skips its start page, and opens in immersive view if that URL calls for it.
options.features (string) – Additional window features to give the new window.
options.all (boolean) – True if “all” should be included as a window feature. If omitted, defaults to true.
options.args (nsIArray|nsISupportsString) – Arguments to pass to the new window.
options.remote (boolean) – A boolean indicating if the window should run remote browser tabs or not. If omitted, the window will choose the profile default state.
options.fission (boolean) – A boolean indicating if the window should run with fission enabled or not. If omitted, the window will choose the profile default state.
- Returns:
Window
- static BrowserWindowTracker.promiseOpenWindow(options)
Async version of
openWindowwaiting for delayed startup of the new window before returning.- Arguments:
options (object) – Options for the new window. See
openWindowfor details.
- Returns:
Window
- static BrowserWindowTracker.getPendingWindow(options)
Get a window that is in the process of loading. Only supports windows opened via the
openWindowfunction in this module or that have been registered with theregisterOpeningWindowfunction.- Arguments:
options (object) – Options for the search.
options.private (boolean) – true to restrict the search to private windows only, false to restrict the search to non-private only. Omit the property to search in both groups.
- Returns:
Promise.<Window>|null
- static BrowserWindowTracker.registerOpeningWindow(window, isPrivate)
Registers a browser window that is in the process of opening. Normally it would be preferable to use the standard method for opening the window from this module.
- Arguments:
window (Window) – The opening window.
isPrivate (boolean) – Whether the opening window is a private browsing window.