Thu, 22 Jan 2015 13:21:57 +0100
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 /**
6 * MozTCPSocket exposes a TCP client and server sockets
7 * to highly privileged apps. It provides a buffered, non-blocking
8 * interface for sending. For receiving, it uses an asynchronous,
9 * event handler based interface.
10 */
12 #include "domstubs.idl"
13 #include "nsIDOMEvent.idl"
14 #include "nsITCPSocketChild.idl"
15 #include "nsIDOMTCPServerSocket.idl"
17 interface nsISocketTransport;
19 // Bug 731746 - Allow chrome JS object to implement nsIDOMEventTarget
20 // nsITCPSocket should be an nsIEventTarget but js objects
21 // cannot be an nsIEventTarget yet
22 // #include "nsIEventTarget.idl"
24 // Bug 723206 - Constructors implemented in JS from IDL should be
25 // allowed to have arguments
26 //
27 // Once bug 723206 will be fixed, this method could be replaced by
28 // arguments when instantiating a TCPSocket object. For example it will
29 // be possible to do (similarly to the WebSocket API):
30 // var s = new MozTCPSocket(host, port);
32 // Bug 797561 - Expose a server tcp socket API to web applications
35 [scriptable, uuid(65f6d2c8-4be6-4695-958d-0735e8935289)]
36 interface nsIDOMTCPSocket : nsISupports
37 {
38 /**
39 * Create and return a socket object which will attempt to connect to
40 * the given host and port.
41 *
42 * @param host The hostname of the server to connect to.
43 * @param port The port to connect to.
44 * @param options An object specifying one or more parameters which
45 * determine the details of the socket.
46 *
47 * useSecureTransport: true to create an SSL socket. Defaults to false.
48 *
49 * binaryType: "arraybuffer" to use ArrayBuffer
50 * instances in the ondata callback and as the argument
51 * to send. Defaults to "string", to use JavaScript strings.
52 *
53 * @return The new TCPSocket instance.
54 */
55 nsIDOMTCPSocket open(in DOMString host, in unsigned short port, [optional] in jsval options);
57 /**
58 * Listen on a port
59 *
60 * @param localPort The port of the server socket. Pass -1 to indicate no preference,
61 * and a port will be selected automatically.
62 * @param options An object specifying one or more parameters which
63 * determine the details of the socket.
64 *
65 * binaryType: "arraybuffer" to use ArrayBuffer
66 * instances in the ondata callback and as the argument
67 * to send. Defaults to "string", to use JavaScript strings.
68 * @param backlog The maximum length the queue of pending connections may grow to.
69 * This parameter may be silently limited by the operating system.
70 * Pass -1 to use the default value.
71 *
72 * @return The new TCPServerSocket instance.
73 */
74 nsIDOMTCPServerSocket listen(in unsigned short localPort, [optional] in jsval options,
75 [optional] in unsigned short backlog);
77 /**
78 * Enable secure on channel.
79 */
80 void upgradeToSecure();
82 /**
83 * The host of this socket object.
84 */
85 readonly attribute DOMString host;
87 /**
88 * The port of this socket object.
89 */
90 readonly attribute unsigned short port;
92 /**
93 * True if this socket object is an SSL socket.
94 */
95 readonly attribute boolean ssl;
97 /**
98 * The number of bytes which have previously been buffered by calls to
99 * send on this socket.
100 */
101 readonly attribute unsigned long bufferedAmount;
103 /**
104 * Pause reading incoming data and invocations of the ondata handler until
105 * resume is called.
106 */
107 void suspend();
109 /**
110 * Resume reading incoming data and invoking ondata as usual.
111 */
112 void resume();
114 /**
115 * Close the socket.
116 */
117 void close();
119 /**
120 * Write data to the socket.
121 *
122 * @param data The data to write to the socket. If
123 * binaryType: "arraybuffer" was passed in the options
124 * object, then this object should be an ArrayBuffer instance.
125 * If binaryType: "string" was passed, or if no binaryType
126 * option was specified, then this object should be an
127 * ordinary JavaScript string.
128 * @param byteOffset The offset within the data from which to begin writing.
129 * Has no effect on non-ArrayBuffer data.
130 * @param byteLength The number of bytes to write. Has no effect on
131 * non-ArrayBuffer data.
132 *
133 * @return Send returns true or false as a hint to the caller that
134 * they may either continue sending more data immediately, or
135 * may want to wait until the other side has read some of the
136 * data which has already been written to the socket before
137 * buffering more. If send returns true, then less than 64k
138 * has been buffered and it's safe to immediately write more.
139 * If send returns false, then more than 64k has been buffered,
140 * and the caller may wish to wait until the ondrain event
141 * handler has been called before buffering more data by more
142 * calls to send.
143 */
144 boolean send(in jsval data, [optional] in unsigned long byteOffset, [optional] in unsigned long byteLength);
146 /**
147 * The readyState attribute indicates which state the socket is currently
148 * in. The state will be either "connecting", "open", "closing", or "closed".
149 */
150 readonly attribute DOMString readyState;
152 /**
153 * The binaryType attribute indicates which mode this socket uses for
154 * sending and receiving data. If the binaryType: "arraybuffer" option
155 * was passed to the open method that created this socket, binaryType
156 * will be "arraybuffer". Otherwise, it will be "string".
157 */
158 readonly attribute DOMString binaryType;
160 /**
161 * The onopen event handler is called when the connection to the server
162 * has been established. If the connection is refused, onerror will be
163 * called, instead.
164 */
165 attribute jsval onopen;
167 /**
168 * After send has buffered more than 64k of data, it returns false to
169 * indicate that the client should pause before sending more data, to
170 * avoid accumulating large buffers. This is only advisory, and the client
171 * is free to ignore it and buffer as much data as desired, but if reducing
172 * the size of buffers is important (especially for a streaming application)
173 * ondrain will be called once the previously-buffered data has been written
174 * to the network, at which point the client can resume calling send again.
175 */
176 attribute jsval ondrain;
178 /**
179 * The ondata handler will be called repeatedly and asynchronously after
180 * onopen has been called, every time some data was available from the server
181 * and was read. If binaryType: "arraybuffer" was passed to open, the data
182 * attribute of the event object will be an ArrayBuffer. If not, it will be a
183 * normal JavaScript string.
184 *
185 * At any time, the client may choose to pause reading and receiving ondata
186 * callbacks, by calling the socket's suspend() method. Further invocations
187 * of ondata will be paused until resume() is called.
188 */
189 attribute jsval ondata;
191 /**
192 * The onerror handler will be called when there is an error. The data
193 * attribute of the event passed to the onerror handler will have a
194 * description of the kind of error.
195 *
196 * If onerror is called before onopen, the error was connection refused,
197 * and onclose will not be called. If onerror is called after onopen,
198 * the connection was lost, and onclose will be called after onerror.
199 */
200 attribute jsval onerror;
202 /**
203 * The onclose handler is called once the underlying network socket
204 * has been closed, either by the server, or by the client calling
205 * close.
206 *
207 * If onerror was not called before onclose, then either side cleanly
208 * closed the connection.
209 */
210 attribute jsval onclose;
211 };
213 /*
214 * This interface is implemented in TCPSocket.js as an internal interfaces
215 * for use in cross-process socket implementation.
216 * Needed to account for multiple possible types that can be provided to
217 * the socket callbacks as arguments.
218 */
219 [scriptable, uuid(017f130f-2477-4215-8783-57eada957699)]
220 interface nsITCPSocketInternal : nsISupports {
221 // Trigger the callback for |type| and provide a DOMError() object with the given data
222 void callListenerError(in DOMString type, in DOMString name);
224 // Trigger the callback for |type| and provide a string argument
225 void callListenerData(in DOMString type, in DOMString data);
227 // Trigger the callback for |type| and provide an ArrayBuffer argument
228 void callListenerArrayBuffer(in DOMString type, in jsval data);
230 // Trigger the callback for |type| with no argument
231 void callListenerVoid(in DOMString type);
233 // Update the DOM object's readyState.
234 // @param readyState
235 // new ready state to be set to TCPSocket.
236 void updateReadyState(in DOMString readyState);
238 // Update the DOM object's bufferedAmount value with a tracking number to
239 // ensure the update request is sent after child's send() invocation.
240 // @param bufferedAmount
241 // TCPSocket parent's bufferedAmount.
242 // @param trackingNumber
243 // A number to ensure the bufferedAmount is updated after data
244 // from child are sent to parent.
245 void updateBufferedAmount(in uint32_t bufferedAmount,
246 in uint32_t trackingNumber);
248 // Create a socket object on the parent side.
249 // This is called in accepting any open request on the parent side.
250 //
251 // @param transport
252 // The accepted socket transport.
253 // @param binaryType
254 // "arraybuffer" to use ArrayBuffer instances
255 // in the ondata callback and as the argument to send.
256 nsIDOMTCPSocket createAcceptedParent(in nsISocketTransport transport,
257 in DOMString binaryType);
259 // Create a DOM socket on the child side
260 // This is called when the socket is accepted on the parent side.
261 //
262 // @param socketChild
263 // The socket child object for the IPC implementation.
264 // @param binaryType
265 // "arraybuffer" to use ArrayBuffer instances
266 // in the ondata callback and as the argument to send.
267 // @param window
268 // An object to create ArrayBuffer for this window. See Bug 831107.
269 nsIDOMTCPSocket createAcceptedChild(in nsITCPSocketChild socketChild,
270 in DOMString binaryType,
271 in nsIDOMWindow window);
273 // Set App ID.
274 void setAppId(in unsigned long appId);
276 // Set a callback that handles the request from a TCP socket parent when that
277 // socket parent wants to notify that its bufferedAmount is updated.
278 void setOnUpdateBufferedAmountHandler(in jsval handler);
280 // Providing child process with ability to pass more arguments to parent's
281 // send() function.
282 // @param trackingNumber
283 // To ensure the request to update bufferedAmount in child is after
284 // lastest send() invocation from child.
285 void onRecvSendFromChild(in jsval data, in unsigned long byteOffset,
286 in unsigned long byteLength, in unsigned long trackingNumber);
287 };
289 /**
290 * nsITCPSocketEvent is the event object which is passed as the
291 * first argument to all the event handler callbacks. It contains
292 * the socket that was associated with the event, the type of event,
293 * and the data associated with the event (if any).
294 */
296 [scriptable, uuid(0f2abcca-b483-4539-a3e8-345707f75c44)]
297 interface nsITCPSocketEvent : nsISupports {
298 /**
299 * The socket object which produced this event.
300 */
301 readonly attribute nsIDOMTCPSocket target;
303 /**
304 * The type of this event. One of:
305 *
306 * open
307 * error
308 * data
309 * drain
310 * close
311 */
312 readonly attribute DOMString type;
314 /**
315 * The data related to this event, if any. In the ondata callback,
316 * data will be the bytes read from the network; if the binaryType
317 * of the socket was "arraybuffer", this value will be of type ArrayBuffer;
318 * otherwise, it will be a normal JavaScript string.
319 *
320 * In the onerror callback, data will be a string with a description
321 * of the error.
322 *
323 * In the other callbacks, data will be an empty string.
324 */
325 readonly attribute jsval data;
326 };