Bug 1845599 - Skip tests for private identifiers in decorators; r=mgaudet
[gecko.git] / toolkit / modules / AsyncPrefs.sys.mjs
blob959ba83d53de1b224d5cbeaae162eaa4a4afe082
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
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const kInChildProcess =
6   Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT;
8 const kAllowedPrefs = new Set([
9   // NB: please leave the testing prefs at the top, and sort the rest alphabetically if you add
10   // anything.
11   "testing.allowed-prefs.some-bool-pref",
12   "testing.allowed-prefs.some-char-pref",
13   "testing.allowed-prefs.some-int-pref",
15   "browser.contentblocking.report.hide_vpn_banner",
16   "browser.contentblocking.report.show_mobile_app",
18   "browser.shopping.experience2023.optedIn",
20   "narrate.rate",
21   "narrate.voice",
23   "reader.font_size",
24   "reader.font_type",
25   "reader.color_scheme",
26   "reader.content_width",
27   "reader.line_height",
29   "security.tls.version.enable-deprecated",
30   "security.xfocsp.errorReporting.automatic",
32   "network.trr.display_fallback_warning",
33 ]);
35 const kPrefTypeMap = new Map([
36   ["boolean", Services.prefs.PREF_BOOL],
37   ["number", Services.prefs.PREF_INT],
38   ["string", Services.prefs.PREF_STRING],
39 ]);
41 function maybeReturnErrorForReset(pref) {
42   if (!kAllowedPrefs.has(pref)) {
43     return `Resetting pref ${pref} from content is not allowed.`;
44   }
45   return false;
48 function maybeReturnErrorForSet(pref, value) {
49   if (!kAllowedPrefs.has(pref)) {
50     return `Setting pref ${pref} from content is not allowed.`;
51   }
53   let valueType = typeof value;
54   if (!kPrefTypeMap.has(valueType)) {
55     return `Can't set pref ${pref} to value of type ${valueType}.`;
56   }
57   let prefType = Services.prefs.getPrefType(pref);
58   if (
59     prefType != Services.prefs.PREF_INVALID &&
60     prefType != kPrefTypeMap.get(valueType)
61   ) {
62     return `Can't set pref ${pref} to a value with type ${valueType} that doesn't match the pref's type ${prefType}.`;
63   }
64   return false;
67 export class AsyncPrefsChild extends JSProcessActorChild {
68   set(pref, value) {
69     let error = maybeReturnErrorForSet(pref, value);
70     if (error) {
71       return Promise.reject(error);
72     }
74     return this.sendQuery("AsyncPrefs:SetPref", {
75       pref,
76       value,
77     });
78   }
80   reset(pref) {
81     let error = maybeReturnErrorForReset(pref);
82     if (error) {
83       return Promise.reject(error);
84     }
86     return this.sendQuery("AsyncPrefs:ResetPref", { pref });
87   }
90 export var AsyncPrefs = {
91   set(pref, value) {
92     if (kInChildProcess) {
93       return ChromeUtils.domProcessChild
94         .getActor("AsyncPrefs")
95         .set(pref, value);
96     }
97     return AsyncPrefsParent.set(pref, value);
98   },
100   reset(pref, value) {
101     if (kInChildProcess) {
102       return ChromeUtils.domProcessChild.getActor("AsyncPrefs").reset(pref);
103     }
104     return AsyncPrefsParent.reset(pref);
105   },
108 const methodForType = {
109   number: "setIntPref",
110   boolean: "setBoolPref",
111   string: "setCharPref",
114 export class AsyncPrefsParent extends JSProcessActorParent {
115   static set(pref, value) {
116     let error = maybeReturnErrorForSet(pref, value);
117     if (error) {
118       return Promise.reject(error);
119     }
120     let methodToUse = methodForType[typeof value];
121     try {
122       Services.prefs[methodToUse](pref, value);
123     } catch (ex) {
124       console.error(ex);
125       return Promise.reject(ex.message);
126     }
128     return Promise.resolve(value);
129   }
131   static reset(pref) {
132     let error = maybeReturnErrorForReset(pref);
133     if (error) {
134       return Promise.reject(error);
135     }
137     try {
138       Services.prefs.clearUserPref(pref);
139     } catch (ex) {
140       console.error(ex);
141       return Promise.reject(ex.message);
142     }
144     return Promise.resolve();
145   }
147   receiveMessage(msg) {
148     if (msg.name == "AsyncPrefs:SetPref") {
149       return AsyncPrefsParent.set(msg.data.pref, msg.data.value);
150     }
151     return AsyncPrefsParent.reset(msg.data.pref);
152   }