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 <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 | 72 |
73 } // namespace | 73 } // namespace |
74 | 74 |
75 namespace webkit { | 75 namespace webkit { |
76 namespace ppapi { | 76 namespace ppapi { |
77 | 77 |
78 PPB_WebSocket_Impl::PPB_WebSocket_Impl(PP_Instance instance) | 78 PPB_WebSocket_Impl::PPB_WebSocket_Impl(PP_Instance instance) |
79 : Resource(instance), | 79 : Resource(instance), |
80 state_(PP_WEBSOCKETREADYSTATE_INVALID_DEV), | 80 state_(PP_WEBSOCKETREADYSTATE_INVALID_DEV), |
81 error_was_received_(false), | 81 error_was_received_(false), |
82 connect_callback_(PP_BlockUntilComplete()), | |
83 receive_callback_(PP_BlockUntilComplete()), | |
82 receive_callback_var_(NULL), | 84 receive_callback_var_(NULL), |
83 wait_for_receive_(false), | 85 wait_for_receive_(false), |
86 close_callback_(PP_BlockUntilComplete()), | |
84 close_code_(0), | 87 close_code_(0), |
85 close_was_clean_(PP_FALSE), | 88 close_was_clean_(PP_FALSE), |
86 buffered_amount_(0), | 89 buffered_amount_(0), |
87 buffered_amount_after_close_(0) { | 90 buffered_amount_after_close_(0) { |
Takashi Toyoshima
2011/12/22 07:56:29
Replace initializers for callbacks by my ongoing c
| |
88 connect_callback_.func = NULL; | |
89 connect_callback_.user_data = NULL; | |
90 receive_callback_.func = NULL; | |
91 receive_callback_.user_data = NULL; | |
92 close_callback_.func = NULL; | |
93 close_callback_.user_data = NULL; | |
94 empty_string_ = new StringVar("", 0); | 91 empty_string_ = new StringVar("", 0); |
95 } | 92 } |
96 | 93 |
97 PPB_WebSocket_Impl::~PPB_WebSocket_Impl() { | 94 PPB_WebSocket_Impl::~PPB_WebSocket_Impl() { |
95 // Abort outstanding processings having a callback. | |
96 if (connect_callback_.func) | |
97 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_ABORTED); | |
98 AbortOutstandingReceive(); | |
99 if (close_callback_.func) | |
100 PP_RunAndClearCompletionCallback(&close_callback_, PP_ERROR_ABORTED); | |
101 | |
98 if (websocket_.get()) | 102 if (websocket_.get()) |
99 websocket_->disconnect(); | 103 websocket_->disconnect(); |
100 | 104 |
101 // Clean up received and unread messages | 105 // Clean up received and unread messages |
102 VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker(); | 106 VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker(); |
103 while (!received_messages_.empty()) { | 107 while (!received_messages_.empty()) { |
104 PP_Var var = received_messages_.front(); | 108 PP_Var var = received_messages_.front(); |
105 received_messages_.pop(); | 109 received_messages_.pop(); |
106 var_tracker->ReleaseVar(var); | 110 var_tracker->ReleaseVar(var); |
107 } | 111 } |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 // Abort ongoing connect. | 259 // Abort ongoing connect. |
256 if (state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) { | 260 if (state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) { |
257 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; | 261 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; |
258 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_ABORTED); | 262 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_ABORTED); |
259 websocket_->fail( | 263 websocket_->fail( |
260 "WebSocket was closed before the connection was established."); | 264 "WebSocket was closed before the connection was established."); |
261 return PP_OK_COMPLETIONPENDING; | 265 return PP_OK_COMPLETIONPENDING; |
262 } | 266 } |
263 | 267 |
264 // Abort ongoing receive. | 268 // Abort ongoing receive. |
265 if (wait_for_receive_) { | 269 AbortOutstandingReceive(); |
266 wait_for_receive_ = false; | |
267 receive_callback_var_ = NULL; | |
268 PP_RunAndClearCompletionCallback(&receive_callback_, PP_ERROR_ABORTED); | |
269 } | |
270 | 270 |
271 // Close connection. | 271 // Close connection. |
272 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; | 272 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; |
273 WebString web_reason = WebString::fromUTF8(reason_string->value()); | 273 WebString web_reason = WebString::fromUTF8(reason_string->value()); |
274 websocket_->close(code, web_reason); | 274 websocket_->close(code, web_reason); |
275 | 275 |
276 return PP_OK_COMPLETIONPENDING; | 276 return PP_OK_COMPLETIONPENDING; |
277 } | 277 } |
278 | 278 |
279 int32_t PPB_WebSocket_Impl::ReceiveMessage(PP_Var* message, | 279 int32_t PPB_WebSocket_Impl::ReceiveMessage(PP_Var* message, |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 buffered_amount_ = unhandled_buffered_amount; | 477 buffered_amount_ = unhandled_buffered_amount; |
478 | 478 |
479 // Handle state transition and invoking callback. | 479 // Handle state transition and invoking callback. |
480 DCHECK_NE(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, state_); | 480 DCHECK_NE(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, state_); |
481 PP_WebSocketReadyState_Dev state = state_; | 481 PP_WebSocketReadyState_Dev state = state_; |
482 state_ = PP_WEBSOCKETREADYSTATE_CLOSED_DEV; | 482 state_ = PP_WEBSOCKETREADYSTATE_CLOSED_DEV; |
483 | 483 |
484 if (state == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) | 484 if (state == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) |
485 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_FAILED); | 485 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_FAILED); |
486 | 486 |
487 if (wait_for_receive_) { | 487 AbortOutstandingReceive(); |
488 wait_for_receive_ = false; | |
489 receive_callback_var_ = NULL; | |
490 PP_RunAndClearCompletionCallback(&receive_callback_, PP_ERROR_ABORTED); | |
491 } | |
492 | 488 |
493 if (state == PP_WEBSOCKETREADYSTATE_CLOSING_DEV) | 489 if (state == PP_WEBSOCKETREADYSTATE_CLOSING_DEV) |
494 PP_RunAndClearCompletionCallback(&close_callback_, PP_OK); | 490 PP_RunAndClearCompletionCallback(&close_callback_, PP_OK); |
495 | 491 |
496 // Disconnect. | 492 // Disconnect. |
497 if (websocket_.get()) | 493 if (websocket_.get()) |
498 websocket_->disconnect(); | 494 websocket_->disconnect(); |
499 } | 495 } |
500 | 496 |
501 int32_t PPB_WebSocket_Impl::DoReceive() { | 497 int32_t PPB_WebSocket_Impl::DoReceive() { |
502 if (!receive_callback_var_) | 498 if (!receive_callback_var_) |
503 return PP_OK; | 499 return PP_OK; |
504 | 500 |
505 *receive_callback_var_ = received_messages_.front(); | 501 *receive_callback_var_ = received_messages_.front(); |
506 received_messages_.pop(); | 502 received_messages_.pop(); |
507 receive_callback_var_ = NULL; | 503 receive_callback_var_ = NULL; |
508 wait_for_receive_ = false; | 504 wait_for_receive_ = false; |
509 return PP_OK; | 505 return PP_OK; |
510 } | 506 } |
511 | 507 |
508 void PPB_WebSocket_Impl::AbortOutstandingReceive() { | |
509 if (!wait_for_receive_) | |
510 return; | |
511 wait_for_receive_ = false; | |
512 receive_callback_var_ = NULL; | |
513 PP_RunAndClearCompletionCallback(&receive_callback_, PP_ERROR_ABORTED); | |
514 } | |
515 | |
512 } // namespace ppapi | 516 } // namespace ppapi |
513 } // namespace webkit | 517 } // namespace webkit |
OLD | NEW |