From f0ba7c168c484dde3918691de600cf7e0a327862 Mon Sep 17 00:00:00 2001 From: Tim Taubert Date: Wed, 13 Nov 2013 12:43:27 +0100 Subject: [PATCH] Bug 938093 - Swap TabStateCache contents when docShells are swapped r=billm From ca18aa2560b9380aa67ccfb6b90f4bbe8d38fc57 Mon Sep 17 00:00:00 2001 --- .../sessionstore/src/SessionStore.jsm | 1 + .../sessionstore/src/TabStateCache.jsm | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/browser/components/sessionstore/src/SessionStore.jsm b/browser/components/sessionstore/src/SessionStore.jsm index 0d9b338e00f5..b41bfb4bd465 100644 --- a/browser/components/sessionstore/src/SessionStore.jsm +++ b/browser/components/sessionstore/src/SessionStore.jsm @@ -648,6 +648,7 @@ let SessionStoreInternal = { browser = aEvent.currentTarget; let otherBrowser = aEvent.detail; TabState.onSwapDocShells(browser, otherBrowser); + TabStateCache.onSwapDocShells(browser, otherBrowser); break; case "TabOpen": this.onTabAdd(win, aEvent.originalTarget); diff --git a/browser/components/sessionstore/src/TabStateCache.jsm b/browser/components/sessionstore/src/TabStateCache.jsm index 2fcab3594324..5f932055bae5 100644 --- a/browser/components/sessionstore/src/TabStateCache.jsm +++ b/browser/components/sessionstore/src/TabStateCache.jsm @@ -96,6 +96,18 @@ this.TabStateCache = Object.freeze({ return TabStateCacheInternal.removeField(aKey, aField); }, + /** + * Swap cached data for two given browsers. + * + * @param {xul:browser} browser + * The first of the two browsers that swapped docShells. + * @param {xul:browser} otherBrowser + * The second of the two browsers that swapped docShells. + */ + onSwapDocShells: function(browser, otherBrowser) { + TabStateCacheInternal.onSwapDocShells(browser, otherBrowser); + }, + /** * Total number of cache hits during the session. */ @@ -213,6 +225,37 @@ let TabStateCacheInternal = { TabStateCacheTelemetry.recordAccess(!!data); }, + /** + * Swap cached data for two given browsers. + * + * @param {xul:browser} browser + * The first of the two browsers that swapped docShells. + * @param {xul:browser} otherBrowser + * The second of the two browsers that swapped docShells. + */ + onSwapDocShells: function(browser, otherBrowser) { + // Make sure that one or the other of these has cached data, + // and let it be |browser|. + if (!this._data.has(browser)) { + [browser, otherBrowser] = [otherBrowser, browser]; + if (!this._data.has(browser)) { + return; + } + } + + // At this point, |browser| is guaranteed to have cached data, + // although |otherBrowser| may not. Perform the swap. + let data = this._data.get(browser); + if (this._data.has(otherBrowser)) { + let otherData = this._data.get(otherBrowser); + this._data.set(browser, otherData); + this._data.set(otherBrowser, data); + } else { + this._data.set(otherBrowser, data); + this._data.delete(browser); + } + }, + _normalizeToBrowser: function(aKey) { let nodeName = aKey.localName; if (nodeName == "tab") {