Bug 1375546 - Fix sessions.getRecentlyClosed to consider the current tab history index, r?mixedpuppy r?mikedeboer
If the back button is used to navigate a tab backwards in its history, and then the tab is closed,
the current implementation of sessions.getRecentlyClosed does not take this into account and
just uses the last item in the tab history for the values returned for the tab. This patch fixes
this by using the values for the current position of the tab in its history.
MozReview-Commit-ID: LcgtA5FqVWi
--- a/browser/components/extensions/ext-browser.js
+++ b/browser/components/extensions/ext-browser.js
@@ -660,17 +660,20 @@ class Tab extends TabBase {
active: false,
pinned: false,
incognito: Boolean(tabData.state && tabData.state.isPrivate),
lastAccessed: tabData.state ? tabData.state.lastAccessed : tabData.lastAccessed,
};
if (extension.tabManager.hasTabPermission(tabData)) {
let entries = tabData.state ? tabData.state.entries : tabData.entries;
- let entry = entries[entries.length - 1];
+ let lastTabIndex = tabData.state ? tabData.state.index : tabData.index;
+ // We need to take lastTabIndex - 1 because the index in the tab data is
+ // 1-based rather than 0-based.
+ let entry = entries[lastTabIndex - 1];
result.url = entry.url;
result.title = entry.title;
if (tabData.image) {
result.favIconUrl = tabData.image;
}
}
return result;
--- a/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_tabs.js
+++ b/browser/components/extensions/test/browser/browser_ext_sessions_getRecentlyClosed_tabs.js
@@ -33,18 +33,29 @@ add_task(async function test_sessions_ge
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["sessions", "tabs"],
},
background,
});
let win = await BrowserTestUtils.openNewBrowserWindow();
- await BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, "about:mozilla");
- await BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
+ let tabBrowser = win.gBrowser.selectedBrowser;
+ for (let url of ["about:robots", "about:mozilla", "about:config"]) {
+ await BrowserTestUtils.loadURI(tabBrowser, url);
+ await BrowserTestUtils.browserLoaded(tabBrowser, false, url);
+ }
+
+ // Ensure that getRecentlyClosed returns correct results after the back
+ // button has been used.
+ let goBackPromise = BrowserTestUtils.waitForLocationChange(
+ win.gBrowser, "about:mozilla");
+ tabBrowser.goBack();
+ await goBackPromise;
+
let expectedTabs = [];
let tab = win.gBrowser.selectedTab;
expectedTabs.push(expectedTabInfo(tab, win));
let lastAccessedTimes = new Map();
lastAccessedTimes.set("about:mozilla", tab.lastAccessed);
for (let url of ["about:robots", "about:buildconfig"]) {
tab = await BrowserTestUtils.openNewForegroundTab(win.gBrowser, url);