/* Copyright (c) 2012 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* From ppb_websocket.idl modified Thu May 31 15:47:38 2012. */
#ifndef PPAPI_C_PPB_WEBSOCKET_H_
#define PPAPI_C_PPB_WEBSOCKET_H_
#include "ppapi/c/pp_bool.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/c/pp_macros.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/c/pp_stdint.h"
#include "ppapi/c/pp_var.h"
#define PPB_WEBSOCKET_INTERFACE_1_0 "PPB_WebSocket;1.0"
#define PPB_WEBSOCKET_INTERFACE PPB_WEBSOCKET_INTERFACE_1_0
/**
* @file
* This file defines the PPB_WebSocket
interface providing
* bi-directional, full-duplex, communications over a single TCP socket.
*/
/**
* @addtogroup Enums
* @{
*/
/**
* This enumeration contains the types representing the WebSocket ready state
* and these states are based on the JavaScript WebSocket API specification.
* GetReadyState() returns one of these states.
*/
typedef enum {
/**
* Ready state is queried on an invalid resource.
*/
PP_WEBSOCKETREADYSTATE_INVALID = -1,
/**
* Ready state that the connection has not yet been established.
*/
PP_WEBSOCKETREADYSTATE_CONNECTING = 0,
/**
* Ready state that the WebSocket connection is established and communication
* is possible.
*/
PP_WEBSOCKETREADYSTATE_OPEN = 1,
/**
* Ready state that the connection is going through the closing handshake.
*/
PP_WEBSOCKETREADYSTATE_CLOSING = 2,
/**
* Ready state that the connection has been closed or could not be opened.
*/
PP_WEBSOCKETREADYSTATE_CLOSED = 3
} PP_WebSocketReadyState;
PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_WebSocketReadyState, 4);
/**
* This enumeration contains status codes. These codes are used in Close() and
* GetCloseCode(). Refer to RFC 6455, The WebSocket Protocol, for further
* information.
* PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE
and codes in the range
* PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MIN
to
* PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MAX
, and
* PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MIN
to
* PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MAX
are valid for Close().
*/
typedef enum {
/**
* Indicates to request closing connection without status code and reason.
*
* (Note that the code 1005 is forbidden to send in actual close frames by
* the RFC. PP_WebSocket reuses this code internally and the code will never
* appear in the actual close frames.)
*/
PP_WEBSOCKETSTATUSCODE_NOT_SPECIFIED = 1005,
/**
* Status codes in the range 0-999 are not used.
*/
/**
* Indicates a normal closure.
*/
PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE = 1000,
/**
* Indicates that an endpoint is "going away", such as a server going down.
*/
PP_WEBSOCKETSTATUSCODE_GOING_AWAY = 1001,
/**
* Indicates that an endpoint is terminating the connection due to a protocol
* error.
*/
PP_WEBSOCKETSTATUSCODE_PROTOCOL_ERROR = 1002,
/**
* Indicates that an endpoint is terminating the connection because it has
* received a type of data it cannot accept.
*/
PP_WEBSOCKETSTATUSCODE_UNSUPPORTED_DATA = 1003,
/**
* Status code 1004 is reserved.
*/
/**
* Pseudo code to indicate that receiving close frame doesn't contain any
* status code.
*/
PP_WEBSOCKETSTATUSCODE_NO_STATUS_RECEIVED = 1005,
/**
* Pseudo code to indicate that connection was closed abnormally, e.g.,
* without closing handshake.
*/
PP_WEBSOCKETSTATUSCODE_ABNORMAL_CLOSURE = 1006,
/**
* Indicates that an endpoint is terminating the connection because it has
* received data within a message that was not consistent with the type of
* the message (e.g., non-UTF-8 data within a text message).
*/
PP_WEBSOCKETSTATUSCODE_INVALID_FRAME_PAYLOAD_DATA = 1007,
/**
* Indicates that an endpoint is terminating the connection because it has
* received a message that violates its policy.
*/
PP_WEBSOCKETSTATUSCODE_POLICY_VIOLATION = 1008,
/**
* Indicates that an endpoint is terminating the connection because it has
* received a message that is too big for it to process.
*/
PP_WEBSOCKETSTATUSCODE_MESSAGE_TOO_BIG = 1009,
/**
* Indicates that an endpoint (client) is terminating the connection because
* it has expected the server to negotiate one or more extension, but the
* server didn't return them in the response message of the WebSocket
* handshake.
*/
PP_WEBSOCKETSTATUSCODE_MANDATORY_EXTENSION = 1010,
/**
* Indicates that a server is terminating the connection because it
* encountered an unexpected condition.
*/
PP_WEBSOCKETSTATUSCODE_INTERNAL_SERVER_ERROR = 1011,
/**
* Status codes in the range 1012-1014 are reserved.
*/
/**
* Pseudo code to indicate that the connection was closed due to a failure to
* perform a TLS handshake.
*/
PP_WEBSOCKETSTATUSCODE_TLS_HANDSHAKE = 1015,
/**
* Status codes in the range 1016-2999 are reserved.
*/
/**
* Status codes in the range 3000-3999 are reserved for use by libraries,
* frameworks, and applications. These codes are registered directly with
* IANA.
*/
PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MIN = 3000,
PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MAX = 3999,
/**
* Status codes in the range 4000-4999 are reserved for private use.
* Application can use these codes for application specific purposes freely.
*/
PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MIN = 4000,
PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MAX = 4999
} PP_WebSocketCloseCode;
PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_WebSocketCloseCode, 4);
/**
* @}
*/
/**
* @addtogroup Interfaces
* @{
*/
/**
* The PPB_WebSocket
interface provides bi-directional,
* full-duplex, communications over a single TCP socket.
*/
struct PPB_WebSocket_1_0 {
/**
* Create() creates a WebSocket instance.
*
* @param[in] instance A PP_Instance
identifying the instance
* with the WebSocket.
*
* @return A PP_Resource
corresponding to a WebSocket if
* successful.
*/
PP_Resource (*Create)(PP_Instance instance);
/**
* IsWebSocket() determines if the provided resource
is a
* WebSocket instance.
*
* @param[in] resource A PP_Resource
corresponding to a
* WebSocket.
*
* @return Returns PP_TRUE
if resource
is a
* PPB_WebSocket
, PP_FALSE
if the
* resource
is invalid or some type other than
* PPB_WebSocket
.
*/
PP_Bool (*IsWebSocket)(PP_Resource resource);
/**
* Connect() connects to the specified WebSocket server. You can call this
* function once for a web_socket
.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @param[in] url A PP_Var
representing a WebSocket server URL.
* The PP_VarType
must be PP_VARTYPE_STRING
.
*
* @param[in] protocols A pointer to an array of PP_Var
* specifying sub-protocols. Each PP_Var
represents one
* sub-protocol and its PP_VarType
must be
* PP_VARTYPE_STRING
. This argument can be null only if
* protocol_count
is 0.
*
* @param[in] protocol_count The number of sub-protocols in
* protocols
.
*
* @param[in] callback A PP_CompletionCallback
called
* when a connection is established or an error occurs in establishing
* connection.
*
* @return An int32_t containing an error code from pp_errors.h
.
* Returns PP_ERROR_BADARGUMENT
if the specified
* url
, or protocols
contain an invalid string as
* defined in the WebSocket API specification.
* PP_ERROR_BADARGUMENT
corresponds to a SyntaxError in the
* WebSocket API specification.
* Returns PP_ERROR_NOACCESS
if the protocol specified in the
* url
is not a secure protocol, but the origin of the caller
* has a secure scheme. Also returns PP_ERROR_NOACCESS
if the
* port specified in the url
is a port that the user agent
* is configured to block access to because it is a well-known port like
* SMTP. PP_ERROR_NOACCESS
corresponds to a SecurityError of the
* specification.
* Returns PP_ERROR_INPROGRESS
if this is not the first call to
* Connect().
*/
int32_t (*Connect)(PP_Resource web_socket,
struct PP_Var url,
const struct PP_Var protocols[],
uint32_t protocol_count,
struct PP_CompletionCallback callback);
/**
* Close() closes the specified WebSocket connection by specifying
* code
and reason
.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @param[in] code The WebSocket close code. This is ignored if it is
* PP_WEBSOCKETSTATUSCODE_NOT_SPECIFIED
.
* PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE
must be used for the
* usual case. To indicate some specific error cases, codes in the range
* PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MIN
to
* PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MAX
, and in the range
* PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MIN
to
* PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MAX
are available.
*
* @param[in] reason A PP_Var
representing the WebSocket
* close reason. This is ignored if it is PP_VARTYPE_UNDEFINED
.
* Otherwise, its PP_VarType
must be
* PP_VARTYPE_STRING
.
*
* @param[in] callback A PP_CompletionCallback
called
* when the connection is closed or an error occurs in closing the
* connection.
*
* @return An int32_t containing an error code from pp_errors.h
.
* Returns PP_ERROR_BADARGUMENT
if reason
contains
* an invalid character as a UTF-8 string, or is longer than 123 bytes.
* PP_ERROR_BADARGUMENT
corresponds to a JavaScript SyntaxError
* in the WebSocket API specification.
* Returns PP_ERROR_NOACCESS
if the code is not an integer
* equal to 1000 or in the range 3000 to 4999. PP_ERROR_NOACCESS
* corresponds to an InvalidAccessError in the WebSocket API specification.
* Returns PP_ERROR_INPROGRESS
if a previous call to Close() is
* not finished.
*/
int32_t (*Close)(PP_Resource web_socket,
uint16_t code,
struct PP_Var reason,
struct PP_CompletionCallback callback);
/**
* ReceiveMessage() receives a message from the WebSocket server.
* This interface only returns a single message. That is, this interface must
* be called at least N times to receive N messages, no matter the size of
* each message.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @param[out] message The received message is copied to provided
* message
. The message
must remain valid until
* ReceiveMessage() completes. Its received PP_VarType
will be
* PP_VARTYPE_STRING
or PP_VARTYPE_ARRAY_BUFFER
.
*
* @param[in] callback A PP_CompletionCallback
called
* when ReceiveMessage() completes. This callback is ignored if
* ReceiveMessage() completes synchronously and returns PP_OK
.
*
* @return An int32_t containing an error code from pp_errors.h
.
* If an error is detected or connection is closed, ReceiveMessage() returns
* PP_ERROR_FAILED
after all buffered messages are received.
* Until buffered message become empty, ReceiveMessage() continues to return
* PP_OK
as if connection is still established without errors.
*/
int32_t (*ReceiveMessage)(PP_Resource web_socket,
struct PP_Var* message,
struct PP_CompletionCallback callback);
/**
* SendMessage() sends a message to the WebSocket server.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @param[in] message A message to send. The message is copied to an internal
* buffer, so the caller can free message
safely after returning
* from the function. Its sent PP_VarType
must be
* PP_VARTYPE_STRING
or PP_VARTYPE_ARRAY_BUFFER
.
*
* @return An int32_t containing an error code from pp_errors.h
.
* Returns PP_ERROR_FAILED
if the ReadyState is
* PP_WEBSOCKETREADYSTATE_CONNECTING
.
* PP_ERROR_FAILED
corresponds to a JavaScript
* InvalidStateError in the WebSocket API specification.
* Returns PP_ERROR_BADARGUMENT
if the provided
* message
contains an invalid character as a UTF-8 string.
* PP_ERROR_BADARGUMENT
corresponds to a JavaScript
* SyntaxError in the WebSocket API specification.
* Otherwise, returns PP_OK
, which doesn't necessarily mean
* that the server received the message.
*/
int32_t (*SendMessage)(PP_Resource web_socket, struct PP_Var message);
/**
* GetBufferedAmount() returns the number of bytes of text and binary
* messages that have been queued for the WebSocket connection to send, but
* have not been transmitted to the network yet.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @return Returns the number of bytes.
*/
uint64_t (*GetBufferedAmount)(PP_Resource web_socket);
/**
* GetCloseCode() returns the connection close code for the WebSocket
* connection.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @return Returns 0 if called before the close code is set.
*/
uint16_t (*GetCloseCode)(PP_Resource web_socket);
/**
* GetCloseReason() returns the connection close reason for the WebSocket
* connection.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @return Returns a PP_VARTYPE_STRING
var. If called before the
* close reason is set, the return value contains an empty string. Returns a
* PP_VARTYPE_UNDEFINED
if called on an invalid resource.
*/
struct PP_Var (*GetCloseReason)(PP_Resource web_socket);
/**
* GetCloseWasClean() returns if the connection was closed cleanly for the
* specified WebSocket connection.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @return Returns PP_FALSE
if called before the connection is
* closed, called on an invalid resource, or closed for abnormal reasons.
* Otherwise, returns PP_TRUE
if the connection was closed
* cleanly.
*/
PP_Bool (*GetCloseWasClean)(PP_Resource web_socket);
/**
* GetExtensions() returns the extensions selected by the server for the
* specified WebSocket connection.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @return Returns a PP_VARTYPE_STRING
var. If called before the
* connection is established, the var's data is an empty string. Returns a
* PP_VARTYPE_UNDEFINED
if called on an invalid resource.
*/
struct PP_Var (*GetExtensions)(PP_Resource web_socket);
/**
* GetProtocol() returns the sub-protocol chosen by the server for the
* specified WebSocket connection.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @return Returns a PP_VARTYPE_STRING
var. If called before the
* connection is established, the var contains the empty string. Returns a
* PP_VARTYPE_UNDEFINED
if called on an invalid resource.
*/
struct PP_Var (*GetProtocol)(PP_Resource web_socket);
/**
* GetReadyState() returns the ready state of the specified WebSocket
* connection.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @return Returns PP_WEBSOCKETREADYSTATE_INVALID
if called
* before Connect() is called, or if this function is called on an
* invalid resource.
*/
PP_WebSocketReadyState (*GetReadyState)(PP_Resource web_socket);
/**
* GetURL() returns the URL associated with specified WebSocket connection.
*
* @param[in] web_socket A PP_Resource
corresponding to a
* WebSocket.
*
* @return Returns a PP_VARTYPE_STRING
var. If called before the
* connection is established, the var contains the empty string. Returns a
* PP_VARTYPE_UNDEFINED
if this function is called on an
* invalid resource.
*/
struct PP_Var (*GetURL)(PP_Resource web_socket);
};
typedef struct PPB_WebSocket_1_0 PPB_WebSocket;
/**
* @}
*/
#endif /* PPAPI_C_PPB_WEBSOCKET_H_ */