1 /* vim: set ts=2 sw=2 sts=2 et tw=80: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
10 ChromeUtils.defineESModuleGetters(lazy, {
11 _methodsCallableFromChild: "resource://gre/modules/ContentPrefUtils.sys.mjs",
14 let loadContext = Cu.createLoadContext();
15 let privateLoadContext = Cu.createPrivateLoadContext();
17 function contextArg(context) {
18 return context && context.usePrivateBrowsing
23 export class ContentPrefsParent extends JSProcessActorParent {
27 // The names we're using this observer object for, used to keep track
28 // of the number of names we care about as well as for removing this
29 // observer if its associated process goes away.
30 this._prefsToObserve = new Set();
31 this._observer = null;
36 for (let i of this._prefsToObserve) {
37 lazy.cps2.removeObserverForName(i, this._observer);
39 this._observer = null;
45 case "ContentPrefs:AddObserverForName": {
46 // The child process is responsible for not adding multiple parent
47 // observers for the same name.
49 if (!this._observer) {
51 onContentPrefSet(group, name, value, isPrivate) {
52 actor.onContentPrefSet(group, name, value, isPrivate);
54 onContentPrefRemoved(group, name, isPrivate) {
55 actor.onContentPrefRemoved(group, name, isPrivate);
60 let prefName = msg.data.name;
61 this._prefsToObserve.add(prefName);
62 lazy.cps2.addObserverForName(prefName, this._observer);
66 case "ContentPrefs:RemoveObserverForName": {
67 let prefName = msg.data.name;
68 this._prefsToObserve.delete(prefName);
69 if (this._prefsToObserve.size == 0) {
70 lazy.cps2.removeObserverForName(prefName, this._observer);
71 this._observer = null;
76 case "ContentPrefs:FunctionCall":
81 !lazy._methodsCallableFromChild.some(([method, args]) => {
82 if (method == data.call) {
89 throw new Error(`Can't call ${data.call} from child!`);
95 return new Promise(() => {
98 actor.sendAsyncMessage("ContentPrefs:HandleResult", {
99 requestId: data.requestId,
109 actor.sendAsyncMessage("ContentPrefs:HandleError", {
110 requestId: data.requestId,
114 handleCompletion(reason) {
115 actor.sendAsyncMessage("ContentPrefs:HandleCompletion", {
116 requestId: data.requestId,
121 // Push our special listener.
124 // Process context argument for forwarding
125 let contextIndex = signature.indexOf("context");
126 if (contextIndex > -1) {
127 args[contextIndex] = contextArg(args[contextIndex]);
130 // And call the function.
131 lazy.cps2[data.call](...args);
138 onContentPrefSet(group, name, value, isPrivate) {
139 this.sendAsyncMessage("ContentPrefs:NotifyObservers", {
141 callback: "onContentPrefSet",
142 args: [group, name, value, isPrivate],
146 onContentPrefRemoved(group, name, isPrivate) {
147 this.sendAsyncMessage("ContentPrefs:NotifyObservers", {
149 callback: "onContentPrefRemoved",
150 args: [group, name, isPrivate],
155 XPCOMUtils.defineLazyServiceGetter(
158 "@mozilla.org/content-pref/service;1",
159 "nsIContentPrefService2"