diff --git a/browser/components/sessionstore/src/DocShellCapabilities.jsm b/browser/components/sessionstore/src/DocShellCapabilities.jsm new file mode 100644 index 00000000000..293a6bbcd1c --- /dev/null +++ b/browser/components/sessionstore/src/DocShellCapabilities.jsm @@ -0,0 +1,50 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public +* 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/. */ + +"use strict"; + +this.EXPORTED_SYMBOLS = ["DocShellCapabilities"]; + +/** + * The external API exported by this module. + */ +this.DocShellCapabilities = Object.freeze({ + collect: function (docShell) { + return DocShellCapabilitiesInternal.collect(docShell); + }, + + restore: function (docShell, disallow) { + return DocShellCapabilitiesInternal.restore(docShell, disallow); + }, +}); + +/** + * Internal functionality to save and restore the docShell.allow* properties. + */ +let DocShellCapabilitiesInternal = { + // List of docShell capabilities to (re)store. These are automatically + // retrieved from a given docShell if not already collected before. + // This is made so they're automatically in sync with all nsIDocShell.allow* + // properties. + caps: null, + + allCapabilities: function (docShell) { + if (!this.caps) { + let keys = Object.keys(docShell); + this.caps = keys.filter(k => k.startsWith("allow")).map(k => k.slice(5)); + } + return this.caps; + }, + + collect: function (docShell) { + let caps = this.allCapabilities(docShell); + return caps.filter(cap => !docShell["allow" + cap]); + }, + + restore: function (docShell, disallow) { + let caps = this.allCapabilities(docShell); + for (let cap of caps) + docShell["allow" + cap] = !disallow.has(cap); + }, +}; diff --git a/browser/components/sessionstore/src/SessionStore.jsm b/browser/components/sessionstore/src/SessionStore.jsm index 5569fb80a83..388c251d12b 100644 --- a/browser/components/sessionstore/src/SessionStore.jsm +++ b/browser/components/sessionstore/src/SessionStore.jsm @@ -94,23 +94,6 @@ XPCOMUtils.defineLazyServiceGetter(this, "gSessionStartup", XPCOMUtils.defineLazyServiceGetter(this, "gScreenManager", "@mozilla.org/gfx/screenmanager;1", "nsIScreenManager"); -// List of docShell capabilities to (re)store. These are automatically -// retrieved from a given docShell if not already collected before. -// This is made so they're automatically in sync with all nsIDocShell.allow* -// properties. -let gDocShellCapabilities = (function () { - let caps; - - return docShell => { - if (!caps) { - let keys = Object.keys(docShell); - caps = keys.filter(k => k.startsWith("allow")).map(k => k.slice(5)); - } - - return caps; - }; -})(); - /** * Get nsIURI from string * @param string @@ -122,6 +105,8 @@ function makeURI(aString) { XPCOMUtils.defineLazyModuleGetter(this, "ScratchpadManager", "resource:///modules/devtools/scratchpad-manager.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "DocShellCapabilities", + "resource:///modules/sessionstore/DocShellCapabilities.jsm"); XPCOMUtils.defineLazyModuleGetter(this, "DocumentUtils", "resource:///modules/sessionstore/DocumentUtils.jsm"); XPCOMUtils.defineLazyModuleGetter(this, "Messenger", @@ -2584,7 +2569,8 @@ let SessionStoreInternal = { */ restoreHistory: function ssi_restoreHistory(aWindow, aTabs, aTabData, aIdMap, aDocIdentMap, - aRestoreImmediately) { + aRestoreImmediately) + { // if the tab got removed before being completely restored, then skip it while (aTabs.length > 0 && !(this._canRestoreTabHistory(aTabs[0]))) { aTabs.shift(); @@ -2631,8 +2617,7 @@ let SessionStoreInternal = { // make sure to reset the capabilities and attributes, in case this tab gets reused let disallow = new Set(tabData.disallow && tabData.disallow.split(",")); - for (let cap of gDocShellCapabilities(browser.docShell)) - browser.docShell["allow" + cap] = !disallow.has(cap); + DocShellCapabilities.restore(browser.docShell, disallow); // Restore tab attributes. if ("attributes" in tabData) { @@ -4503,10 +4488,7 @@ let TabState = { delete tabData.pinned; tabData.hidden = tab.hidden; - let disallow = []; - for (let cap of gDocShellCapabilities(browser.docShell)) - if (!browser.docShell["allow" + cap]) - disallow.push(cap); + let disallow = DocShellCapabilities.collect(browser.docShell); if (disallow.length > 0) tabData.disallow = disallow.join(","); else if (tabData.disallow) diff --git a/browser/components/sessionstore/src/moz.build b/browser/components/sessionstore/src/moz.build index 4c5fe85ef08..39d9dd99e84 100644 --- a/browser/components/sessionstore/src/moz.build +++ b/browser/components/sessionstore/src/moz.build @@ -13,6 +13,7 @@ EXTRA_COMPONENTS += [ JS_MODULES_PATH = 'modules/sessionstore' EXTRA_JS_MODULES = [ + 'DocShellCapabilities.jsm', 'DocumentUtils.jsm', 'Messenger.jsm', 'PrivacyLevel.jsm',