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_CPP_DEV_WEBSOCKET_DEV_H_ |
| 6 #define PPAPI_CPP_DEV_WEBSOCKET_DEV_H_ |
| 7 |
| 8 #include "ppapi/c/dev/ppb_websocket_dev.h" |
| 9 |
| 10 /// @file |
| 11 /// This file defines the WebSocket_Dev interface. |
| 12 |
| 13 namespace pp { |
| 14 |
| 15 class Var; |
| 16 |
| 17 /// The <code>WebSocket_Dev</code> class |
| 18 /// A version that use virtual functions |
| 19 class WebSocket_Dev : public Resource { |
| 20 public: |
| 21 /// Constructs a WebSocket_Dev object. |
| 22 WebSocket_Dev(); |
| 23 |
| 24 /// Destructs a WebSocket_Dev object. |
| 25 virtual ~WebSocket_Dev(); |
| 26 |
| 27 /// Connect() connects to the specified WebSocket server. Caller can call |
| 28 /// this method at most once. |
| 29 /// |
| 30 /// @param[in] url A <code>PP_Var</code> representing a WebSocket server URL. |
| 31 /// The <code>PP_VarType</code> must be <code>PP_VARTYPE_STRING</code>. |
| 32 /// @param[in] protocols A pointer to an array of <code>PP_Var</code> |
| 33 /// specifying sub-protocols. Each <code>PP_Var</code> represents one |
| 34 /// sub-protocol and its <code>PP_VarType</code> must be |
| 35 /// <code>PP_VARTYPE_STRING</code>. This argument can be null only if |
| 36 /// <code>protocol_count</code> is 0. |
| 37 /// @param[in] protocol_count The number of sub-protocols in |
| 38 /// <code>protocols</code>. |
| 39 /// |
| 40 /// @return In case of immediate failure, returns an error code as follows. |
| 41 /// Returns <code>PP_ERROR_BADARGUMENT</code> corresponding to JavaScript |
| 42 /// SyntaxError and <code>PP_ERROR_NOACCESS</code> corresponding to |
| 43 /// JavaScript SecurityError. Otherwise, returns |
| 44 /// <code>PP_OK_COMPLETIONPENDING</code> and later invokes |
| 45 /// <code>OnOpen()</code> on success or <code>OnClose()</code> on failure. |
| 46 int32_t Connect(const Var& url, const Var protocols[], |
| 47 uint32_t protocol_count); |
| 48 |
| 49 /// Close() closes the specified WebSocket connection by specifying |
| 50 /// <code>code</code> and <code>reason</code>. |
| 51 /// |
| 52 /// @param[in] code The WebSocket close code. Ignored if it is 0. |
| 53 /// @param[in] reason A <code>PP_Var</code> which represents the WebSocket |
| 54 /// close reason. Ignored if it is <code>PP_VARTYPE_UNDEFINED</code>. |
| 55 /// Otherwise, its <code>PP_VarType</code> must be |
| 56 /// <code>PP_VARTYPE_STRING</code>. |
| 57 /// |
| 58 /// @return In case of immediate failure, returns an error code as follows. |
| 59 /// Returns <code>PP_ERROR_BADARGUMENT</code> corresponding to JavaScript |
| 60 /// SyntaxError and <code>PP_ERROR_NOACCESS</code> corresponding to |
| 61 /// JavaScript InvalidAccessError. Otherwise, returns |
| 62 /// <code>PP_OK_COMPLETIONPENDING</code> and invokes <code>OnClose</code>. |
| 63 int32_t Close(uint16_t code, const Var& reason); |
| 64 |
| 65 /// Send() sends a message to the WebSocket server. |
| 66 /// |
| 67 /// @param[in] data A message to send. The message is copied to internal |
| 68 /// buffer. So caller can free <code>data</code> safely after returning |
| 69 /// from the function. |
| 70 /// |
| 71 /// @return In case of immediate failure, returns an error code as follows. |
| 72 /// Returns <code>PP_ERROR_FAILED</code> corresponding to JavaScript |
| 73 /// InvalidStateError and <code>PP_ERROR_BADARGUMENT</code> corresponding to |
| 74 /// JavaScript SyntaxError. Otherwise, return <code>PP_OK</code>. |
| 75 /// <code>PP_OK</code> doesn't necessarily mean that the server received the |
| 76 /// message. |
| 77 int32_t Send(const Var& data); |
| 78 |
| 79 /// GetBufferedAmount() returns the number of bytes of text and binary |
| 80 /// messages that have been queued for the WebSocket connection to send but |
| 81 /// have not been transmitted to the network yet. |
| 82 /// |
| 83 /// Note: This interface might not be able to return exact bytes in the first |
| 84 /// release. Current WebSocket implementation can not estimate exact protocol |
| 85 /// frame overheads. |
| 86 /// |
| 87 /// @return Returns the number of bytes. |
| 88 uint64_t GetBufferedAmount(); |
| 89 |
| 90 /// GetExtensions() returns the extensions selected by the server for the |
| 91 /// specified WebSocket connection. |
| 92 /// |
| 93 /// @return Returns a <code>PP_VARTYPE_STRING</code> var. If called before |
| 94 /// the connection is established, its data is empty string. |
| 95 /// Currently its data is always empty string. |
| 96 Var GetExtensions(); |
| 97 |
| 98 /// GetProtocol() returns the sub-protocol chosen by the server for the |
| 99 /// specified WebSocket connection. |
| 100 /// |
| 101 /// @return Returns a <code>PP_VARTYPE_STRING</code> var. If called before |
| 102 /// the connection is established, its data is empty string. |
| 103 Var GetProtocol(); |
| 104 |
| 105 /// GetReadyState() returns the ready state of the specified WebSocket |
| 106 /// connection. |
| 107 /// |
| 108 /// @return Returns <code>PP_WEBSOCKETREADYSTATE_CONNECTING</code> if called |
| 109 /// before the connection is established. |
| 110 PP_WebSocketReadyState_Dev GetReadyState(); |
| 111 |
| 112 /// GetURL() returns the URL associated with specified WebSocket connection. |
| 113 /// |
| 114 /// @return Returns a <code>PP_VARTYPE_STRING</code> var. If called before |
| 115 /// the connection is established, its data is empty string. |
| 116 Var GetURL(); |
| 117 |
| 118 /// OnOpen() is invoked when the connection is established by Connect(). |
| 119 virtual void OnOpen() = 0; |
| 120 |
| 121 /// OnMessage() is invoked when a message is received. |
| 122 virtual void OnMessage(Var message) = 0; |
| 123 |
| 124 /// OnError() is invoked if the user agent was required to fail the WebSocket |
| 125 /// connection or the WebSocket connection is closed with prejudice. |
| 126 /// OnClose() always follows OnError(). |
| 127 virtual void OnError() = 0; |
| 128 |
| 129 /// OnClose() is invoked when the connection is closed by errors or Close(). |
| 130 virtual void OnClose(bool wasClean, uint16_t code, const Var& reason) = 0; |
| 131 }; |
| 132 |
| 133 } // namespace pp |
| 134 |
| 135 #endif // PPAPI_CPP_DEV_WEBSOCKET_DEV_H_ |
OLD | NEW |