1 /* Copyright (c) 2012 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.
7 * This file defines the <code>PPB_WebSocket</code> interface providing
8 * bi-directional, full-duplex, communications over a single TCP socket.
15 * This enumeration contains the types representing the WebSocket ready state
16 * and these states are based on the JavaScript WebSocket API specification.
17 * GetReadyState() returns one of these states.
20 enum PP_WebSocketReadyState
{
22 * Ready state is queried on an invalid resource.
24 PP_WEBSOCKETREADYSTATE_INVALID
= -1,
27 * Ready state that the connection has not yet been established.
29 PP_WEBSOCKETREADYSTATE_CONNECTING
= 0,
32 * Ready state that the WebSocket connection is established and communication
35 PP_WEBSOCKETREADYSTATE_OPEN
= 1,
38 * Ready state that the connection is going through the closing handshake.
40 PP_WEBSOCKETREADYSTATE_CLOSING
= 2,
43 * Ready state that the connection has been closed or could not be opened.
45 PP_WEBSOCKETREADYSTATE_CLOSED
= 3
49 * This enumeration contains status codes. These codes are used in Close() and
50 * GetCloseCode(). Refer to RFC 6455, The WebSocket Protocol, for further
52 * <code>PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE</code> and codes in the range
53 * <code>PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MIN</code> to
54 * <code>PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MAX</code>, and
55 * <code>PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MIN</code> to
56 * <code>PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MAX</code> are valid for Close().
59 enum PP_WebSocketCloseCode
{
61 * Indicates to request closing connection without status code and reason.
63 * (Note that the code 1005 is forbidden to send in actual close frames by
64 * the RFC. PP_WebSocket reuses this code internally and the code will never
65 * appear in the actual close frames.)
67 PP_WEBSOCKETSTATUSCODE_NOT_SPECIFIED
= 1005,
70 * Status codes in the range 0-999 are not used.
74 * Indicates a normal closure.
76 PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE
= 1000,
79 * Indicates that an endpoint is "going away", such as a server going down.
81 PP_WEBSOCKETSTATUSCODE_GOING_AWAY
= 1001,
84 * Indicates that an endpoint is terminating the connection due to a protocol
87 PP_WEBSOCKETSTATUSCODE_PROTOCOL_ERROR
= 1002,
90 * Indicates that an endpoint is terminating the connection because it has
91 * received a type of data it cannot accept.
93 PP_WEBSOCKETSTATUSCODE_UNSUPPORTED_DATA
= 1003,
96 * Status code 1004 is reserved.
100 * Pseudo code to indicate that receiving close frame doesn't contain any
103 PP_WEBSOCKETSTATUSCODE_NO_STATUS_RECEIVED
= 1005,
106 * Pseudo code to indicate that connection was closed abnormally, e.g.,
107 * without closing handshake.
109 PP_WEBSOCKETSTATUSCODE_ABNORMAL_CLOSURE
= 1006,
112 * Indicates that an endpoint is terminating the connection because it has
113 * received data within a message that was not consistent with the type of
114 * the message (e.g., non-UTF-8 data within a text message).
116 PP_WEBSOCKETSTATUSCODE_INVALID_FRAME_PAYLOAD_DATA
= 1007,
119 * Indicates that an endpoint is terminating the connection because it has
120 * received a message that violates its policy.
122 PP_WEBSOCKETSTATUSCODE_POLICY_VIOLATION
= 1008,
125 * Indicates that an endpoint is terminating the connection because it has
126 * received a message that is too big for it to process.
128 PP_WEBSOCKETSTATUSCODE_MESSAGE_TOO_BIG
= 1009,
131 * Indicates that an endpoint (client) is terminating the connection because
132 * it has expected the server to negotiate one or more extension, but the
133 * server didn't return them in the response message of the WebSocket
136 PP_WEBSOCKETSTATUSCODE_MANDATORY_EXTENSION
= 1010,
139 * Indicates that a server is terminating the connection because it
140 * encountered an unexpected condition.
142 PP_WEBSOCKETSTATUSCODE_INTERNAL_SERVER_ERROR
= 1011,
145 * Status codes in the range 1012-1014 are reserved.
149 * Pseudo code to indicate that the connection was closed due to a failure to
150 * perform a TLS handshake.
152 PP_WEBSOCKETSTATUSCODE_TLS_HANDSHAKE
= 1015,
155 * Status codes in the range 1016-2999 are reserved.
159 * Status codes in the range 3000-3999 are reserved for use by libraries,
160 * frameworks, and applications. These codes are registered directly with
163 PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MIN
= 3000,
164 PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MAX
= 3999,
167 * Status codes in the range 4000-4999 are reserved for private use.
168 * Application can use these codes for application specific purposes freely.
170 PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MIN
= 4000,
171 PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MAX
= 4999
175 * The <code>PPB_WebSocket</code> interface provides bi-directional,
176 * full-duplex, communications over a single TCP socket.
178 interface PPB_WebSocket
{
180 * Create() creates a WebSocket instance.
182 * @param[in] instance A <code>PP_Instance</code> identifying the instance
183 * with the WebSocket.
185 * @return A <code>PP_Resource</code> corresponding to a WebSocket if
188 PP_Resource Create
([in] PP_Instance instance
);
191 * IsWebSocket() determines if the provided <code>resource</code> is a
192 * WebSocket instance.
194 * @param[in] resource A <code>PP_Resource</code> corresponding to a
197 * @return Returns <code>PP_TRUE</code> if <code>resource</code> is a
198 * <code>PPB_WebSocket</code>, <code>PP_FALSE</code> if the
199 * <code>resource</code> is invalid or some type other than
200 * <code>PPB_WebSocket</code>.
202 PP_Bool IsWebSocket
([in] PP_Resource resource
);
205 * Connect() connects to the specified WebSocket server. You can call this
206 * function once for a <code>web_socket</code>.
208 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
211 * @param[in] url A <code>PP_Var</code> representing a WebSocket server URL.
212 * The <code>PP_VarType</code> must be <code>PP_VARTYPE_STRING</code>.
214 * @param[in] protocols A pointer to an array of <code>PP_Var</code>
215 * specifying sub-protocols. Each <code>PP_Var</code> represents one
216 * sub-protocol and its <code>PP_VarType</code> must be
217 * <code>PP_VARTYPE_STRING</code>. This argument can be null only if
218 * <code>protocol_count</code> is 0.
220 * @param[in] protocol_count The number of sub-protocols in
221 * <code>protocols</code>.
223 * @param[in] callback A <code>PP_CompletionCallback</code> called
224 * when a connection is established or an error occurs in establishing
227 * @return An int32_t containing an error code from <code>pp_errors.h</code>.
228 * Returns <code>PP_ERROR_BADARGUMENT</code> if the specified
229 * <code>url</code>, or <code>protocols</code> contain an invalid string as
230 * defined in the WebSocket API specification.
231 * <code>PP_ERROR_BADARGUMENT</code> corresponds to a SyntaxError in the
232 * WebSocket API specification.
233 * Returns <code>PP_ERROR_NOACCESS</code> if the protocol specified in the
234 * <code>url</code> is not a secure protocol, but the origin of the caller
235 * has a secure scheme. Also returns <code>PP_ERROR_NOACCESS</code> if the
236 * port specified in the <code>url</code> is a port that the user agent
237 * is configured to block access to because it is a well-known port like
238 * SMTP. <code>PP_ERROR_NOACCESS</code> corresponds to a SecurityError of the
240 * Returns <code>PP_ERROR_INPROGRESS</code> if this is not the first call to
243 int32_t Connect
([in] PP_Resource web_socket
,
245 [in, size_as
=protocol_count
] PP_Var
[] protocols
,
246 [in] uint32_t protocol_count
,
247 [in] PP_CompletionCallback
callback);
250 * Close() closes the specified WebSocket connection by specifying
251 * <code>code</code> and <code>reason</code>.
253 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
256 * @param[in] code The WebSocket close code. This is ignored if it is
257 * <code>PP_WEBSOCKETSTATUSCODE_NOT_SPECIFIED</code>.
258 * <code>PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE</code> must be used for the
259 * usual case. To indicate some specific error cases, codes in the range
260 * <code>PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MIN</code> to
261 * <code>PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MAX</code>, and in the range
262 * <code>PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MIN</code> to
263 * <code>PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MAX</code> are available.
265 * @param[in] reason A <code>PP_Var</code> representing the WebSocket
266 * close reason. This is ignored if it is <code>PP_VARTYPE_UNDEFINED</code>.
267 * Otherwise, its <code>PP_VarType</code> must be
268 * <code>PP_VARTYPE_STRING</code>.
270 * @param[in] callback A <code>PP_CompletionCallback</code> called
271 * when the connection is closed or an error occurs in closing the
274 * @return An int32_t containing an error code from <code>pp_errors.h</code>.
275 * Returns <code>PP_ERROR_BADARGUMENT</code> if <code>reason</code> contains
276 * an invalid character as a UTF-8 string, or is longer than 123 bytes.
277 * <code>PP_ERROR_BADARGUMENT</code> corresponds to a JavaScript SyntaxError
278 * in the WebSocket API specification.
279 * Returns <code>PP_ERROR_NOACCESS</code> if the code is not an integer
280 * equal to 1000 or in the range 3000 to 4999. <code>PP_ERROR_NOACCESS</code>
281 * corresponds to an InvalidAccessError in the WebSocket API specification.
282 * Returns <code>PP_ERROR_INPROGRESS</code> if a previous call to Close() is
285 int32_t Close
([in] PP_Resource web_socket
,
288 [in] PP_CompletionCallback
callback);
291 * ReceiveMessage() receives a message from the WebSocket server.
292 * This interface only returns a single message. That is, this interface must
293 * be called at least N times to receive N messages, no matter the size of
296 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
299 * @param[out] message The received message is copied to provided
300 * <code>message</code>. The <code>message</code> must remain valid until
301 * ReceiveMessage() completes. Its received <code>PP_VarType</code> will be
302 * <code>PP_VARTYPE_STRING</code> or <code>PP_VARTYPE_ARRAY_BUFFER</code>.
304 * @param[in] callback A <code>PP_CompletionCallback</code> called
305 * when ReceiveMessage() completes. This callback is ignored if
306 * ReceiveMessage() completes synchronously and returns <code>PP_OK</code>.
308 * @return An int32_t containing an error code from <code>pp_errors.h</code>.
309 * If an error is detected or connection is closed, ReceiveMessage() returns
310 * <code>PP_ERROR_FAILED</code> after all buffered messages are received.
311 * Until buffered message become empty, ReceiveMessage() continues to return
312 * <code>PP_OK</code> as if connection is still established without errors.
314 int32_t ReceiveMessage
([in] PP_Resource web_socket
,
315 [out] PP_Var
message,
316 [in] PP_CompletionCallback
callback);
319 * SendMessage() sends a message to the WebSocket server.
321 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
324 * @param[in] message A message to send. The message is copied to an internal
325 * buffer, so the caller can free <code>message</code> safely after returning
326 * from the function. Its sent <code>PP_VarType</code> must be
327 * <code>PP_VARTYPE_STRING</code> or <code>PP_VARTYPE_ARRAY_BUFFER</code>.
329 * @return An int32_t containing an error code from <code>pp_errors.h</code>.
330 * Returns <code>PP_ERROR_FAILED</code> if the ReadyState is
331 * <code>PP_WEBSOCKETREADYSTATE_CONNECTING</code>.
332 * <code>PP_ERROR_FAILED</code> corresponds to a JavaScript
333 * InvalidStateError in the WebSocket API specification.
334 * Returns <code>PP_ERROR_BADARGUMENT</code> if the provided
335 * <code>message</code> contains an invalid character as a UTF-8 string.
336 * <code>PP_ERROR_BADARGUMENT</code> corresponds to a JavaScript
337 * SyntaxError in the WebSocket API specification.
338 * Otherwise, returns <code>PP_OK</code>, which doesn't necessarily mean
339 * that the server received the message.
341 int32_t SendMessage
([in] PP_Resource web_socket
,
342 [in] PP_Var
message);
345 * GetBufferedAmount() returns the number of bytes of text and binary
346 * messages that have been queued for the WebSocket connection to send, but
347 * have not been transmitted to the network yet.
349 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
352 * @return Returns the number of bytes.
354 uint64_t GetBufferedAmount
([in] PP_Resource web_socket
);
357 * GetCloseCode() returns the connection close code for the WebSocket
360 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
363 * @return Returns 0 if called before the close code is set.
365 uint16_t GetCloseCode
([in] PP_Resource web_socket
);
368 * GetCloseReason() returns the connection close reason for the WebSocket
371 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
374 * @return Returns a <code>PP_VARTYPE_STRING</code> var. If called before the
375 * close reason is set, the return value contains an empty string. Returns a
376 * <code>PP_VARTYPE_UNDEFINED</code> if called on an invalid resource.
378 PP_Var GetCloseReason
([in] PP_Resource web_socket
);
381 * GetCloseWasClean() returns if the connection was closed cleanly for the
382 * specified WebSocket connection.
384 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
387 * @return Returns <code>PP_FALSE</code> if called before the connection is
388 * closed, called on an invalid resource, or closed for abnormal reasons.
389 * Otherwise, returns <code>PP_TRUE</code> if the connection was closed
392 PP_Bool GetCloseWasClean
([in] PP_Resource web_socket
);
395 * GetExtensions() returns the extensions selected by the server for the
396 * specified WebSocket connection.
398 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
401 * @return Returns a <code>PP_VARTYPE_STRING</code> var. If called before the
402 * connection is established, the var's data is an empty string. Returns a
403 * <code>PP_VARTYPE_UNDEFINED</code> if called on an invalid resource.
405 PP_Var GetExtensions
([in] PP_Resource web_socket
);
408 * GetProtocol() returns the sub-protocol chosen by the server for the
409 * specified WebSocket connection.
411 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
414 * @return Returns a <code>PP_VARTYPE_STRING</code> var. If called before the
415 * connection is established, the var contains the empty string. Returns a
416 * <code>PP_VARTYPE_UNDEFINED</code> if called on an invalid resource.
418 PP_Var GetProtocol
([in] PP_Resource web_socket
);
421 * GetReadyState() returns the ready state of the specified WebSocket
424 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
427 * @return Returns <code>PP_WEBSOCKETREADYSTATE_INVALID</code> if called
428 * before Connect() is called, or if this function is called on an
431 PP_WebSocketReadyState GetReadyState
([in] PP_Resource web_socket
);
434 * GetURL() returns the URL associated with specified WebSocket connection.
436 * @param[in] web_socket A <code>PP_Resource</code> corresponding to a
439 * @return Returns a <code>PP_VARTYPE_STRING</code> var. If called before the
440 * connection is established, the var contains the empty string. Returns a
441 * <code>PP_VARTYPE_UNDEFINED</code> if this function is called on an
444 PP_Var GetURL
([in] PP_Resource web_socket
);