From 6c0172f04991dcae66d3d0a5b3fa5fcc842d4e53 Mon Sep 17 00:00:00 2001 From: Greg Stoll Date: Thu, 21 Nov 2024 12:34:15 +0000 Subject: [PATCH] Bug 1927609 - correctly set process priority when rapidly switching tabs r=tabbrowser-reviewers,dao Differential Revision: https://phabricator.services.mozilla.com/D229696 --- .../tabbrowser/AsyncTabSwitcher.sys.mjs | 13 ++++++- .../tests/browser_ProcessPriorityManager.js | 39 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/browser/components/tabbrowser/AsyncTabSwitcher.sys.mjs b/browser/components/tabbrowser/AsyncTabSwitcher.sys.mjs index c8e246184314..234eae789110 100644 --- a/browser/components/tabbrowser/AsyncTabSwitcher.sys.mjs +++ b/browser/components/tabbrowser/AsyncTabSwitcher.sys.mjs @@ -928,8 +928,8 @@ export class AsyncTabSwitcher { } /** - * Check if the browser should be deactivated. If the browser is a print preivew or - * PiP browser then we won't deactive it. + * Check if the browser should be deactivated. If the browser is a print preview or + * PiP browser then we won't deactivate it. * @param browser The browser to check if it should be deactivated * @returns false if a print preview or PiP browser else true */ @@ -1061,6 +1061,15 @@ export class AsyncTabSwitcher { this.requestedTab = tab; if (tabState == this.STATE_LOADED) { this.maybeVisibleTabs.clear(); + // We're switching to a tab that is still loaded. + // Make sure its priority is correct as it may + // have been deprioritized when it was switched + // away from (bug 1927609) + let browser = tab.linkedBrowser; + let remoteTab = browser.frameLoader?.remoteTab; + if (remoteTab) { + remoteTab.priorityHint = true; + } } tab.linkedBrowser.setAttribute("primary", "true"); diff --git a/dom/ipc/tests/browser_ProcessPriorityManager.js b/dom/ipc/tests/browser_ProcessPriorityManager.js index df0f80a93f73..f4e2b5e2c577 100644 --- a/dom/ipc/tests/browser_ProcessPriorityManager.js +++ b/dom/ipc/tests/browser_ProcessPriorityManager.js @@ -993,3 +993,42 @@ add_task(async function test_tab_moved_to_new_window() { } ); }); + +/** + * Test that if a tab is quickly switched away from and back to, it ends up at + * PROCESS_PRIORITY_FOREGROUND. + * See bug 1927609. + */ +add_task(async function test_tab_quickly_switched() { + let originalTab = gBrowser.selectedTab; + let origtabID = browsingContextChildID( + originalTab.linkedBrowser.browsingContext + ); + + await BrowserTestUtils.withNewTab( + "https://example.com/browser/dom/ipc/tests/file_dummy.html", + async browser => { + let tab = gBrowser.getTabForBrowser(browser); + let tabID = browsingContextChildID(tab.linkedBrowser.browsingContext); + + // Don't use BrowserTestUtils.switchTab() because things have settled + // by the time it's done, which doesn't expose this bug. + gBrowser.selectedTab = originalTab; + gBrowser.selectedTab = tab; + await new Promise(resolve => + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + setTimeout(resolve, WAIT_FOR_CHANGE_TIME_MS) + ); + Assert.equal( + gTabPriorityWatcher.currentPriority(tabID), + PROCESS_PRIORITY_FOREGROUND, + "Active tab should be foreground priority" + ); + Assert.equal( + gTabPriorityWatcher.currentPriority(origtabID), + PROCESS_PRIORITY_BACKGROUND, + "Inactive tab should be background priority" + ); + } + ); +});