Bug 1885602 - Part 5: Implement navigating to the SUMO help topic from the menu heade...
[gecko.git] / toolkit / modules / AsyncPrefs.sys.mjs
blob07f08c119e3201d6f1ef0c0dad7e15ee07ed8b48
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",
19   "browser.shopping.experience2023.active",
20   "browser.shopping.experience2023.ads.userEnabled",
21   "browser.shopping.experience2023.autoOpen.enabled",
22   "browser.shopping.experience2023.autoOpen.userEnabled",
23   "browser.shopping.experience2023.showKeepSidebarClosedMessage",
24   "browser.shopping.experience2023.sidebarClosedCount",
26   "narrate.rate",
27   "narrate.voice",
29   "reader.font_size",
30   "reader.font_type",
31   "reader.color_scheme",
32   "reader.content_width",
33   "reader.line_height",
34   "reader.custom_colors.foreground",
35   "reader.custom_colors.background",
36   "reader.custom_colors.unvisited-links",
37   "reader.custom_colors.visited-links",
38   "reader.custom_colors.selection-highlight",
40   "security.tls.version.enable-deprecated",
41   "security.xfocsp.errorReporting.automatic",
43   "network.trr.display_fallback_warning",
44 ]);
46 const kPrefTypeMap = new Map([
47   ["boolean", Services.prefs.PREF_BOOL],
48   ["number", Services.prefs.PREF_INT],
49   ["string", Services.prefs.PREF_STRING],
50 ]);
52 function maybeReturnErrorForReset(pref) {
53   if (!kAllowedPrefs.has(pref)) {
54     return `Resetting pref ${pref} from content is not allowed.`;
55   }
56   return false;
59 function maybeReturnErrorForSet(pref, value) {
60   if (!kAllowedPrefs.has(pref)) {
61     return `Setting pref ${pref} from content is not allowed.`;
62   }
64   let valueType = typeof value;
65   if (!kPrefTypeMap.has(valueType)) {
66     return `Can't set pref ${pref} to value of type ${valueType}.`;
67   }
68   let prefType = Services.prefs.getPrefType(pref);
69   if (
70     prefType != Services.prefs.PREF_INVALID &&
71     prefType != kPrefTypeMap.get(valueType)
72   ) {
73     return `Can't set pref ${pref} to a value with type ${valueType} that doesn't match the pref's type ${prefType}.`;
74   }
75   return false;
78 export class AsyncPrefsChild extends JSProcessActorChild {
79   set(pref, value) {
80     let error = maybeReturnErrorForSet(pref, value);
81     if (error) {
82       return Promise.reject(error);
83     }
85     return this.sendQuery("AsyncPrefs:SetPref", {
86       pref,
87       value,
88     });
89   }
91   reset(pref) {
92     let error = maybeReturnErrorForReset(pref);
93     if (error) {
94       return Promise.reject(error);
95     }
97     return this.sendQuery("AsyncPrefs:ResetPref", { pref });
98   }
101 export var AsyncPrefs = {
102   set(pref, value) {
103     if (kInChildProcess) {
104       return ChromeUtils.domProcessChild
105         .getActor("AsyncPrefs")
106         .set(pref, value);
107     }
108     return AsyncPrefsParent.set(pref, value);
109   },
111   reset(pref) {
112     if (kInChildProcess) {
113       return ChromeUtils.domProcessChild.getActor("AsyncPrefs").reset(pref);
114     }
115     return AsyncPrefsParent.reset(pref);
116   },
119 const methodForType = {
120   number: "setIntPref",
121   boolean: "setBoolPref",
122   string: "setCharPref",
125 export class AsyncPrefsParent extends JSProcessActorParent {
126   static set(pref, value) {
127     let error = maybeReturnErrorForSet(pref, value);
128     if (error) {
129       return Promise.reject(error);
130     }
131     let methodToUse = methodForType[typeof value];
132     try {
133       Services.prefs[methodToUse](pref, value);
134     } catch (ex) {
135       console.error(ex);
136       return Promise.reject(ex.message);
137     }
139     return Promise.resolve(value);
140   }
142   static reset(pref) {
143     let error = maybeReturnErrorForReset(pref);
144     if (error) {
145       return Promise.reject(error);
146     }
148     try {
149       Services.prefs.clearUserPref(pref);
150     } catch (ex) {
151       console.error(ex);
152       return Promise.reject(ex.message);
153     }
155     return Promise.resolve();
156   }
158   receiveMessage(msg) {
159     if (msg.name == "AsyncPrefs:SetPref") {
160       return AsyncPrefsParent.set(msg.data.pref, msg.data.value);
161     }
162     return AsyncPrefsParent.reset(msg.data.pref);
163   }