Bug 1344748 - Make testing/marionette/server.js a class; r=maja_zf,whimboo, a=test-only
authorAndreas Tolfsen <ato@mozilla.com>
Mon, 06 Mar 2017 17:09:07 +0000
changeset 395538 d30b685a8811d6e495ea4b9195cfa153fc0c06e9
parent 395537 0b16ceaefbeb2b8d2661cae10616d4276185619a
child 395539 f41b674de525db4f6962712ab5399285f04949ff
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmaja_zf, whimboo, test-only
bugs1344748
milestone54.0a2
Bug 1344748 - Make testing/marionette/server.js a class; r=maja_zf,whimboo, a=test-only No functional changes apart from class'ifying the file and harmonising the export symbol with the rest of the Marionette code base by renaming the MarionetteServer class to server.TCPListener. MozReview-Commit-ID: 8WJKoprGGvr
testing/marionette/components/marionette.js
testing/marionette/server.js
tools/lint/eslint/modules.json
--- a/testing/marionette/components/marionette.js
+++ b/testing/marionette/components/marionette.js
@@ -236,17 +236,17 @@ MarionetteComponent.prototype.init = fun
     insaneSacrificialGoat.asyncListen(this);
   }
 
   let port = Preferences.get(PORT_PREF, DEFAULT_PORT);
 
   let s;
   try {
     Cu.import("chrome://marionette/content/server.js");
-    s = new MarionetteServer(port, forceLocal);
+    s = new server.TCPListener(port, forceLocal);
     s.start();
     this.logger.info(`Listening on port ${s.port}`);
   } catch (e) {
     this.logger.error(`Error on starting server: ${e}`);
     dump(e.toString() + "\n" + e.stack + "\n");
   } finally {
     if (s) {
       this.server = s;
--- a/testing/marionette/server.js
+++ b/testing/marionette/server.js
@@ -18,17 +18,18 @@ Cu.import("chrome://marionette/content/d
 Cu.import("chrome://marionette/content/element.js");
 Cu.import("chrome://marionette/content/simpletest.js");
 
 // Bug 1083711: Load transport.js as an SDK module instead of subscript
 loader.loadSubScript("resource://devtools/shared/transport/transport.js");
 
 const logger = Log.repository.getLogger("Marionette");
 
-this.EXPORTED_SYMBOLS = ["MarionetteServer"];
+this.EXPORTED_SYMBOLS = ["server"];
+this.server = {};
 
 const CONTENT_LISTENER_PREF = "marionette.contentListener";
 
 // Marionette sets preferences recommended for automation when it starts,
 // unless |marionette.prefs.recommended| has been set to false.
 // Where noted, some prefs should also be set in the profile passed to
 // Marionette to prevent them from affecting startup, since some of these
 // are checked before Marionette initialises.
@@ -250,120 +251,122 @@ const RECOMMENDED_PREFS = new Map([
 ]);
 
 /**
  * Bootstraps Marionette and handles incoming client connections.
  *
  * Once started, it opens a TCP socket sporting the debugger transport
  * protocol on the provided port.  For every new client a Dispatcher is
  * created.
- *
- * @param {number} port
- *     Port for server to listen to.
- * @param {boolean} forceLocal
- *     Listen only to connections from loopback if true.  If false,
- *     accept all connections.
  */
-this.MarionetteServer = function (port, forceLocal) {
-  this.port = port;
-  this.forceLocal = forceLocal;
-  this.conns = {};
-  this.nextConnId = 0;
-  this.alive = false;
-  this._acceptConnections = false;
-  this.alteredPrefs = new Set();
-};
-
-/**
- * Function produces a GeckoDriver.
- *
- * Determines application nameto initialise the driver with.
- *
- * @return {GeckoDriver}
- *     A driver instance.
- */
-MarionetteServer.prototype.driverFactory = function() {
-  Preferences.set(CONTENT_LISTENER_PREF, false);
-  return new GeckoDriver(Services.appinfo.name, this);
-};
-
-MarionetteServer.prototype.__defineSetter__("acceptConnections", function (value) {
-  if (!value) {
-    logger.info("New connections will no longer be accepted");
-  } else {
-    logger.info("New connections are accepted again");
+server.TCPListener = class {
+  /**
+   * @param {number} port
+   *     Port for server to listen to.
+   * @param {boolean} forceLocal
+   *     Listen only to connections from loopback if true.  If false,
+   *     accept all connections.
+   */
+  constructor (port, forceLocal) {
+    this.port = port;
+    this.forceLocal = forceLocal;
+    this.conns = {};
+    this.nextConnId = 0;
+    this.alive = false;
+    this._acceptConnections = false;
+    this.alteredPrefs = new Set();
   }
 
-  this._acceptConnections = value;
-});
-
-MarionetteServer.prototype.start = function() {
-  if (this.alive) {
-    return;
+  /**
+   * Function produces a GeckoDriver.
+   *
+   * Determines application nameto initialise the driver with.
+   *
+   * @return {GeckoDriver}
+   *     A driver instance.
+   */
+  driverFactory () {
+    Preferences.set(CONTENT_LISTENER_PREF, false);
+    return new GeckoDriver(Services.appinfo.name, this);
   }
 
-  // set recommended preferences if they are not already user-defined
-  for (let [k, v] of RECOMMENDED_PREFS) {
-    if (!Preferences.isSet(k)) {
-      logger.debug(`Setting recommended pref ${k} to ${v}`);
-      Preferences.set(k, v);
-      this.alteredPrefs.add(k);
+  set acceptConnections (value) {
+    if (!value) {
+      logger.info("New connections will no longer be accepted");
+    } else {
+      logger.info("New connections are accepted again");
     }
+
+    this._acceptConnections = value;
   }
 
-  let flags = Ci.nsIServerSocket.KeepWhenOffline;
-  if (this.forceLocal) {
-    flags |= Ci.nsIServerSocket.LoopbackOnly;
-  }
-  this.listener = new ServerSocket(this.port, flags, 1);
-  this.listener.asyncListen(this);
+  start () {
+    if (this.alive) {
+      return;
+    }
 
-  this.alive = true;
-  this._acceptConnections = true;
-};
+    // set recommended preferences if they are not already user-defined
+    for (let [k, v] of RECOMMENDED_PREFS) {
+      if (!Preferences.isSet(k)) {
+        logger.debug(`Setting recommended pref ${k} to ${v}`);
+        Preferences.set(k, v);
+        this.alteredPrefs.add(k);
+      }
+    }
 
-MarionetteServer.prototype.stop = function() {
-  if (!this.alive) {
-    return;
+    let flags = Ci.nsIServerSocket.KeepWhenOffline;
+    if (this.forceLocal) {
+      flags |= Ci.nsIServerSocket.LoopbackOnly;
+    }
+    this.listener = new ServerSocket(this.port, flags, 1);
+    this.listener.asyncListen(this);
+
+    this.alive = true;
+    this._acceptConnections = true;
   }
 
-  for (let k of this.alteredPrefs) {
-    logger.debug(`Resetting recommended pref ${k}`);
-    Preferences.reset(k);
-  }
-  this.closeListener();
-
-  this.alteredPrefs.clear();
-  this.alive = false;
-  this._acceptConnections = false;
-};
+  stop () {
+    if (!this.alive) {
+      return;
+    }
 
-MarionetteServer.prototype.closeListener = function() {
-  this.listener.close();
-  this.listener = null;
-};
+    for (let k of this.alteredPrefs) {
+      logger.debug(`Resetting recommended pref ${k}`);
+      Preferences.reset(k);
+    }
+    this.closeListener();
 
-MarionetteServer.prototype.onSocketAccepted = function (
-    serverSocket, clientSocket) {
-  if (!this._acceptConnections) {
-    logger.warn("New connections are currently not accepted");
-    return;
+    this.alteredPrefs.clear();
+    this.alive = false;
+    this._acceptConnections = false;
+  }
+
+  closeListener () {
+    this.listener.close();
+    this.listener = null;
   }
 
-  let input = clientSocket.openInputStream(0, 0, 0);
-  let output = clientSocket.openOutputStream(0, 0, 0);
-  let transport = new DebuggerTransport(input, output);
-  let connId = "conn" + this.nextConnId++;
+  onSocketAccepted (serverSocket, clientSocket) {
+    if (!this._acceptConnections) {
+      logger.warn("New connections are currently not accepted");
+      return;
+    }
 
-  let dispatcher = new Dispatcher(connId, transport, this.driverFactory.bind(this));
-  dispatcher.onclose = this.onConnectionClosed.bind(this);
-  this.conns[connId] = dispatcher;
+    let input = clientSocket.openInputStream(0, 0, 0);
+    let output = clientSocket.openOutputStream(0, 0, 0);
+    let transport = new DebuggerTransport(input, output);
+    let connId = "conn" + this.nextConnId++;
 
-  logger.debug(`Accepted connection ${connId} from ${clientSocket.host}:${clientSocket.port}`);
-  dispatcher.sayHello();
-  transport.ready();
-};
+    let dispatcher = new Dispatcher(connId, transport, this.driverFactory.bind(this));
+    dispatcher.onclose = this.onConnectionClosed.bind(this);
+    this.conns[connId] = dispatcher;
 
-MarionetteServer.prototype.onConnectionClosed = function (conn) {
-  let id = conn.connId;
-  delete this.conns[id];
-  logger.debug(`Closed connection ${id}`);
+    logger.debug(`Accepted connection ${connId} from ${clientSocket.host}:${clientSocket.port}`);
+    dispatcher.sayHello();
+    transport.ready();
+  }
+
+  onConnectionClosed (conn) {
+    let id = conn.connId;
+    delete this.conns[id];
+    logger.debug(`Closed connection ${id}`);
+  }
 };
--- a/tools/lint/eslint/modules.json
+++ b/tools/lint/eslint/modules.json
@@ -189,17 +189,17 @@
   "RemoteWebProgress.jsm": ["RemoteWebProgressManager"],
   "resource.js": ["AsyncResource", "Resource"],
   "responsivedesign.jsm": ["ResponsiveUIManager"],
   "rest.js": ["RESTRequest", "RESTResponse", "TokenAuthenticatedRESTRequest", "SyncStorageRequest"],
   "rotaryengine.js": ["RotaryEngine", "RotaryRecord", "RotaryStore", "RotaryTracker"],
   "require.js": ["require"],
   "RTCStatsReport.jsm": ["convertToRTCStatsReport"],
   "scratchpad-manager.jsm": ["ScratchpadManager"],
-  "server.js": ["MarionetteServer"],
+  "server.js": ["server"],
   "service.js": ["Service"],
   "SharedPromptUtils.jsm": ["PromptUtils", "EnableDelayHelper"],
   "ShutdownLeaksCollector.jsm": ["ContentCollector"],
   "SignInToWebsite.jsm": ["SignInToWebsiteController"],
   "Social.jsm": ["Social", "OpenGraphBuilder", "DynamicResizeWatcher", "sizeSocialPanelToContent"],
   "SpecialPowersObserver.jsm": ["SpecialPowersObserver", "SpecialPowersObserverFactory"],
   "stack.js": ["findCallerFrame"],
   "StateMachineHelper.jsm": ["State", "CommandType"],