From 6b6b1a4bd332d7fca120de9c5c186b03012b8a4f Mon Sep 17 00:00:00 2001 From: Temidayo Date: Mon, 2 Dec 2024 13:40:06 +0000 Subject: [PATCH] Bug 1761445 - TabManager.getBrowsingContextById should not return closed browsing contexts. r=jdescottes,webdriver-reviewers Differential Revision: https://phabricator.services.mozilla.com/D225303 --- remote/shared/TabManager.sys.mjs | 13 +++++++++--- .../shared/test/browser/browser_TabManager.js | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/remote/shared/TabManager.sys.mjs b/remote/shared/TabManager.sys.mjs index 89377e833944..b7b7975d2e43 100644 --- a/remote/shared/TabManager.sys.mjs +++ b/remote/shared/TabManager.sys.mjs @@ -231,15 +231,22 @@ class TabManagerClass { * @param {string} id * A browsing context unique id (created by getIdForBrowsingContext). * @returns {BrowsingContext=} - * The browsing context found for this id, null if none was found. + * The browsing context found for this id, null if none was found or + * browsing context is discarded. */ getBrowsingContextById(id) { const browser = this.getBrowserById(id); + let browsingContext; if (browser) { - return browser.browsingContext; + browsingContext = browser.browsingContext; + } else { + browsingContext = BrowsingContext.get(id); } - return BrowsingContext.get(id); + if (!browsingContext || browsingContext.isDiscarded) { + return null; + } + return browsingContext; } /** diff --git a/remote/shared/test/browser/browser_TabManager.js b/remote/shared/test/browser/browser_TabManager.js index fae2ea953330..a4d5d112f705 100644 --- a/remote/shared/test/browser/browser_TabManager.js +++ b/remote/shared/test/browser/browser_TabManager.js @@ -144,6 +144,27 @@ add_task(async function test_getBrowsingContextById() { is(TabManager.getBrowsingContextById(childContextId), contexts[1]); }); +add_task(async function test_getDiscardedBrowsingContextById() { + const tab = await TabManager.addTab(); + const browser = tab.linkedBrowser; + const browsingContext = browser.browsingContext; + const contextId = TabManager.getIdForBrowsingContext(browsingContext); + + is( + TabManager.getBrowsingContextById(contextId), + browsingContext, + "Browsing context is accessible by its ID" + ); + + gBrowser.removeTab(tab); + + is( + TabManager.getBrowsingContextById(contextId), + null, + "Browsing context is no longer accessible after the tab is removed" + ); +}); + add_task(async function test_getIdForBrowsingContext() { const browser = gBrowser.selectedBrowser;