1 // Copyright 2013 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.tcpServer</code> API to create server
6 // applications using TCP connections. This API supersedes the TCP functionality
7 // previously found in the <code>chrome.socket</code> API. Note that the socket
8 // ids created from this namespace are not compatible with ids created in other
10 namespace sockets.tcpServer {
11 // The socket properties specified in the <code>create</code> or
12 // <code>update</code> function. Each property is optional. If a property
13 // value is not specified, a default value is used when calling
14 // <code>create</code>, or the existing value if preserved when calling
15 // <code>update</code>.
16 dictionary SocketProperties {
17 // Flag indicating if the socket remains open when the event page of the
18 // application is unloaded (see
19 // <a href="http://developer.chrome.com/apps/app_lifecycle.html">Manage App
20 // Lifecycle</a>). The default value is "false." When the application is
21 // loaded, any sockets previously opened with persistent=true can be fetched
22 // with <code>getSockets</code>.
25 // An application-defined string associated with the socket.
29 // Result of <code>create</code> call.
30 dictionary CreateInfo {
31 // The ID of the newly created server socket.
35 // Callback from the <code>create</code> method.
36 // |createInfo| : The result of the socket creation.
37 callback CreateCallback = void (CreateInfo createInfo);
39 // Callback from the <code>listen</code> method.
40 // |result| : The result code returned from the underlying network call.
41 // A negative value indicates an error.
42 callback ListenCallback = void (long result);
44 // Callback from the <code>disconnect</code> method.
45 callback DisconnectCallback = void ();
47 // Callback from the <code>close<code> method.
48 callback CloseCallback = void ();
50 // Callback from the <code>update</code> method.
51 callback UpdateCallback = void ();
53 // Callback from the <code>setPaused<code> method.
54 callback SetPausedCallback = void ();
56 // Result of the <code>getInfo</code> method.
57 dictionary SocketInfo {
58 // The socket identifier.
61 // Flag indicating if the socket remains open when the event page of the
62 // application is unloaded (see <code>SocketProperties.persistent</code>).
63 // The default value is "false".
66 // Application-defined string associated with the socket.
69 // Flag indicating whether connection requests on a listening socket are
70 // dispatched through the <code>onAccept<code> event or queued up in the
71 // listen queue backlog.
72 // See <code>setPaused<code>. The default value is "false".
75 // If the socket is listening, contains its local IPv4/6 address.
76 DOMString? localAddress;
78 // If the socket is listening, contains its local port.
82 // Callback from the <code>getInfo</code> method.
83 // |socketInfo| : Object containing the socket information.
84 callback GetInfoCallback = void (SocketInfo socketInfo);
86 // Callback from the <code>getSockets</code> method.
87 // |socketInfos| : Array of object containing socket information.
88 callback GetSocketsCallback = void (SocketInfo[] socketInfos);
90 // Data from an <code>onAccept</code> event.
91 dictionary AcceptInfo {
92 // The server socket identifier.
95 // The client socket identifier, i.e. the socket identifier of the newly
96 // established connection. This socket identifier should be used only with
97 // functions from the <code>chrome.sockets.tcp<code> namespace.
101 // Data from an <code>onAcceptError</code> event.
102 dictionary AcceptErrorInfo {
103 // The server socket identifier.
106 // The result code returned from the underlying network call.
110 interface Functions {
111 // Creates a TCP server socket.
112 // |properties| : The socket properties (optional).
113 // |callback| : Called when the socket has been created.
114 static void create(optional SocketProperties properties,
115 CreateCallback callback);
117 // Updates the socket properties.
118 // |socketId| : The socket identifier.
119 // |properties| : The properties to update.
120 // |callback| : Called when the properties are updated.
121 static void update(long socketId,
122 SocketProperties properties,
123 optional UpdateCallback callback);
125 // Enables or disables a listening socket from accepting new connections.
126 // When paused, a listening socket accepts new connections until its backlog
127 // (see <code>listen<code> function) is full then refuses additional
128 // connection requests. <code>onAccept<code> events are raised only when
129 // the socket is un-paused.
130 static void setPaused(long socketId,
132 optional SetPausedCallback callback);
134 // Listens for connections on the specified port and address.
135 // If the port/address is in use, the callback indicates a failure.
136 // |socketId| : The socket identifier.
137 // |address| : The address of the local machine.
138 // |port| : The port of the local machine.
139 // |backlog| : Length of the socket's listen queue. The default value
140 // depends on the Operating System (SOMAXCONN), which ensures a reasonable
141 // queue length for most applications.
142 // |callback| : Called when listen operation completes.
143 static void listen(long socketId,
146 optional long backlog,
147 ListenCallback callback);
149 // Disconnects the listening socket, i.e. stops accepting new connections
150 // and releases the address/port the socket is bound to. The socket
151 // identifier remains valid, e.g. it can be used with <code>listen<code> to
152 // accept connections on a new port and address.
153 // |socketId| : The socket identifier.
154 // |callback| : Called when the disconnect attempt is complete.
155 static void disconnect(long socketId,
156 optional DisconnectCallback callback);
158 // Disconnects and destroys the socket. Each socket created should be
159 // closed after use. The socket id is no longer valid as soon at the
160 // function is called. However, the socket is guaranteed to be closed only
161 // when the callback is invoked.
162 // |socketId| : The socket identifier.
163 // |callback| : Called when the <code>close</code> operation completes.
164 static void close(long socketId,
165 optional CloseCallback callback);
167 // Retrieves the state of the given socket.
168 // |socketId| : The socket identifier.
169 // |callback| : Called when the socket state is available.
170 static void getInfo(long socketId,
171 GetInfoCallback callback);
173 // Retrieves the list of currently opened sockets owned by the application.
174 // |callback| : Called when the list of sockets is available.
175 static void getSockets(GetSocketsCallback callback);
179 // Event raised when a connection has been made to the server socket.
180 // |info| : The event data.
181 static void onAccept(AcceptInfo info);
183 // Event raised when a network error occured while the runtime was waiting
184 // for new connections on the socket address and port. Once this event is
185 // raised, the socket is set to <code>paused</code> and no more
186 // <code>onAccept</code> events are raised for this socket until the socket
188 // |info| : The event data.
189 static void onAcceptError(AcceptErrorInfo info);