Bug 1388082 - Drop Task.jsm from Marionette. r?automatedtester draft
authorAndreas Tolfsen <ato@sny.no>
Mon, 07 Aug 2017 19:02:20 +0100
changeset 645940 651960625f719e7927b214f819c54937102eaa3b
parent 645939 33bb0813c29dcaceb2f17f74455fe3343fb4d476
child 726084 cb09252962e6f82c5cc47771875dfa41620ca01d
push id73947
push userbmo:ato@sny.no
push dateMon, 14 Aug 2017 16:03:22 +0000
reviewersautomatedtester
bugs1388082
milestone57.0a1
Bug 1388082 - Drop Task.jsm from Marionette. r?automatedtester MozReview-Commit-ID: HbEHjOv1W3g
testing/marionette/server.js
--- a/testing/marionette/server.js
+++ b/testing/marionette/server.js
@@ -11,17 +11,16 @@ const loader = Cc["@mozilla.org/moz/jssu
 const ServerSocket = CC(
     "@mozilla.org/network/server-socket;1",
     "nsIServerSocket",
     "initSpecialConnection");
 
 Cu.import("resource://gre/modules/Log.jsm");
 Cu.import("resource://gre/modules/Preferences.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
-Cu.import("resource://gre/modules/Task.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 Cu.import("chrome://marionette/content/assert.js");
 const {GeckoDriver} = Cu.import("chrome://marionette/content/driver.js", {});
 const {
   error,
   UnknownCommandError,
 } = Cu.import("chrome://marionette/content/error.js", {});
@@ -488,68 +487,77 @@ server.TCPConnection = class {
     // look up previous command we received a response for
     if (msg instanceof Response) {
       let cmd = this.commands_.get(msg.id);
       this.commands_.delete(msg.id);
       cmd.onresponse(msg);
 
     // execute new command
     } else if (msg instanceof Command) {
-      this.lastID = msg.id;
-      this.execute(msg);
+      (async () => {
+        await this.execute(msg);
+      })();
     }
   }
 
   /**
-   * Executes a WebDriver command and sends back a response when it has
-   * finished executing.
-   *
-   * Commands implemented in GeckoDriver and registered in its
-   * {@code GeckoDriver.commands} attribute.  The return values from
-   * commands are expected to be Promises.  If the resolved value of said
-   * promise is not an object, the response body will be wrapped in
-   * an object under a "value" field.
+   * Executes a Marionette command and sends back a response when it
+   * has finished executing.
    *
    * If the command implementation sends the response itself by calling
-   * {@code resp.send()}, the response is guaranteed to not be sent twice.
+   * <code>resp.send()</code>, the response is guaranteed to not be
+   * sent twice.
    *
    * Errors thrown in commands are marshaled and sent back, and if they
-   * are not WebDriverError instances, they are additionally propagated
-   * and reported to {@code Components.utils.reportError}.
+   * are not {@link WebDriverError} instances, they are additionally
+   * propagated and reported to {@link Components.utils.reportError}.
    *
    * @param {Command} cmd
-   *     The requested command to execute.
+   *     Command to execute.
    */
-  execute(cmd) {
+  async execute(cmd) {
     let resp = this.createResponse(cmd.id);
     let sendResponse = () => resp.sendConditionally(resp => !resp.sent);
     let sendError = resp.sendError.bind(resp);
 
-    let req = Task.spawn(function* () {
-      let fn = this.driver.commands[cmd.name];
-      if (typeof fn == "undefined") {
-        throw new UnknownCommandError(cmd.name);
-      }
-
-      if (cmd.name !== "newSession") {
-        assert.session(this.driver);
-      }
+    await this.despatch(cmd, resp)
+        .then(sendResponse, sendError).catch(error.report);
+  }
 
-      let rv = yield fn.bind(this.driver)(cmd, resp);
+  /**
+   * Despatches command to appropriate Marionette service.
+   *
+   * @param {Command} cmd
+   *     Command to run.
+   * @param {Response} resp
+   *     Mutable response where the command's return value will be
+   *     assigned.
+   *
+   * @throws {Error}
+   *     A command's implementation may throw at any time.
+   */
+  async despatch(cmd, resp) {
+    let fn = this.driver.commands[cmd.name];
+    if (typeof fn == "undefined") {
+      throw new UnknownCommandError(cmd.name);
+    }
 
-      if (typeof rv != "undefined") {
-        if (typeof rv != "object") {
-          resp.body = {value: rv};
-        } else {
-          resp.body = rv;
-        }
+    if (!["newSession", "WebDriver:NewSession"].includes(cmd.name)) {
+      assert.session(this.driver);
+    }
+
+    let rv = await fn.bind(this.driver)(cmd, resp);
+
+    if (typeof rv != "undefined") {
+      if (typeof rv != "object") {
+        resp.body = {value: rv};
+      } else {
+        resp.body = rv;
       }
-    }.bind(this));
-
-    req.then(sendResponse, sendError).catch(error.report);
+    }
   }
 
   /**
    * Fail-safe creation of a new instance of |message.Response|.
    *
    * @param {number} msgID
    *     Message ID to respond to.  If it is not a number, -1 is used.
    *