OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "ppapi/proxy/websocket_resource.h" | 5 #include "ppapi/proxy/websocket_resource.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 109 |
110 protocol_strings.push_back(protocol->value()); | 110 protocol_strings.push_back(protocol->value()); |
111 } | 111 } |
112 | 112 |
113 // Install callback. | 113 // Install callback. |
114 connect_callback_ = callback; | 114 connect_callback_ = callback; |
115 | 115 |
116 // Create remote host in the renderer, then request to check the URL and | 116 // Create remote host in the renderer, then request to check the URL and |
117 // establish the connection. | 117 // establish the connection. |
118 state_ = PP_WEBSOCKETREADYSTATE_CONNECTING; | 118 state_ = PP_WEBSOCKETREADYSTATE_CONNECTING; |
119 SendCreateToRenderer(PpapiHostMsg_WebSocket_Create()); | 119 SendCreate(RENDERER, PpapiHostMsg_WebSocket_Create()); |
120 PpapiHostMsg_WebSocket_Connect msg(url_->value(), protocol_strings); | 120 PpapiHostMsg_WebSocket_Connect msg(url_->value(), protocol_strings); |
121 CallRenderer<PpapiPluginMsg_WebSocket_ConnectReply>(msg, | 121 Call<PpapiPluginMsg_WebSocket_ConnectReply>(RENDERER, msg, |
122 base::Bind(&WebSocketResource::OnPluginMsgConnectReply, this)); | 122 base::Bind(&WebSocketResource::OnPluginMsgConnectReply, this)); |
123 | 123 |
124 return PP_OK_COMPLETIONPENDING; | 124 return PP_OK_COMPLETIONPENDING; |
125 } | 125 } |
126 | 126 |
127 int32_t WebSocketResource::Close(uint16_t code, | 127 int32_t WebSocketResource::Close(uint16_t code, |
128 const PP_Var& reason, | 128 const PP_Var& reason, |
129 scoped_refptr<TrackedCallback> callback) { | 129 scoped_refptr<TrackedCallback> callback) { |
130 if (TrackedCallback::IsPending(close_callback_)) | 130 if (TrackedCallback::IsPending(close_callback_)) |
131 return PP_ERROR_INPROGRESS; | 131 return PP_ERROR_INPROGRESS; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 | 171 |
172 // Install |callback|. | 172 // Install |callback|. |
173 close_callback_ = callback; | 173 close_callback_ = callback; |
174 | 174 |
175 // Abort ongoing connect. | 175 // Abort ongoing connect. |
176 if (TrackedCallback::IsPending(connect_callback_)) { | 176 if (TrackedCallback::IsPending(connect_callback_)) { |
177 state_ = PP_WEBSOCKETREADYSTATE_CLOSING; | 177 state_ = PP_WEBSOCKETREADYSTATE_CLOSING; |
178 // Need to do a "Post" to avoid reentering the plugin. | 178 // Need to do a "Post" to avoid reentering the plugin. |
179 connect_callback_->PostAbort(); | 179 connect_callback_->PostAbort(); |
180 connect_callback_ = NULL; | 180 connect_callback_ = NULL; |
181 PostToRenderer(PpapiHostMsg_WebSocket_Fail( | 181 Post(RENDERER, PpapiHostMsg_WebSocket_Fail( |
182 "WebSocket was closed before the connection was established.")); | 182 "WebSocket was closed before the connection was established.")); |
183 return PP_OK_COMPLETIONPENDING; | 183 return PP_OK_COMPLETIONPENDING; |
184 } | 184 } |
185 | 185 |
186 // Abort ongoing receive. | 186 // Abort ongoing receive. |
187 if (TrackedCallback::IsPending(receive_callback_)) { | 187 if (TrackedCallback::IsPending(receive_callback_)) { |
188 receive_callback_var_ = NULL; | 188 receive_callback_var_ = NULL; |
189 // Need to do a "Post" to avoid reentering the plugin. | 189 // Need to do a "Post" to avoid reentering the plugin. |
190 receive_callback_->PostAbort(); | 190 receive_callback_->PostAbort(); |
191 receive_callback_ = NULL; | 191 receive_callback_ = NULL; |
192 } | 192 } |
193 | 193 |
194 // Close connection. | 194 // Close connection. |
195 state_ = PP_WEBSOCKETREADYSTATE_CLOSING; | 195 state_ = PP_WEBSOCKETREADYSTATE_CLOSING; |
196 PpapiHostMsg_WebSocket_Close msg(static_cast<int32_t>(event_code), | 196 PpapiHostMsg_WebSocket_Close msg(static_cast<int32_t>(event_code), |
197 reason_string); | 197 reason_string); |
198 CallRenderer<PpapiPluginMsg_WebSocket_CloseReply>(msg, | 198 Call<PpapiPluginMsg_WebSocket_CloseReply>(RENDERER, msg, |
199 base::Bind(&WebSocketResource::OnPluginMsgCloseReply, this)); | 199 base::Bind(&WebSocketResource::OnPluginMsgCloseReply, this)); |
200 return PP_OK_COMPLETIONPENDING; | 200 return PP_OK_COMPLETIONPENDING; |
201 } | 201 } |
202 | 202 |
203 int32_t WebSocketResource::ReceiveMessage( | 203 int32_t WebSocketResource::ReceiveMessage( |
204 PP_Var* message, | 204 PP_Var* message, |
205 scoped_refptr<TrackedCallback> callback) { | 205 scoped_refptr<TrackedCallback> callback) { |
206 if (TrackedCallback::IsPending(receive_callback_)) | 206 if (TrackedCallback::IsPending(receive_callback_)) |
207 return PP_ERROR_INPROGRESS; | 207 return PP_ERROR_INPROGRESS; |
208 | 208 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 262 |
263 return PP_ERROR_FAILED; | 263 return PP_ERROR_FAILED; |
264 } | 264 } |
265 | 265 |
266 // Send the message. | 266 // Send the message. |
267 if (message.type == PP_VARTYPE_STRING) { | 267 if (message.type == PP_VARTYPE_STRING) { |
268 // Convert message to std::string, then send it. | 268 // Convert message to std::string, then send it. |
269 scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); | 269 scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); |
270 if (!message_string) | 270 if (!message_string) |
271 return PP_ERROR_BADARGUMENT; | 271 return PP_ERROR_BADARGUMENT; |
272 PostToRenderer(PpapiHostMsg_WebSocket_SendText(message_string->value())); | 272 Post(RENDERER, PpapiHostMsg_WebSocket_SendText(message_string->value())); |
273 } else if (message.type == PP_VARTYPE_ARRAY_BUFFER) { | 273 } else if (message.type == PP_VARTYPE_ARRAY_BUFFER) { |
274 // Convert message to std::vector<uint8_t>, then send it. | 274 // Convert message to std::vector<uint8_t>, then send it. |
275 scoped_refptr<ArrayBufferVar> message_arraybuffer = | 275 scoped_refptr<ArrayBufferVar> message_arraybuffer = |
276 ArrayBufferVar::FromPPVar(message); | 276 ArrayBufferVar::FromPPVar(message); |
277 if (!message_arraybuffer) | 277 if (!message_arraybuffer) |
278 return PP_ERROR_BADARGUMENT; | 278 return PP_ERROR_BADARGUMENT; |
279 uint8_t* message_data = static_cast<uint8_t*>(message_arraybuffer->Map()); | 279 uint8_t* message_data = static_cast<uint8_t*>(message_arraybuffer->Map()); |
280 uint32 message_length = message_arraybuffer->ByteLength(); | 280 uint32 message_length = message_arraybuffer->ByteLength(); |
281 std::vector<uint8_t> message_vector(message_data, | 281 std::vector<uint8_t> message_vector(message_data, |
282 message_data + message_length); | 282 message_data + message_length); |
283 PostToRenderer(PpapiHostMsg_WebSocket_SendBinary(message_vector)); | 283 Post(RENDERER, PpapiHostMsg_WebSocket_SendBinary(message_vector)); |
284 } else { | 284 } else { |
285 // TODO(toyoshim): Support Blob. | 285 // TODO(toyoshim): Support Blob. |
286 return PP_ERROR_NOTSUPPORTED; | 286 return PP_ERROR_NOTSUPPORTED; |
287 } | 287 } |
288 return PP_OK; | 288 return PP_OK; |
289 } | 289 } |
290 | 290 |
291 uint64_t WebSocketResource::GetBufferedAmount() { | 291 uint64_t WebSocketResource::GetBufferedAmount() { |
292 return SaturateAdd(buffered_amount_, buffered_amount_after_close_); | 292 return SaturateAdd(buffered_amount_, buffered_amount_after_close_); |
293 } | 293 } |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 return PP_OK; | 500 return PP_OK; |
501 | 501 |
502 *receive_callback_var_ = received_messages_.front()->GetPPVar(); | 502 *receive_callback_var_ = received_messages_.front()->GetPPVar(); |
503 received_messages_.pop(); | 503 received_messages_.pop(); |
504 receive_callback_var_ = NULL; | 504 receive_callback_var_ = NULL; |
505 return PP_OK; | 505 return PP_OK; |
506 } | 506 } |
507 | 507 |
508 } // namespace proxy | 508 } // namespace proxy |
509 } // namespace ppapi | 509 } // namespace ppapi |
OLD | NEW |