Bug 1059001 - Part 2: Move discovery into socket listener. r=past
authorJ. Ryan Stinnett <jryans@gmail.com>
Wed, 10 Dec 2014 20:55:51 -0600
changeset 219131 20a3c5ad55c69e9c2f6de60066a3e3951ce15832
parent 219130 b16c58046543597ba9b4b6632cbaacf2a32f2814
child 219132 40e8d75da54f24fc2f48361bdb5760d570f4d993
push id27956
push userkwierso@gmail.com
push dateFri, 12 Dec 2014 00:47:19 +0000
treeherdermozilla-central@32a2c5bd2f68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspast
bugs1059001
milestone37.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 1059001 - Part 2: Move discovery into socket listener. r=past Don't make the socket listener set up discover manually. Also, future additions (authentication) for WiFi debugging will add extra information to the discovery packet.
b2g/chrome/content/devtools/debugger.js
toolkit/devtools/security/socket.js
--- a/b2g/chrome/content/devtools/debugger.js
+++ b/b2g/chrome/content/devtools/debugger.js
@@ -12,20 +12,16 @@ XPCOMUtils.defineLazyGetter(this, "Debug
 });
 
 XPCOMUtils.defineLazyGetter(this, "devtools", function() {
   const { devtools } =
     Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
   return devtools;
 });
 
-XPCOMUtils.defineLazyGetter(this, "discovery", function() {
-  return devtools.require("devtools/toolkit/discovery/discovery");
-});
-
 XPCOMUtils.defineLazyGetter(this, "B2GTabList", function() {
   const { B2GTabList } =
     devtools.require("resource://gre/modules/DebuggerActors.js");
   return B2GTabList;
 });
 
 let RemoteDebugger = {
   _promptDone: false,
@@ -181,32 +177,31 @@ let WiFiRemoteDebugger = {
 
     RemoteDebugger.initServer();
 
     try {
       debug("Starting WiFi debugger");
       this._listener = DebuggerServer.createListener();
       this._listener.portOrPath = -1 /* any available port */;
       this._listener.allowConnection = RemoteDebugger.prompt;
+      this._listener.discoverable = true;
       this._listener.open();
       let port = this._listener.port;
       debug("Started WiFi debugger on " + port);
-      discovery.addService("devtools", { port: port });
     } catch (e) {
       debug("Unable to start WiFi debugger server: " + e);
     }
   },
 
   stop: function() {
     if (!this._listener) {
       return;
     }
 
     try {
-      discovery.removeService("devtools");
       this._listener.close();
       this._listener = null;
     } catch (e) {
       debug("Unable to stop WiFi debugger server: " + e);
     }
   }
 
 };
--- a/toolkit/devtools/security/socket.js
+++ b/toolkit/devtools/security/socket.js
@@ -9,16 +9,18 @@
 let { Ci, Cc, CC, Cr } = require("chrome");
 let Services = require("Services");
 let DevToolsUtils = require("devtools/toolkit/DevToolsUtils");
 let { dumpn } = DevToolsUtils;
 loader.lazyRequireGetter(this, "DebuggerTransport",
   "devtools/toolkit/transport/transport", true);
 loader.lazyRequireGetter(this, "DebuggerServer",
   "devtools/server/main", true);
+loader.lazyRequireGetter(this, "discovery",
+  "devtools/toolkit/discovery/discovery");
 
 DevToolsUtils.defineLazyGetter(this, "ServerSocket", () => {
   return CC("@mozilla.org/network/server-socket;1",
             "nsIServerSocket",
             "initSpecialConnection");
 });
 
 DevToolsUtils.defineLazyGetter(this, "UnixDomainServerSocket", () => {
@@ -121,22 +123,31 @@ SocketListener.prototype = {
    * implementation is used unless this is overridden on a particular socket
    * listener instance.
    *
    * @return true if the connection should be permitted, false otherwise
    */
   allowConnection: SocketListener.defaultAllowConnection,
 
   /**
+   * Controls whether this listener is announced via the service discovery
+   * mechanism.
+   */
+  discoverable: false,
+
+  /**
    * Validate that all options have been set to a supported configuration.
    */
   _validateOptions: function() {
     if (this.portOrPath === null) {
       throw new Error("Must set a port / path to listen on.");
     }
+    if (this.discoverable && !Number(this.portOrPath)) {
+      throw new Error("Discovery only supported for TCP sockets.");
+    }
   },
 
   /**
    * Listens on the given port or socket file for remote debugger connections.
    */
   open: function() {
     this._validateOptions();
     DebuggerServer._addListener(this);
@@ -161,23 +172,30 @@ SocketListener.prototype = {
       }
       this._socket.asyncListen(this);
     } catch (e) {
       dumpn("Could not start debugging listener on '" + this.portOrPath +
             "': " + e);
       this.close();
       throw Cr.NS_ERROR_NOT_AVAILABLE;
     }
+
+    if (this.discoverable && this.port) {
+      discovery.addService("devtools", { port: this.port });
+    }
   },
 
   /**
    * Closes the SocketListener.  Notifies the server to remove the listener from
    * the set of active SocketListeners.
    */
   close: function() {
+    if (this.discoverable && this.port) {
+      discovery.removeService("devtools");
+    }
     if (this._socket) {
       this._socket.close();
       this._socket = null;
     }
     DebuggerServer._removeListener(this);
   },
 
   /**