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",
23 "reader.color_scheme",
24 "reader.content_width",
27 "security.tls.version.enable-deprecated",
28 "security.xfocsp.errorReporting.automatic",
30 "network.trr.display_fallback_warning",
33 const kPrefTypeMap = new Map([
34 ["boolean", Services.prefs.PREF_BOOL],
35 ["number", Services.prefs.PREF_INT],
36 ["string", Services.prefs.PREF_STRING],
39 function maybeReturnErrorForReset(pref) {
40 if (!kAllowedPrefs.has(pref)) {
41 return `Resetting pref ${pref} from content is not allowed.`;
46 function maybeReturnErrorForSet(pref, value) {
47 if (!kAllowedPrefs.has(pref)) {
48 return `Setting pref ${pref} from content is not allowed.`;
51 let valueType = typeof value;
52 if (!kPrefTypeMap.has(valueType)) {
53 return `Can't set pref ${pref} to value of type ${valueType}.`;
55 let prefType = Services.prefs.getPrefType(pref);
57 prefType != Services.prefs.PREF_INVALID &&
58 prefType != kPrefTypeMap.get(valueType)
60 return `Can't set pref ${pref} to a value with type ${valueType} that doesn't match the pref's type ${prefType}.`;
65 export class AsyncPrefsChild extends JSProcessActorChild {
67 let error = maybeReturnErrorForSet(pref, value);
69 return Promise.reject(error);
72 return this.sendQuery("AsyncPrefs:SetPref", {
79 let error = maybeReturnErrorForReset(pref);
81 return Promise.reject(error);
84 return this.sendQuery("AsyncPrefs:ResetPref", { pref });
88 export var AsyncPrefs = {
90 if (kInChildProcess) {
91 return ChromeUtils.domProcessChild
92 .getActor("AsyncPrefs")
95 return AsyncPrefsParent.set(pref, value);
99 if (kInChildProcess) {
100 return ChromeUtils.domProcessChild.getActor("AsyncPrefs").reset(pref);
102 return AsyncPrefsParent.reset(pref);
106 const methodForType = {
107 number: "setIntPref",
108 boolean: "setBoolPref",
109 string: "setCharPref",
112 export class AsyncPrefsParent extends JSProcessActorParent {
113 static set(pref, value) {
114 let error = maybeReturnErrorForSet(pref, value);
116 return Promise.reject(error);
118 let methodToUse = methodForType[typeof value];
120 Services.prefs[methodToUse](pref, value);
123 return Promise.reject(ex.message);
126 return Promise.resolve(value);
130 let error = maybeReturnErrorForReset(pref);
132 return Promise.reject(error);
136 Services.prefs.clearUserPref(pref);
139 return Promise.reject(ex.message);
142 return Promise.resolve();
145 receiveMessage(msg) {
146 if (msg.name == "AsyncPrefs:SetPref") {
147 return AsyncPrefsParent.set(msg.data.pref, msg.data.value);
149 return AsyncPrefsParent.reset(msg.data.pref);