OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_CPP_DEV_WEBSOCKET_DEV_H_ | 5 #ifndef PPAPI_CPP_DEV_WEBSOCKET_DEV_H_ |
6 #define PPAPI_CPP_DEV_WEBSOCKET_DEV_H_ | 6 #define PPAPI_CPP_DEV_WEBSOCKET_DEV_H_ |
7 | 7 |
8 #include "ppapi/c/dev/ppb_websocket_dev.h" | 8 #include "ppapi/c/dev/ppb_websocket_dev.h" |
| 9 #include "ppapi/cpp/resource.h" |
9 | 10 |
10 /// @file | 11 /// @file |
11 /// This file defines the WebSocket_Dev interface. | 12 /// This file defines the WebSocket_Dev interface. |
12 | 13 |
13 namespace pp { | 14 namespace pp { |
14 | 15 |
| 16 class CompletionCallback; |
| 17 class Instance; |
15 class Var; | 18 class Var; |
16 | 19 |
17 /// The <code>WebSocket_Dev</code> class | 20 /// The <code>WebSocket_Dev</code> class |
18 /// A version that use virtual functions | 21 /// A version that use virtual functions |
19 class WebSocket_Dev : public Resource { | 22 class WebSocket_Dev : public Resource { |
20 public: | 23 public: |
21 /// Constructs a WebSocket_Dev object. | 24 /// Constructs a WebSocket_Dev object. |
22 WebSocket_Dev(); | 25 WebSocket_Dev(Instance* instance); |
23 | 26 |
24 /// Destructs a WebSocket_Dev object. | 27 /// Destructs a WebSocket_Dev object. |
25 virtual ~WebSocket_Dev(); | 28 virtual ~WebSocket_Dev(); |
26 | 29 |
27 /// Connect() connects to the specified WebSocket server. Caller can call | 30 /// Connect() connects to the specified WebSocket server. Caller can call |
28 /// this method at most once. | 31 /// this method at most once. |
29 /// | 32 /// |
30 /// @param[in] url A <code>PP_Var</code> representing a WebSocket server URL. | 33 /// @param[in] url A <code>Var</code> of string type representing a WebSocket |
31 /// The <code>PP_VarType</code> must be <code>PP_VARTYPE_STRING</code>. | 34 /// server URL. |
32 /// @param[in] protocols A pointer to an array of <code>PP_Var</code> | 35 /// @param[in] protocols A pointer to an array of string type |
33 /// specifying sub-protocols. Each <code>PP_Var</code> represents one | 36 /// <code>Var</code> specifying sub-protocols. Each <code>Var</code> |
34 /// sub-protocol and its <code>PP_VarType</code> must be | 37 /// represents one sub-protocol. This argument can be null only if |
35 /// <code>PP_VARTYPE_STRING</code>. This argument can be null only if | |
36 /// <code>protocol_count</code> is 0. | 38 /// <code>protocol_count</code> is 0. |
37 /// @param[in] protocol_count The number of sub-protocols in | 39 /// @param[in] protocol_count The number of sub-protocols in |
38 /// <code>protocols</code>. | 40 /// <code>protocols</code>. |
| 41 /// @param[in] callback A <code>CompletionCallback</code> which is called |
| 42 /// when a connection is established or an error occurs in establishing |
| 43 /// connection. |
39 /// | 44 /// |
40 /// @return In case of immediate failure, returns an error code as follows. | 45 /// @return An int32_t containing an error code from |
41 /// Returns <code>PP_ERROR_BADARGUMENT</code> corresponding to JavaScript | 46 /// <code>pp_errors.h</code>. |
42 /// SyntaxError and <code>PP_ERROR_NOACCESS</code> corresponding to | 47 /// Returns <code>PP_ERROR_BADARGUMENT</code> if specified <code>url</code>, |
43 /// JavaScript SecurityError. Otherwise, returns | 48 /// or <code>protocols</code> contains invalid string as |
44 /// <code>PP_OK_COMPLETIONPENDING</code> and later invokes | 49 /// <code>The WebSocket API specification</code> defines. It corresponds to |
45 /// <code>OnOpen()</code> on success or <code>OnClose()</code> on failure. | 50 /// SyntaxError of the specification. |
| 51 /// Returns <code>PP_ERROR_NOACCESS</code> if the protocol specified in the |
| 52 /// <code>url</code> is not secure protocol, but the origin of the caller |
| 53 /// has a secure scheme. Also returns it if the port specified in the |
| 54 /// <code>url</code> is a port to which the user agent is configured to block |
| 55 /// access because the port is well-known ports like SMTP, or so. It |
| 56 /// corresponds to SecurityError of the specification. |
| 57 /// Returns <code>PP_ERROR_INPROGRESS</code> if the call is not the first |
| 58 /// time. |
46 int32_t Connect(const Var& url, const Var protocols[], | 59 int32_t Connect(const Var& url, const Var protocols[], |
47 uint32_t protocol_count); | 60 uint32_t protocol_count, const CompletionCallback& callback); |
48 | 61 |
49 /// Close() closes the specified WebSocket connection by specifying | 62 /// Close() closes the specified WebSocket connection by specifying |
50 /// <code>code</code> and <code>reason</code>. | 63 /// <code>code</code> and <code>reason</code>. |
51 /// | 64 /// |
52 /// @param[in] code The WebSocket close code. Ignored if it is 0. | 65 /// @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 | 66 /// @param[in] reason A <code>Var</code> of string type which represents the |
54 /// close reason. Ignored if it is <code>PP_VARTYPE_UNDEFINED</code>. | 67 /// WebSocket close reason. Ignored if it is undefined type. |
55 /// Otherwise, its <code>PP_VarType</code> must be | 68 /// @param[in] callback A <code>CompletionCallback</code> which is called |
56 /// <code>PP_VARTYPE_STRING</code>. | 69 /// when the connection is closed or an error occurs in closing the |
| 70 /// connection. |
57 /// | 71 /// |
58 /// @return In case of immediate failure, returns an error code as follows. | 72 /// @return An int32_t containing an error code from |
59 /// Returns <code>PP_ERROR_BADARGUMENT</code> corresponding to JavaScript | 73 /// <code>pp_errors.h</code>. |
60 /// SyntaxError and <code>PP_ERROR_NOACCESS</code> corresponding to | 74 /// Returns <code>PP_ERROR_BADARGUMENT</code> if <code>reason</code> contains |
61 /// JavaScript InvalidAccessError. Otherwise, returns | 75 /// an invalid character as a UTF-8 string, or longer than 123 bytes. It |
62 /// <code>PP_OK_COMPLETIONPENDING</code> and invokes <code>OnClose</code>. | 76 /// corresponds to JavaScript SyntaxError of the specification. |
63 int32_t Close(uint16_t code, const Var& reason); | 77 /// Returns <code>PP_ERROR_NOACCESS</code> if the code is not an integer |
| 78 /// equal to 1000 or in the range 3000 to 4999. It corresponds to |
| 79 /// InvalidAccessError of the specification. Returns |
| 80 /// <code>PP_ERROR_INPROGRESS</code> if the call is not the first time. |
| 81 int32_t Close(uint16_t code, const Var& reason, |
| 82 const CompletionCallback& callback); |
| 83 |
| 84 /// ReceiveMessage() receives a message from the WebSocket server. |
| 85 /// This interface only returns a single message. That is, this interface |
| 86 /// must be called at least N times to receive N messages, no matter how |
| 87 /// small each message is. |
| 88 /// |
| 89 /// @param[out] message The received message is copied to provided |
| 90 /// <code>message</code>. The <code>message</code> must remain valid until |
| 91 /// the ReceiveMessage operation completes. |
| 92 /// @param[in] callback A <code>CompletionCallback</code> which is called |
| 93 /// when the receiving message is completed. It is ignored if ReceiveMessage |
| 94 /// completes synchronously and returns <code>PP_OK</code>. |
| 95 /// |
| 96 /// @return An int32_t containing an error code from |
| 97 /// <code>pp_errors.h</code>. |
| 98 /// If an error is detected or connection is closed, returns |
| 99 /// <code>PP_ERROR_FAILED</code> after all buffered messages are received. |
| 100 /// Until buffered message become empty, continues to returns |
| 101 /// <code>PP_OK</code> as if connection is still established without errors. |
| 102 int32_t ReceiveMessage(Var* message, |
| 103 const CompletionCallback& callback); |
64 | 104 |
65 /// Send() sends a message to the WebSocket server. | 105 /// Send() sends a message to the WebSocket server. |
66 /// | 106 /// |
67 /// @param[in] data A message to send. The message is copied to internal | 107 /// @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 | 108 /// buffer. So caller can free <code>data</code> safely after returning |
69 /// from the function. | 109 /// from the function. |
70 /// | 110 /// |
71 /// @return In case of immediate failure, returns an error code as follows. | 111 /// @return An int32_t containing an error code from |
72 /// Returns <code>PP_ERROR_FAILED</code> corresponding to JavaScript | 112 /// <code>pp_errors.h</code>. |
73 /// InvalidStateError and <code>PP_ERROR_BADARGUMENT</code> corresponding to | 113 /// Returns <code>PP_ERROR_FAILED</code> if the ReadyState is |
74 /// JavaScript SyntaxError. Otherwise, return <code>PP_OK</code>. | 114 /// <code>PP_WEBSOCKETREADYSTATE_CONNECTING_DEV</code>. It corresponds |
75 /// <code>PP_OK</code> doesn't necessarily mean that the server received the | 115 /// JavaScript InvalidStateError of the specification. |
76 /// message. | 116 /// Returns <code>PP_ERROR_BADARGUMENT</code> if provided |
77 int32_t Send(const Var& data); | 117 /// <code>message</code> of string type contains an invalid character as a |
| 118 /// UTF-8 string. It corresponds to JavaScript SyntaxError of the |
| 119 /// specification. |
| 120 /// Otherwise, returns <code>PP_OK</code>, but it doesn't necessarily mean |
| 121 /// that the server received the message. |
| 122 int32_t SendMessage(const Var& message); |
78 | 123 |
79 /// GetBufferedAmount() returns the number of bytes of text and binary | 124 /// GetBufferedAmount() returns the number of bytes of text and binary |
80 /// messages that have been queued for the WebSocket connection to send but | 125 /// messages that have been queued for the WebSocket connection to send but |
81 /// have not been transmitted to the network yet. | 126 /// have not been transmitted to the network yet. |
82 /// | 127 /// |
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. | 128 /// @return Returns the number of bytes. |
88 uint64_t GetBufferedAmount(); | 129 uint64_t GetBufferedAmount(); |
89 | 130 |
| 131 /// GetCloseCode() returns the connection close code for the WebSocket |
| 132 /// connection. |
| 133 /// |
| 134 /// @return Returns 0 if called before the close code is set. |
| 135 uint16_t GetCloseCode(); |
| 136 |
| 137 /// GetCloseReason() returns the connection close reason for the WebSocket |
| 138 /// connection. |
| 139 /// |
| 140 /// @return Returns a <code>Var</code> of string type. If called before the |
| 141 /// close reason is set, it contains an empty string. |
| 142 Var GetCloseReason(); |
| 143 |
| 144 /// GetCloseWasClean() returns if the connection was closed cleanly for the |
| 145 /// specified WebSocket connection. |
| 146 /// |
| 147 /// @return Returns <code>false</code> if called before the connection is |
| 148 /// closed, or called on an invalid resource. Otherwise, returns |
| 149 /// <code>true</code> if the connection was closed cleanly, or returns |
| 150 /// <code>false</code> if the connection was closed for abnormal reasons. |
| 151 bool GetCloseWasClean(); |
| 152 |
90 /// GetExtensions() returns the extensions selected by the server for the | 153 /// GetExtensions() returns the extensions selected by the server for the |
91 /// specified WebSocket connection. | 154 /// specified WebSocket connection. |
92 /// | 155 /// |
93 /// @return Returns a <code>PP_VARTYPE_STRING</code> var. If called before | 156 /// @return Returns a <code>Var</code> of string type. If called before the |
94 /// the connection is established, its data is empty string. | 157 /// connection is established, its data is an empty string. |
95 /// Currently its data is always empty string. | 158 /// Currently its data is always an empty string. |
96 Var GetExtensions(); | 159 Var GetExtensions(); |
97 | 160 |
98 /// GetProtocol() returns the sub-protocol chosen by the server for the | 161 /// GetProtocol() returns the sub-protocol chosen by the server for the |
99 /// specified WebSocket connection. | 162 /// specified WebSocket connection. |
100 /// | 163 /// |
101 /// @return Returns a <code>PP_VARTYPE_STRING</code> var. If called before | 164 /// @return Returns a <code>Var</code> of string type. If called before the |
102 /// the connection is established, its data is empty string. | 165 /// connection is established, it contains the empty string. |
103 Var GetProtocol(); | 166 Var GetProtocol(); |
104 | 167 |
105 /// GetReadyState() returns the ready state of the specified WebSocket | 168 /// GetReadyState() returns the ready state of the specified WebSocket |
106 /// connection. | 169 /// connection. |
107 /// | 170 /// |
108 /// @return Returns <code>PP_WEBSOCKETREADYSTATE_INVALID_DEV</code> if called | 171 /// @return Returns <code>PP_WEBSOCKETREADYSTATE_INVALID_DEV</code> if called |
109 /// before connect() is called. | 172 /// before connect() is called. |
110 PP_WebSocketReadyState_Dev GetReadyState(); | 173 PP_WebSocketReadyState_Dev GetReadyState(); |
111 | 174 |
112 /// GetURL() returns the URL associated with specified WebSocket connection. | 175 /// GetURL() returns the URL associated with specified WebSocket connection. |
113 /// | 176 /// |
114 /// @return Returns a <code>PP_VARTYPE_STRING</code> var. If called before | 177 /// @return Returns a <code>Var</code> of string type. If called before the |
115 /// the connection is established, its data is empty string. | 178 /// connection is established, it contains the empty string. |
116 Var GetURL(); | 179 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 }; | 180 }; |
132 | 181 |
133 } // namespace pp | 182 } // namespace pp |
134 | 183 |
135 #endif // PPAPI_CPP_DEV_WEBSOCKET_DEV_H_ | 184 #endif // PPAPI_CPP_DEV_WEBSOCKET_DEV_H_ |
OLD | NEW |