dom/network/src/TCPServerSocket.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 const Cc = Components.classes;
     8 const Ci = Components.interfaces;
     9 const Cu = Components.utils;
    10 const Cr = Components.results;
    11 const CC = Components.Constructor;
    13 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    15 const ServerSocket = CC(
    16         '@mozilla.org/network/server-socket;1', 'nsIServerSocket', 'init'),
    17       TCPSocketInternal = Cc[
    18         '@mozilla.org/tcp-socket;1'].createInstance(Ci.nsITCPSocketInternal);
    20 /*
    21  * Debug logging function
    22  */
    24 let debug = true;
    25 function LOG(msg) {
    26   if (debug) {
    27     dump("TCPServerSocket: " + msg + "\n");
    28   }
    29 }
    31 /*
    32  * nsIDOMTCPServerSocket object
    33  */
    35 function TCPServerSocket() {
    36   this._localPort = 0;
    37   this._binaryType = null;
    39   this._onconnect = null;
    40   this._onerror = null;
    42   this._inChild = false;
    43   this._neckoTCPServerSocket = null;
    44   this._serverBridge = null;
    45   this.useWin = null;
    46 }
    48 TCPServerSocket.prototype = {
    49   __exposedProps__: {
    50     port: 'r',
    51     onconnect: 'rw',
    52     onerror: 'rw'
    53   },
    54   get localPort() {
    55     return this._localPort;
    56   },
    57   get onconnect() {
    58     return this._onconnect;
    59   },
    60   set onconnect(f) {
    61     this._onconnect = f;
    62   },
    63   get onerror() {
    64     return this._onerror;
    65   },
    66   set onerror(f) {
    67     this._onerror = f;
    68   },
    70   _callListenerAcceptCommon: function tss_callListenerAcceptCommon(socket) {
    71     if (this._onconnect) {
    72       try {
    73         this["onconnect"].call(null, socket);
    74       } catch (e) {
    75         socket.close();
    76       }      
    77     }
    78     else {
    79       socket.close();
    80       dump("Received unexpected connection!");
    81     }
    82   },
    83   init: function tss_init(aWindowObj) {
    84     this.useWin = aWindowObj;
    85   },
    87   /* nsITCPServerSocketInternal method */
    88   listen: function tss_listen(localPort, options, backlog) {
    89     this._inChild = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
    90                        .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
    91     this._binaryType = options.binaryType;
    93     if (this._inChild) {
    94       if (this._serverBridge == null) {
    95         this._serverBridge = Cc["@mozilla.org/tcp-server-socket-child;1"]
    96                                 .createInstance(Ci.nsITCPServerSocketChild);
    97         this._serverBridge.listen(this, localPort, backlog, options.binaryType);
    98       }
    99       else {
   100         throw new Error("Child TCPServerSocket has already listening. \n");
   101       }
   102     }
   103     else {
   104       if (this._neckoTCPServerSocket == null) {
   105         this._neckoTCPServerSocket = new ServerSocket(localPort, false, backlog);
   106         this._localPort = this._neckoTCPServerSocket.port;
   107         this._neckoTCPServerSocket.asyncListen(this);
   108       }
   109       else {
   110         throw new Error("Parent TCPServerSocket has already listening. \n");
   111       }
   112     }
   113   },
   115   callListenerAccept: function tss_callListenerSocket(socketChild) {
   116     // this method is called at child process when the socket is accepted at parent process.
   117     let socket = TCPSocketInternal.createAcceptedChild(socketChild, this._binaryType, this.useWin);
   118     this._callListenerAcceptCommon(socket);
   119   },
   121   callListenerError: function tss_callListenerError(message, filename, lineNumber, columnNumber) {
   122     if (this._onerror) {
   123       var type = "error";
   124       var error = new Error(message, filename, lineNumber, columnNumber);
   126       this["onerror"].call(null, new TCPSocketEvent(type, this, error));
   127     }    
   128   },
   129   /* end nsITCPServerSocketInternal method */
   131   close: function tss_close() {
   132     if (this._inChild) {
   133       this._serverBridge.close();
   134       return;
   135     }
   137     /* Close ServerSocket */
   138     if (this._neckoTCPServerSocket) {
   139       this._neckoTCPServerSocket.close();
   140     }
   141   },
   143   // nsIServerSocketListener (Triggered by _neckoTCPServerSocket.asyncListen)
   144   onSocketAccepted: function tss_onSocketAccepted(server, trans) {
   145     // precondition: this._inChild == false
   146     try {
   147       let that = TCPSocketInternal.createAcceptedParent(trans, this._binaryType);
   148       this._callListenerAcceptCommon(that);
   149     }
   150     catch(e) {
   151       trans.close(Cr.NS_BINDING_ABORTED);
   152     }
   153   },
   155   // nsIServerSocketListener (Triggered by _neckoTCPServerSocket.asyncListen)
   156   onStopListening: function tss_onStopListening(server, status) {
   157     if (status != Cr.NS_BINDING_ABORTED) {
   158       throw new Error("Server socket was closed by unexpected reason.");
   159     }
   160     this._neckoTCPServerSocket = null;
   161   },
   163   classID: Components.ID("{73065eae-27dc-11e2-895a-000c29987aa2}"),
   165   classInfo: XPCOMUtils.generateCI({
   166     classID: Components.ID("{73065eae-27dc-11e2-895a-000c29987aa2}"),
   167     classDescription: "Server TCP Socket",
   168     interfaces: [
   169       Ci.nsIDOMTCPServerSocket,
   170       Ci.nsISupportsWeakReference
   171     ],
   172     flags: Ci.nsIClassInfo.DOM_OBJECT,
   173   }),
   175   QueryInterface: XPCOMUtils.generateQI([
   176     Ci.nsIDOMTCPServerSocket,
   177     Ci.nsITCPServerSocketInternal,
   178     Ci.nsISupportsWeakReference
   179   ])
   180 }
   182 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TCPServerSocket]);

mercurial