Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | let EXPORTED_SYMBOLS = [ |
michael@0 | 6 | "ContentPrefStore", |
michael@0 | 7 | ]; |
michael@0 | 8 | |
michael@0 | 9 | function ContentPrefStore() { |
michael@0 | 10 | this._groups = {}; |
michael@0 | 11 | this._globalNames = {}; |
michael@0 | 12 | } |
michael@0 | 13 | |
michael@0 | 14 | ContentPrefStore.prototype = { |
michael@0 | 15 | |
michael@0 | 16 | set: function CPS_set(group, name, val) { |
michael@0 | 17 | if (group) { |
michael@0 | 18 | if (!this._groups.hasOwnProperty(group)) |
michael@0 | 19 | this._groups[group] = {}; |
michael@0 | 20 | this._groups[group][name] = val; |
michael@0 | 21 | } |
michael@0 | 22 | else { |
michael@0 | 23 | this._globalNames[name] = val; |
michael@0 | 24 | } |
michael@0 | 25 | }, |
michael@0 | 26 | |
michael@0 | 27 | setWithCast: function CPS_setWithCast(group, name, val) { |
michael@0 | 28 | if (typeof(val) == "boolean") |
michael@0 | 29 | val = val ? 1 : 0; |
michael@0 | 30 | else if (val === undefined) |
michael@0 | 31 | val = null; |
michael@0 | 32 | this.set(group, name, val); |
michael@0 | 33 | }, |
michael@0 | 34 | |
michael@0 | 35 | has: function CPS_has(group, name) { |
michael@0 | 36 | return (group && |
michael@0 | 37 | this._groups.hasOwnProperty(group) && |
michael@0 | 38 | this._groups[group].hasOwnProperty(name)) || |
michael@0 | 39 | (!group && |
michael@0 | 40 | this._globalNames.hasOwnProperty(name)); |
michael@0 | 41 | }, |
michael@0 | 42 | |
michael@0 | 43 | get: function CPS_get(group, name) { |
michael@0 | 44 | if (group) { |
michael@0 | 45 | if (this._groups.hasOwnProperty(group) && |
michael@0 | 46 | this._groups[group].hasOwnProperty(name)) |
michael@0 | 47 | return this._groups[group][name]; |
michael@0 | 48 | } |
michael@0 | 49 | else if (this._globalNames.hasOwnProperty(name)) { |
michael@0 | 50 | return this._globalNames[name]; |
michael@0 | 51 | } |
michael@0 | 52 | return undefined; |
michael@0 | 53 | }, |
michael@0 | 54 | |
michael@0 | 55 | remove: function CPS_remove(group, name) { |
michael@0 | 56 | if (group) { |
michael@0 | 57 | if (this._groups.hasOwnProperty(group)) { |
michael@0 | 58 | delete this._groups[group][name]; |
michael@0 | 59 | if (!Object.keys(this._groups[group]).length) |
michael@0 | 60 | delete this._groups[group]; |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | else { |
michael@0 | 64 | delete this._globalNames[name]; |
michael@0 | 65 | } |
michael@0 | 66 | }, |
michael@0 | 67 | |
michael@0 | 68 | removeGroup: function CPS_removeGroup(group) { |
michael@0 | 69 | if (group) { |
michael@0 | 70 | delete this._groups[group]; |
michael@0 | 71 | } |
michael@0 | 72 | else { |
michael@0 | 73 | this._globalNames = {}; |
michael@0 | 74 | } |
michael@0 | 75 | }, |
michael@0 | 76 | |
michael@0 | 77 | removeAllGroups: function CPS_removeAllGroups() { |
michael@0 | 78 | this._groups = {}; |
michael@0 | 79 | }, |
michael@0 | 80 | |
michael@0 | 81 | removeAll: function CPS_removeAll() { |
michael@0 | 82 | this.removeAllGroups(); |
michael@0 | 83 | this._globalNames = {}; |
michael@0 | 84 | }, |
michael@0 | 85 | |
michael@0 | 86 | __iterator__: function CPS___iterator__() { |
michael@0 | 87 | for (let [group, names] in Iterator(this._groups)) { |
michael@0 | 88 | for (let [name, val] in Iterator(names)) { |
michael@0 | 89 | yield [group, name, val]; |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | for (let [name, val] in Iterator(this._globalNames)) { |
michael@0 | 93 | yield [null, name, val]; |
michael@0 | 94 | } |
michael@0 | 95 | }, |
michael@0 | 96 | |
michael@0 | 97 | match: function CPS_match(group, name, includeSubdomains) { |
michael@0 | 98 | for (let sgroup in this.matchGroups(group, includeSubdomains)) { |
michael@0 | 99 | if (this.has(sgroup, name)) |
michael@0 | 100 | yield [sgroup, this.get(sgroup, name)]; |
michael@0 | 101 | } |
michael@0 | 102 | }, |
michael@0 | 103 | |
michael@0 | 104 | matchGroups: function CPS_matchGroups(group, includeSubdomains) { |
michael@0 | 105 | if (group) { |
michael@0 | 106 | if (includeSubdomains) { |
michael@0 | 107 | for (let [sgroup, , ] in this) { |
michael@0 | 108 | if (sgroup) { |
michael@0 | 109 | let idx = sgroup.indexOf(group); |
michael@0 | 110 | if (idx == sgroup.length - group.length && |
michael@0 | 111 | (idx == 0 || sgroup[idx - 1] == ".")) |
michael@0 | 112 | yield sgroup; |
michael@0 | 113 | } |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | else if (this._groups.hasOwnProperty(group)) { |
michael@0 | 117 | yield group; |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | else if (Object.keys(this._globalNames).length) { |
michael@0 | 121 | yield null; |
michael@0 | 122 | } |
michael@0 | 123 | }, |
michael@0 | 124 | }; |