From 3f4bce8633d8f79dd138573626d178fc4b4d8e89 Mon Sep 17 00:00:00 2001 From: Bob Silverberg Date: Fri, 14 Oct 2016 07:12:18 -0400 Subject: [PATCH] Bug 1309702 - Update getClosedTabData and getClosedWindowData in SessionStore.jsm to not return JSON, r=mikedeboer MozReview-Commit-ID: 9SevLkTp0G7 --HG-- extra : rebase_source : 0a13bf3ed4afcedadd6aacb6c14b08a5c2bb406d --- .../RecentlyClosedTabsAndWindowsMenuUtils.jsm | 9 +++--- .../components/sessionstore/SessionStore.jsm | 20 +++++++------ .../sessionstore/test/browser_394759_basic.js | 24 ++++++++++------ .../sessionstore/test/browser_461634.js | 28 +++++++++++++------ 4 files changed, 52 insertions(+), 29 deletions(-) diff --git a/browser/components/sessionstore/RecentlyClosedTabsAndWindowsMenuUtils.jsm b/browser/components/sessionstore/RecentlyClosedTabsAndWindowsMenuUtils.jsm index c83ed09c02b2..cbf809a868f1 100644 --- a/browser/components/sessionstore/RecentlyClosedTabsAndWindowsMenuUtils.jsm +++ b/browser/components/sessionstore/RecentlyClosedTabsAndWindowsMenuUtils.jsm @@ -17,9 +17,10 @@ Cu.import("resource://gre/modules/PlacesUtils.jsm"); XPCOMUtils.defineLazyModuleGetter(this, "PluralForm", "resource://gre/modules/PluralForm.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "SessionStore", + "resource:///modules/sessionstore/SessionStore.jsm"); var navigatorBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); -var ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore); this.RecentlyClosedTabsAndWindowsMenuUtils = { @@ -40,8 +41,8 @@ this.RecentlyClosedTabsAndWindowsMenuUtils = { aRestoreAllLabel="menuRestoreAllTabs.label") { let doc = aWindow.document; let fragment = doc.createDocumentFragment(); - if (ss.getClosedTabCount(aWindow) != 0) { - let closedTabs = JSON.parse(ss.getClosedTabData(aWindow)); + if (SessionStore.getClosedTabCount(aWindow) != 0) { + let closedTabs = SessionStore.getClosedTabData(aWindow, false); for (let i = 0; i < closedTabs.length; i++) { let element = doc.createElementNS(kNSXUL, aTagName); element.setAttribute("label", closedTabs[i].title); @@ -99,7 +100,7 @@ this.RecentlyClosedTabsAndWindowsMenuUtils = { */ getWindowsFragment: function(aWindow, aTagName, aPrefixRestoreAll=false, aRestoreAllLabel="menuRestoreAllWindows.label") { - let closedWindowData = JSON.parse(ss.getClosedWindowData()); + let closedWindowData = SessionStore.getClosedWindowData(false); let fragment = aWindow.document.createDocumentFragment(); if (closedWindowData.length != 0) { let menuLabelString = navigatorBundle.GetStringFromName("menuUndoCloseWindowLabel"); diff --git a/browser/components/sessionstore/SessionStore.jsm b/browser/components/sessionstore/SessionStore.jsm index aaa0e304f2e7..793f3bd22415 100644 --- a/browser/components/sessionstore/SessionStore.jsm +++ b/browser/components/sessionstore/SessionStore.jsm @@ -246,8 +246,8 @@ this.SessionStore = { return SessionStoreInternal.getClosedTabCount(aWindow); }, - getClosedTabData: function ss_getClosedTabDataAt(aWindow) { - return SessionStoreInternal.getClosedTabData(aWindow); + getClosedTabData: function ss_getClosedTabData(aWindow, aAsString = true) { + return SessionStoreInternal.getClosedTabData(aWindow, aAsString); }, undoCloseTab: function ss_undoCloseTab(aWindow, aIndex) { @@ -262,8 +262,8 @@ this.SessionStore = { return SessionStoreInternal.getClosedWindowCount(); }, - getClosedWindowData: function ss_getClosedWindowData() { - return SessionStoreInternal.getClosedWindowData(); + getClosedWindowData: function ss_getClosedWindowData(aAsString = true) { + return SessionStoreInternal.getClosedWindowData(aAsString); }, undoCloseWindow: function ss_undoCloseWindow(aIndex) { @@ -2184,9 +2184,11 @@ var SessionStoreInternal = { return DyingWindowCache.get(aWindow)._closedTabs.length; }, - getClosedTabData: function ssi_getClosedTabDataAt(aWindow) { + getClosedTabData: function ssi_getClosedTabData(aWindow, aAsString = true) { if ("__SSi" in aWindow) { - return JSON.stringify(this._windows[aWindow.__SSi]._closedTabs); + return aAsString ? + JSON.stringify(this._windows[aWindow.__SSi]._closedTabs) : + Cu.cloneInto(this._windows[aWindow.__SSi]._closedTabs, {}); } if (!DyingWindowCache.has(aWindow)) { @@ -2194,7 +2196,7 @@ var SessionStoreInternal = { } let data = DyingWindowCache.get(aWindow); - return JSON.stringify(data._closedTabs); + return aAsString ? JSON.stringify(data._closedTabs) : Cu.cloneInto(data._closedTabs, {}); }, undoCloseTab: function ssi_undoCloseTab(aWindow, aIndex) { @@ -2250,8 +2252,8 @@ var SessionStoreInternal = { return this._closedWindows.length; }, - getClosedWindowData: function ssi_getClosedWindowData() { - return JSON.stringify(this._closedWindows); + getClosedWindowData: function ssi_getClosedWindowData(aAsString = true) { + return aAsString ? JSON.stringify(this._closedWindows) : Cu.cloneInto(this._closedWindows, {}); }, undoCloseWindow: function ssi_undoCloseWindow(aIndex) { diff --git a/browser/components/sessionstore/test/browser_394759_basic.js b/browser/components/sessionstore/test/browser_394759_basic.js index dbb697b90eaa..1b1650e277a3 100644 --- a/browser/components/sessionstore/test/browser_394759_basic.js +++ b/browser/components/sessionstore/test/browser_394759_basic.js @@ -7,6 +7,8 @@ const TEST_URL = "data:text/html;charset=utf-8," + ""; +Cu.import("resource:///modules/sessionstore/SessionStore.jsm"); + /** * This test ensures that closing a window is a reversible action. We will * close the the window, restore it and check that all data has been restored. @@ -25,7 +27,7 @@ function test() { provideWindow(function onTestURLLoaded(newWin) { newWin.gBrowser.addTab().linkedBrowser.stop(); - // mark the window with some unique data to be restored later on + // Mark the window with some unique data to be restored later on. ss.setWindowValue(newWin, uniqueKey, uniqueValue); let [txt, chk] = newWin.content.document.querySelectorAll("#txt, #chk"); txt.value = uniqueText; @@ -35,11 +37,17 @@ function test() { BrowserTestUtils.closeWindow(newWin).then(() => { is(ss.getClosedWindowCount(), 1, "The closed window was added to Recently Closed Windows"); - let data = JSON.parse(ss.getClosedWindowData())[0]; - ok(data.title == TEST_URL && JSON.stringify(data).indexOf(uniqueText) > -1, + + let data = SessionStore.getClosedWindowData(false); + + // Verify that non JSON serialized data is the same as JSON serialized data. + is(JSON.stringify(data), ss.getClosedWindowData(), + "Non-serialized data is the same as serialized data") + + ok(data[0].title == TEST_URL && JSON.stringify(data[0]).indexOf(uniqueText) > -1, "The closed window data was stored correctly"); - // reopen the closed window and ensure its integrity + // Reopen the closed window and ensure its integrity. let newWin2 = ss.undoCloseWindow(0); ok(newWin2 instanceof ChromeWindow, @@ -47,9 +55,9 @@ function test() { is(ss.getClosedWindowCount(), 0, "The reopened window was removed from Recently Closed Windows"); - // SSTabRestored will fire more than once, so we need to make sure we count them + // SSTabRestored will fire more than once, so we need to make sure we count them. let restoredTabs = 0; - let expectedTabs = data.tabs.length; + let expectedTabs = data[0].tabs.length; newWin2.addEventListener("SSTabRestored", function sstabrestoredListener(aEvent) { ++restoredTabs; info("Restored tab " + restoredTabs + "/" + expectedTabs); @@ -57,7 +65,7 @@ function test() { return; } - is(restoredTabs, expectedTabs, "correct number of tabs restored"); + is(restoredTabs, expectedTabs, "Correct number of tabs restored"); newWin2.removeEventListener("SSTabRestored", sstabrestoredListener, true); is(newWin2.gBrowser.tabs.length, 2, @@ -71,7 +79,7 @@ function test() { is(ss.getWindowValue(newWin2, uniqueKey), uniqueValue, "The window correctly restored the data associated with it"); - // clean up + // Clean up. BrowserTestUtils.closeWindow(newWin2).then(finish); }, true); }); diff --git a/browser/components/sessionstore/test/browser_461634.js b/browser/components/sessionstore/test/browser_461634.js index 6baad950b8ee..01d3a4b0da3e 100644 --- a/browser/components/sessionstore/test/browser_461634.js +++ b/browser/components/sessionstore/test/browser_461634.js @@ -2,6 +2,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +Cu.import("resource:///modules/sessionstore/SessionStore.jsm"); + function test() { /** Test for Bug 461634 **/ @@ -30,23 +32,28 @@ function test() { } } - // open a window and add the above closed tab list + // Open a window and add the above closed tab list. let newWin = openDialog(location, "", "chrome,all,dialog=no"); promiseWindowLoaded(newWin).then(() => { gPrefService.setIntPref("browser.sessionstore.max_tabs_undo", test_state.windows[0]._closedTabs.length); ss.setWindowState(newWin, JSON.stringify(test_state), true); - let closedTabs = JSON.parse(ss.getClosedTabData(newWin)); + let closedTabs = SessionStore.getClosedTabData(newWin, false); + + // Verify that non JSON serialized data is the same as JSON serialized data. + is(JSON.stringify(closedTabs), SessionStore.getClosedTabData(newWin), + "Non-serialized data is the same as serialized data") + is(closedTabs.length, test_state.windows[0]._closedTabs.length, "Closed tab list has the expected length"); is(countByTitle(closedTabs, FORGET), test_state.windows[0]._closedTabs.length - remember_count, "The correct amout of tabs are to be forgotten"); is(countByTitle(closedTabs, REMEMBER), remember_count, - "Everything is set up."); + "Everything is set up"); - // all of the following calls with illegal arguments should throw NS_ERROR_ILLEGAL_VALUE + // All of the following calls with illegal arguments should throw NS_ERROR_ILLEGAL_VALUE. ok(testForError(() => ss.forgetClosedTab({}, 0)), "Invalid window for forgetClosedTab throws"); ok(testForError(() => ss.forgetClosedTab(newWin, -1)), @@ -54,19 +61,24 @@ function test() { ok(testForError(() => ss.forgetClosedTab(newWin, test_state.windows[0]._closedTabs.length + 1)), "Invalid tab for forgetClosedTab throws"); - // Remove third tab, then first tab + // Remove third tab, then first tab. ss.forgetClosedTab(newWin, 2); ss.forgetClosedTab(newWin, null); - closedTabs = JSON.parse(ss.getClosedTabData(newWin)); + closedTabs = SessionStore.getClosedTabData(newWin, false); + + // Verify that non JSON serialized data is the same as JSON serialized data. + is(JSON.stringify(closedTabs), SessionStore.getClosedTabData(newWin), + "Non-serialized data is the same as serialized data") + is(closedTabs.length, remember_count, "The correct amout of tabs was removed"); is(countByTitle(closedTabs, FORGET), 0, "All tabs specifically forgotten were indeed removed"); is(countByTitle(closedTabs, REMEMBER), remember_count, - "... and tabs not specifically forgetten weren't."); + "... and tabs not specifically forgetten weren't"); - // clean up + // Clean up. gPrefService.clearUserPref("browser.sessionstore.max_tabs_undo"); BrowserTestUtils.closeWindow(newWin).then(finish); });