1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Use the <code>chrome.socket</code> API to send and receive data over the
6 // network using TCP and UDP connections. <b>Note:</b> Starting with Chrome 33,
7 // this API is deprecated in favor of the $(ref:sockets.udp), $(ref:sockets.tcp) and
8 // $(ref:sockets.tcpServer) APIs.
15 // The socket options.
16 dictionary CreateOptions
{
19 dictionary CreateInfo
{
20 // The id of the newly created socket.
24 callback CreateCallback
= void (CreateInfo createInfo
);
26 callback ConnectCallback
= void (long result
);
28 callback BindCallback
= void (long result
);
30 callback ListenCallback
= void (long result
);
32 callback SecureCallback
= void (long result
);
34 dictionary AcceptInfo
{
36 // The id of the accepted socket.
40 callback AcceptCallback
= void (AcceptInfo acceptInfo
);
43 // The resultCode returned from the underlying read() call.
49 callback ReadCallback
= void (ReadInfo readInfo
);
51 dictionary WriteInfo
{
52 // The number of bytes sent, or a negative error code.
56 callback WriteCallback
= void (WriteInfo writeInfo
);
58 dictionary RecvFromInfo
{
59 // The resultCode returned from the underlying recvfrom() call.
64 // The address of the remote machine.
70 dictionary SocketInfo
{
71 // The type of the passed socket. This will be <code>tcp</code> or
73 SocketType socketType
;
75 // Whether or not the underlying socket is connected.
77 // For <code>tcp</code> sockets, this will remain true even if the remote
78 // peer has disconnected. Reading or writing to the socket may then result
79 // in an error, hinting that this socket should be disconnected via
80 // <code>disconnect()</code>.
82 // For <code>udp</code> sockets, this just represents whether a default
83 // remote address has been specified for reading and writing packets.
86 // If the underlying socket is connected, contains the IPv4/6 address of
88 DOMString? peerAddress
;
90 // If the underlying socket is connected, contains the port of the
94 // If the underlying socket is bound or connected, contains its local
96 DOMString? localAddress
;
98 // If the underlying socket is bound or connected, contains its local port.
102 dictionary NetworkInterface
{
103 // The underlying name of the adapter. On *nix, this will typically be
104 // "eth0", "lo", etc.
107 // The available IPv4/6 address.
114 dictionary TLSVersionConstraints
{
115 // The minimum and maximum acceptable versions of TLS. These will
116 // be <code>tls1</code>, <code>tls1.1</code>, or <code>tls1.2</code>.
121 dictionary SecureOptions
{
122 TLSVersionConstraints? tlsVersion
;
125 callback RecvFromCallback
= void (RecvFromInfo recvFromInfo
);
127 callback SendToCallback
= void (WriteInfo writeInfo
);
129 callback SetKeepAliveCallback
= void (boolean result
);
131 callback SetNoDelayCallback
= void (boolean result
);
133 callback GetInfoCallback
= void (SocketInfo result
);
135 callback GetNetworkCallback
= void (NetworkInterface
[] result
);
137 callback JoinGroupCallback
= void (long result
);
139 callback LeaveGroupCallback
= void (long result
);
141 callback SetMulticastTimeToLiveCallback
= void (long result
);
143 callback SetMulticastLoopbackModeCallback
= void (long result
);
145 callback GetJoinedGroupsCallback
= void (DOMString
[] groups
);
147 interface Functions
{
148 // Creates a socket of the specified type that will connect to the specified
150 // |type| : The type of socket to create. Must be <code>tcp</code> or
152 // |options| : The socket options.
153 // |callback| : Called when the socket has been created.
154 static
void create
(SocketType type
,
155 optional CreateOptions options
,
156 CreateCallback
callback);
158 // Destroys the socket. Each socket created should be destroyed after use.
159 // |socketId| : The socketId.
160 static
void destroy
(long socketId
);
162 // Connects the socket to the remote machine (for a <code>tcp</code>
163 // socket). For a <code>udp</code> socket, this sets the default address
164 // which packets are sent to and read from for <code>read()</code>
165 // and <code>write()</code> calls.
166 // |socketId| : The socketId.
167 // |hostname| : The hostname or IP address of the remote machine.
168 // |port| : The port of the remote machine.
169 // |callback| : Called when the connection attempt is complete.
170 static
void connect
(long socketId
,
173 ConnectCallback
callback);
175 // Binds the local address for socket. Currently, it does not support
177 // |socketId| : The socketId.
178 // |address| : The address of the local machine.
179 // |port| : The port of the local machine.
180 // |callback| : Called when the bind attempt is complete.
181 static
void bind
(long socketId
,
184 BindCallback
callback);
186 // Disconnects the socket. For UDP sockets, <code>disconnect</code> is a
187 // non-operation but is safe to call.
188 // |socketId| : The socketId.
189 static
void disconnect
(long socketId
);
191 // Reads data from the given connected socket.
192 // |socketId| : The socketId.
193 // |bufferSize| : The read buffer size.
194 // |callback| : Delivers data that was available to be read without
196 static
void read
(long socketId
,
197 optional long bufferSize
,
198 ReadCallback
callback);
200 // Writes data on the given connected socket.
201 // |socketId| : The socketId.
202 // |data| : The data to write.
203 // |callback| : Called when the write operation completes without blocking
204 // or an error occurs.
205 static
void write
(long socketId
,
207 WriteCallback
callback);
209 // Receives data from the given UDP socket.
210 // |socketId| : The socketId.
211 // |bufferSize| : The receive buffer size.
212 // |callback| : Returns result of the recvFrom operation.
213 static
void recvFrom
(long socketId
,
214 optional long bufferSize
,
215 RecvFromCallback
callback);
217 // Sends data on the given UDP socket to the given address and port.
218 // |socketId| : The socketId.
219 // |data| : The data to write.
220 // |address| : The address of the remote machine.
221 // |port| : The port of the remote machine.
222 // |callback| : Called when the send operation completes without blocking
223 // or an error occurs.
224 static
void sendTo
(long socketId
,
228 SendToCallback
callback);
230 // This method applies to TCP sockets only.
231 // Listens for connections on the specified port and address. This
232 // effectively makes this a server socket, and client socket
233 // functions (connect, read, write) can no longer be used on this socket.
234 // |socketId| : The socketId.
235 // |address| : The address of the local machine.
236 // |port| : The port of the local machine.
237 // |backlog| : Length of the socket's listen queue.
238 // |callback| : Called when listen operation completes.
239 static
void listen
(long socketId
,
242 optional long backlog
,
243 ListenCallback
callback);
245 // This method applies to TCP sockets only.
246 // Registers a callback function to be called when a connection is
247 // accepted on this listening server socket. Listen must be called first.
248 // If there is already an active accept callback, this callback will be
249 // invoked immediately with an error as the resultCode.
250 // |socketId| : The socketId.
251 // |callback| : The callback is invoked when a new socket is accepted.
252 static
void accept
(long socketId
,
253 AcceptCallback
callback);
255 // Enables or disables the keep-alive functionality for a TCP connection.
256 // |socketId| : The socketId.
257 // |enable| : If true, enable keep-alive functionality.
258 // |delay| : Set the delay seconds between the last data packet received
259 // and the first keepalive probe. Default is 0.
260 // |callback| : Called when the setKeepAlive attempt is complete.
261 static
void setKeepAlive
(long socketId
,
264 SetKeepAliveCallback
callback);
266 // Sets or clears <code>TCP_NODELAY</code> for a TCP connection. Nagle's
267 // algorithm will be disabled when <code>TCP_NODELAY</code> is set.
268 // |socketId| : The socketId.
269 // |noDelay| : If true, disables Nagle's algorithm.
270 // |callback| : Called when the setNoDelay attempt is complete.
271 static
void setNoDelay
(long socketId
,
273 SetNoDelayCallback
callback);
275 // Retrieves the state of the given socket.
276 // |socketId| : The socketId.
277 // |callback| : Called when the state is available.
278 static
void getInfo
(long socketId
,
279 GetInfoCallback
callback);
281 // Retrieves information about local adapters on this system.
282 // |callback| : Called when local adapter information is available.
283 static
void getNetworkList
(GetNetworkCallback
callback);
285 // Join the multicast group and start to receive packets from that group.
286 // The socket must be of UDP type and must be bound to a local port
287 // before calling this method.
288 // |socketId| : The socketId.
289 // |address| : The group address to join. Domain names are not supported.
290 // |callback| : Called when the join group operation is done with an
291 // integer parameter indicating the platform-independent error code.
292 static
void joinGroup
(long socketId
,
294 JoinGroupCallback
callback);
296 // Leave the multicast group previously joined using <code>joinGroup</code>.
297 // It's not necessary to leave the multicast group before destroying the
298 // socket or exiting. This is automatically called by the OS.
300 // Leaving the group will prevent the router from sending multicast
301 // datagrams to the local host, presuming no other process on the host is
302 // still joined to the group.
304 // |socketId| : The socketId.
305 // |address| : The group address to leave. Domain names are not supported.
306 // |callback| : Called when the leave group operation is done with an
307 // integer parameter indicating the platform-independent error code.
308 static
void leaveGroup
(long socketId
, DOMString address
,
309 LeaveGroupCallback
callback);
311 // Set the time-to-live of multicast packets sent to the multicast group.
313 // Calling this method does not require multicast permissions.
315 // |socketId| : The socketId.
316 // |ttl| : The time-to-live value.
317 // |callback| : Called when the configuration operation is done.
318 static
void setMulticastTimeToLive
(
321 SetMulticastTimeToLiveCallback
callback);
323 // Set whether multicast packets sent from the host to the multicast
324 // group will be looped back to the host.
326 // Note: the behavior of <code>setMulticastLoopbackMode</code> is slightly
327 // different between Windows and Unix-like systems. The inconsistency
328 // happens only when there is more than one application on the same host
329 // joined to the same multicast group while having different settings on
330 // multicast loopback mode. On Windows, the applications with loopback off
331 // will not RECEIVE the loopback packets; while on Unix-like systems, the
332 // applications with loopback off will not SEND the loopback packets to
333 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
335 // Calling this method does not require multicast permissions.
337 // |socketId| : The socketId.
338 // |enabled| : Indicate whether to enable loopback mode.
339 // |callback| : Called when the configuration operation is done.
340 static
void setMulticastLoopbackMode
(
343 SetMulticastLoopbackModeCallback
callback);
345 // Get the multicast group addresses the socket is currently joined to.
346 // |socketId| : The socketId.
347 // |callback| : Called with an array of strings of the result.
348 static
void getJoinedGroups
(long socketId
,
349 GetJoinedGroupsCallback
callback);
351 // Start a TLS client connection over a connected TCP client socket.
352 // |socketId| : The connected socket to use.
353 // |options| : Constraints and parameters for the TLS connection.
354 // |callback| : Called when the TLS connection attempt is complete.
355 static
void secure
(long socketId
,
356 optional SecureOptions options
,
357 SecureCallback
callback);