michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["TabStateCache"]; michael@0: michael@0: /** michael@0: * A cache for tabs data. michael@0: * michael@0: * This cache implements a weak map from tabs (as XUL elements) michael@0: * to tab data (as objects). michael@0: * michael@0: * Note that we should never cache private data, as: michael@0: * - that data is used very seldom by SessionStore; michael@0: * - caching private data in addition to public data is memory consuming. michael@0: */ michael@0: this.TabStateCache = Object.freeze({ michael@0: /** michael@0: * Retrieves cached data for a given |browser|. michael@0: * michael@0: * @param browser (xul:browser) michael@0: * The browser to retrieve cached data for. michael@0: * @return (object) michael@0: * The cached data stored for the given |browser|. michael@0: */ michael@0: get: function (browser) { michael@0: return TabStateCacheInternal.get(browser); michael@0: }, michael@0: michael@0: /** michael@0: * Updates cached data for a given |browser|. michael@0: * michael@0: * @param browser (xul:browser) michael@0: * The browser belonging to the given tab data. michael@0: * @param newData (object) michael@0: * The new data to be stored for the given |browser|. michael@0: */ michael@0: update: function (browser, newData) { michael@0: TabStateCacheInternal.update(browser, newData); michael@0: } michael@0: }); michael@0: michael@0: let TabStateCacheInternal = { michael@0: _data: new WeakMap(), michael@0: michael@0: /** michael@0: * Retrieves cached data for a given |browser|. michael@0: * michael@0: * @param browser (xul:browser) michael@0: * The browser to retrieve cached data for. michael@0: * @return (object) michael@0: * The cached data stored for the given |browser|. michael@0: */ michael@0: get: function (browser) { michael@0: return this._data.get(browser.permanentKey); michael@0: }, michael@0: michael@0: /** michael@0: * Updates cached data for a given |browser|. michael@0: * michael@0: * @param browser (xul:browser) michael@0: * The browser belonging to the given tab data. michael@0: * @param newData (object) michael@0: * The new data to be stored for the given |browser|. michael@0: */ michael@0: update: function (browser, newData) { michael@0: let data = this._data.get(browser.permanentKey) || {}; michael@0: michael@0: for (let key of Object.keys(newData)) { michael@0: let value = newData[key]; michael@0: if (value === null) { michael@0: delete data[key]; michael@0: } else { michael@0: data[key] = value; michael@0: } michael@0: } michael@0: michael@0: this._data.set(browser.permanentKey, data); michael@0: } michael@0: };