OLD | NEW |
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 "chrome/browser/chromeos/web_socket_proxy.h" | 5 #include "chrome/browser/chromeos/web_socket_proxy.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 PHASE_CONNECTING, | 454 PHASE_CONNECTING, |
455 PHASE_RUNNING, | 455 PHASE_RUNNING, |
456 PHASE_CLOSING, | 456 PHASE_CLOSING, |
457 PHASE_CLOSED | 457 PHASE_CLOSED |
458 }; | 458 }; |
459 | 459 |
460 class DerivedIOBufferWithSize : public net::IOBufferWithSize { | 460 class DerivedIOBufferWithSize : public net::IOBufferWithSize { |
461 public: | 461 public: |
462 DerivedIOBufferWithSize(net::IOBuffer* host, int size) | 462 DerivedIOBufferWithSize(net::IOBuffer* host, int size) |
463 : IOBufferWithSize(host->data(), size), host_(host) { | 463 : IOBufferWithSize(host->data(), size), host_(host) { |
464 DCHECK(host_); | 464 DCHECK(host_.get()); |
465 DCHECK(host_->data()); | 465 DCHECK(host_->data()); |
466 } | 466 } |
467 | 467 |
468 protected: | 468 protected: |
469 virtual ~DerivedIOBufferWithSize() { | 469 virtual ~DerivedIOBufferWithSize() { |
470 data_ = NULL; // We do not own memory, bypass base class destructor. | 470 data_ = NULL; // We do not own memory, bypass base class destructor. |
471 } | 471 } |
472 | 472 |
473 scoped_refptr<net::IOBuffer> host_; | 473 scoped_refptr<net::IOBuffer> host_; |
474 }; | 474 }; |
475 | 475 |
476 // Provides queue of data represented as IOBuffers. | 476 // Provides queue of data represented as IOBuffers. |
477 class IOBufferQueue { | 477 class IOBufferQueue { |
478 public: | 478 public: |
479 // We do not allocate all capacity at once but lazily in |buf_size_| chunks. | 479 // We do not allocate all capacity at once but lazily in |buf_size_| chunks. |
480 explicit IOBufferQueue(int capacity) | 480 explicit IOBufferQueue(int capacity) |
481 : buf_size_(1 + capacity / kNumBuffersLimit) { | 481 : buf_size_(1 + capacity / kNumBuffersLimit) { |
482 } | 482 } |
483 | 483 |
484 // Obtains IOBuffer to add new data to back. | 484 // Obtains IOBuffer to add new data to back. |
485 net::IOBufferWithSize* GetIOBufferToFill() { | 485 net::IOBufferWithSize* GetIOBufferToFill() { |
486 if (back_ == NULL) { | 486 if (back_.get() == NULL) { |
487 if (storage_.size() >= kNumBuffersLimit) | 487 if (storage_.size() >= kNumBuffersLimit) |
488 return NULL; | 488 return NULL; |
489 storage_.push_back(new net::IOBufferWithSize(buf_size_)); | 489 storage_.push_back(new net::IOBufferWithSize(buf_size_)); |
490 back_ = new net::DrainableIOBuffer(storage_.back(), buf_size_); | 490 back_ = new net::DrainableIOBuffer(storage_.back().get(), buf_size_); |
491 } | 491 } |
492 return new DerivedIOBufferWithSize( | 492 return new DerivedIOBufferWithSize( |
493 back_.get(), back_->BytesRemaining()); | 493 back_.get(), back_->BytesRemaining()); |
494 } | 494 } |
495 | 495 |
496 // Obtains IOBuffer with some data from front. | 496 // Obtains IOBuffer with some data from front. |
497 net::IOBufferWithSize* GetIOBufferToProcess() { | 497 net::IOBufferWithSize* GetIOBufferToProcess() { |
498 if (front_ == NULL) { | 498 if (front_.get() == NULL) { |
499 if (storage_.empty()) | 499 if (storage_.empty()) |
500 return NULL; | 500 return NULL; |
501 front_ = new net::DrainableIOBuffer(storage_.front(), buf_size_); | 501 front_ = new net::DrainableIOBuffer(storage_.front().get(), buf_size_); |
502 } | 502 } |
503 int front_capacity = (storage_.size() == 1 && back_) ? | 503 int front_capacity = |
504 back_->BytesConsumed() : buf_size_; | 504 (storage_.size() == 1 && back_.get()) ? back_->BytesConsumed() |
| 505 : buf_size_; |
505 return new DerivedIOBufferWithSize( | 506 return new DerivedIOBufferWithSize( |
506 front_.get(), front_capacity - front_->BytesConsumed()); | 507 front_.get(), front_capacity - front_->BytesConsumed()); |
507 } | 508 } |
508 | 509 |
509 // Records number of bytes as added to back. | 510 // Records number of bytes as added to back. |
510 void DidFill(int bytes) { | 511 void DidFill(int bytes) { |
511 DCHECK(back_); | 512 DCHECK(back_.get()); |
512 back_->DidConsume(bytes); | 513 back_->DidConsume(bytes); |
513 if (back_->BytesRemaining() == 0) | 514 if (back_->BytesRemaining() == 0) |
514 back_ = NULL; | 515 back_ = NULL; |
515 } | 516 } |
516 | 517 |
517 // Pops number of bytes from front. | 518 // Pops number of bytes from front. |
518 void DidProcess(int bytes) { | 519 void DidProcess(int bytes) { |
519 DCHECK(front_); | 520 DCHECK(front_.get()); |
520 front_->DidConsume(bytes); | 521 front_->DidConsume(bytes); |
521 if (front_->BytesRemaining() == 0) { | 522 if (front_->BytesRemaining() == 0) { |
522 storage_.pop_front(); | 523 storage_.pop_front(); |
523 front_ = NULL; | 524 front_ = NULL; |
524 } | 525 } |
525 } | 526 } |
526 | 527 |
527 void Clear() { | 528 void Clear() { |
528 front_ = NULL; | 529 front_ = NULL; |
529 back_ = NULL; | 530 back_ = NULL; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 } | 579 } |
579 | 580 |
580 void Shut(int ALLOW_UNUSED net_error_code) { | 581 void Shut(int ALLOW_UNUSED net_error_code) { |
581 if (phase_ != PHASE_CLOSED) { | 582 if (phase_ != PHASE_CLOSED) { |
582 phase_ = PHASE_CLOSING; | 583 phase_ = PHASE_CLOSING; |
583 scoped_refptr<net::IOBufferWithSize> buf[] = { | 584 scoped_refptr<net::IOBufferWithSize> buf[] = { |
584 outbound_stream_.GetIOBufferToProcess(), | 585 outbound_stream_.GetIOBufferToProcess(), |
585 inbound_stream_.GetIOBufferToProcess() | 586 inbound_stream_.GetIOBufferToProcess() |
586 }; | 587 }; |
587 for (int i = arraysize(buf); i--;) { | 588 for (int i = arraysize(buf); i--;) { |
588 if (buf[i] && buf[i]->size() > 0) { | 589 if (buf[i].get() && buf[i]->size() > 0) { |
589 base::MessageLoop::current()->PostTask( | 590 base::MessageLoop::current()->PostTask( |
590 FROM_HERE, | 591 FROM_HERE, |
591 base::Bind(&SSLChan::Proceed, weak_factory_.GetWeakPtr())); | 592 base::Bind(&SSLChan::Proceed, weak_factory_.GetWeakPtr())); |
592 return; | 593 return; |
593 } | 594 } |
594 } | 595 } |
595 phase_ = PHASE_CLOSED; | 596 phase_ = PHASE_CLOSED; |
596 if (socket_ != NULL) { | 597 if (socket_ != NULL) { |
597 socket_->Disconnect(); | 598 socket_->Disconnect(); |
598 socket_.reset(); | 599 socket_.reset(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 | 698 |
698 private: | 699 private: |
699 void Proceed() { | 700 void Proceed() { |
700 if (phase_ != PHASE_RUNNING && phase_ != PHASE_CLOSING) | 701 if (phase_ != PHASE_RUNNING && phase_ != PHASE_CLOSING) |
701 return; | 702 return; |
702 for (bool proceed = true; proceed;) { | 703 for (bool proceed = true; proceed;) { |
703 proceed = false; | 704 proceed = false; |
704 if (!is_read_pipe_blocked_ && phase_ == PHASE_RUNNING) { | 705 if (!is_read_pipe_blocked_ && phase_ == PHASE_RUNNING) { |
705 scoped_refptr<net::IOBufferWithSize> buf = | 706 scoped_refptr<net::IOBufferWithSize> buf = |
706 outbound_stream_.GetIOBufferToFill(); | 707 outbound_stream_.GetIOBufferToFill(); |
707 if (buf && buf->size() > 0) { | 708 if (buf.get() && buf->size() > 0) { |
708 int rv = read(read_pipe_, buf->data(), buf->size()); | 709 int rv = read(read_pipe_, buf->data(), buf->size()); |
709 if (rv > 0) { | 710 if (rv > 0) { |
710 outbound_stream_.DidFill(rv); | 711 outbound_stream_.DidFill(rv); |
711 proceed = true; | 712 proceed = true; |
712 } else if (rv == -1 && errno == EAGAIN) { | 713 } else if (rv == -1 && errno == EAGAIN) { |
713 is_read_pipe_blocked_ = true; | 714 is_read_pipe_blocked_ = true; |
714 base::MessageLoopForIO::current()->WatchFileDescriptor( | 715 base::MessageLoopForIO::current()->WatchFileDescriptor( |
715 read_pipe_, false, base::MessageLoopForIO::WATCH_READ, | 716 read_pipe_, false, base::MessageLoopForIO::WATCH_READ, |
716 &read_pipe_controller_, this); | 717 &read_pipe_controller_, this); |
717 } else if (rv == 0) { | 718 } else if (rv == 0) { |
718 Shut(0); | 719 Shut(0); |
719 } else { | 720 } else { |
720 DCHECK_LT(rv, 0); | 721 DCHECK_LT(rv, 0); |
721 Shut(net::ERR_UNEXPECTED); | 722 Shut(net::ERR_UNEXPECTED); |
722 return; | 723 return; |
723 } | 724 } |
724 } | 725 } |
725 } | 726 } |
726 if (!is_socket_read_pending_ && phase_ == PHASE_RUNNING) { | 727 if (!is_socket_read_pending_ && phase_ == PHASE_RUNNING) { |
727 scoped_refptr<net::IOBufferWithSize> buf = | 728 scoped_refptr<net::IOBufferWithSize> buf = |
728 inbound_stream_.GetIOBufferToFill(); | 729 inbound_stream_.GetIOBufferToFill(); |
729 if (buf && buf->size() > 0) { | 730 if (buf.get() && buf->size() > 0) { |
730 int rv = socket_->Read( | 731 int rv = socket_->Read( |
731 buf, buf->size(), | 732 buf.get(), |
| 733 buf->size(), |
732 base::Bind(&SSLChan::OnSocketRead, base::Unretained(this))); | 734 base::Bind(&SSLChan::OnSocketRead, base::Unretained(this))); |
733 is_socket_read_pending_ = true; | 735 is_socket_read_pending_ = true; |
734 if (rv != net::ERR_IO_PENDING) { | 736 if (rv != net::ERR_IO_PENDING) { |
735 base::MessageLoop::current()->PostTask( | 737 base::MessageLoop::current()->PostTask( |
736 FROM_HERE, base::Bind(&SSLChan::OnSocketRead, | 738 FROM_HERE, base::Bind(&SSLChan::OnSocketRead, |
737 weak_factory_.GetWeakPtr(), rv)); | 739 weak_factory_.GetWeakPtr(), rv)); |
738 } | 740 } |
739 } | 741 } |
740 } | 742 } |
741 if (!is_socket_write_pending_) { | 743 if (!is_socket_write_pending_) { |
742 scoped_refptr<net::IOBufferWithSize> buf = | 744 scoped_refptr<net::IOBufferWithSize> buf = |
743 outbound_stream_.GetIOBufferToProcess(); | 745 outbound_stream_.GetIOBufferToProcess(); |
744 if (buf && buf->size() > 0) { | 746 if (buf.get() && buf->size() > 0) { |
745 int rv = socket_->Write( | 747 int rv = socket_->Write( |
746 buf, buf->size(), | 748 buf.get(), |
| 749 buf->size(), |
747 base::Bind(&SSLChan::OnSocketWrite, base::Unretained(this))); | 750 base::Bind(&SSLChan::OnSocketWrite, base::Unretained(this))); |
748 is_socket_write_pending_ = true; | 751 is_socket_write_pending_ = true; |
749 if (rv != net::ERR_IO_PENDING) { | 752 if (rv != net::ERR_IO_PENDING) { |
750 base::MessageLoop::current()->PostTask( | 753 base::MessageLoop::current()->PostTask( |
751 FROM_HERE, base::Bind(&SSLChan::OnSocketWrite, | 754 FROM_HERE, base::Bind(&SSLChan::OnSocketWrite, |
752 weak_factory_.GetWeakPtr(), rv)); | 755 weak_factory_.GetWeakPtr(), rv)); |
753 } | 756 } |
754 } else if (phase_ == PHASE_CLOSING) { | 757 } else if (phase_ == PHASE_CLOSING) { |
755 Shut(0); | 758 Shut(0); |
756 } | 759 } |
757 } | 760 } |
758 if (!is_write_pipe_blocked_) { | 761 if (!is_write_pipe_blocked_) { |
759 scoped_refptr<net::IOBufferWithSize> buf = | 762 scoped_refptr<net::IOBufferWithSize> buf = |
760 inbound_stream_.GetIOBufferToProcess(); | 763 inbound_stream_.GetIOBufferToProcess(); |
761 if (buf && buf->size() > 0) { | 764 if (buf.get() && buf->size() > 0) { |
762 int rv = write(write_pipe_, buf->data(), buf->size()); | 765 int rv = write(write_pipe_, buf->data(), buf->size()); |
763 if (rv > 0) { | 766 if (rv > 0) { |
764 inbound_stream_.DidProcess(rv); | 767 inbound_stream_.DidProcess(rv); |
765 proceed = true; | 768 proceed = true; |
766 } else if (rv == -1 && errno == EAGAIN) { | 769 } else if (rv == -1 && errno == EAGAIN) { |
767 is_write_pipe_blocked_ = true; | 770 is_write_pipe_blocked_ = true; |
768 base::MessageLoopForIO::current()->WatchFileDescriptor( | 771 base::MessageLoopForIO::current()->WatchFileDescriptor( |
769 write_pipe_, false, base::MessageLoopForIO::WATCH_WRITE, | 772 write_pipe_, false, base::MessageLoopForIO::WATCH_WRITE, |
770 &write_pipe_controller_, this); | 773 &write_pipe_controller_, this); |
771 } else { | 774 } else { |
(...skipping 1134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1906 | 1909 |
1907 void WebSocketProxy::Shutdown() { | 1910 void WebSocketProxy::Shutdown() { |
1908 static_cast<Serv*>(impl_)->Shutdown(); | 1911 static_cast<Serv*>(impl_)->Shutdown(); |
1909 } | 1912 } |
1910 | 1913 |
1911 void WebSocketProxy::OnNetworkChange() { | 1914 void WebSocketProxy::OnNetworkChange() { |
1912 static_cast<Serv*>(impl_)->OnNetworkChange(); | 1915 static_cast<Serv*>(impl_)->OnNetworkChange(); |
1913 } | 1916 } |
1914 | 1917 |
1915 } // namespace chromeos | 1918 } // namespace chromeos |
OLD | NEW |