Bug 832000 - Use unix domain socket file for devtools remote connection. r=jimb, r=vingtetun
authorAlexandre Poirot <poirot.alex@gmail.com>
Tue, 10 Sep 2013 12:52:33 -0400
changeset 146376 6a8d3277faaf0a7dcc073075e8b3a14e9651153d
parent 146375 16af6a5074b4506465a679c49d2f50d74f0eb03b
child 146377 511d7aa49409a6f18f5199d20d6036947b9b3b3d
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersjimb, vingtetun
bugs832000
milestone26.0a1
Bug 832000 - Use unix domain socket file for devtools remote connection. r=jimb, r=vingtetun
b2g/app/b2g.js
b2g/chrome/content/shell.js
toolkit/devtools/server/actors/webapps.js
toolkit/devtools/server/main.js
--- a/b2g/app/b2g.js
+++ b/b2g/app/b2g.js
@@ -746,12 +746,16 @@ pref("disk_space_watcher.enabled", true)
 // Enable promise
 pref("dom.promise.enabled", false);
 
 // Allow ADB to run for this many hours before disabling
 // (only applies when marionette is disabled)
 // 0 disables the timer.
 pref("b2g.adb.timeout-hours", 12);
 
+// Absolute path to the devtool unix domain socket file used
+// to communicate with a usb cable via adb forward
+pref("devtools.debugger.unix-domain-socket", "/data/local/debugger-socket");
+
 // enable Skia/GL (OpenGL-accelerated 2D drawing) for large enough 2d canvases,
 // falling back to Skia/software for smaller canvases
 pref("gfx.canvas.azure.backends", "skia");
 pref("gfx.canvas.azure.accelerated", true);
--- a/b2g/chrome/content/shell.js
+++ b/b2g/chrome/content/shell.js
@@ -1025,19 +1025,20 @@ let RemoteDebugger = {
       DebuggerServer.addActors("resource://gre/modules/devtools/server/actors/webapps.js");
       DebuggerServer.registerModule("devtools/server/actors/device");
 
       DebuggerServer.onConnectionChange = function(what) {
         AdbController.updateState();
       }
     }
 
-    let port = Services.prefs.getIntPref('devtools.debugger.remote-port') || 6000;
+    let path = Services.prefs.getCharPref("devtools.debugger.unix-domain-socket") ||
+               "/data/local/debugger-socket";
     try {
-      DebuggerServer.openListener(port);
+      DebuggerServer.openListener(path);
     } catch (e) {
       dump('Unable to start debugger server: ' + e + '\n');
     }
   },
 
   stop: function debugger_stop() {
     if (!DebuggerServer.initialized) {
       return;
--- a/toolkit/devtools/server/actors/webapps.js
+++ b/toolkit/devtools/server/actors/webapps.js
@@ -180,17 +180,18 @@ WebappsActor.prototype = {
                        });
 
         // We can't have appcache for packaged apps.
         if (!aApp.origin.startsWith("app://")) {
           reg.startOfflineCacheDownload(new ManifestHelper(manifest, aApp.origin));
         }
       });
       // Cleanup by removing the temporary directory.
-      aDir.remove(true);
+      if (aDir.exists())
+        aDir.remove(true);
     });
   },
 
   _sendError: function wa_actorSendError(aMsg, aId) {
     debug("Sending error: " + aMsg);
     this.conn.send(
       { from: this.actorID,
         type: "webappsEvent",
--- a/toolkit/devtools/server/main.js
+++ b/toolkit/devtools/server/main.js
@@ -38,16 +38,17 @@ if (this.require) {
   localRequire = id => require(id);
 } else {
   let { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
   localRequire = id => devtools.require(id);
 }
 
 const DBG_STRINGS_URI = "chrome://global/locale/devtools/debugger.properties";
 
+const nsFile = CC("@mozilla.org/file/local;1", "nsIFile", "initWithPath");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 let wantLogging = Services.prefs.getBoolPref("devtools.debugger.log");
 const promptConnections = Services.prefs.getBoolPref("devtools.debugger.prompt-connection");
 
 Cu.import("resource://gre/modules/jsdebugger.jsm");
 addDebuggerToGlobal(this);
 
@@ -87,16 +88,19 @@ function dbg_assert(cond, e) {
 }
 
 loadSubScript.call(this, "resource://gre/modules/devtools/server/transport.js");
 
 // XPCOM constructors
 const ServerSocket = CC("@mozilla.org/network/server-socket;1",
                         "nsIServerSocket",
                         "initSpecialConnection");
+const UnixDomainServerSocket = CC("@mozilla.org/network/server-socket;1",
+                                  "nsIServerSocket",
+                                  "initWithFilename");
 
 var gRegisteredModules = Object.create(null);
 
 /**
  * The ModuleAPI object is passed to modules loaded using the
  * DebuggerServer.registerModule() API.  Modules can use this
  * object to register actor factories.
  * Factories registered through the module API will be removed
@@ -379,22 +383,23 @@ var DebuggerServer = {
       this.registerModule("devtools/server/actors/inspector");
     }
     if (!("ContentTabActor" in DebuggerServer)) {
       this.addActors("resource://gre/modules/devtools/server/actors/childtab.js");
     }
   },
 
   /**
-   * Listens on the given port for remote debugger connections.
+   * Listens on the given port or socket file for remote debugger connections.
    *
-   * @param aPort int
-   *        The port to listen on.
+   * @param aPortOrPath int, string
+   *        If given an integer, the port to listen on.
+   *        Otherwise, the path to the unix socket domain file to listen on.
    */
-  openListener: function DS_openListener(aPort) {
+  openListener: function DS_openListener(aPortOrPath) {
     if (!Services.prefs.getBoolPref("devtools.debugger.remote-enabled")) {
       return false;
     }
     this._checkInit();
 
     // Return early if the server is already listening.
     if (this._listener) {
       return true;
@@ -402,21 +407,31 @@ var DebuggerServer = {
 
     let flags = Ci.nsIServerSocket.KeepWhenOffline;
     // A preference setting can force binding on the loopback interface.
     if (Services.prefs.getBoolPref("devtools.debugger.force-local")) {
       flags |= Ci.nsIServerSocket.LoopbackOnly;
     }
 
     try {
-      let socket = new ServerSocket(aPort, flags, 4);
+      let backlog = 4;
+      let socket;
+      let port = Number(aPortOrPath);
+      if (port) {
+        socket = new ServerSocket(port, flags, backlog);
+      } else {
+        let file = nsFile(aPortOrPath);
+        if (file.exists())
+          file.remove(false);
+        socket = new UnixDomainServerSocket(file, parseInt("600", 8), backlog);
+      }
       socket.asyncListen(this);
       this._listener = socket;
     } catch (e) {
-      dumpn("Could not start debugging listener on port " + aPort + ": " + e);
+      dumpn("Could not start debugging listener on '" + aPortOrPath + "': " + e);
       throw Cr.NS_ERROR_NOT_AVAILABLE;
     }
     this._socketConnections++;
 
     return true;
   },
 
   /**