2013-09-30 09:01:02 -07:00
|
|
|
/* 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 = ["TabStateCache"];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A cache for tabs data.
|
|
|
|
*
|
|
|
|
* This cache implements a weak map from tabs (as XUL elements)
|
|
|
|
* to tab data (as objects).
|
|
|
|
*
|
|
|
|
* Note that we should never cache private data, as:
|
|
|
|
* - that data is used very seldom by SessionStore;
|
|
|
|
* - caching private data in addition to public data is memory consuming.
|
|
|
|
*/
|
|
|
|
this.TabStateCache = Object.freeze({
|
2013-10-27 07:30:56 -07:00
|
|
|
/**
|
2014-01-20 08:37:41 -08:00
|
|
|
* Retrieves cached data for a given |browser|.
|
2013-10-27 07:30:56 -07:00
|
|
|
*
|
|
|
|
* @param browser (xul:browser)
|
|
|
|
* The browser to retrieve cached data for.
|
|
|
|
* @return (object)
|
2014-01-20 08:37:41 -08:00
|
|
|
* The cached data stored for the given |browser|.
|
2013-10-27 07:30:56 -07:00
|
|
|
*/
|
2014-01-20 08:37:41 -08:00
|
|
|
get: function (browser) {
|
|
|
|
return TabStateCacheInternal.get(browser);
|
2013-10-27 07:30:56 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-01-20 08:37:41 -08:00
|
|
|
* Updates cached data for a given |browser|.
|
2013-10-27 07:30:56 -07:00
|
|
|
*
|
|
|
|
* @param browser (xul:browser)
|
|
|
|
* The browser belonging to the given tab data.
|
|
|
|
* @param newData (object)
|
|
|
|
* The new data to be stored for the given |browser|.
|
|
|
|
*/
|
2014-01-20 08:37:41 -08:00
|
|
|
update: function (browser, newData) {
|
|
|
|
TabStateCacheInternal.update(browser, newData);
|
|
|
|
}
|
2013-09-30 09:01:02 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
let TabStateCacheInternal = {
|
|
|
|
_data: new WeakMap(),
|
|
|
|
|
2013-10-27 07:30:56 -07:00
|
|
|
/**
|
2014-01-20 08:37:41 -08:00
|
|
|
* Retrieves cached data for a given |browser|.
|
2013-10-27 07:30:56 -07:00
|
|
|
*
|
|
|
|
* @param browser (xul:browser)
|
|
|
|
* The browser to retrieve cached data for.
|
|
|
|
* @return (object)
|
2014-01-20 08:37:41 -08:00
|
|
|
* The cached data stored for the given |browser|.
|
2013-10-27 07:30:56 -07:00
|
|
|
*/
|
2014-01-20 08:37:41 -08:00
|
|
|
get: function (browser) {
|
2014-01-20 11:50:17 -08:00
|
|
|
return this._data.get(browser.permanentKey);
|
2013-10-27 07:30:56 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-01-20 08:37:41 -08:00
|
|
|
* Updates cached data for a given |browser|.
|
2013-10-27 07:30:56 -07:00
|
|
|
*
|
|
|
|
* @param browser (xul:browser)
|
|
|
|
* The browser belonging to the given tab data.
|
|
|
|
* @param newData (object)
|
|
|
|
* The new data to be stored for the given |browser|.
|
|
|
|
*/
|
2014-01-20 08:37:41 -08:00
|
|
|
update: function (browser, newData) {
|
2014-01-20 11:50:17 -08:00
|
|
|
let data = this._data.get(browser.permanentKey) || {};
|
2013-10-27 07:30:56 -07:00
|
|
|
|
|
|
|
for (let key of Object.keys(newData)) {
|
|
|
|
let value = newData[key];
|
|
|
|
if (value === null) {
|
|
|
|
delete data[key];
|
|
|
|
} else {
|
|
|
|
data[key] = value;
|
2013-11-13 03:43:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 11:50:17 -08:00
|
|
|
this._data.set(browser.permanentKey, data);
|
2013-09-30 09:01:02 -07:00
|
|
|
}
|
|
|
|
};
|