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