Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: webkit/plugins/ppapi/ppb_websocket_impl.cc

Issue 8956008: WebSocket Pepper API: C++ utility class implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix completion callback leaks? Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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) {
88 empty_string_ = new StringVar("", 0); 91 empty_string_ = new StringVar("", 0);
89 } 92 }
90 93
91 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
92 if (websocket_.get()) 102 if (websocket_.get())
93 websocket_->disconnect(); 103 websocket_->disconnect();
94 104
95 // Clean up received and unread messages 105 // Clean up received and unread messages
96 VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker(); 106 VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker();
97 while (!received_messages_.empty()) { 107 while (!received_messages_.empty()) {
98 PP_Var var = received_messages_.front(); 108 PP_Var var = received_messages_.front();
99 received_messages_.pop(); 109 received_messages_.pop();
100 var_tracker->ReleaseVar(var); 110 var_tracker->ReleaseVar(var);
101 } 111 }
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 // Abort ongoing connect. 259 // Abort ongoing connect.
250 if (state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) { 260 if (state_ == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) {
251 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; 261 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV;
252 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_ABORTED); 262 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_ABORTED);
253 websocket_->fail( 263 websocket_->fail(
254 "WebSocket was closed before the connection was established."); 264 "WebSocket was closed before the connection was established.");
255 return PP_OK_COMPLETIONPENDING; 265 return PP_OK_COMPLETIONPENDING;
256 } 266 }
257 267
258 // Abort ongoing receive. 268 // Abort ongoing receive.
259 if (wait_for_receive_) { 269 AbortOutstandingReceive();
260 wait_for_receive_ = false;
261 receive_callback_var_ = NULL;
262 PP_RunAndClearCompletionCallback(&receive_callback_, PP_ERROR_ABORTED);
263 }
264 270
265 // Close connection. 271 // Close connection.
266 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV; 272 state_ = PP_WEBSOCKETREADYSTATE_CLOSING_DEV;
267 WebString web_reason = WebString::fromUTF8(reason_string->value()); 273 WebString web_reason = WebString::fromUTF8(reason_string->value());
268 websocket_->close(code, web_reason); 274 websocket_->close(code, web_reason);
269 275
270 return PP_OK_COMPLETIONPENDING; 276 return PP_OK_COMPLETIONPENDING;
271 } 277 }
272 278
273 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
471 buffered_amount_ = unhandled_buffered_amount; 477 buffered_amount_ = unhandled_buffered_amount;
472 478
473 // Handle state transition and invoking callback. 479 // Handle state transition and invoking callback.
474 DCHECK_NE(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, state_); 480 DCHECK_NE(PP_WEBSOCKETREADYSTATE_CLOSED_DEV, state_);
475 PP_WebSocketReadyState_Dev state = state_; 481 PP_WebSocketReadyState_Dev state = state_;
476 state_ = PP_WEBSOCKETREADYSTATE_CLOSED_DEV; 482 state_ = PP_WEBSOCKETREADYSTATE_CLOSED_DEV;
477 483
478 if (state == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV) 484 if (state == PP_WEBSOCKETREADYSTATE_CONNECTING_DEV)
479 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_FAILED); 485 PP_RunAndClearCompletionCallback(&connect_callback_, PP_ERROR_FAILED);
480 486
481 if (wait_for_receive_) { 487 AbortOutstandingReceive();
482 wait_for_receive_ = false;
483 receive_callback_var_ = NULL;
484 PP_RunAndClearCompletionCallback(&receive_callback_, PP_ERROR_ABORTED);
485 }
486 488
487 if (state == PP_WEBSOCKETREADYSTATE_CLOSING_DEV) 489 if (state == PP_WEBSOCKETREADYSTATE_CLOSING_DEV)
488 PP_RunAndClearCompletionCallback(&close_callback_, PP_OK); 490 PP_RunAndClearCompletionCallback(&close_callback_, PP_OK);
489 491
490 // Disconnect. 492 // Disconnect.
491 if (websocket_.get()) 493 if (websocket_.get())
492 websocket_->disconnect(); 494 websocket_->disconnect();
493 } 495 }
494 496
495 int32_t PPB_WebSocket_Impl::DoReceive() { 497 int32_t PPB_WebSocket_Impl::DoReceive() {
496 if (!receive_callback_var_) 498 if (!receive_callback_var_)
497 return PP_OK; 499 return PP_OK;
498 500
499 *receive_callback_var_ = received_messages_.front(); 501 *receive_callback_var_ = received_messages_.front();
500 received_messages_.pop(); 502 received_messages_.pop();
501 receive_callback_var_ = NULL; 503 receive_callback_var_ = NULL;
502 wait_for_receive_ = false; 504 wait_for_receive_ = false;
503 return PP_OK; 505 return PP_OK;
504 } 506 }
505 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
506 } // namespace ppapi 516 } // namespace ppapi
507 } // namespace webkit 517 } // namespace webkit
OLDNEW
« ppapi/cpp/helper/dev/websocket_api_dev.cc ('K') | « webkit/plugins/ppapi/ppb_websocket_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698