| 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 #include "webkit/plugins/ppapi/ppb_websocket_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_websocket_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 PPB_WebSocket_Impl::PPB_WebSocket_Impl(PP_Instance instance) | 77 PPB_WebSocket_Impl::PPB_WebSocket_Impl(PP_Instance instance) |
| 78 : Resource(instance), | 78 : Resource(instance), |
| 79 state_(PP_WEBSOCKETREADYSTATE_INVALID_DEV), | 79 state_(PP_WEBSOCKETREADYSTATE_INVALID_DEV), |
| 80 error_was_received_(false), | 80 error_was_received_(false), |
| 81 receive_callback_var_(NULL), | 81 receive_callback_var_(NULL), |
| 82 wait_for_receive_(false), | 82 wait_for_receive_(false), |
| 83 close_code_(0), | 83 close_code_(0), |
| 84 close_was_clean_(PP_FALSE), | 84 close_was_clean_(PP_FALSE), |
| 85 buffered_amount_(0), | 85 buffered_amount_(0), |
| 86 buffered_amount_after_close_(0) { | 86 buffered_amount_after_close_(0) { |
| 87 empty_string_ = new StringVar( | 87 empty_string_ = new StringVar("", 0); |
| 88 PpapiGlobals::Get()->GetModuleForInstance(instance), "", 0); | |
| 89 } | 88 } |
| 90 | 89 |
| 91 PPB_WebSocket_Impl::~PPB_WebSocket_Impl() { | 90 PPB_WebSocket_Impl::~PPB_WebSocket_Impl() { |
| 92 if (websocket_.get()) | 91 if (websocket_.get()) |
| 93 websocket_->disconnect(); | 92 websocket_->disconnect(); |
| 94 | 93 |
| 95 // Clean up received and unread messages | 94 // Clean up received and unread messages |
| 96 VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker(); | 95 VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker(); |
| 97 while (!received_messages_.empty()) { | 96 while (!received_messages_.empty()) { |
| 98 PP_Var var = received_messages_.front(); | 97 PP_Var var = received_messages_.front(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 126 return PP_ERROR_INPROGRESS; | 125 return PP_ERROR_INPROGRESS; |
| 127 if (state_ != PP_WEBSOCKETREADYSTATE_INVALID_DEV) | 126 if (state_ != PP_WEBSOCKETREADYSTATE_INVALID_DEV) |
| 128 return PP_ERROR_INPROGRESS; | 127 return PP_ERROR_INPROGRESS; |
| 129 state_ = PP_WEBSOCKETREADYSTATE_CLOSED_DEV; | 128 state_ = PP_WEBSOCKETREADYSTATE_CLOSED_DEV; |
| 130 | 129 |
| 131 // Validate url and convert it to WebURL. | 130 // Validate url and convert it to WebURL. |
| 132 scoped_refptr<StringVar> url_string = StringVar::FromPPVar(url); | 131 scoped_refptr<StringVar> url_string = StringVar::FromPPVar(url); |
| 133 if (!url_string) | 132 if (!url_string) |
| 134 return PP_ERROR_BADARGUMENT; | 133 return PP_ERROR_BADARGUMENT; |
| 135 GURL gurl(url_string->value()); | 134 GURL gurl(url_string->value()); |
| 136 url_ = new StringVar( | 135 url_ = new StringVar(gurl.spec()); |
| 137 PpapiGlobals::Get()->GetModuleForInstance(pp_instance()), gurl.spec()); | |
| 138 if (!gurl.is_valid()) | 136 if (!gurl.is_valid()) |
| 139 return PP_ERROR_BADARGUMENT; | 137 return PP_ERROR_BADARGUMENT; |
| 140 if (!gurl.SchemeIs("ws") && !gurl.SchemeIs("wss")) | 138 if (!gurl.SchemeIs("ws") && !gurl.SchemeIs("wss")) |
| 141 return PP_ERROR_BADARGUMENT; | 139 return PP_ERROR_BADARGUMENT; |
| 142 if (gurl.has_ref()) | 140 if (gurl.has_ref()) |
| 143 return PP_ERROR_BADARGUMENT; | 141 return PP_ERROR_BADARGUMENT; |
| 144 if (!net::IsPortAllowedByDefault(gurl.IntPort())) | 142 if (!net::IsPortAllowedByDefault(gurl.IntPort())) |
| 145 return PP_ERROR_BADARGUMENT; | 143 return PP_ERROR_BADARGUMENT; |
| 146 WebURL web_url(gurl); | 144 WebURL web_url(gurl); |
| 147 | 145 |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 return empty_string_->GetPPVar(); | 359 return empty_string_->GetPPVar(); |
| 362 return extensions_->GetPPVar(); | 360 return extensions_->GetPPVar(); |
| 363 } | 361 } |
| 364 | 362 |
| 365 PP_Var PPB_WebSocket_Impl::GetProtocol() { | 363 PP_Var PPB_WebSocket_Impl::GetProtocol() { |
| 366 // Check mandatory interfaces. | 364 // Check mandatory interfaces. |
| 367 if (!websocket_.get()) | 365 if (!websocket_.get()) |
| 368 return empty_string_->GetPPVar(); | 366 return empty_string_->GetPPVar(); |
| 369 | 367 |
| 370 std::string protocol = websocket_->subprotocol().utf8(); | 368 std::string protocol = websocket_->subprotocol().utf8(); |
| 371 return StringVar::StringToPPVar( | 369 return StringVar::StringToPPVar(protocol); |
| 372 PpapiGlobals::Get()->GetModuleForInstance(pp_instance()), protocol); | |
| 373 } | 370 } |
| 374 | 371 |
| 375 PP_WebSocketReadyState_Dev PPB_WebSocket_Impl::GetReadyState() { | 372 PP_WebSocketReadyState_Dev PPB_WebSocket_Impl::GetReadyState() { |
| 376 return state_; | 373 return state_; |
| 377 } | 374 } |
| 378 | 375 |
| 379 PP_Var PPB_WebSocket_Impl::GetURL() { | 376 PP_Var PPB_WebSocket_Impl::GetURL() { |
| 380 if (!url_) | 377 if (!url_) |
| 381 return empty_string_->GetPPVar(); | 378 return empty_string_->GetPPVar(); |
| 382 return url_->GetPPVar(); | 379 return url_->GetPPVar(); |
| 383 } | 380 } |
| 384 | 381 |
| 385 void PPB_WebSocket_Impl::didConnect() { | 382 void PPB_WebSocket_Impl::didConnect() { |
| 386 DCHECK_EQ(PP_WEBSOCKETREADYSTATE_CONNECTING_DEV, state_); | 383 DCHECK_EQ(PP_WEBSOCKETREADYSTATE_CONNECTING_DEV, state_); |
| 387 state_ = PP_WEBSOCKETREADYSTATE_OPEN_DEV; | 384 state_ = PP_WEBSOCKETREADYSTATE_OPEN_DEV; |
| 388 PP_RunAndClearCompletionCallback(&connect_callback_, PP_OK); | 385 PP_RunAndClearCompletionCallback(&connect_callback_, PP_OK); |
| 389 } | 386 } |
| 390 | 387 |
| 391 void PPB_WebSocket_Impl::didReceiveMessage(const WebString& message) { | 388 void PPB_WebSocket_Impl::didReceiveMessage(const WebString& message) { |
| 392 // Dispose packets after receiving an error or in invalid state. | 389 // Dispose packets after receiving an error or in invalid state. |
| 393 if (error_was_received_ || !InValidStateToReceive(state_)) | 390 if (error_was_received_ || !InValidStateToReceive(state_)) |
| 394 return; | 391 return; |
| 395 | 392 |
| 396 // Append received data to queue. | 393 // Append received data to queue. |
| 397 std::string string = message.utf8(); | 394 std::string string = message.utf8(); |
| 398 PP_Var var = StringVar::StringToPPVar( | 395 PP_Var var = StringVar::StringToPPVar(string); |
| 399 PpapiGlobals::Get()->GetModuleForInstance(pp_instance()), string); | |
| 400 received_messages_.push(var); | 396 received_messages_.push(var); |
| 401 | 397 |
| 402 if (!wait_for_receive_) | 398 if (!wait_for_receive_) |
| 403 return; | 399 return; |
| 404 | 400 |
| 405 PP_RunAndClearCompletionCallback(&receive_callback_, DoReceive()); | 401 PP_RunAndClearCompletionCallback(&receive_callback_, DoReceive()); |
| 406 } | 402 } |
| 407 | 403 |
| 408 void PPB_WebSocket_Impl::didReceiveBinaryData(const WebData& binaryData) { | 404 void PPB_WebSocket_Impl::didReceiveBinaryData(const WebData& binaryData) { |
| 409 // Dispose packets after receiving an error or in invalid state. | 405 // Dispose packets after receiving an error or in invalid state. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; | 440 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; |
| 445 } | 441 } |
| 446 | 442 |
| 447 void PPB_WebSocket_Impl::didClose(unsigned long unhandled_buffered_amount, | 443 void PPB_WebSocket_Impl::didClose(unsigned long unhandled_buffered_amount, |
| 448 ClosingHandshakeCompletionStatus status, | 444 ClosingHandshakeCompletionStatus status, |
| 449 unsigned short code, | 445 unsigned short code, |
| 450 const WebString& reason) { | 446 const WebString& reason) { |
| 451 // Store code and reason. | 447 // Store code and reason. |
| 452 close_code_ = code; | 448 close_code_ = code; |
| 453 std::string reason_string = reason.utf8(); | 449 std::string reason_string = reason.utf8(); |
| 454 close_reason_ = new StringVar( | 450 close_reason_ = new StringVar(reason_string); |
| 455 PpapiGlobals::Get()->GetModuleForInstance(pp_instance()), reason_string); | |
| 456 | 451 |
| 457 // Set close_was_clean_. | 452 // Set close_was_clean_. |
| 458 bool was_clean = | 453 bool was_clean = |
| 459 state_ == PP_WEBSOCKETREADYSTATE_CLOSING_DEV && | 454 state_ == PP_WEBSOCKETREADYSTATE_CLOSING_DEV && |
| 460 !unhandled_buffered_amount && | 455 !unhandled_buffered_amount && |
| 461 status == WebSocketClient::ClosingHandshakeComplete; | 456 status == WebSocketClient::ClosingHandshakeComplete; |
| 462 close_was_clean_ = was_clean ? PP_TRUE : PP_FALSE; | 457 close_was_clean_ = was_clean ? PP_TRUE : PP_FALSE; |
| 463 | 458 |
| 464 // Update buffered_amount_. | 459 // Update buffered_amount_. |
| 465 buffered_amount_ = unhandled_buffered_amount; | 460 buffered_amount_ = unhandled_buffered_amount; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 492 | 487 |
| 493 *receive_callback_var_ = received_messages_.front(); | 488 *receive_callback_var_ = received_messages_.front(); |
| 494 received_messages_.pop(); | 489 received_messages_.pop(); |
| 495 receive_callback_var_ = NULL; | 490 receive_callback_var_ = NULL; |
| 496 wait_for_receive_ = false; | 491 wait_for_receive_ = false; |
| 497 return PP_OK; | 492 return PP_OK; |
| 498 } | 493 } |
| 499 | 494 |
| 500 } // namespace ppapi | 495 } // namespace ppapi |
| 501 } // namespace webkit | 496 } // namespace webkit |
| OLD | NEW |