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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 if (TrackedCallback::IsPending(connect_callback_)) | 82 if (TrackedCallback::IsPending(connect_callback_)) |
83 return PP_ERROR_INPROGRESS; | 83 return PP_ERROR_INPROGRESS; |
84 | 84 |
85 // Connect() can be called at most once. | 85 // Connect() can be called at most once. |
86 if (state_ != PP_WEBSOCKETREADYSTATE_INVALID) | 86 if (state_ != PP_WEBSOCKETREADYSTATE_INVALID) |
87 return PP_ERROR_INPROGRESS; | 87 return PP_ERROR_INPROGRESS; |
88 state_ = PP_WEBSOCKETREADYSTATE_CLOSED; | 88 state_ = PP_WEBSOCKETREADYSTATE_CLOSED; |
89 | 89 |
90 // Get the URL. | 90 // Get the URL. |
91 url_ = StringVar::FromPPVar(url); | 91 url_ = StringVar::FromPPVar(url); |
92 if (!url_) | 92 if (!url_.get()) |
93 return PP_ERROR_BADARGUMENT; | 93 return PP_ERROR_BADARGUMENT; |
94 | 94 |
95 // Get the protocols. | 95 // Get the protocols. |
96 std::set<std::string> protocol_set; | 96 std::set<std::string> protocol_set; |
97 std::vector<std::string> protocol_strings; | 97 std::vector<std::string> protocol_strings; |
98 protocol_strings.reserve(protocol_count); | 98 protocol_strings.reserve(protocol_count); |
99 for (uint32_t i = 0; i < protocol_count; ++i) { | 99 for (uint32_t i = 0; i < protocol_count; ++i) { |
100 scoped_refptr<StringVar> protocol(StringVar::FromPPVar(protocols[i])); | 100 scoped_refptr<StringVar> protocol(StringVar::FromPPVar(protocols[i])); |
101 | 101 |
102 // Check invalid and empty entries. | 102 // Check invalid and empty entries. |
103 if (!protocol || !protocol->value().length()) | 103 if (!protocol.get() || !protocol->value().length()) |
104 return PP_ERROR_BADARGUMENT; | 104 return PP_ERROR_BADARGUMENT; |
105 | 105 |
106 // Check duplicated protocol entries. | 106 // Check duplicated protocol entries. |
107 if (protocol_set.find(protocol->value()) != protocol_set.end()) | 107 if (protocol_set.find(protocol->value()) != protocol_set.end()) |
108 return PP_ERROR_BADARGUMENT; | 108 return PP_ERROR_BADARGUMENT; |
109 protocol_set.insert(protocol->value()); | 109 protocol_set.insert(protocol->value()); |
110 | 110 |
111 protocol_strings.push_back(protocol->value()); | 111 protocol_strings.push_back(protocol->value()); |
112 } | 112 } |
113 | 113 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // RFC 6455 limits applications to use reserved connection close code in | 150 // RFC 6455 limits applications to use reserved connection close code in |
151 // section 7.4.2.. The WebSocket API (http://www.w3.org/TR/websockets/) | 151 // section 7.4.2.. The WebSocket API (http://www.w3.org/TR/websockets/) |
152 // defines this out of range error as InvalidAccessError in JavaScript. | 152 // defines this out of range error as InvalidAccessError in JavaScript. |
153 return PP_ERROR_NOACCESS; | 153 return PP_ERROR_NOACCESS; |
154 | 154 |
155 // |reason| must be ignored if it is PP_VARTYPE_UNDEFINED or |code| is | 155 // |reason| must be ignored if it is PP_VARTYPE_UNDEFINED or |code| is |
156 // PP_WEBSOCKETSTATUSCODE_NOT_SPECIFIED. | 156 // PP_WEBSOCKETSTATUSCODE_NOT_SPECIFIED. |
157 if (reason.type != PP_VARTYPE_UNDEFINED) { | 157 if (reason.type != PP_VARTYPE_UNDEFINED) { |
158 // Validate |reason|. | 158 // Validate |reason|. |
159 reason_string_var = StringVar::FromPPVar(reason); | 159 reason_string_var = StringVar::FromPPVar(reason); |
160 if (!reason_string_var || | 160 if (!reason_string_var.get() || |
161 reason_string_var->value().size() > kMaxReasonSizeInBytes) | 161 reason_string_var->value().size() > kMaxReasonSizeInBytes) |
162 return PP_ERROR_BADARGUMENT; | 162 return PP_ERROR_BADARGUMENT; |
163 reason_string = reason_string_var->value(); | 163 reason_string = reason_string_var->value(); |
164 } | 164 } |
165 } | 165 } |
166 | 166 |
167 // Check state. | 167 // Check state. |
168 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSING) | 168 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSING) |
169 return PP_ERROR_INPROGRESS; | 169 return PP_ERROR_INPROGRESS; |
170 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED) | 170 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 if (state_ == PP_WEBSOCKETREADYSTATE_INVALID || | 239 if (state_ == PP_WEBSOCKETREADYSTATE_INVALID || |
240 state_ == PP_WEBSOCKETREADYSTATE_CONNECTING) | 240 state_ == PP_WEBSOCKETREADYSTATE_CONNECTING) |
241 return PP_ERROR_BADARGUMENT; | 241 return PP_ERROR_BADARGUMENT; |
242 | 242 |
243 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSING || | 243 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSING || |
244 state_ == PP_WEBSOCKETREADYSTATE_CLOSED) { | 244 state_ == PP_WEBSOCKETREADYSTATE_CLOSED) { |
245 // Handle buffered_amount_after_close_. | 245 // Handle buffered_amount_after_close_. |
246 uint64_t payload_size = 0; | 246 uint64_t payload_size = 0; |
247 if (message.type == PP_VARTYPE_STRING) { | 247 if (message.type == PP_VARTYPE_STRING) { |
248 scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); | 248 scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); |
249 if (message_string) | 249 if (message_string.get()) |
250 payload_size += message_string->value().length(); | 250 payload_size += message_string->value().length(); |
251 } else if (message.type == PP_VARTYPE_ARRAY_BUFFER) { | 251 } else if (message.type == PP_VARTYPE_ARRAY_BUFFER) { |
252 scoped_refptr<ArrayBufferVar> message_array_buffer = | 252 scoped_refptr<ArrayBufferVar> message_array_buffer = |
253 ArrayBufferVar::FromPPVar(message); | 253 ArrayBufferVar::FromPPVar(message); |
254 if (message_array_buffer) | 254 if (message_array_buffer.get()) |
255 payload_size += message_array_buffer->ByteLength(); | 255 payload_size += message_array_buffer->ByteLength(); |
256 } else { | 256 } else { |
257 // TODO(toyoshim): Support Blob. | 257 // TODO(toyoshim): Support Blob. |
258 return PP_ERROR_NOTSUPPORTED; | 258 return PP_ERROR_NOTSUPPORTED; |
259 } | 259 } |
260 | 260 |
261 buffered_amount_after_close_ = | 261 buffered_amount_after_close_ = |
262 SaturateAdd(buffered_amount_after_close_, GetFrameSize(payload_size)); | 262 SaturateAdd(buffered_amount_after_close_, GetFrameSize(payload_size)); |
263 | 263 |
264 return PP_ERROR_FAILED; | 264 return PP_ERROR_FAILED; |
265 } | 265 } |
266 | 266 |
267 // Send the message. | 267 // Send the message. |
268 if (message.type == PP_VARTYPE_STRING) { | 268 if (message.type == PP_VARTYPE_STRING) { |
269 // Convert message to std::string, then send it. | 269 // Convert message to std::string, then send it. |
270 scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); | 270 scoped_refptr<StringVar> message_string = StringVar::FromPPVar(message); |
271 if (!message_string) | 271 if (!message_string.get()) |
272 return PP_ERROR_BADARGUMENT; | 272 return PP_ERROR_BADARGUMENT; |
273 Post(RENDERER, PpapiHostMsg_WebSocket_SendText(message_string->value())); | 273 Post(RENDERER, PpapiHostMsg_WebSocket_SendText(message_string->value())); |
274 } else if (message.type == PP_VARTYPE_ARRAY_BUFFER) { | 274 } else if (message.type == PP_VARTYPE_ARRAY_BUFFER) { |
275 // Convert message to std::vector<uint8_t>, then send it. | 275 // Convert message to std::vector<uint8_t>, then send it. |
276 scoped_refptr<ArrayBufferVar> message_arraybuffer = | 276 scoped_refptr<ArrayBufferVar> message_arraybuffer = |
277 ArrayBufferVar::FromPPVar(message); | 277 ArrayBufferVar::FromPPVar(message); |
278 if (!message_arraybuffer) | 278 if (!message_arraybuffer.get()) |
279 return PP_ERROR_BADARGUMENT; | 279 return PP_ERROR_BADARGUMENT; |
280 uint8_t* message_data = static_cast<uint8_t*>(message_arraybuffer->Map()); | 280 uint8_t* message_data = static_cast<uint8_t*>(message_arraybuffer->Map()); |
281 uint32 message_length = message_arraybuffer->ByteLength(); | 281 uint32 message_length = message_arraybuffer->ByteLength(); |
282 std::vector<uint8_t> message_vector(message_data, | 282 std::vector<uint8_t> message_vector(message_data, |
283 message_data + message_length); | 283 message_data + message_length); |
284 Post(RENDERER, PpapiHostMsg_WebSocket_SendBinary(message_vector)); | 284 Post(RENDERER, PpapiHostMsg_WebSocket_SendBinary(message_vector)); |
285 } else { | 285 } else { |
286 // TODO(toyoshim): Support Blob. | 286 // TODO(toyoshim): Support Blob. |
287 return PP_ERROR_NOTSUPPORTED; | 287 return PP_ERROR_NOTSUPPORTED; |
288 } | 288 } |
289 return PP_OK; | 289 return PP_OK; |
290 } | 290 } |
291 | 291 |
292 uint64_t WebSocketResource::GetBufferedAmount() { | 292 uint64_t WebSocketResource::GetBufferedAmount() { |
293 return SaturateAdd(buffered_amount_, buffered_amount_after_close_); | 293 return SaturateAdd(buffered_amount_, buffered_amount_after_close_); |
294 } | 294 } |
295 | 295 |
296 uint16_t WebSocketResource::GetCloseCode() { | 296 uint16_t WebSocketResource::GetCloseCode() { |
297 return close_code_; | 297 return close_code_; |
298 } | 298 } |
299 | 299 |
300 PP_Var WebSocketResource::GetCloseReason() { | 300 PP_Var WebSocketResource::GetCloseReason() { |
301 if (!close_reason_) | 301 if (!close_reason_.get()) |
302 return empty_string_->GetPPVar(); | 302 return empty_string_->GetPPVar(); |
303 return close_reason_->GetPPVar(); | 303 return close_reason_->GetPPVar(); |
304 } | 304 } |
305 | 305 |
306 PP_Bool WebSocketResource::GetCloseWasClean() { | 306 PP_Bool WebSocketResource::GetCloseWasClean() { |
307 return close_was_clean_; | 307 return close_was_clean_; |
308 } | 308 } |
309 | 309 |
310 PP_Var WebSocketResource::GetExtensions() { | 310 PP_Var WebSocketResource::GetExtensions() { |
311 return StringVar::StringToPPVar(std::string()); | 311 return StringVar::StringToPPVar(std::string()); |
312 } | 312 } |
313 | 313 |
314 PP_Var WebSocketResource::GetProtocol() { | 314 PP_Var WebSocketResource::GetProtocol() { |
315 if (!protocol_) | 315 if (!protocol_.get()) |
316 return empty_string_->GetPPVar(); | 316 return empty_string_->GetPPVar(); |
317 return protocol_->GetPPVar(); | 317 return protocol_->GetPPVar(); |
318 } | 318 } |
319 | 319 |
320 PP_WebSocketReadyState WebSocketResource::GetReadyState() { | 320 PP_WebSocketReadyState WebSocketResource::GetReadyState() { |
321 return state_; | 321 return state_; |
322 } | 322 } |
323 | 323 |
324 PP_Var WebSocketResource::GetURL() { | 324 PP_Var WebSocketResource::GetURL() { |
325 if (!url_) | 325 if (!url_.get()) |
326 return empty_string_->GetPPVar(); | 326 return empty_string_->GetPPVar(); |
327 return url_->GetPPVar(); | 327 return url_->GetPPVar(); |
328 } | 328 } |
329 | 329 |
330 void WebSocketResource::OnReplyReceived( | 330 void WebSocketResource::OnReplyReceived( |
331 const ResourceMessageReplyParams& params, | 331 const ResourceMessageReplyParams& params, |
332 const IPC::Message& msg) { | 332 const IPC::Message& msg) { |
333 if (params.sequence()) { | 333 if (params.sequence()) { |
334 PluginResource::OnReplyReceived(params, msg); | 334 PluginResource::OnReplyReceived(params, msg); |
335 return; | 335 return; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 return PP_OK; | 474 return PP_OK; |
475 | 475 |
476 *receive_callback_var_ = received_messages_.front()->GetPPVar(); | 476 *receive_callback_var_ = received_messages_.front()->GetPPVar(); |
477 received_messages_.pop(); | 477 received_messages_.pop(); |
478 receive_callback_var_ = NULL; | 478 receive_callback_var_ = NULL; |
479 return PP_OK; | 479 return PP_OK; |
480 } | 480 } |
481 | 481 |
482 } // namespace proxy | 482 } // namespace proxy |
483 } // namespace ppapi | 483 } // namespace ppapi |
OLD | NEW |