OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2011 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. |
| 4 */ |
| 5 #ifndef PPAPI_C_DEV_PPB_WEBSOCKET_DEV_H_ |
| 6 #define PPAPI_C_DEV_PPB_WEBSOCKET_DEV_H_ |
| 7 |
| 8 #include "ppapi/c/pp_instance.h" |
| 9 #include "ppapi/c/pp_macros.h" |
| 10 #include "ppapi/c/pp_var.h" |
| 11 |
| 12 #define PPB_WEBSOCKET_DEV_INTERFACE_0_1 "PPB_WebSocket(Dev);0.1" |
| 13 #define PPB_WEBSOCKET_DEV_INTERFACE PPB_WEBSOCKET_DEV_INTERFACE_0_1 |
| 14 |
| 15 typedef enum { |
| 16 PP_WEBSOCKETREADYSTATE_CONNECTING = 0, |
| 17 PP_WEBSOCKETREADYSTATE_OPEN = 1, |
| 18 PP_WEBSOCKETREADYSTATE_CLOSING = 2, |
| 19 PP_WEBSOCKETREADYSTATE_CLOSED = 3 |
| 20 } PP_WebSocketReadyState_Dev; |
| 21 PP_COMPILE_ASSERT_ENUM_SIZE_IN_BYTES(PP_WebSocketReadyState_Dev, 4); |
| 22 |
| 23 typedef enum { |
| 24 PP_WEBSOCKET_MESSAGE_TYPE_TEXT = 0, |
| 25 PP_WEBSOCKET_MESSAGE_TYPE_BINARY = 1 |
| 26 } PP_WebSocketMessageType_Dev; |
| 27 PP_COMPILE_ASSERT_ENUM_SIZE_IN_BYTES(PP_WebSocketMessageType_Dev, 4); |
| 28 |
| 29 struct PPB_WebSocket_Dev { |
| 30 // Note on the differences from JavaScript WebSocket API |
| 31 // http://dev.w3.org/html5/websockets/ |
| 32 // |
| 33 // This interface has some differences from its JavaScript counterpart mainly |
| 34 // to make best use of PP_CompletionCallback and be consistent with other |
| 35 // interfaces such as PPB_Transport_Dev. |
| 36 // |
| 37 // - Constructor doesn't take URL or protocols and there is a method to |
| 38 // establish connection. |
| 39 // |
| 40 // - Error handlers or close handlers cannot be registered. (A callback can be |
| 41 // specified on explicitly closing a connection.) To detect an error or |
| 42 // connection closure, ready state, closure cleanness, closure code, and |
| 43 // closure reason should be queried instead. |
| 44 // |
| 45 // - Message size is represented as int32_t rather than uint64_t. |
| 46 // PP_CompletionCallback handles int32_t better. Messages out of that range |
| 47 // are too large to handle efficiently anyway. |
| 48 |
| 49 // Note on error codes. |
| 50 // |
| 51 // Methods that take PP_CompletionCallback can return error codes. They |
| 52 // correspond to JavaScript exceptions as follows: |
| 53 // |
| 54 // PP_ERROR_BADARGUMENT: SYNTAX_ERR |
| 55 // PP_ERROR_NOACCESS: SECURITY_ERR, INVALID_ACCESS_ERR |
| 56 // PP_ERROR_FAILED: INVALID_STATE_ERR |
| 57 // |
| 58 // Other error codes such as PP_ERROR_ABORTED and PP_ERROR_BADRESOURCE can |
| 59 // also be returned. |
| 60 |
| 61 // Creates a WebSocket instance for plugin module |instance|. |
| 62 PP_Resource (*Create)(PP_Instance instance); |
| 63 |
| 64 // Returns PP_TRUE if |resource| is a WebSocket instance, PP_FALSE otherwise. |
| 65 PP_Bool (*IsWebSocket)(PP_Resource resource); |
| 66 |
| 67 // Connects |web_socket| to null-terminated |url| specifying |protocols|. |
| 68 // |protocols| points to an array of null-terminated sub-protocols. In case of |
| 69 // immediate failure, returns an error code as specified above. Returns |
| 70 // PP_OK_COMPLETIONPENDING otherwise. Calls |callback| when the connection is |
| 71 // established or an error occurs. |
| 72 // |
| 73 // Caller's responsibilities: |
| 74 // - Caller can call this method at most once for a |web_socket|. |
| 75 // - |protocols| can be null only if |protocol_count| is 0. |
| 76 int32_t (*Connect)(PP_Resource web_socket, |
| 77 const char* url, |
| 78 const char* const* protocols, |
| 79 uint32_t protocol_count, |
| 80 PP_CompletionCallback callback); |
| 81 |
| 82 // Closes |web_socket| specifying |code| and null-terminated |reason|. |code| |
| 83 // is ignored if it is 0. |reason| is ignored if it is null. In case of |
| 84 // immediate failure, returns an error code as specified above. Returns |
| 85 // PP_OK_COMPLETIONPENDING otherwise. Calls |callback| when the connection is |
| 86 // closed or an error occurs. |
| 87 int32_t (*Close)(PP_Resource web_socket, |
| 88 uint16_t code, |
| 89 const char* reason, |
| 90 PP_CompletionCallback callback); |
| 91 |
| 92 // Receives a message from |web_socket|. In case of immediate failure, returns |
| 93 // an error code as specified above. If a message is currently available, |
| 94 // returns the number of bytes copied to |message_bytes| (see below). |
| 95 // Otherwise, returns PP_OK_COMPLETIONPENDING and calls |callback| when the |
| 96 // message is available. |
| 97 // |
| 98 // Messages are queued internally. Hence this method can be called either |
| 99 // before or after a message is received by the browser. If the queue grows |
| 100 // beyond the browser-dependent limit, |web_socket| is closed and messages are |
| 101 // discarded. |
| 102 // |
| 103 // The message is copied to |message_bytes| up to |message_byte_count| bytes. |
| 104 // |message_bytes| can be null only if |message_byte_count| is 0. The number |
| 105 // of bytes copied is passed to the |callback| as its second argument in case |
| 106 // PP_OK_COMPLETIONPENDING has been returned. The number of remaining bytes, |
| 107 // if any, is set to |*remaining_message_byte_count| if |
| 108 // |remaining_message_byte_count| is not null. The remaining bytes of the |
| 109 // message are returned for subsequent call(s) to this method. |
| 110 // This function only returns bytes of a single message. |
| 111 // That is, |
| 112 // this method must be called at least N times to receive N messages, no |
| 113 // matter how small each message is. |
| 114 // |
| 115 // The message type is set to |
| 116 // |*message_type| if |message_type| is not null. If the message is text, it |
| 117 // is encoded in UTF-8. |
| 118 // |
| 119 // In copying message to |message_bytes|, UTF-8 byte |
| 120 // sequence boundaries are not considered. Hence if the message is larger than |
| 121 // |message_bytes|, the last byte sequence may end prematurely. Likewise, the |
| 122 // first sequence of the bytes returned for the subsequent call may start in |
| 123 // the middle. |
| 124 // |
| 125 // Caller's responsibilities: |
| 126 // - Caller must keep |message_bytes|, |remaining_message_byte_count|, and |
| 127 // |message_type| valid until |callback| is issued or Close() is called. |
| 128 // - |message_bytes| can be null only if |message_byte_count| is 0. |
| 129 int32_t (*ReceiveMessage)(PP_Resource web_socket, |
| 130 void* message_bytes, |
| 131 int32_t message_byte_count, |
| 132 int32_t* remaining_message_byte_count, |
| 133 PP_WebSocketMessageType_Dev* message_type, |
| 134 PP_CompletionCallback callback); |
| 135 |
| 136 // Sends a message to |web_socket|. In case of immediate failure, returns an |
| 137 // error code as specified above. Returns PP_OK otherwise. PP_OK doesn't |
| 138 // necessarily mean that the server received the message. |
| 139 // |
| 140 // Message is specified as |message_bytes| of |message_type| whose length is |
| 141 // |message_byte_count|. |
| 142 int32_t (*SendMessage)(PP_Resource web_socket, |
| 143 void* message_bytes, |
| 144 int32_t message_byte_count, |
| 145 PP_WebSocketMessageType_Dev message_type); |
| 146 |
| 147 // Returns the number of bytes of text and binary messages that have been |
| 148 // queued for |web_socket| for sending but have not been transmitted to the |
| 149 // network yet. |
| 150 uint64_t (*GetBufferedAmount)(PP_Resource web_socket); |
| 151 |
| 152 // Returns the connection close code for |web_socket|. Returns 0 if called |
| 153 // before the close code is set. |
| 154 uint16_t (*GetCloseCode)(PP_Resource web_socket); |
| 155 |
| 156 // Returns the connection close reason for |web_socket|. Returns a |
| 157 // PP_VARTYPE_NULL var if called before the close reason is set, or |
| 158 // PP_VARTYPE_UNDEFINED if called on an invalid resource. |
| 159 PP_Var (*GetCloseReason)(PP_Resource web_socket); |
| 160 |
| 161 // Returns if the connection was closed cleanly for |web_socket|. Returns |
| 162 // false if called before the connection is closed. |
| 163 PP_Bool (*GetCloseWasClean)(PP_Resource web_socket); |
| 164 |
| 165 // Returns the extension selected by the server for |web_socket|. Returns a |
| 166 // PP_VARTYPE_NULL var if called before the connection is established, or |
| 167 // PP_VARTYPE_UNDEFINED if called on an invalid resource. |
| 168 PP_Var (*GetExtensions)(PP_Resource web_socket); |
| 169 |
| 170 // Returns the sub-protocol chosen by the server for |web_socket|. Returns a |
| 171 // PP_VARTYPE_NULL var if called before the connection is established, or |
| 172 // PP_VARTYPE_UNDEFINED if called on an invalid resource. |
| 173 PP_Var (*GetProtocol)(PP_Resource web_socket); |
| 174 |
| 175 // Returns the ready state of |web_socket|. Returns |
| 176 // PP_WEBSOCKETREADYSTATE_CONNECTING if called before the connection is |
| 177 // established. |
| 178 PP_WebSocketReadyState_Dev (*GetReadyState)(PP_Resource web_socket); |
| 179 |
| 180 // Returns the URL associated with |web_socket|. Returns an empty string if |
| 181 // Returns a PP_VARTYPE_NULL var if called before the connection is |
| 182 // established, or PP_VARTYPE_UNDEFINED if called on an invalid resource. |
| 183 PP_Var (*GetUrl)(PP_Resource web_socket); |
| 184 }; |
| 185 |
| 186 #endif // PPAPI_C_DEV_PPB_WEBSOCKET_DEV_H_ |
OLD | NEW |