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