1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/src/TabAttributes.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,68 @@ 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 = ["TabAttributes"]; 1.11 + 1.12 +// A set of tab attributes to persist. We will read a given list of tab 1.13 +// attributes when collecting tab data and will re-set those attributes when 1.14 +// the given tab data is restored to a new tab. 1.15 +this.TabAttributes = Object.freeze({ 1.16 + persist: function (name) { 1.17 + return TabAttributesInternal.persist(name); 1.18 + }, 1.19 + 1.20 + get: function (tab) { 1.21 + return TabAttributesInternal.get(tab); 1.22 + }, 1.23 + 1.24 + set: function (tab, data = {}) { 1.25 + TabAttributesInternal.set(tab, data); 1.26 + } 1.27 +}); 1.28 + 1.29 +let TabAttributesInternal = { 1.30 + _attrs: new Set(), 1.31 + 1.32 + // We never want to directly read or write those attributes. 1.33 + // 'image' should not be accessed directly but handled by using the 1.34 + // gBrowser.getIcon()/setIcon() methods. 1.35 + // 'pending' is used internal by sessionstore and managed accordingly. 1.36 + _skipAttrs: new Set(["image", "pending"]), 1.37 + 1.38 + persist: function (name) { 1.39 + if (this._attrs.has(name) || this._skipAttrs.has(name)) { 1.40 + return false; 1.41 + } 1.42 + 1.43 + this._attrs.add(name); 1.44 + return true; 1.45 + }, 1.46 + 1.47 + get: function (tab) { 1.48 + let data = {}; 1.49 + 1.50 + for (let name of this._attrs) { 1.51 + if (tab.hasAttribute(name)) { 1.52 + data[name] = tab.getAttribute(name); 1.53 + } 1.54 + } 1.55 + 1.56 + return data; 1.57 + }, 1.58 + 1.59 + set: function (tab, data = {}) { 1.60 + // Clear attributes. 1.61 + for (let name of this._attrs) { 1.62 + tab.removeAttribute(name); 1.63 + } 1.64 + 1.65 + // Set attributes. 1.66 + for (let name in data) { 1.67 + tab.setAttribute(name, data[name]); 1.68 + } 1.69 + } 1.70 +}; 1.71 +