Bug 1332501. r=bholley, r=jld, a=jcristau
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Sun, 22 Jan 2017 15:10:40 +0000
changeset 377987 555ce1a07b911aa9445aed099f98e63d27c59136
parent 377986 2548ffbed0f886ca2666f161dd63b2cf029b4680
child 377988 d5487c1011247bd8a6279a18bffb276a426ae494
push id1419
push userjlund@mozilla.com
push dateMon, 10 Apr 2017 20:44:07 +0000
treeherdermozilla-release@5e6801b73ef6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley, jld, jcristau
bugs1332501
milestone53.0a2
Bug 1332501. r=bholley, r=jld, a=jcristau MozReview-Commit-ID: BpR0emUsbLL
toolkit/components/contentprefs/ContentPrefServiceChild.jsm
toolkit/components/contentprefs/ContentPrefServiceParent.jsm
toolkit/components/contentprefs/ContentPrefUtils.jsm
--- a/toolkit/components/contentprefs/ContentPrefServiceChild.jsm
+++ b/toolkit/components/contentprefs/ContentPrefServiceChild.jsm
@@ -108,96 +108,20 @@ var ContentPrefServiceChild = {
     let requestId = this._getRandomId();
     let data = { call, args, requestId };
 
     this._mm.sendAsyncMessage("ContentPrefs:FunctionCall", data);
 
     this._requests.set(requestId, new CallbackCaller(callback));
   },
 
-  getByName(name, context, callback) {
-    return this._callFunction("getByName",
-                              [ name, contextArg(context) ],
-                              callback);
-  },
-
-  getByDomainAndName(domain, name, context, callback) {
-    return this._callFunction("getByDomainAndName",
-                              [ domain, name, contextArg(context) ],
-                              callback);
-  },
-
-  getBySubdomainAndName(domain, name, context, callback) {
-    return this._callFunction("getBySubdomainAndName",
-                              [ domain, name, contextArg(context) ],
-                              callback);
-  },
-
-  getGlobal(name, context, callback) {
-    return this._callFunction("getGlobal",
-                              [ name, contextArg(context) ],
-                              callback);
-  },
-
   getCachedByDomainAndName: NYI,
   getCachedBySubdomainAndName: NYI,
   getCachedGlobal: NYI,
 
-  set(domain, name, value, context, callback) {
-    this._callFunction("set",
-                       [ domain, name, value, contextArg(context) ],
-                       callback);
-  },
-
-  setGlobal(name, value, context, callback) {
-    this._callFunction("setGlobal",
-                       [ name, value, contextArg(context) ],
-                       callback);
-  },
-
-  removeByDomainAndName(domain, name, context, callback) {
-    this._callFunction("removeByDomainAndName",
-                       [ domain, name, contextArg(context) ],
-                       callback);
-  },
-
-  removeBySubdomainAndName(domain, name, context, callback) {
-    this._callFunction("removeBySubdomainAndName",
-                       [ domain, name, contextArg(context) ],
-                       callback);
-  },
-
-  removeGlobal(name, context, callback) {
-    this._callFunction("removeGlobal", [ name, contextArg(context) ], callback);
-  },
-
-  removeByDomain(domain, context, callback) {
-    this._callFunction("removeByDomain", [ domain, contextArg(context) ],
-                       callback);
-  },
-
-  removeBySubdomain(domain, context, callback) {
-    this._callFunction("removeBySubdomain", [ domain, contextArg(context) ],
-                       callback);
-  },
-
-  removeByName(name, context, callback) {
-    this._callFunction("removeByName", [ name, value, contextArg(context) ],
-                       callback);
-  },
-
-  removeAllDomains(context, callback) {
-    this._callFunction("removeAllDomains", [ contextArg(context) ], callback);
-  },
-
-  removeAllGlobals(context, callback) {
-    this._callFunction("removeAllGlobals", [ contextArg(context) ],
-                       callback);
-  },
-
   addObserverForName(name, observer) {
     let set = this._observers.get(name);
     if (!set) {
       set = new Set();
       if (this._observers.size === 0) {
         // This is the first observer of any kind. Start listening for changes.
         this._mm.addMessageListener("ContentPrefs:NotifyObservers", this);
       }
@@ -228,9 +152,31 @@ var ContentPrefServiceChild = {
         this._mm.removeMessageListener("ContentPrefs:NotifyObservers", this);
       }
     }
   },
 
   extractDomain: NYI
 };
 
+function forwardMethodToParent(method, signature, ...args) {
+  // Ignore superfluous arguments
+  args = args.slice(0, signature.length);
+
+  // Process context argument for forwarding
+  let contextIndex = signature.indexOf("context");
+  if (contextIndex > -1) {
+    args[contextIndex] = contextArg(args[contextIndex]);
+  }
+  // Take out the callback argument, if present.
+  let callbackIndex = signature.indexOf("callback");
+  let callback = null;
+  if (callbackIndex > -1 && args.length > callbackIndex) {
+    callback = args.splice(callbackIndex, 1)[0];
+  }
+  this._callFunction(method, args, callback);
+}
+
+for (let [method, signature] of _methodsCallableFromChild) {
+  ContentPrefServiceChild[method] = forwardMethodToParent.bind(ContentPrefServiceChild, method, signature);
+}
+
 ContentPrefServiceChild.init();
--- a/toolkit/components/contentprefs/ContentPrefServiceParent.jsm
+++ b/toolkit/components/contentprefs/ContentPrefServiceParent.jsm
@@ -6,16 +6,18 @@
 "use strict";
 
 this.EXPORTED_SYMBOLS = [ "ContentPrefServiceParent" ];
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
+Cu.import("resource://gre/modules/ContentPrefUtils.jsm");
+
 var ContentPrefServiceParent = {
   _cps2: null,
 
   init() {
     let globalMM = Cc["@mozilla.org/parentprocessmessagemanager;1"]
                      .getService(Ci.nsIMessageListenerManager);
 
     this._cps2 = Cc["@mozilla.org/content-pref/service;1"]
@@ -90,16 +92,20 @@ var ContentPrefServiceParent = {
         this._observers.delete(msg.target);
       }
     }
   },
 
   receiveMessage(msg) {
     let data = msg.data;
 
+    if (!_methodsCallableFromChild.some(([method, args]) => method == data.call)) {
+      throw new Error(`Can't call ${data.call} from child!`);
+    }
+
     let args = data.args;
     let requestId = data.requestId;
 
     let listener = {
       handleResult(pref) {
         msg.target.sendAsyncMessage("ContentPrefs:HandleResult",
                                     { requestId,
                                       contentPref: {
--- a/toolkit/components/contentprefs/ContentPrefUtils.jsm
+++ b/toolkit/components/contentprefs/ContentPrefUtils.jsm
@@ -6,16 +6,17 @@
 "use strict";
 
 this.EXPORTED_SYMBOLS = [
   "ContentPref",
   "cbHandleResult",
   "cbHandleError",
   "cbHandleCompletion",
   "safeCallback",
+  "_methodsCallableFromChild",
 ];
 
 const { interfaces: Ci, classes: Cc, results: Cr, utils: Cu } = Components;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
 function ContentPref(domain, name, value) {
@@ -44,8 +45,25 @@ function safeCallback(callbackObj, metho
   if (!callbackObj || typeof(callbackObj[methodName]) != "function")
     return;
   try {
     callbackObj[methodName].apply(callbackObj, args);
   } catch (err) {
     Cu.reportError(err);
   }
 }
+
+const _methodsCallableFromChild = Object.freeze([
+  ["getByName", ["name", "context", "callback"]],
+  ["getByDomainAndName", ["domain", "name", "context", "callback"]],
+  ["getBySubdomainAndName", ["domain", "name", "context", "callback"]],
+  ["getGlobal", ["name", "context", "callback"]],
+  ["set", ["domain", "name", "value", "context", "callback"]],
+  ["setGlobal", ["name", "value", "context", "callback"]],
+  ["removeByDomainAndName", ["domain", "name", "context", "callback"]],
+  ["removeBySubdomainAndName", ["domain", "name", "context", "callback"]],
+  ["removeGlobal", ["name", "context", "callback"]],
+  ["removeByDomain", ["domain", "context", "callback"]],
+  ["removeBySubdomain", ["domain", "context", "callback"]],
+  ["removeByName", ["name", "context", "callback"]],
+  ["removeAllDomains", ["context", "callback"]],
+  ["removeAllGlobals", ["context", "callback"]],
+]);