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/. */
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);
21 * Debug logging function
27 dump("TCPServerSocket: " + msg + "\n");
32 * nsIDOMTCPServerSocket object
35 function TCPServerSocket() {
37 this._binaryType = null;
39 this._onconnect = null;
42 this._inChild = false;
43 this._neckoTCPServerSocket = null;
44 this._serverBridge = null;
48 // When this API moves to WebIDL and these __exposedProps__ go away, remove
49 // this call here and remove the API from XPConnect.
50 Cu.skipCOWCallableChecks();
52 TCPServerSocket.prototype = {
59 return this._localPort;
62 return this._onconnect;
74 _callListenerAcceptCommon: function tss_callListenerAcceptCommon(socket) {
75 if (this._onconnect) {
77 this["onconnect"].call(null, socket);
84 dump("Received unexpected connection!");
87 init: function tss_init(aWindowObj) {
88 this.useWin = aWindowObj;
91 /* nsITCPServerSocketInternal method */
92 listen: function tss_listen(localPort, options, backlog) {
93 this._inChild = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
94 .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
95 this._binaryType = options.binaryType;
98 if (this._serverBridge == null) {
99 this._serverBridge = Cc["@mozilla.org/tcp-server-socket-child;1"]
100 .createInstance(Ci.nsITCPServerSocketChild);
101 this._serverBridge.listen(this, localPort, backlog, options.binaryType);
104 throw new Error("Child TCPServerSocket has already listening. \n");
108 if (this._neckoTCPServerSocket == null) {
109 this._neckoTCPServerSocket = new ServerSocket(localPort, false, backlog);
110 this._localPort = this._neckoTCPServerSocket.port;
111 this._neckoTCPServerSocket.asyncListen(this);
114 throw new Error("Parent TCPServerSocket has already listening. \n");
119 callListenerAccept: function tss_callListenerSocket(socketChild) {
120 // this method is called at child process when the socket is accepted at parent process.
121 let socket = TCPSocketInternal.createAcceptedChild(socketChild, this._binaryType, this.useWin);
122 this._callListenerAcceptCommon(socket);
125 callListenerError: function tss_callListenerError(message, filename, lineNumber, columnNumber) {
128 var error = new Error(message, filename, lineNumber, columnNumber);
130 this["onerror"].call(null, new TCPSocketEvent(type, this, error));
133 /* end nsITCPServerSocketInternal method */
135 close: function tss_close() {
137 this._serverBridge.close();
141 /* Close ServerSocket */
142 if (this._neckoTCPServerSocket) {
143 this._neckoTCPServerSocket.close();
147 // nsIServerSocketListener (Triggered by _neckoTCPServerSocket.asyncListen)
148 onSocketAccepted: function tss_onSocketAccepted(server, trans) {
149 // precondition: this._inChild == false
151 let that = TCPSocketInternal.createAcceptedParent(trans, this._binaryType,
153 this._callListenerAcceptCommon(that);
156 trans.close(Cr.NS_BINDING_ABORTED);
160 // nsIServerSocketListener (Triggered by _neckoTCPServerSocket.asyncListen)
161 onStopListening: function tss_onStopListening(server, status) {
162 if (status != Cr.NS_BINDING_ABORTED) {
163 throw new Error("Server socket was closed by unexpected reason.");
165 this._neckoTCPServerSocket = null;
168 classID: Components.ID("{73065eae-27dc-11e2-895a-000c29987aa2}"),
170 classInfo: XPCOMUtils.generateCI({
171 classID: Components.ID("{73065eae-27dc-11e2-895a-000c29987aa2}"),
172 classDescription: "Server TCP Socket",
174 Ci.nsIDOMTCPServerSocket,
175 Ci.nsISupportsWeakReference
177 flags: Ci.nsIClassInfo.DOM_OBJECT,
180 QueryInterface: XPCOMUtils.generateQI([
181 Ci.nsIDOMTCPServerSocket,
182 Ci.nsITCPServerSocketInternal,
183 Ci.nsISupportsWeakReference
187 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TCPServerSocket]);