bug 1523104: remote: refactor Session#dispatch; r=ochameau
authorAndreas Tolfsen <ato@sny.no>
Sat, 23 Feb 2019 16:38:41 +0000
changeset 521110 a1a5fdbf9bc8
parent 521109 5ce6f3fcc572
child 521111 40a677ed5cc4
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersochameau
bugs1523104
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 1523104: remote: refactor Session#dispatch; r=ochameau
remote/ContentProcessSession.jsm
remote/Session.jsm
remote/domains/Domains.jsm
--- a/remote/ContentProcessSession.jsm
+++ b/remote/ContentProcessSession.jsm
@@ -41,22 +41,22 @@ class ContentProcessSession {
     // the two message managers.
     if (browsingContextId != this.browsingContext.id) {
       return;
     }
 
     switch (name) {
     case "remote:request":
       try {
-        const {id, domainName, methodName, params} = data.request;
+        const {id, domain, method, params} = data.request;
 
-        const inst = this.domains.get(domainName);
-        const methodFn = inst[methodName];
+        const inst = this.domains.get(domain);
+        const methodFn = inst[method];
         if (!methodFn || typeof methodFn != "function") {
-          throw new Error(`Method implementation of ${methodName} missing`);
+          throw new Error(`Method implementation of ${method} missing`);
         }
 
         const result = await methodFn.call(inst, params);
 
         this.messageManager.sendAsyncMessage("remote:result", {
           browsingContextId,
           id,
           result,
--- a/remote/Session.jsm
+++ b/remote/Session.jsm
@@ -42,38 +42,41 @@ class Session {
     try {
       if (typeof id == "undefined") {
         throw new TypeError("Message missing 'id' field");
       }
       if (typeof method == "undefined") {
         throw new TypeError("Message missing 'method' field");
       }
 
-      const [domainName, methodName] = split(method, ".", 1);
+      const [domainName, methodName] = Domains.splitMethod(method);
       if (this.domains.domainSupportsMethod(domainName, methodName)) {
-        const inst = this.domains.get(domainName);
-        const methodFn = inst[methodName];
-        if (!methodFn || typeof methodFn != "function") {
-          throw new Error(`Method implementation of ${methodName} missing`);
-        }
-
-        const result = await methodFn.call(inst, params);
-        this.connection.send({id, result});
+        await this.executeInParent(id, domainName, methodName, params);
       } else {
-        this.mm.sendAsyncMessage("remote:request", {
-          browsingContextId: this.browsingContext.id,
-          request: {id, domainName, methodName, params},
-        });
+        this.executeInChild(id, domainName, methodName, params);
       }
     } catch (e) {
       const error = formatError(e, {stack: true});
       this.connection.send({id, error});
     }
   }
 
+  async executeInParent(id, domain, method, params) {
+    const inst = this.domains.get(domain);
+    const result = await inst[method](params);
+    this.connection.send({id, result});
+  }
+
+  executeInChild(id, domain, method, params) {
+    this.mm.sendAsyncMessage("remote:request", {
+      browsingContextId: this.browsingContext.id,
+      request: {id, domain, method, params},
+    });
+  }
+
   get mm() {
     return this.target.mm;
   }
 
   get browsingContext() {
     return this.target.browsingContext;
   }
 
@@ -101,40 +104,8 @@ class Session {
 
   onevent(eventName, params) {
     this.connection.send({
       method: eventName,
       params,
     });
   }
 }
-
-/**
- * Split s by sep, returning list of substrings.
- * If max is given, at most max splits are done.
- * If max is 0, there is no limit on the number of splits.
- */
-function split(s, sep, max = 0) {
-  if (typeof s != "string" ||
-      typeof sep != "string" ||
-      typeof max != "number") {
-    throw new TypeError();
-  }
-  if (!Number.isInteger(max) || max < 0) {
-    throw new RangeError();
-  }
-
-  const rv = [];
-  let i = 0;
-
-  while (rv.length < max) {
-    const si = s.indexOf(sep, i);
-    if (!si) {
-      break;
-    }
-
-    rv.push(s.substring(i, si));
-    i = si + sep.length;
-  }
-
-  rv.push(s.substring(i));
-  return rv;
-}
--- a/remote/domains/Domains.jsm
+++ b/remote/domains/Domains.jsm
@@ -52,9 +52,44 @@ class Domains extends Map {
     }
   }
 
   clear() {
     for (const name of this.keys()) {
       this.delete(name);
     }
   }
+
+  // Splits a method name, e.g. "Browser.getVersion", into two components.
+  static splitMethod(method) {
+    return split(method, ".", 1);
+  }
 }
+
+// Split s by sep, returning list of substrings.
+// If max is given, at most max splits are done.
+// If max is 0, there is no limit on the number of splits.
+function split(s, sep, max = 0) {
+  if (typeof s != "string" ||
+      typeof sep != "string" ||
+      typeof max != "number") {
+    throw new TypeError();
+  }
+  if (!Number.isInteger(max) || max < 0) {
+    throw new RangeError();
+  }
+
+  const rv = [];
+  let i = 0;
+
+  while (rv.length < max) {
+    const si = s.indexOf(sep, i);
+    if (!si) {
+      break;
+    }
+
+    rv.push(s.substring(i, si));
+    i = si + sep.length;
+  }
+
+  rv.push(s.substring(i));
+  return rv;
+}