diff --git a/remote/shared/TabManager.sys.mjs b/remote/shared/TabManager.sys.mjs index c9dadcb33abd..7b592b611545 100644 --- a/remote/shared/TabManager.sys.mjs +++ b/remote/shared/TabManager.sys.mjs @@ -267,6 +267,25 @@ export var TabManager = { return count; }, + /** + * Retrieve the tab owning a Browsing Context. + * + * @param {BrowsingContext=} browsingContext + * The browsing context to get the tab from. + * + * @returns {Tab|null} + * The tab owning the Browsing Context. + */ + getTabForBrowsingContext(browsingContext) { + const browser = browsingContext?.top.embedderElement; + if (!browser) { + return null; + } + + const tabBrowser = this.getTabBrowser(browser.ownerGlobal); + return tabBrowser.getTabForBrowser(browser); + }, + /** * Remove the given tab. * diff --git a/remote/shared/test/browser/browser_TabManager.js b/remote/shared/test/browser/browser_TabManager.js index 2affd9336873..587b10fe5125 100644 --- a/remote/shared/test/browser/browser_TabManager.js +++ b/remote/shared/test/browser/browser_TabManager.js @@ -127,3 +127,22 @@ add_task(async function test_addTab_window() { await BrowserTestUtils.closeWindow(win2); } }); + +add_task(async function test_getTabForBrowsingContext() { + const tab = await TabManager.addTab(); + try { + const browser = tab.linkedBrowser; + + info(`Navigate to ${TEST_URL}`); + const loaded = BrowserTestUtils.browserLoaded(browser); + BrowserTestUtils.loadURI(browser, TEST_URL); + await loaded; + + const contexts = browser.browsingContext.getAllBrowsingContextsInSubtree(); + is(TabManager.getTabForBrowsingContext(contexts[0]), tab); + is(TabManager.getTabForBrowsingContext(contexts[1]), tab); + is(TabManager.getTabForBrowsingContext(null), null); + } finally { + gBrowser.removeTab(tab); + } +}); diff --git a/remote/webdriver-bidi/modules/root/browsingContext.sys.mjs b/remote/webdriver-bidi/modules/root/browsingContext.sys.mjs index 314dd800d6f8..a6ce389a07a6 100644 --- a/remote/webdriver-bidi/modules/root/browsingContext.sys.mjs +++ b/remote/webdriver-bidi/modules/root/browsingContext.sys.mjs @@ -123,10 +123,7 @@ class BrowsingContextModule extends Module { ); } - const browser = context.embedderElement; - const tabBrowser = lazy.TabManager.getTabBrowser(browser.ownerGlobal); - const tab = tabBrowser.getTabForBrowser(browser); - + const tab = lazy.TabManager.getTabForBrowsingContext(context); await lazy.TabManager.removeTab(tab); }