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

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

Issue 10909244: PPAPI: Get TrackedCallback ready for running on non-main threads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months 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) 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 "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 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 408
409 PP_Var PPB_WebSocket_Impl::GetURL() { 409 PP_Var PPB_WebSocket_Impl::GetURL() {
410 if (!url_) 410 if (!url_)
411 return empty_string_->GetPPVar(); 411 return empty_string_->GetPPVar();
412 return url_->GetPPVar(); 412 return url_->GetPPVar();
413 } 413 }
414 414
415 void PPB_WebSocket_Impl::didConnect() { 415 void PPB_WebSocket_Impl::didConnect() {
416 DCHECK_EQ(PP_WEBSOCKETREADYSTATE_CONNECTING, state_); 416 DCHECK_EQ(PP_WEBSOCKETREADYSTATE_CONNECTING, state_);
417 state_ = PP_WEBSOCKETREADYSTATE_OPEN; 417 state_ = PP_WEBSOCKETREADYSTATE_OPEN;
418 TrackedCallback::ClearAndRun(&connect_callback_, PP_OK); 418 connect_callback_->Run(PP_OK);
419 } 419 }
420 420
421 void PPB_WebSocket_Impl::didReceiveMessage(const WebString& message) { 421 void PPB_WebSocket_Impl::didReceiveMessage(const WebString& message) {
422 // Dispose packets after receiving an error or in invalid state. 422 // Dispose packets after receiving an error or in invalid state.
423 if (error_was_received_ || !InValidStateToReceive(state_)) 423 if (error_was_received_ || !InValidStateToReceive(state_))
424 return; 424 return;
425 425
426 // Append received data to queue. 426 // Append received data to queue.
427 std::string string = message.utf8(); 427 std::string string = message.utf8();
428 received_messages_.push(scoped_refptr<Var>(new StringVar(string))); 428 received_messages_.push(scoped_refptr<Var>(new StringVar(string)));
429 429
430 if (!wait_for_receive_) 430 if (!wait_for_receive_)
431 return; 431 return;
432 432
433 TrackedCallback::ClearAndRun(&receive_callback_, DoReceive()); 433 receive_callback_->Run(DoReceive());
434 } 434 }
435 435
436 void PPB_WebSocket_Impl::didReceiveArrayBuffer( 436 void PPB_WebSocket_Impl::didReceiveArrayBuffer(
437 const WebArrayBuffer& binaryData) { 437 const WebArrayBuffer& binaryData) {
438 // Dispose packets after receiving an error or in invalid state. 438 // Dispose packets after receiving an error or in invalid state.
439 if (error_was_received_ || !InValidStateToReceive(state_)) 439 if (error_was_received_ || !InValidStateToReceive(state_))
440 return; 440 return;
441 441
442 // Append received data to queue. 442 // Append received data to queue.
443 received_messages_.push( 443 received_messages_.push(
444 scoped_refptr<Var>(new HostArrayBufferVar(binaryData))); 444 scoped_refptr<Var>(new HostArrayBufferVar(binaryData)));
445 445
446 if (!wait_for_receive_) 446 if (!wait_for_receive_)
447 return; 447 return;
448 448
449 TrackedCallback::ClearAndRun(&receive_callback_, DoReceive()); 449 receive_callback_->Run(DoReceive());
450 } 450 }
451 451
452 void PPB_WebSocket_Impl::didReceiveMessageError() { 452 void PPB_WebSocket_Impl::didReceiveMessageError() {
453 // Ignore error notification in invalid state. 453 // Ignore error notification in invalid state.
454 if (!InValidStateToReceive(state_)) 454 if (!InValidStateToReceive(state_))
455 return; 455 return;
456 456
457 // Records the error, then stops receiving any frames after this error. 457 // Records the error, then stops receiving any frames after this error.
458 // The error will be notified after all queued messages are read via 458 // The error will be notified after all queued messages are read via
459 // ReceiveMessage(). 459 // ReceiveMessage().
460 error_was_received_ = true; 460 error_was_received_ = true;
461 if (!wait_for_receive_) 461 if (!wait_for_receive_)
462 return; 462 return;
463 463
464 // But, if no messages are queued and ReceiveMessage() is now on going. 464 // But, if no messages are queued and ReceiveMessage() is now on going.
465 // We must invoke the callback with error code here. 465 // We must invoke the callback with error code here.
466 wait_for_receive_ = false; 466 wait_for_receive_ = false;
467 receive_callback_var_ = NULL; 467 receive_callback_var_ = NULL;
468 TrackedCallback::ClearAndRun(&receive_callback_, PP_ERROR_FAILED); 468 receive_callback_->Run(PP_ERROR_FAILED);
469 } 469 }
470 470
471 void PPB_WebSocket_Impl::didUpdateBufferedAmount( 471 void PPB_WebSocket_Impl::didUpdateBufferedAmount(
472 unsigned long buffered_amount) { 472 unsigned long buffered_amount) {
473 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED) 473 if (state_ == PP_WEBSOCKETREADYSTATE_CLOSED)
474 return; 474 return;
475 buffered_amount_ = buffered_amount; 475 buffered_amount_ = buffered_amount;
476 } 476 }
477 477
478 void PPB_WebSocket_Impl::didStartClosingHandshake() { 478 void PPB_WebSocket_Impl::didStartClosingHandshake() {
(...skipping 21 matching lines...) Expand all
500 // Handle state transition and invoking callback. 500 // Handle state transition and invoking callback.
501 DCHECK_NE(PP_WEBSOCKETREADYSTATE_CLOSED, state_); 501 DCHECK_NE(PP_WEBSOCKETREADYSTATE_CLOSED, state_);
502 PP_WebSocketReadyState state = state_; 502 PP_WebSocketReadyState state = state_;
503 state_ = PP_WEBSOCKETREADYSTATE_CLOSED; 503 state_ = PP_WEBSOCKETREADYSTATE_CLOSED;
504 504
505 // User handlers may release WebSocket PP_Resource in the following 505 // User handlers may release WebSocket PP_Resource in the following
506 // completion callbacks. Retain |this| here to assure that this object 506 // completion callbacks. Retain |this| here to assure that this object
507 // keep on being valid in this function. 507 // keep on being valid in this function.
508 scoped_refptr<PPB_WebSocket_Impl> retain_this(this); 508 scoped_refptr<PPB_WebSocket_Impl> retain_this(this);
509 if (state == PP_WEBSOCKETREADYSTATE_CONNECTING) 509 if (state == PP_WEBSOCKETREADYSTATE_CONNECTING)
510 TrackedCallback::ClearAndRun(&connect_callback_, PP_ERROR_FAILED); 510 connect_callback_->Run(PP_ERROR_FAILED);
511 511
512 if (wait_for_receive_) { 512 if (wait_for_receive_) {
513 wait_for_receive_ = false; 513 wait_for_receive_ = false;
514 receive_callback_var_ = NULL; 514 receive_callback_var_ = NULL;
515 TrackedCallback::ClearAndRun(&receive_callback_, PP_ERROR_FAILED); 515 receive_callback_->Run(PP_ERROR_FAILED);
516 } 516 }
517 517
518 if ((state == PP_WEBSOCKETREADYSTATE_CLOSING) && close_callback_.get()) 518 if ((state == PP_WEBSOCKETREADYSTATE_CLOSING) && close_callback_.get())
519 TrackedCallback::ClearAndRun(&close_callback_, PP_OK); 519 close_callback_->Run(PP_OK);
520 520
521 // Disconnect. 521 // Disconnect.
522 if (websocket_.get()) 522 if (websocket_.get())
523 websocket_->disconnect(); 523 websocket_->disconnect();
524 } 524 }
525 525
526 int32_t PPB_WebSocket_Impl::DoReceive() { 526 int32_t PPB_WebSocket_Impl::DoReceive() {
527 if (!receive_callback_var_) 527 if (!receive_callback_var_)
528 return PP_OK; 528 return PP_OK;
529 529
530 *receive_callback_var_ = received_messages_.front()->GetPPVar(); 530 *receive_callback_var_ = received_messages_.front()->GetPPVar();
531 received_messages_.pop(); 531 received_messages_.pop();
532 receive_callback_var_ = NULL; 532 receive_callback_var_ = NULL;
533 wait_for_receive_ = false; 533 wait_for_receive_ = false;
534 return PP_OK; 534 return PP_OK;
535 } 535 }
536 536
537 } // namespace ppapi 537 } // namespace ppapi
538 } // namespace webkit 538 } // namespace webkit
OLDNEW
« ppapi/shared_impl/tracked_callback.cc ('K') | « webkit/plugins/ppapi/ppb_url_loader_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698