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