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",
25 "reader.color_scheme",
26 "reader.content_width",
29 "security.tls.version.enable-deprecated",
30 "security.xfocsp.errorReporting.automatic",
32 "network.trr.display_fallback_warning",
35 const kPrefTypeMap = new Map([
36 ["boolean", Services.prefs.PREF_BOOL],
37 ["number", Services.prefs.PREF_INT],
38 ["string", Services.prefs.PREF_STRING],
41 function maybeReturnErrorForReset(pref) {
42 if (!kAllowedPrefs.has(pref)) {
43 return `Resetting pref ${pref} from content is not allowed.`;
48 function maybeReturnErrorForSet(pref, value) {
49 if (!kAllowedPrefs.has(pref)) {
50 return `Setting pref ${pref} from content is not allowed.`;
53 let valueType = typeof value;
54 if (!kPrefTypeMap.has(valueType)) {
55 return `Can't set pref ${pref} to value of type ${valueType}.`;
57 let prefType = Services.prefs.getPrefType(pref);
59 prefType != Services.prefs.PREF_INVALID &&
60 prefType != kPrefTypeMap.get(valueType)
62 return `Can't set pref ${pref} to a value with type ${valueType} that doesn't match the pref's type ${prefType}.`;
67 export class AsyncPrefsChild extends JSProcessActorChild {
69 let error = maybeReturnErrorForSet(pref, value);
71 return Promise.reject(error);
74 return this.sendQuery("AsyncPrefs:SetPref", {
81 let error = maybeReturnErrorForReset(pref);
83 return Promise.reject(error);
86 return this.sendQuery("AsyncPrefs:ResetPref", { pref });
90 export var AsyncPrefs = {
92 if (kInChildProcess) {
93 return ChromeUtils.domProcessChild
94 .getActor("AsyncPrefs")
97 return AsyncPrefsParent.set(pref, value);
101 if (kInChildProcess) {
102 return ChromeUtils.domProcessChild.getActor("AsyncPrefs").reset(pref);
104 return AsyncPrefsParent.reset(pref);
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);
118 return Promise.reject(error);
120 let methodToUse = methodForType[typeof value];
122 Services.prefs[methodToUse](pref, value);
125 return Promise.reject(ex.message);
128 return Promise.resolve(value);
132 let error = maybeReturnErrorForReset(pref);
134 return Promise.reject(error);
138 Services.prefs.clearUserPref(pref);
141 return Promise.reject(ex.message);
144 return Promise.resolve();
147 receiveMessage(msg) {
148 if (msg.name == "AsyncPrefs:SetPref") {
149 return AsyncPrefsParent.set(msg.data.pref, msg.data.value);
151 return AsyncPrefsParent.reset(msg.data.pref);