Bumping manifests a=b2g-bump
[gecko.git] / dom / network / TCPServerSocket.js
blobbea8308b7b22be763b68b297bc94ae93e854947b
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);
21  * Debug logging function
22  */
24 let debug = true;
25 function LOG(msg) {
26   if (debug) {
27     dump("TCPServerSocket: " + msg + "\n");
28   }
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;
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 = {
53   __exposedProps__: {
54     localPort: 'r',
55     onconnect: 'rw',
56     onerror: 'rw'
57   },
58   get localPort() {
59     return this._localPort;
60   },
61   get onconnect() {
62     return this._onconnect;
63   },
64   set onconnect(f) {
65     this._onconnect = f;
66   },
67   get onerror() {
68     return this._onerror;
69   },
70   set onerror(f) {
71     this._onerror = f;
72   },
74   _callListenerAcceptCommon: function tss_callListenerAcceptCommon(socket) {
75     if (this._onconnect) {
76       try {
77         this["onconnect"].call(null, socket);
78       } catch (e) {
79         socket.close();
80       }
81     }
82     else {
83       socket.close();
84       dump("Received unexpected connection!");
85     }
86   },
87   init: function tss_init(aWindowObj) {
88     this.useWin = aWindowObj;
89   },
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;
97     if (this._inChild) {
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);
102       }
103       else {
104         throw new Error("Child TCPServerSocket has already listening. \n");
105       }
106     }
107     else {
108       if (this._neckoTCPServerSocket == null) {
109         this._neckoTCPServerSocket = new ServerSocket(localPort, false, backlog);
110         this._localPort = this._neckoTCPServerSocket.port;
111         this._neckoTCPServerSocket.asyncListen(this);
112       }
113       else {
114         throw new Error("Parent TCPServerSocket has already listening. \n");
115       }
116     }
117   },
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);
123   },
125   callListenerError: function tss_callListenerError(message, filename, lineNumber, columnNumber) {
126     if (this._onerror) {
127       var type = "error";
128       var error = new Error(message, filename, lineNumber, columnNumber);
130       this["onerror"].call(null, new TCPSocketEvent(type, this, error));
131     }
132   },
133   /* end nsITCPServerSocketInternal method */
135   close: function tss_close() {
136     if (this._inChild) {
137       this._serverBridge.close();
138       return;
139     }
141     /* Close ServerSocket */
142     if (this._neckoTCPServerSocket) {
143       this._neckoTCPServerSocket.close();
144     }
145   },
147   // nsIServerSocketListener (Triggered by _neckoTCPServerSocket.asyncListen)
148   onSocketAccepted: function tss_onSocketAccepted(server, trans) {
149     // precondition: this._inChild == false
150     try {
151       let that = TCPSocketInternal.createAcceptedParent(trans, this._binaryType,
152                                                         this.useWin);
153       this._callListenerAcceptCommon(that);
154     }
155     catch(e) {
156       trans.close(Cr.NS_BINDING_ABORTED);
157     }
158   },
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.");
164     }
165     this._neckoTCPServerSocket = null;
166   },
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",
173     interfaces: [
174       Ci.nsIDOMTCPServerSocket,
175       Ci.nsISupportsWeakReference
176     ],
177     flags: Ci.nsIClassInfo.DOM_OBJECT,
178   }),
180   QueryInterface: XPCOMUtils.generateQI([
181     Ci.nsIDOMTCPServerSocket,
182     Ci.nsITCPServerSocketInternal,
183     Ci.nsISupportsWeakReference
184   ])
187 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TCPServerSocket]);