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
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",
27 "reader.color_scheme",
28 "reader.content_width",
31 "security.tls.version.enable-deprecated",
32 "security.xfocsp.errorReporting.automatic",
34 "network.trr.display_fallback_warning",
37 const kPrefTypeMap = new Map([
38 ["boolean", Services.prefs.PREF_BOOL],
39 ["number", Services.prefs.PREF_INT],
40 ["string", Services.prefs.PREF_STRING],
43 function maybeReturnErrorForReset(pref) {
44 if (!kAllowedPrefs.has(pref)) {
45 return `Resetting pref ${pref} from content is not allowed.`;
50 function maybeReturnErrorForSet(pref, value) {
51 if (!kAllowedPrefs.has(pref)) {
52 return `Setting pref ${pref} from content is not allowed.`;
55 let valueType = typeof value;
56 if (!kPrefTypeMap.has(valueType)) {
57 return `Can't set pref ${pref} to value of type ${valueType}.`;
59 let prefType = Services.prefs.getPrefType(pref);
61 prefType != Services.prefs.PREF_INVALID &&
62 prefType != kPrefTypeMap.get(valueType)
64 return `Can't set pref ${pref} to a value with type ${valueType} that doesn't match the pref's type ${prefType}.`;
69 export class AsyncPrefsChild extends JSProcessActorChild {
71 let error = maybeReturnErrorForSet(pref, value);
73 return Promise.reject(error);
76 return this.sendQuery("AsyncPrefs:SetPref", {
83 let error = maybeReturnErrorForReset(pref);
85 return Promise.reject(error);
88 return this.sendQuery("AsyncPrefs:ResetPref", { pref });
92 export var AsyncPrefs = {
94 if (kInChildProcess) {
95 return ChromeUtils.domProcessChild
96 .getActor("AsyncPrefs")
99 return AsyncPrefsParent.set(pref, value);
103 if (kInChildProcess) {
104 return ChromeUtils.domProcessChild.getActor("AsyncPrefs").reset(pref);
106 return AsyncPrefsParent.reset(pref);
110 const methodForType = {
111 number: "setIntPref",
112 boolean: "setBoolPref",
113 string: "setCharPref",
116 export class AsyncPrefsParent extends JSProcessActorParent {
117 static set(pref, value) {
118 let error = maybeReturnErrorForSet(pref, value);
120 return Promise.reject(error);
122 let methodToUse = methodForType[typeof value];
124 Services.prefs[methodToUse](pref, value);
127 return Promise.reject(ex.message);
130 return Promise.resolve(value);
134 let error = maybeReturnErrorForReset(pref);
136 return Promise.reject(error);
140 Services.prefs.clearUserPref(pref);
143 return Promise.reject(ex.message);
146 return Promise.resolve();
149 receiveMessage(msg) {
150 if (msg.name == "AsyncPrefs:SetPref") {
151 return AsyncPrefsParent.set(msg.data.pref, msg.data.value);
153 return AsyncPrefsParent.reset(msg.data.pref);