Bug 1758502 - [webdriver-bidi] Implement the browsingContext.close command r=webdriver-reviewers,whimboo

Depends on D141944

Differential Revision: https://phabricator.services.mozilla.com/D141678
This commit is contained in:
Julian Descottes 2022-03-27 08:58:11 +00:00
parent 45df9472d7
commit 591076c81b

View File

@ -17,12 +17,17 @@ XPCOMUtils.defineLazyModuleGetters(this, {
ContextDescriptorType:
"chrome://remote/content/shared/messagehandler/MessageHandler.jsm",
error: "chrome://remote/content/shared/webdriver/Errors.jsm",
Log: "chrome://remote/content/shared/Log.jsm",
Module: "chrome://remote/content/shared/messagehandler/Module.jsm",
TabManager: "chrome://remote/content/shared/TabManager.jsm",
waitForInitialNavigationCompleted:
"chrome://remote/content/shared/Navigate.jsm",
});
XPCOMUtils.defineLazyGetter(this, "logger", () =>
Log.get(Log.TYPES.WEBDRIVER_BIDI)
);
class BrowsingContextModule extends Module {
#contextListener;
@ -45,6 +50,53 @@ class BrowsingContextModule extends Module {
this.#contextListener.destroy();
}
/**
* Close the provided browsing context.
*
* @param {Object=} options
* @param {string} context
* Id of the browsing context to close.
*
* @throws {NoSuchFrameError}
* If the browsing context cannot be found.
* @throws {InvalidArgumentError}
* If the browsing context is not a top-level one.
*/
close(options = {}) {
const { context: contextId } = options;
assert.string(
contextId,
`Expected "context" to be a string, got ${contextId}`
);
const context = TabManager.getBrowsingContextById(contextId);
if (!context) {
throw new error.NoSuchFrameError(
`Browsing Context with id ${contextId} not found`
);
}
if (context.parent) {
throw new error.InvalidArgumentError(
`Browsing Context with id ${contextId} is not top-level`
);
}
if (TabManager.getTabCount() === 1) {
// The behavior when closing the last tab is currently unspecified.
// Warn the consumer about potential issues
logger.warn(
`Closing the last open tab (Browsing Context id ${contextId}), expect inconsistent behavior across platforms`
);
}
const browser = context.embedderElement;
const tabBrowser = TabManager.getTabBrowser(browser.ownerGlobal);
const tab = tabBrowser.getTabForBrowser(browser);
TabManager.removeTab(tab);
}
/**
* An object that holds the WebDriver Bidi browsing context information.
*