Backed out changeset b06f19b95b94 (bug 1784438) for causing build bustages CLOSED...
[gecko.git] / toolkit / components / contentprefs / ContentPrefStore.sys.mjs
blob67ba7de31758f27399561c8fdb96cee46a8146e3
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/. */
5 export function ContentPrefStore() {
6   this._groups = new Map();
7   this._globalNames = new Map();
10 ContentPrefStore.prototype = {
11   set: function CPS_set(group, name, val) {
12     if (group) {
13       if (!this._groups.has(group)) {
14         this._groups.set(group, new Map());
15       }
16       this._groups.get(group).set(name, val);
17     } else {
18       this._globalNames.set(name, val);
19     }
20   },
22   setWithCast: function CPS_setWithCast(group, name, val) {
23     if (typeof val == "boolean") {
24       val = val ? 1 : 0;
25     } else if (val === undefined) {
26       val = null;
27     }
28     this.set(group, name, val);
29   },
31   has: function CPS_has(group, name) {
32     if (group) {
33       return this._groups.has(group) && this._groups.get(group).has(name);
34     }
35     return this._globalNames.has(name);
36   },
38   get: function CPS_get(group, name) {
39     if (group && this._groups.has(group)) {
40       return this._groups.get(group).get(name);
41     }
42     return this._globalNames.get(name);
43   },
45   remove: function CPS_remove(group, name) {
46     if (group) {
47       if (this._groups.has(group)) {
48         this._groups.get(group).delete(name);
49         if (this._groups.get(group).size == 0) {
50           this._groups.delete(group);
51         }
52       }
53     } else {
54       this._globalNames.delete(name);
55     }
56   },
58   removeGroup: function CPS_removeGroup(group) {
59     if (group) {
60       this._groups.delete(group);
61     } else {
62       this._globalNames.clear();
63     }
64   },
66   removeAllGroups: function CPS_removeAllGroups() {
67     this._groups.clear();
68   },
70   removeAll: function CPS_removeAll() {
71     this.removeAllGroups();
72     this._globalNames.clear();
73   },
75   groupsMatchIncludingSubdomains: function CPS_groupsMatchIncludingSubdomains(
76     group,
77     group2
78   ) {
79     let idx = group2.indexOf(group);
80     return (
81       idx == group2.length - group.length &&
82       (idx == 0 || group2[idx - 1] == ".")
83     );
84   },
86   *[Symbol.iterator]() {
87     for (let [group, names] of this._groups) {
88       for (let [name, val] of names) {
89         yield [group, name, val];
90       }
91     }
92     for (let [name, val] of this._globalNames) {
93       yield [null, name, val];
94     }
95   },
97   *match(group, name, includeSubdomains) {
98     for (let sgroup of this.matchGroups(group, includeSubdomains)) {
99       if (this.has(sgroup, name)) {
100         yield [sgroup, this.get(sgroup, name)];
101       }
102     }
103   },
105   *matchGroups(group, includeSubdomains) {
106     if (group) {
107       if (includeSubdomains) {
108         for (let [sgroup, ,] of this) {
109           if (sgroup) {
110             if (this.groupsMatchIncludingSubdomains(group, sgroup)) {
111               yield sgroup;
112             }
113           }
114         }
115       } else if (this._groups.has(group)) {
116         yield group;
117       }
118     } else if (this._globalNames.size) {
119       yield null;
120     }
121   },