1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/src/TabStateCache.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +this.EXPORTED_SYMBOLS = ["TabStateCache"]; 1.11 + 1.12 +/** 1.13 + * A cache for tabs data. 1.14 + * 1.15 + * This cache implements a weak map from tabs (as XUL elements) 1.16 + * to tab data (as objects). 1.17 + * 1.18 + * Note that we should never cache private data, as: 1.19 + * - that data is used very seldom by SessionStore; 1.20 + * - caching private data in addition to public data is memory consuming. 1.21 + */ 1.22 +this.TabStateCache = Object.freeze({ 1.23 + /** 1.24 + * Retrieves cached data for a given |browser|. 1.25 + * 1.26 + * @param browser (xul:browser) 1.27 + * The browser to retrieve cached data for. 1.28 + * @return (object) 1.29 + * The cached data stored for the given |browser|. 1.30 + */ 1.31 + get: function (browser) { 1.32 + return TabStateCacheInternal.get(browser); 1.33 + }, 1.34 + 1.35 + /** 1.36 + * Updates cached data for a given |browser|. 1.37 + * 1.38 + * @param browser (xul:browser) 1.39 + * The browser belonging to the given tab data. 1.40 + * @param newData (object) 1.41 + * The new data to be stored for the given |browser|. 1.42 + */ 1.43 + update: function (browser, newData) { 1.44 + TabStateCacheInternal.update(browser, newData); 1.45 + } 1.46 +}); 1.47 + 1.48 +let TabStateCacheInternal = { 1.49 + _data: new WeakMap(), 1.50 + 1.51 + /** 1.52 + * Retrieves cached data for a given |browser|. 1.53 + * 1.54 + * @param browser (xul:browser) 1.55 + * The browser to retrieve cached data for. 1.56 + * @return (object) 1.57 + * The cached data stored for the given |browser|. 1.58 + */ 1.59 + get: function (browser) { 1.60 + return this._data.get(browser.permanentKey); 1.61 + }, 1.62 + 1.63 + /** 1.64 + * Updates cached data for a given |browser|. 1.65 + * 1.66 + * @param browser (xul:browser) 1.67 + * The browser belonging to the given tab data. 1.68 + * @param newData (object) 1.69 + * The new data to be stored for the given |browser|. 1.70 + */ 1.71 + update: function (browser, newData) { 1.72 + let data = this._data.get(browser.permanentKey) || {}; 1.73 + 1.74 + for (let key of Object.keys(newData)) { 1.75 + let value = newData[key]; 1.76 + if (value === null) { 1.77 + delete data[key]; 1.78 + } else { 1.79 + data[key] = value; 1.80 + } 1.81 + } 1.82 + 1.83 + this._data.set(browser.permanentKey, data); 1.84 + } 1.85 +};