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.sockets.udp</code> API to send and receive data over the
6 // network using UDP connections. This API supersedes the UDP functionality
7 // previously found in the "socket" API.
8 namespace sockets.udp
{
9 // The socket properties specified in the <code>create</code> or
10 // <code>update</code> function. Each property is optional. If a property
11 // value is not specified, a default value is used when calling
12 // <code>create</code>, or the existing value if preserved when calling
13 // <code>update</code>.
14 dictionary SocketProperties
{
15 // Flag indicating if the socket is left open when the event page of the
16 // application is unloaded (see
17 // <a href="http://developer.chrome.com/apps/app_lifecycle.html">Manage App
18 // Lifecycle</a>). The default value is "false." When the application is
19 // loaded, any sockets previously opened with persistent=true can be fetched
20 // with <code>getSockets</code>.
23 // An application-defined string associated with the socket.
26 // The size of the buffer used to receive data. If the buffer is too small
27 // to receive the UDP packet, data is lost. The default value is 4096.
31 // Result of <code>create</code> call.
32 dictionary CreateInfo
{
33 // The ID of the newly created socket. Note that socket IDs created from
34 // this API are not compatible with socket IDs created from other APIs, such
35 // as the deprecated <code>$(ref:socket)</code> API.
39 // Callback from the <code>create</code> method.
40 // |createInfo| : The result of the socket creation.
41 callback CreateCallback
= void (CreateInfo createInfo
);
43 // Callback from the <code>bind</code> method.
44 // |result| : The result code returned from the underlying network call.
45 // A negative value indicates an error.
46 callback BindCallback
= void (long result
);
48 // Result of the <code>send</code> method.
50 // The result code returned from the underlying network call.
51 // A negative value indicates an error.
54 // The number of bytes sent (if result == 0)
58 // Callback from the <code>send</code> method.
59 // |sendInfo| : Result of the <code>send</code> method.
60 callback SendCallback
= void (SendInfo sendInfo
);
62 // Callback from the <code>close</code> method.
63 callback CloseCallback
= void ();
65 // Callback from the <code>update</code> method.
66 callback UpdateCallback
= void ();
68 // Callback from the <code>setPaused</code> method.
69 callback SetPausedCallback
= void ();
71 // Result of the <code>getInfo</code> method.
72 dictionary SocketInfo
{
73 // The socket identifier.
76 // Flag indicating whether the socket is left open when the application is
77 // suspended (see <code>SocketProperties.persistent</code>).
80 // Application-defined string associated with the socket.
83 // The size of the buffer used to receive data. If no buffer size has been
84 // specified explictly, the value is not provided.
87 // Flag indicating whether the socket is blocked from firing onReceive
91 // If the underlying socket is bound, contains its local
93 DOMString? localAddress
;
95 // If the underlying socket is bound, contains its local port.
99 // Callback from the <code>getInfo</code> method.
100 // |socketInfo| : Object containing the socket information.
101 callback GetInfoCallback
= void (SocketInfo socketInfo
);
103 // Callback from the <code>getSockets</code> method.
104 // |socketInfos| : Array of object containing socket information.
105 callback GetSocketsCallback
= void (SocketInfo
[] socketInfos
);
107 // Callback from the <code>joinGroup</code> method.
108 // |result| : The result code returned from the underlying network call.
109 // A negative value indicates an error.
110 callback JoinGroupCallback
= void (long result
);
112 // Callback from the <code>leaveGroup</code> method.
113 // |result| : The result code returned from the underlying network call.
114 // A negative value indicates an error.
115 callback LeaveGroupCallback
= void (long result
);
117 // Callback from the <code>setMulticastTimeToLive</code> method.
118 // |result| : The result code returned from the underlying network call.
119 // A negative value indicates an error.
120 callback SetMulticastTimeToLiveCallback
= void (long result
);
122 // Callback from the <code>setMulticastLoopbackMode</code> method.
123 // |result| : The result code returned from the underlying network call.
124 // A negative value indicates an error.
125 callback SetMulticastLoopbackModeCallback
= void (long result
);
127 // Callback from the <code>getJoinedGroupsCallback</code> method.
128 // |groups| : Array of groups the socket joined.
129 callback GetJoinedGroupsCallback
= void (DOMString
[] groups
);
131 // Callback from the <code>setBroadcast</code> method.
132 // |result| : The result code returned from the underlying network call.
133 callback SetBroadcastCallback
= void (long result
);
135 // Data from an <code>onReceive</code> event.
136 dictionary ReceiveInfo
{
140 // The UDP packet content (truncated to the current buffer size).
143 // The address of the host the packet comes from.
144 DOMString remoteAddress
;
146 // The port of the host the packet comes from.
150 // Data from an <code>onReceiveError</code> event.
151 dictionary ReceiveErrorInfo
{
155 // The result code returned from the underlying recvfrom() call.
159 interface Functions
{
160 // Creates a UDP socket with the given properties.
161 // |properties| : The socket properties (optional).
162 // |callback| : Called when the socket has been created.
163 static
void create
(optional SocketProperties properties
,
164 CreateCallback
callback);
166 // Updates the socket properties.
167 // |socketId| : The socket ID.
168 // |properties| : The properties to update.
169 // |callback| : Called when the properties are updated.
170 static
void update
(long socketId
,
171 SocketProperties properties
,
172 optional UpdateCallback
callback);
174 // Pauses or unpauses a socket. A paused socket is blocked from firing
175 // <code>onReceive</code> events.
176 // |connectionId| : The socket ID.
177 // |paused| : Flag to indicate whether to pause or unpause.
178 // |callback| : Called when the socket has been successfully paused or
180 static
void setPaused
(long socketId
,
182 optional SetPausedCallback
callback);
184 // Binds the local address and port for the socket. For a client socket, it
185 // is recommended to use port 0 to let the platform pick a free port.
187 // Once the <code>bind</code> operation completes successfully,
188 // <code>onReceive</code> events are raised when UDP packets arrive on the
189 // address/port specified -- unless the socket is paused.
191 // |socketId| : The socket ID.
192 // |address| : The address of the local machine. DNS name, IPv4 and IPv6
193 // formats are supported. Use "0.0.0.0" to accept packets from all local
194 // available network interfaces.
195 // |port| : The port of the local machine. Use "0" to bind to a free port.
196 // |callback| : Called when the <code>bind</code> operation completes.
197 static
void bind
(long socketId
,
200 BindCallback
callback);
202 // Sends data on the given socket to the given address and port. The socket
203 // must be bound to a local port before calling this method.
204 // |socketId| : The socket ID.
205 // |data| : The data to send.
206 // |address| : The address of the remote machine.
207 // |port| : The port of the remote machine.
208 // |callback| : Called when the <code>send</code> operation completes.
209 static
void send
(long socketId
,
213 SendCallback
callback);
215 // Closes the socket and releases the address/port the socket is bound to.
216 // Each socket created should be closed after use. The socket id is no
217 // longer valid as soon at the function is called. However, the socket is
218 // guaranteed to be closed only when the callback is invoked.
219 // |socketId| : The socket ID.
220 // |callback| : Called when the <code>close</code> operation completes.
221 static
void close
(long socketId
,
222 optional CloseCallback
callback);
224 // Retrieves the state of the given socket.
225 // |socketId| : The socket ID.
226 // |callback| : Called when the socket state is available.
227 static
void getInfo
(long socketId
,
228 GetInfoCallback
callback);
230 // Retrieves the list of currently opened sockets owned by the application.
231 // |callback| : Called when the list of sockets is available.
232 static
void getSockets
(GetSocketsCallback
callback);
234 // Joins the multicast group and starts to receive packets from that group.
235 // The socket must be bound to a local port before calling this method.
236 // |socketId| : The socket ID.
237 // |address| : The group address to join. Domain names are not supported.
238 // |callback| : Called when the <code>joinGroup</code> operation completes.
239 static
void joinGroup
(long socketId
,
241 JoinGroupCallback
callback);
243 // Leaves the multicast group previously joined using
244 // <code>joinGroup</code>. This is only necessary to call if you plan to
245 // keep using the socketafterwards, since it will be done automatically by
246 // the OS when the socket is closed.
248 // Leaving the group will prevent the router from sending multicast
249 // datagrams to the local host, presuming no other process on the host is
250 // still joined to the group.
252 // |socketId| : The socket ID.
253 // |address| : The group address to leave. Domain names are not supported.
254 // |callback| : Called when the <code>leaveGroup</code> operation completes.
255 static
void leaveGroup
(long socketId
,
257 LeaveGroupCallback
callback);
259 // Sets the time-to-live of multicast packets sent to the multicast group.
261 // Calling this method does not require multicast permissions.
263 // |socketId| : The socket ID.
264 // |ttl| : The time-to-live value.
265 // |callback| : Called when the configuration operation completes.
266 static
void setMulticastTimeToLive
(
269 SetMulticastTimeToLiveCallback
callback);
271 // Sets whether multicast packets sent from the host to the multicast group
272 // will be looped back to the host.
274 // Note: the behavior of <code>setMulticastLoopbackMode</code> is slightly
275 // different between Windows and Unix-like systems. The inconsistency
276 // happens only when there is more than one application on the same host
277 // joined to the same multicast group while having different settings on
278 // multicast loopback mode. On Windows, the applications with loopback off
279 // will not RECEIVE the loopback packets; while on Unix-like systems, the
280 // applications with loopback off will not SEND the loopback packets to
281 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
283 // Calling this method does not require multicast permissions.
285 // |socketId| : The socket ID.
286 // |enabled| : Indicate whether to enable loopback mode.
287 // |callback| : Called when the configuration operation completes.
288 static
void setMulticastLoopbackMode
(
291 SetMulticastLoopbackModeCallback
callback);
293 // Gets the multicast group addresses the socket is currently joined to.
294 // |socketId| : The socket ID.
295 // |callback| : Called with an array of strings of the result.
296 static
void getJoinedGroups
(long socketId
,
297 GetJoinedGroupsCallback
callback);
299 // Enables or disables broadcast packets on this socket.
301 // |socketId| : The socket ID.
302 // |enabled| : <code>true</code> to enable broadcast packets,
303 // <code>false</code> to disable them.
304 static
void setBroadcast
(long socketId
,
306 SetBroadcastCallback
callback);
310 // Event raised when a UDP packet has been received for the given socket.
311 // |info| : The event data.
312 static
void onReceive
(ReceiveInfo info
);
314 // Event raised when a network error occured while the runtime was waiting
315 // for data on the socket address and port. Once this event is raised, the
316 // socket is paused and no more <code>onReceive</code> events will be raised
317 // for this socket until the socket is resumed.
318 // |info| : The event data.
319 static
void onReceiveError
(ReceiveErrorInfo info
);