Chromium Code Reviews| 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 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 430 | 430 |
| 431 // Used to schedule a timeout for initial phase of connection. | 431 // Used to schedule a timeout for initial phase of connection. |
| 432 scoped_ptr<struct event> destconnect_timeout_event_; | 432 scoped_ptr<struct event> destconnect_timeout_event_; |
| 433 | 433 |
| 434 static base::LazyInstance<EventKeyMap>::Leaky evkey_map_; | 434 static base::LazyInstance<EventKeyMap>::Leaky evkey_map_; |
| 435 static EventKey last_evkey_; | 435 static EventKey last_evkey_; |
| 436 | 436 |
| 437 DISALLOW_COPY_AND_ASSIGN(Conn); | 437 DISALLOW_COPY_AND_ASSIGN(Conn); |
| 438 }; | 438 }; |
| 439 | 439 |
| 440 class SSLChan : public MessageLoopForIO::Watcher { | 440 class SSLChan : public base::MessageLoopForIO::Watcher { |
| 441 public: | 441 public: |
| 442 static void Start(const net::AddressList& address_list, | 442 static void Start(const net::AddressList& address_list, |
| 443 const net::HostPortPair& host_port_pair, | 443 const net::HostPortPair& host_port_pair, |
| 444 int read_pipe, | 444 int read_pipe, |
| 445 int write_pipe) { | 445 int write_pipe) { |
| 446 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 446 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 447 SSLChan* ALLOW_UNUSED chan = new SSLChan( | 447 SSLChan* ALLOW_UNUSED chan = new SSLChan( |
| 448 address_list, host_port_pair, read_pipe, write_pipe); | 448 address_list, host_port_pair, read_pipe, write_pipe); |
| 449 } | 449 } |
| 450 | 450 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 578 | 578 |
| 579 void Shut(int ALLOW_UNUSED net_error_code) { | 579 void Shut(int ALLOW_UNUSED net_error_code) { |
| 580 if (phase_ != PHASE_CLOSED) { | 580 if (phase_ != PHASE_CLOSED) { |
| 581 phase_ = PHASE_CLOSING; | 581 phase_ = PHASE_CLOSING; |
| 582 scoped_refptr<net::IOBufferWithSize> buf[] = { | 582 scoped_refptr<net::IOBufferWithSize> buf[] = { |
| 583 outbound_stream_.GetIOBufferToProcess(), | 583 outbound_stream_.GetIOBufferToProcess(), |
| 584 inbound_stream_.GetIOBufferToProcess() | 584 inbound_stream_.GetIOBufferToProcess() |
| 585 }; | 585 }; |
| 586 for (int i = arraysize(buf); i--;) { | 586 for (int i = arraysize(buf); i--;) { |
| 587 if (buf[i] && buf[i]->size() > 0) { | 587 if (buf[i] && buf[i]->size() > 0) { |
| 588 MessageLoop::current()->PostTask( | 588 base::MessageLoop::current()->PostTask( |
| 589 FROM_HERE, | 589 FROM_HERE, |
| 590 base::Bind(&SSLChan::Proceed, weak_factory_.GetWeakPtr())); | 590 base::Bind(&SSLChan::Proceed, weak_factory_.GetWeakPtr())); |
| 591 return; | 591 return; |
| 592 } | 592 } |
| 593 } | 593 } |
| 594 phase_ = PHASE_CLOSED; | 594 phase_ = PHASE_CLOSED; |
| 595 if (socket_ != NULL) { | 595 if (socket_ != NULL) { |
| 596 socket_->Disconnect(); | 596 socket_->Disconnect(); |
| 597 socket_.reset(); | 597 socket_.reset(); |
| 598 } | 598 } |
| 599 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 599 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 600 } | 600 } |
| 601 } | 601 } |
| 602 | 602 |
| 603 void OnSocketConnect(int result) { | 603 void OnSocketConnect(int result) { |
| 604 if (phase_ != PHASE_CONNECTING) { | 604 if (phase_ != PHASE_CONNECTING) { |
| 605 NOTREACHED(); | 605 NOTREACHED(); |
| 606 return; | 606 return; |
| 607 } | 607 } |
| 608 if (result) { | 608 if (result) { |
| 609 Shut(result); | 609 Shut(result); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 632 | 632 |
| 633 void OnSSLHandshakeCompleted(int result) { | 633 void OnSSLHandshakeCompleted(int result) { |
| 634 if (result) { | 634 if (result) { |
| 635 Shut(result); | 635 Shut(result); |
| 636 return; | 636 return; |
| 637 } | 637 } |
| 638 is_socket_read_pending_ = false; | 638 is_socket_read_pending_ = false; |
| 639 is_socket_write_pending_ = false; | 639 is_socket_write_pending_ = false; |
| 640 is_read_pipe_blocked_ = false; | 640 is_read_pipe_blocked_ = false; |
| 641 is_write_pipe_blocked_ = false; | 641 is_write_pipe_blocked_ = false; |
| 642 MessageLoopForIO::current()->WatchFileDescriptor( | 642 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 643 read_pipe_, false, MessageLoopForIO::WATCH_READ, | 643 read_pipe_, |
| 644 &read_pipe_controller_, this); | 644 false, |
| 645 MessageLoopForIO::current()->WatchFileDescriptor( | 645 base::MessageLoopForIO::WATCH_READ, |
| 646 write_pipe_, false, MessageLoopForIO::WATCH_WRITE, | 646 &read_pipe_controller_, |
| 647 &write_pipe_controller_, this); | 647 this); |
| 648 base::MessageLoopForIO::current()->WatchFileDescriptor( | |
| 649 write_pipe_, | |
| 650 false, | |
| 651 base::MessageLoopForIO::WATCH_WRITE, | |
| 652 &write_pipe_controller_, | |
| 653 this); | |
| 648 phase_ = PHASE_RUNNING; | 654 phase_ = PHASE_RUNNING; |
| 649 Proceed(); | 655 Proceed(); |
| 650 } | 656 } |
| 651 | 657 |
| 652 void OnSocketRead(int result) { | 658 void OnSocketRead(int result) { |
| 653 DCHECK(is_socket_read_pending_); | 659 DCHECK(is_socket_read_pending_); |
| 654 is_socket_read_pending_ = false; | 660 is_socket_read_pending_ = false; |
| 655 if (result <= 0) { | 661 if (result <= 0) { |
| 656 Shut(result); | 662 Shut(result); |
| 657 return; | 663 return; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 700 if (!is_read_pipe_blocked_ && phase_ == PHASE_RUNNING) { | 706 if (!is_read_pipe_blocked_ && phase_ == PHASE_RUNNING) { |
| 701 scoped_refptr<net::IOBufferWithSize> buf = | 707 scoped_refptr<net::IOBufferWithSize> buf = |
| 702 outbound_stream_.GetIOBufferToFill(); | 708 outbound_stream_.GetIOBufferToFill(); |
| 703 if (buf && buf->size() > 0) { | 709 if (buf && buf->size() > 0) { |
| 704 int rv = read(read_pipe_, buf->data(), buf->size()); | 710 int rv = read(read_pipe_, buf->data(), buf->size()); |
| 705 if (rv > 0) { | 711 if (rv > 0) { |
| 706 outbound_stream_.DidFill(rv); | 712 outbound_stream_.DidFill(rv); |
| 707 proceed = true; | 713 proceed = true; |
| 708 } else if (rv == -1 && errno == EAGAIN) { | 714 } else if (rv == -1 && errno == EAGAIN) { |
| 709 is_read_pipe_blocked_ = true; | 715 is_read_pipe_blocked_ = true; |
| 710 MessageLoopForIO::current()->WatchFileDescriptor( | 716 base::MessageLoopForIO::current()->WatchFileDescriptor( |
|
brettw
2013/04/28 04:26:27
I wouldn't have changed the wrapping for the cases
| |
| 711 read_pipe_, false, MessageLoopForIO::WATCH_READ, | 717 read_pipe_, |
| 712 &read_pipe_controller_, this); | 718 false, |
| 719 base::MessageLoopForIO::WATCH_READ, | |
| 720 &read_pipe_controller_, | |
| 721 this); | |
| 713 } else if (rv == 0) { | 722 } else if (rv == 0) { |
| 714 Shut(0); | 723 Shut(0); |
| 715 } else { | 724 } else { |
| 716 DCHECK_LT(rv, 0); | 725 DCHECK_LT(rv, 0); |
| 717 Shut(net::ERR_UNEXPECTED); | 726 Shut(net::ERR_UNEXPECTED); |
| 718 return; | 727 return; |
| 719 } | 728 } |
| 720 } | 729 } |
| 721 } | 730 } |
| 722 if (!is_socket_read_pending_ && phase_ == PHASE_RUNNING) { | 731 if (!is_socket_read_pending_ && phase_ == PHASE_RUNNING) { |
| 723 scoped_refptr<net::IOBufferWithSize> buf = | 732 scoped_refptr<net::IOBufferWithSize> buf = |
| 724 inbound_stream_.GetIOBufferToFill(); | 733 inbound_stream_.GetIOBufferToFill(); |
| 725 if (buf && buf->size() > 0) { | 734 if (buf && buf->size() > 0) { |
| 726 int rv = socket_->Read( | 735 int rv = socket_->Read( |
| 727 buf, buf->size(), | 736 buf, buf->size(), |
| 728 base::Bind(&SSLChan::OnSocketRead, base::Unretained(this))); | 737 base::Bind(&SSLChan::OnSocketRead, base::Unretained(this))); |
| 729 is_socket_read_pending_ = true; | 738 is_socket_read_pending_ = true; |
| 730 if (rv != net::ERR_IO_PENDING) { | 739 if (rv != net::ERR_IO_PENDING) { |
| 731 MessageLoop::current()->PostTask( | 740 base::MessageLoop::current()->PostTask( |
| 732 FROM_HERE, base::Bind(&SSLChan::OnSocketRead, | 741 FROM_HERE, |
| 733 weak_factory_.GetWeakPtr(), rv)); | 742 base::Bind( |
| 743 &SSLChan::OnSocketRead, weak_factory_.GetWeakPtr(), rv)); | |
| 734 } | 744 } |
| 735 } | 745 } |
| 736 } | 746 } |
| 737 if (!is_socket_write_pending_) { | 747 if (!is_socket_write_pending_) { |
| 738 scoped_refptr<net::IOBufferWithSize> buf = | 748 scoped_refptr<net::IOBufferWithSize> buf = |
| 739 outbound_stream_.GetIOBufferToProcess(); | 749 outbound_stream_.GetIOBufferToProcess(); |
| 740 if (buf && buf->size() > 0) { | 750 if (buf && buf->size() > 0) { |
| 741 int rv = socket_->Write( | 751 int rv = socket_->Write( |
| 742 buf, buf->size(), | 752 buf, buf->size(), |
| 743 base::Bind(&SSLChan::OnSocketWrite, base::Unretained(this))); | 753 base::Bind(&SSLChan::OnSocketWrite, base::Unretained(this))); |
| 744 is_socket_write_pending_ = true; | 754 is_socket_write_pending_ = true; |
| 745 if (rv != net::ERR_IO_PENDING) { | 755 if (rv != net::ERR_IO_PENDING) { |
| 746 MessageLoop::current()->PostTask( | 756 base::MessageLoop::current()->PostTask( |
| 747 FROM_HERE, base::Bind(&SSLChan::OnSocketWrite, | 757 FROM_HERE, |
| 748 weak_factory_.GetWeakPtr(), rv)); | 758 base::Bind( |
| 759 &SSLChan::OnSocketWrite, weak_factory_.GetWeakPtr(), rv)); | |
| 749 } | 760 } |
| 750 } else if (phase_ == PHASE_CLOSING) { | 761 } else if (phase_ == PHASE_CLOSING) { |
| 751 Shut(0); | 762 Shut(0); |
| 752 } | 763 } |
| 753 } | 764 } |
| 754 if (!is_write_pipe_blocked_) { | 765 if (!is_write_pipe_blocked_) { |
| 755 scoped_refptr<net::IOBufferWithSize> buf = | 766 scoped_refptr<net::IOBufferWithSize> buf = |
| 756 inbound_stream_.GetIOBufferToProcess(); | 767 inbound_stream_.GetIOBufferToProcess(); |
| 757 if (buf && buf->size() > 0) { | 768 if (buf && buf->size() > 0) { |
| 758 int rv = write(write_pipe_, buf->data(), buf->size()); | 769 int rv = write(write_pipe_, buf->data(), buf->size()); |
| 759 if (rv > 0) { | 770 if (rv > 0) { |
| 760 inbound_stream_.DidProcess(rv); | 771 inbound_stream_.DidProcess(rv); |
| 761 proceed = true; | 772 proceed = true; |
| 762 } else if (rv == -1 && errno == EAGAIN) { | 773 } else if (rv == -1 && errno == EAGAIN) { |
| 763 is_write_pipe_blocked_ = true; | 774 is_write_pipe_blocked_ = true; |
| 764 MessageLoopForIO::current()->WatchFileDescriptor( | 775 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 765 write_pipe_, false, MessageLoopForIO::WATCH_WRITE, | 776 write_pipe_, |
| 766 &write_pipe_controller_, this); | 777 false, |
| 778 base::MessageLoopForIO::WATCH_WRITE, | |
| 779 &write_pipe_controller_, | |
| 780 this); | |
| 767 } else { | 781 } else { |
| 768 DCHECK_LE(rv, 0); | 782 DCHECK_LE(rv, 0); |
| 769 inbound_stream_.Clear(); | 783 inbound_stream_.Clear(); |
| 770 Shut(net::ERR_UNEXPECTED); | 784 Shut(net::ERR_UNEXPECTED); |
| 771 return; | 785 return; |
| 772 } | 786 } |
| 773 } else if (phase_ == PHASE_CLOSING) { | 787 } else if (phase_ == PHASE_CLOSING) { |
| 774 Shut(0); | 788 Shut(0); |
| 775 } | 789 } |
| 776 } | 790 } |
| 777 } | 791 } |
| 778 } | 792 } |
| 779 | 793 |
| 780 Phase phase_; | 794 Phase phase_; |
| 781 scoped_ptr<net::StreamSocket> socket_; | 795 scoped_ptr<net::StreamSocket> socket_; |
| 782 net::HostPortPair host_port_pair_; | 796 net::HostPortPair host_port_pair_; |
| 783 scoped_ptr<net::CertVerifier> cert_verifier_; | 797 scoped_ptr<net::CertVerifier> cert_verifier_; |
| 784 net::SSLConfig ssl_config_; | 798 net::SSLConfig ssl_config_; |
| 785 IOBufferQueue inbound_stream_; | 799 IOBufferQueue inbound_stream_; |
| 786 IOBufferQueue outbound_stream_; | 800 IOBufferQueue outbound_stream_; |
| 787 int read_pipe_; | 801 int read_pipe_; |
| 788 int write_pipe_; | 802 int write_pipe_; |
| 789 bool is_socket_read_pending_; | 803 bool is_socket_read_pending_; |
| 790 bool is_socket_write_pending_; | 804 bool is_socket_write_pending_; |
| 791 bool is_read_pipe_blocked_; | 805 bool is_read_pipe_blocked_; |
| 792 bool is_write_pipe_blocked_; | 806 bool is_write_pipe_blocked_; |
| 793 base::WeakPtrFactory<SSLChan> weak_factory_; | 807 base::WeakPtrFactory<SSLChan> weak_factory_; |
| 794 MessageLoopForIO::FileDescriptorWatcher read_pipe_controller_; | 808 base::MessageLoopForIO::FileDescriptorWatcher read_pipe_controller_; |
| 795 MessageLoopForIO::FileDescriptorWatcher write_pipe_controller_; | 809 base::MessageLoopForIO::FileDescriptorWatcher write_pipe_controller_; |
| 796 | 810 |
| 797 friend class base::DeleteHelper<SSLChan>; | 811 friend class base::DeleteHelper<SSLChan>; |
| 798 DISALLOW_COPY_AND_ASSIGN(SSLChan); | 812 DISALLOW_COPY_AND_ASSIGN(SSLChan); |
| 799 }; | 813 }; |
| 800 | 814 |
| 801 Serv::Serv() | 815 Serv::Serv() |
| 802 : evbase_(NULL), | 816 : evbase_(NULL), |
| 803 listening_sock_(-1), | 817 listening_sock_(-1), |
| 804 extra_listening_sock_(-1), | 818 extra_listening_sock_(-1), |
| 805 shutdown_requested_(false) { | 819 shutdown_requested_(false) { |
| (...skipping 1095 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1901 | 1915 |
| 1902 void WebSocketProxy::Shutdown() { | 1916 void WebSocketProxy::Shutdown() { |
| 1903 static_cast<Serv*>(impl_)->Shutdown(); | 1917 static_cast<Serv*>(impl_)->Shutdown(); |
| 1904 } | 1918 } |
| 1905 | 1919 |
| 1906 void WebSocketProxy::OnNetworkChange() { | 1920 void WebSocketProxy::OnNetworkChange() { |
| 1907 static_cast<Serv*>(impl_)->OnNetworkChange(); | 1921 static_cast<Serv*>(impl_)->OnNetworkChange(); |
| 1908 } | 1922 } |
| 1909 | 1923 |
| 1910 } // namespace chromeos | 1924 } // namespace chromeos |
| OLD | NEW |