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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 return empty_string_->GetPPVar(); | 344 return empty_string_->GetPPVar(); |
347 return extensions_->GetPPVar(); | 345 return extensions_->GetPPVar(); |
348 } | 346 } |
349 | 347 |
350 PP_Var PPB_WebSocket_Impl::GetProtocol() { | 348 PP_Var PPB_WebSocket_Impl::GetProtocol() { |
351 // Check mandatory interfaces. | 349 // Check mandatory interfaces. |
352 if (!websocket_.get()) | 350 if (!websocket_.get()) |
353 return empty_string_->GetPPVar(); | 351 return empty_string_->GetPPVar(); |
354 | 352 |
355 std::string protocol = websocket_->subprotocol().utf8(); | 353 std::string protocol = websocket_->subprotocol().utf8(); |
356 return StringVar::StringToPPVar( | 354 return StringVar::StringToPPVar(protocol); |
357 PpapiGlobals::Get()->GetModuleForInstance(pp_instance()), protocol); | |
358 } | 355 } |
359 | 356 |
360 PP_WebSocketReadyState_Dev PPB_WebSocket_Impl::GetReadyState() { | 357 PP_WebSocketReadyState_Dev PPB_WebSocket_Impl::GetReadyState() { |
361 return state_; | 358 return state_; |
362 } | 359 } |
363 | 360 |
364 PP_Var PPB_WebSocket_Impl::GetURL() { | 361 PP_Var PPB_WebSocket_Impl::GetURL() { |
365 if (!url_) | 362 if (!url_) |
366 return empty_string_->GetPPVar(); | 363 return empty_string_->GetPPVar(); |
367 return url_->GetPPVar(); | 364 return url_->GetPPVar(); |
368 } | 365 } |
369 | 366 |
370 void PPB_WebSocket_Impl::didConnect() { | 367 void PPB_WebSocket_Impl::didConnect() { |
371 DCHECK_EQ(PP_WEBSOCKETREADYSTATE_CONNECTING_DEV, state_); | 368 DCHECK_EQ(PP_WEBSOCKETREADYSTATE_CONNECTING_DEV, state_); |
372 state_ = PP_WEBSOCKETREADYSTATE_OPEN_DEV; | 369 state_ = PP_WEBSOCKETREADYSTATE_OPEN_DEV; |
373 PP_RunAndClearCompletionCallback(&connect_callback_, PP_OK); | 370 PP_RunAndClearCompletionCallback(&connect_callback_, PP_OK); |
374 } | 371 } |
375 | 372 |
376 void PPB_WebSocket_Impl::didReceiveMessage(const WebString& message) { | 373 void PPB_WebSocket_Impl::didReceiveMessage(const WebString& message) { |
377 // Dispose packets after receiving an error or in invalid state. | 374 // Dispose packets after receiving an error or in invalid state. |
378 if (error_was_received_ || !InValidStateToReceive(state_)) | 375 if (error_was_received_ || !InValidStateToReceive(state_)) |
379 return; | 376 return; |
380 | 377 |
381 // Append received data to queue. | 378 // Append received data to queue. |
382 std::string string = message.utf8(); | 379 std::string string = message.utf8(); |
383 PP_Var var = StringVar::StringToPPVar( | 380 PP_Var var = StringVar::StringToPPVar(string); |
384 PpapiGlobals::Get()->GetModuleForInstance(pp_instance()), string); | |
385 received_messages_.push(var); | 381 received_messages_.push(var); |
386 | 382 |
387 if (!wait_for_receive_) | 383 if (!wait_for_receive_) |
388 return; | 384 return; |
389 | 385 |
390 PP_RunAndClearCompletionCallback(&receive_callback_, DoReceive()); | 386 PP_RunAndClearCompletionCallback(&receive_callback_, DoReceive()); |
391 } | 387 } |
392 | 388 |
393 void PPB_WebSocket_Impl::didReceiveBinaryData(const WebData& binaryData) { | 389 void PPB_WebSocket_Impl::didReceiveBinaryData(const WebData& binaryData) { |
394 // Dispose packets after receiving an error or in invalid state. | 390 // Dispose packets after receiving an error or in invalid state. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; | 424 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; |
429 } | 425 } |
430 | 426 |
431 void PPB_WebSocket_Impl::didClose(unsigned long unhandled_buffered_amount, | 427 void PPB_WebSocket_Impl::didClose(unsigned long unhandled_buffered_amount, |
432 ClosingHandshakeCompletionStatus status, | 428 ClosingHandshakeCompletionStatus status, |
433 unsigned short code, | 429 unsigned short code, |
434 const WebString& reason) { | 430 const WebString& reason) { |
435 // Store code and reason. | 431 // Store code and reason. |
436 close_code_ = code; | 432 close_code_ = code; |
437 std::string reason_string = reason.utf8(); | 433 std::string reason_string = reason.utf8(); |
438 close_reason_ = new StringVar( | 434 close_reason_ = new StringVar(reason_string); |
439 PpapiGlobals::Get()->GetModuleForInstance(pp_instance()), reason_string); | |
440 | 435 |
441 // Set close_was_clean_. | 436 // Set close_was_clean_. |
442 bool was_clean = | 437 bool was_clean = |
443 state_ == PP_WEBSOCKETREADYSTATE_CLOSING_DEV && | 438 state_ == PP_WEBSOCKETREADYSTATE_CLOSING_DEV && |
444 !unhandled_buffered_amount && | 439 !unhandled_buffered_amount && |
445 status == WebSocketClient::ClosingHandshakeComplete; | 440 status == WebSocketClient::ClosingHandshakeComplete; |
446 close_was_clean_ = was_clean ? PP_TRUE : PP_FALSE; | 441 close_was_clean_ = was_clean ? PP_TRUE : PP_FALSE; |
447 | 442 |
448 // Update buffered_amount_. | 443 // Update buffered_amount_. |
449 buffered_amount_ = unhandled_buffered_amount; | 444 buffered_amount_ = unhandled_buffered_amount; |
(...skipping 20 matching lines...) Expand all Loading... |
470 | 465 |
471 *receive_callback_var_ = received_messages_.front(); | 466 *receive_callback_var_ = received_messages_.front(); |
472 received_messages_.pop(); | 467 received_messages_.pop(); |
473 receive_callback_var_ = NULL; | 468 receive_callback_var_ = NULL; |
474 wait_for_receive_ = false; | 469 wait_for_receive_ = false; |
475 return PP_OK; | 470 return PP_OK; |
476 } | 471 } |
477 | 472 |
478 } // namespace ppapi | 473 } // namespace ppapi |
479 } // namespace webkit | 474 } // namespace webkit |
OLD | NEW |