Bug 1862936 - Add a flag to removeAllTabsBut for closing pinned and selected tabs. r=tabbrowser-reviewers,dao

Differential Revision: https://phabricator.services.mozilla.com/D193070
This commit is contained in:
Paul Zuehlcke 2023-11-09 18:04:05 +00:00
parent 36497ee57a
commit d8261b5979
3 changed files with 104 additions and 28 deletions

View File

@ -3558,29 +3558,42 @@
},
/**
* In a multi-select context, all unpinned and unselected tabs are removed.
* Otherwise all unpinned tabs except aTab are removed.
* Remove all tabs but aTab. By default, in a multi-select context, all
* unpinned and unselected tabs are removed. Otherwise all unpinned tabs
* except aTab are removed. This behavior can be changed using the the bool
* flags below.
*
* @param aTab
* The tab we will skip removing
* @param aParams
* An optional set of parameters that will be passed to the
* @param aTab The tab we will skip removing
* @param aParams An optional set of parameters that will be passed to the
* removeTabs function.
* @param {boolean} [skipWarnAboutClosingTabs=false]
* Whether to skip the tab close warning prompt.mach
* @param {boolean} [aParams.skipWarnAboutClosingTabs=false] Skip showing
* the tab close warning prompt.
* @param {boolean} [aParams.skipPinnedOrSelectedTabs=true] Skip closing
* tabs that are selected or pinned.
*/
removeAllTabsBut(aTab, aParams, skipWarnAboutClosingTabs = false) {
let tabsToRemove = [];
if (aTab && aTab.multiselected) {
tabsToRemove = this.visibleTabs.filter(
tab => !tab.multiselected && !tab.pinned
);
removeAllTabsBut(aTab, aParams = {}) {
let {
skipWarnAboutClosingTabs = false,
skipPinnedOrSelectedTabs = true,
} = aParams;
let filterFn;
// If enabled also filter by selected or pinned state.
if (skipPinnedOrSelectedTabs) {
if (aTab?.multiselected) {
filterFn = tab => !tab.multiselected && !tab.pinned;
} else {
filterFn = tab => tab != aTab && !tab.pinned;
}
} else {
tabsToRemove = this.visibleTabs.filter(
tab => tab != aTab && !tab.pinned
);
// Exclude just aTab from being removed.
filterFn = tab => tab != aTab;
}
let tabsToRemove = this.visibleTabs.filter(filterFn);
// If enabled show the tab close warning.
if (
!skipWarnAboutClosingTabs &&
!this.warnAboutClosingTabs(

View File

@ -179,17 +179,15 @@ export const ResetPBMPanel = {
}
// 3. Close all other tabs.
triggeringWindow.gBrowser.removeAllTabsBut(
newTab,
{
skipPermitUnload: true,
// Instruct the SessionStore to not save closed tab data for these tabs.
// We don't want to leak them into the next private browsing session.
skipSessionStore: true,
animate: false,
},
true
);
triggeringWindow.gBrowser.removeAllTabsBut(newTab, {
skipPermitUnload: true,
// Instruct the SessionStore to not save closed tab data for these tabs.
// We don't want to leak them into the next private browsing session.
skipSessionStore: true,
animate: false,
skipWarnAboutClosingTabs: true,
skipPinnedOrSelectedTabs: false,
});
// In the remaining PBM window: If the sidebar is open close it.
triggeringWindow.SidebarUI?.hide();

View File

@ -757,3 +757,68 @@ add_task(async function test_reset_action_purges_session_store() {
// Cleanup: Close the private window that remained open.
await BrowserTestUtils.closeWindow(win);
});
/**
* Test that the the reset action closes all tabs, including pinned and (multi-)selected
* tabs.
*/
add_task(async function test_reset_action_closes_pinned_and_selected_tabs() {
await SpecialPowers.pushPrefEnv({
set: [["browser.privatebrowsing.resetPBM.enabled", true]],
});
info("Open a private browsing window.");
let win = await BrowserTestUtils.openNewBrowserWindow({
private: true,
});
info("Load a list of tabs.");
let loadPromises = [
"https://example.com",
"https://example.org",
"https://example.net",
"about:blank",
].map(async url => {
let tab = BrowserTestUtils.addTab(win.gBrowser, url);
await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
return tab;
});
let tabs = await Promise.all(loadPromises);
info("Pin a tab");
win.gBrowser.pinTab(tabs[0]);
info("Multi-select tabs.");
await BrowserTestUtils.switchTab(win.gBrowser, tabs[2]);
win.gBrowser.addToMultiSelectedTabs(tabs[3]);
// Create promises for tab close for all tabs in the triggering private browsing window.
let promisesTabsClosed = win.gBrowser.tabs.map(tab =>
BrowserTestUtils.waitForTabClosing(tab)
);
info("Trigger the restart PBM action");
await ResetPBMPanel._restartPBM(win);
info("Wait for all tabs to be closed.");
await promisesTabsClosed;
Assert.equal(
win.gBrowser.tabs.length,
1,
"Should only have 1 tab remaining."
);
await BrowserTestUtils.waitForCondition(
() =>
win.gBrowser.selectedBrowser.currentURI.spec == "about:privatebrowsing"
);
Assert.equal(
win.gBrowser.selectedBrowser.currentURI.spec,
"about:privatebrowsing",
"The remaining tab should point to about:privatebrowsing."
);
// Cleanup: Close the private window/
await BrowserTestUtils.closeWindow(win);
});