Bug 1334266 - Add support for WebExtension APIs browser.sessions.{forgetClosedTab|forgetClosedWindow}. r=bsilverberg, r=mixedpuppy

This commit is contained in:
Thomas Wisniewski 2017-04-07 23:30:36 -04:00
Родитель 25d7f57213
Коммит 886e74cea3
5 изменённых файлов: 206 добавлений и 0 удалений

Просмотреть файл

@ -62,6 +62,37 @@ this.sessions = class extends ExtensionAPI {
return Promise.resolve(getRecentlyClosed(maxResults, extension));
},
forgetClosedTab: function(windowId, sessionId) {
let window = context.extension.windowManager.get(windowId).window;
let closedTabData = SessionStore.getClosedTabData(window, false);
let closedTabIndex = closedTabData.findIndex((closedTab) => {
return closedTab.closedId === parseInt(sessionId, 10);
});
if (closedTabIndex < 0) {
return Promise.reject({message: `Could not find closed tab using sessionId ${sessionId}.`});
}
SessionStore.forgetClosedTab(window, closedTabIndex);
return Promise.resolve();
},
forgetClosedWindow: function(sessionId) {
let closedWindowData = SessionStore.getClosedWindowData(false);
let closedWindowIndex = closedWindowData.findIndex((closedWindow) => {
return closedWindow.closedId === parseInt(sessionId, 10);
});
if (closedWindowIndex < 0) {
return Promise.reject({message: `Could not find closed window using sessionId ${sessionId}.`});
}
SessionStore.forgetClosedWindow(closedWindowIndex);
return Promise.resolve();
},
restore: function(sessionId) {
let session, closedId;
if (sessionId) {

Просмотреть файл

@ -55,6 +55,37 @@
}
],
"functions": [
{
"name": "forgetClosedTab",
"type": "function",
"description": "Forget a recently closed tab.",
"async": true,
"parameters": [
{
"name": "windowId",
"type": "integer",
"description": "The windowId of the window to which the recently closed tab to be forgotten belongs."
},
{
"name": "sessionId",
"type": "string",
"description": "The sessionId (closedId) of the recently closed tab to be forgotten."
}
]
},
{
"name": "forgetClosedWindow",
"type": "function",
"description": "Forget a recently closed window.",
"async": true,
"parameters": [
{
"name": "sessionId",
"type": "string",
"description": "The sessionId (closedId) of the recently closed window to be forgotten."
}
]
},
{
"name": "getRecentlyClosed",
"type": "function",

Просмотреть файл

@ -82,6 +82,8 @@ support-files =
[browser_ext_runtime_openOptionsPage.js]
[browser_ext_runtime_openOptionsPage_uninstall.js]
[browser_ext_runtime_setUninstallURL.js]
[browser_ext_sessions_forgetClosedTab.js]
[browser_ext_sessions_forgetClosedWindow.js]
[browser_ext_sessions_getRecentlyClosed.js]
[browser_ext_sessions_getRecentlyClosed_private.js]
[browser_ext_sessions_getRecentlyClosed_tabs.js]

Просмотреть файл

@ -0,0 +1,70 @@
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
add_task(function* test_sessions_forget_closed_tab() {
function background() {
browser.test.onMessage.addListener((msg, windowId, sessionId) => {
if (msg === "check-sessions") {
browser.sessions.getRecentlyClosed().then(recentlyClosed => {
browser.test.sendMessage("recentlyClosed", recentlyClosed);
});
} else if (msg === "forget-tab") {
browser.sessions.forgetClosedTab(windowId, sessionId).then(
() => {
browser.test.sendMessage("forgot-tab");
},
error => {
browser.test.assertEq(error.message,
`Could not find closed tab using sessionId ${sessionId}.`);
browser.test.sendMessage("forget-reject");
}
);
}
});
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["sessions", "tabs"],
},
background,
});
yield extension.startup();
let tabUrl = "http://example.com";
let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, tabUrl);
yield BrowserTestUtils.removeTab(tab);
tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, tabUrl);
yield BrowserTestUtils.removeTab(tab);
extension.sendMessage("check-sessions");
let recentlyClosed = yield extension.awaitMessage("recentlyClosed");
let recentlyClosedLength = recentlyClosed.length;
let recentlyClosedTab = recentlyClosed[0].tab;
// Check that forgetting a tab works properly
extension.sendMessage("forget-tab", recentlyClosedTab.windowId,
recentlyClosedTab.sessionId);
yield extension.awaitMessage("forgot-tab");
extension.sendMessage("check-sessions");
let remainingClosed = yield extension.awaitMessage("recentlyClosed");
is(remainingClosed.length, recentlyClosedLength - 1,
"One tab was forgotten.");
is(remainingClosed[0].tab.sessionId, recentlyClosed[1].tab.sessionId,
"The correct tab was forgotten.");
// Check that re-forgetting the same tab fails properly
extension.sendMessage("forget-tab", recentlyClosedTab.windowId,
recentlyClosedTab.sessionId);
yield extension.awaitMessage("forget-reject");
extension.sendMessage("check-sessions");
remainingClosed = yield extension.awaitMessage("recentlyClosed");
is(remainingClosed.length, recentlyClosedLength - 1,
"No extra tab was forgotten.");
is(remainingClosed[0].tab.sessionId, recentlyClosed[1].tab.sessionId,
"The correct tab remains.");
yield extension.unload();
});

Просмотреть файл

@ -0,0 +1,72 @@
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
add_task(function* test_sessions_forget_closed_window() {
function* openAndCloseWindow(url = "http://example.com") {
let win = yield BrowserTestUtils.openNewBrowserWindow();
yield BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, url);
yield BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
yield BrowserTestUtils.closeWindow(win);
}
function background() {
browser.test.onMessage.addListener((msg, sessionId) => {
if (msg === "check-sessions") {
browser.sessions.getRecentlyClosed().then(recentlyClosed => {
browser.test.sendMessage("recentlyClosed", recentlyClosed);
});
} else if (msg === "forget-window") {
browser.sessions.forgetClosedWindow(sessionId).then(
() => {
browser.test.sendMessage("forgot-window");
},
error => {
browser.test.assertEq(error.message,
`Could not find closed window using sessionId ${sessionId}.`);
browser.test.sendMessage("forget-reject");
}
);
}
});
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["sessions", "tabs"],
},
background,
});
yield extension.startup();
yield openAndCloseWindow("about:config");
yield openAndCloseWindow("about:robots");
extension.sendMessage("check-sessions");
let recentlyClosed = yield extension.awaitMessage("recentlyClosed");
let recentlyClosedLength = recentlyClosed.length;
let recentlyClosedWindow = recentlyClosed[0].window;
// Check that forgetting a window works properly
extension.sendMessage("forget-window", recentlyClosedWindow.sessionId);
yield extension.awaitMessage("forgot-window");
extension.sendMessage("check-sessions");
let remainingClosed = yield extension.awaitMessage("recentlyClosed");
is(remainingClosed.length, recentlyClosedLength - 1,
"One window was forgotten.");
is(remainingClosed[0].window.sessionId, recentlyClosed[1].window.sessionId,
"The correct window was forgotten.");
// Check that re-forgetting the same window fails properly
extension.sendMessage("forget-window", recentlyClosedWindow.sessionId);
yield extension.awaitMessage("forget-reject");
extension.sendMessage("check-sessions");
remainingClosed = yield extension.awaitMessage("recentlyClosed");
is(remainingClosed.length, recentlyClosedLength - 1,
"No extra window was forgotten.");
is(remainingClosed[0].window.sessionId, recentlyClosed[1].window.sessionId,
"The correct window remains.");
yield extension.unload();
});