Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | this.EXPORTED_SYMBOLS = ["TabStateCache"]; |
michael@0 | 8 | |
michael@0 | 9 | /** |
michael@0 | 10 | * A cache for tabs data. |
michael@0 | 11 | * |
michael@0 | 12 | * This cache implements a weak map from tabs (as XUL elements) |
michael@0 | 13 | * to tab data (as objects). |
michael@0 | 14 | * |
michael@0 | 15 | * Note that we should never cache private data, as: |
michael@0 | 16 | * - that data is used very seldom by SessionStore; |
michael@0 | 17 | * - caching private data in addition to public data is memory consuming. |
michael@0 | 18 | */ |
michael@0 | 19 | this.TabStateCache = Object.freeze({ |
michael@0 | 20 | /** |
michael@0 | 21 | * Retrieves cached data for a given |browser|. |
michael@0 | 22 | * |
michael@0 | 23 | * @param browser (xul:browser) |
michael@0 | 24 | * The browser to retrieve cached data for. |
michael@0 | 25 | * @return (object) |
michael@0 | 26 | * The cached data stored for the given |browser|. |
michael@0 | 27 | */ |
michael@0 | 28 | get: function (browser) { |
michael@0 | 29 | return TabStateCacheInternal.get(browser); |
michael@0 | 30 | }, |
michael@0 | 31 | |
michael@0 | 32 | /** |
michael@0 | 33 | * Updates cached data for a given |browser|. |
michael@0 | 34 | * |
michael@0 | 35 | * @param browser (xul:browser) |
michael@0 | 36 | * The browser belonging to the given tab data. |
michael@0 | 37 | * @param newData (object) |
michael@0 | 38 | * The new data to be stored for the given |browser|. |
michael@0 | 39 | */ |
michael@0 | 40 | update: function (browser, newData) { |
michael@0 | 41 | TabStateCacheInternal.update(browser, newData); |
michael@0 | 42 | } |
michael@0 | 43 | }); |
michael@0 | 44 | |
michael@0 | 45 | let TabStateCacheInternal = { |
michael@0 | 46 | _data: new WeakMap(), |
michael@0 | 47 | |
michael@0 | 48 | /** |
michael@0 | 49 | * Retrieves cached data for a given |browser|. |
michael@0 | 50 | * |
michael@0 | 51 | * @param browser (xul:browser) |
michael@0 | 52 | * The browser to retrieve cached data for. |
michael@0 | 53 | * @return (object) |
michael@0 | 54 | * The cached data stored for the given |browser|. |
michael@0 | 55 | */ |
michael@0 | 56 | get: function (browser) { |
michael@0 | 57 | return this._data.get(browser.permanentKey); |
michael@0 | 58 | }, |
michael@0 | 59 | |
michael@0 | 60 | /** |
michael@0 | 61 | * Updates cached data for a given |browser|. |
michael@0 | 62 | * |
michael@0 | 63 | * @param browser (xul:browser) |
michael@0 | 64 | * The browser belonging to the given tab data. |
michael@0 | 65 | * @param newData (object) |
michael@0 | 66 | * The new data to be stored for the given |browser|. |
michael@0 | 67 | */ |
michael@0 | 68 | update: function (browser, newData) { |
michael@0 | 69 | let data = this._data.get(browser.permanentKey) || {}; |
michael@0 | 70 | |
michael@0 | 71 | for (let key of Object.keys(newData)) { |
michael@0 | 72 | let value = newData[key]; |
michael@0 | 73 | if (value === null) { |
michael@0 | 74 | delete data[key]; |
michael@0 | 75 | } else { |
michael@0 | 76 | data[key] = value; |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | this._data.set(browser.permanentKey, data); |
michael@0 | 81 | } |
michael@0 | 82 | }; |