michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let EXPORTED_SYMBOLS = [ michael@0: "ContentPrefStore", michael@0: ]; michael@0: michael@0: function ContentPrefStore() { michael@0: this._groups = {}; michael@0: this._globalNames = {}; michael@0: } michael@0: michael@0: ContentPrefStore.prototype = { michael@0: michael@0: set: function CPS_set(group, name, val) { michael@0: if (group) { michael@0: if (!this._groups.hasOwnProperty(group)) michael@0: this._groups[group] = {}; michael@0: this._groups[group][name] = val; michael@0: } michael@0: else { michael@0: this._globalNames[name] = val; michael@0: } michael@0: }, michael@0: michael@0: setWithCast: function CPS_setWithCast(group, name, val) { michael@0: if (typeof(val) == "boolean") michael@0: val = val ? 1 : 0; michael@0: else if (val === undefined) michael@0: val = null; michael@0: this.set(group, name, val); michael@0: }, michael@0: michael@0: has: function CPS_has(group, name) { michael@0: return (group && michael@0: this._groups.hasOwnProperty(group) && michael@0: this._groups[group].hasOwnProperty(name)) || michael@0: (!group && michael@0: this._globalNames.hasOwnProperty(name)); michael@0: }, michael@0: michael@0: get: function CPS_get(group, name) { michael@0: if (group) { michael@0: if (this._groups.hasOwnProperty(group) && michael@0: this._groups[group].hasOwnProperty(name)) michael@0: return this._groups[group][name]; michael@0: } michael@0: else if (this._globalNames.hasOwnProperty(name)) { michael@0: return this._globalNames[name]; michael@0: } michael@0: return undefined; michael@0: }, michael@0: michael@0: remove: function CPS_remove(group, name) { michael@0: if (group) { michael@0: if (this._groups.hasOwnProperty(group)) { michael@0: delete this._groups[group][name]; michael@0: if (!Object.keys(this._groups[group]).length) michael@0: delete this._groups[group]; michael@0: } michael@0: } michael@0: else { michael@0: delete this._globalNames[name]; michael@0: } michael@0: }, michael@0: michael@0: removeGroup: function CPS_removeGroup(group) { michael@0: if (group) { michael@0: delete this._groups[group]; michael@0: } michael@0: else { michael@0: this._globalNames = {}; michael@0: } michael@0: }, michael@0: michael@0: removeAllGroups: function CPS_removeAllGroups() { michael@0: this._groups = {}; michael@0: }, michael@0: michael@0: removeAll: function CPS_removeAll() { michael@0: this.removeAllGroups(); michael@0: this._globalNames = {}; michael@0: }, michael@0: michael@0: __iterator__: function CPS___iterator__() { michael@0: for (let [group, names] in Iterator(this._groups)) { michael@0: for (let [name, val] in Iterator(names)) { michael@0: yield [group, name, val]; michael@0: } michael@0: } michael@0: for (let [name, val] in Iterator(this._globalNames)) { michael@0: yield [null, name, val]; michael@0: } michael@0: }, michael@0: michael@0: match: function CPS_match(group, name, includeSubdomains) { michael@0: for (let sgroup in this.matchGroups(group, includeSubdomains)) { michael@0: if (this.has(sgroup, name)) michael@0: yield [sgroup, this.get(sgroup, name)]; michael@0: } michael@0: }, michael@0: michael@0: matchGroups: function CPS_matchGroups(group, includeSubdomains) { michael@0: if (group) { michael@0: if (includeSubdomains) { michael@0: for (let [sgroup, , ] in this) { michael@0: if (sgroup) { michael@0: let idx = sgroup.indexOf(group); michael@0: if (idx == sgroup.length - group.length && michael@0: (idx == 0 || sgroup[idx - 1] == ".")) michael@0: yield sgroup; michael@0: } michael@0: } michael@0: } michael@0: else if (this._groups.hasOwnProperty(group)) { michael@0: yield group; michael@0: } michael@0: } michael@0: else if (Object.keys(this._globalNames).length) { michael@0: yield null; michael@0: } michael@0: }, michael@0: };