| 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_, false, base::MessageLoopForIO::WATCH_READ, |
| 644 &read_pipe_controller_, this); | 644 &read_pipe_controller_, this); |
| 645 MessageLoopForIO::current()->WatchFileDescriptor( | 645 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 646 write_pipe_, false, MessageLoopForIO::WATCH_WRITE, | 646 write_pipe_, false, base::MessageLoopForIO::WATCH_WRITE, |
| 647 &write_pipe_controller_, this); | 647 &write_pipe_controller_, this); |
| 648 phase_ = PHASE_RUNNING; | 648 phase_ = PHASE_RUNNING; |
| 649 Proceed(); | 649 Proceed(); |
| 650 } | 650 } |
| 651 | 651 |
| 652 void OnSocketRead(int result) { | 652 void OnSocketRead(int result) { |
| 653 DCHECK(is_socket_read_pending_); | 653 DCHECK(is_socket_read_pending_); |
| 654 is_socket_read_pending_ = false; | 654 is_socket_read_pending_ = false; |
| 655 if (result <= 0) { | 655 if (result <= 0) { |
| 656 Shut(result); | 656 Shut(result); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 if (!is_read_pipe_blocked_ && phase_ == PHASE_RUNNING) { | 700 if (!is_read_pipe_blocked_ && phase_ == PHASE_RUNNING) { |
| 701 scoped_refptr<net::IOBufferWithSize> buf = | 701 scoped_refptr<net::IOBufferWithSize> buf = |
| 702 outbound_stream_.GetIOBufferToFill(); | 702 outbound_stream_.GetIOBufferToFill(); |
| 703 if (buf && buf->size() > 0) { | 703 if (buf && buf->size() > 0) { |
| 704 int rv = read(read_pipe_, buf->data(), buf->size()); | 704 int rv = read(read_pipe_, buf->data(), buf->size()); |
| 705 if (rv > 0) { | 705 if (rv > 0) { |
| 706 outbound_stream_.DidFill(rv); | 706 outbound_stream_.DidFill(rv); |
| 707 proceed = true; | 707 proceed = true; |
| 708 } else if (rv == -1 && errno == EAGAIN) { | 708 } else if (rv == -1 && errno == EAGAIN) { |
| 709 is_read_pipe_blocked_ = true; | 709 is_read_pipe_blocked_ = true; |
| 710 MessageLoopForIO::current()->WatchFileDescriptor( | 710 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 711 read_pipe_, false, MessageLoopForIO::WATCH_READ, | 711 read_pipe_, false, base::MessageLoopForIO::WATCH_READ, |
| 712 &read_pipe_controller_, this); | 712 &read_pipe_controller_, this); |
| 713 } else if (rv == 0) { | 713 } else if (rv == 0) { |
| 714 Shut(0); | 714 Shut(0); |
| 715 } else { | 715 } else { |
| 716 DCHECK_LT(rv, 0); | 716 DCHECK_LT(rv, 0); |
| 717 Shut(net::ERR_UNEXPECTED); | 717 Shut(net::ERR_UNEXPECTED); |
| 718 return; | 718 return; |
| 719 } | 719 } |
| 720 } | 720 } |
| 721 } | 721 } |
| 722 if (!is_socket_read_pending_ && phase_ == PHASE_RUNNING) { | 722 if (!is_socket_read_pending_ && phase_ == PHASE_RUNNING) { |
| 723 scoped_refptr<net::IOBufferWithSize> buf = | 723 scoped_refptr<net::IOBufferWithSize> buf = |
| 724 inbound_stream_.GetIOBufferToFill(); | 724 inbound_stream_.GetIOBufferToFill(); |
| 725 if (buf && buf->size() > 0) { | 725 if (buf && buf->size() > 0) { |
| 726 int rv = socket_->Read( | 726 int rv = socket_->Read( |
| 727 buf, buf->size(), | 727 buf, buf->size(), |
| 728 base::Bind(&SSLChan::OnSocketRead, base::Unretained(this))); | 728 base::Bind(&SSLChan::OnSocketRead, base::Unretained(this))); |
| 729 is_socket_read_pending_ = true; | 729 is_socket_read_pending_ = true; |
| 730 if (rv != net::ERR_IO_PENDING) { | 730 if (rv != net::ERR_IO_PENDING) { |
| 731 MessageLoop::current()->PostTask( | 731 base::MessageLoop::current()->PostTask( |
| 732 FROM_HERE, base::Bind(&SSLChan::OnSocketRead, | 732 FROM_HERE, base::Bind(&SSLChan::OnSocketRead, |
| 733 weak_factory_.GetWeakPtr(), rv)); | 733 weak_factory_.GetWeakPtr(), rv)); |
| 734 } | 734 } |
| 735 } | 735 } |
| 736 } | 736 } |
| 737 if (!is_socket_write_pending_) { | 737 if (!is_socket_write_pending_) { |
| 738 scoped_refptr<net::IOBufferWithSize> buf = | 738 scoped_refptr<net::IOBufferWithSize> buf = |
| 739 outbound_stream_.GetIOBufferToProcess(); | 739 outbound_stream_.GetIOBufferToProcess(); |
| 740 if (buf && buf->size() > 0) { | 740 if (buf && buf->size() > 0) { |
| 741 int rv = socket_->Write( | 741 int rv = socket_->Write( |
| 742 buf, buf->size(), | 742 buf, buf->size(), |
| 743 base::Bind(&SSLChan::OnSocketWrite, base::Unretained(this))); | 743 base::Bind(&SSLChan::OnSocketWrite, base::Unretained(this))); |
| 744 is_socket_write_pending_ = true; | 744 is_socket_write_pending_ = true; |
| 745 if (rv != net::ERR_IO_PENDING) { | 745 if (rv != net::ERR_IO_PENDING) { |
| 746 MessageLoop::current()->PostTask( | 746 base::MessageLoop::current()->PostTask( |
| 747 FROM_HERE, base::Bind(&SSLChan::OnSocketWrite, | 747 FROM_HERE, base::Bind(&SSLChan::OnSocketWrite, |
| 748 weak_factory_.GetWeakPtr(), rv)); | 748 weak_factory_.GetWeakPtr(), rv)); |
| 749 } | 749 } |
| 750 } else if (phase_ == PHASE_CLOSING) { | 750 } else if (phase_ == PHASE_CLOSING) { |
| 751 Shut(0); | 751 Shut(0); |
| 752 } | 752 } |
| 753 } | 753 } |
| 754 if (!is_write_pipe_blocked_) { | 754 if (!is_write_pipe_blocked_) { |
| 755 scoped_refptr<net::IOBufferWithSize> buf = | 755 scoped_refptr<net::IOBufferWithSize> buf = |
| 756 inbound_stream_.GetIOBufferToProcess(); | 756 inbound_stream_.GetIOBufferToProcess(); |
| 757 if (buf && buf->size() > 0) { | 757 if (buf && buf->size() > 0) { |
| 758 int rv = write(write_pipe_, buf->data(), buf->size()); | 758 int rv = write(write_pipe_, buf->data(), buf->size()); |
| 759 if (rv > 0) { | 759 if (rv > 0) { |
| 760 inbound_stream_.DidProcess(rv); | 760 inbound_stream_.DidProcess(rv); |
| 761 proceed = true; | 761 proceed = true; |
| 762 } else if (rv == -1 && errno == EAGAIN) { | 762 } else if (rv == -1 && errno == EAGAIN) { |
| 763 is_write_pipe_blocked_ = true; | 763 is_write_pipe_blocked_ = true; |
| 764 MessageLoopForIO::current()->WatchFileDescriptor( | 764 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 765 write_pipe_, false, MessageLoopForIO::WATCH_WRITE, | 765 write_pipe_, false, base::MessageLoopForIO::WATCH_WRITE, |
| 766 &write_pipe_controller_, this); | 766 &write_pipe_controller_, this); |
| 767 } else { | 767 } else { |
| 768 DCHECK_LE(rv, 0); | 768 DCHECK_LE(rv, 0); |
| 769 inbound_stream_.Clear(); | 769 inbound_stream_.Clear(); |
| 770 Shut(net::ERR_UNEXPECTED); | 770 Shut(net::ERR_UNEXPECTED); |
| 771 return; | 771 return; |
| 772 } | 772 } |
| 773 } else if (phase_ == PHASE_CLOSING) { | 773 } else if (phase_ == PHASE_CLOSING) { |
| 774 Shut(0); | 774 Shut(0); |
| 775 } | 775 } |
| 776 } | 776 } |
| 777 } | 777 } |
| 778 } | 778 } |
| 779 | 779 |
| 780 Phase phase_; | 780 Phase phase_; |
| 781 scoped_ptr<net::StreamSocket> socket_; | 781 scoped_ptr<net::StreamSocket> socket_; |
| 782 net::HostPortPair host_port_pair_; | 782 net::HostPortPair host_port_pair_; |
| 783 scoped_ptr<net::CertVerifier> cert_verifier_; | 783 scoped_ptr<net::CertVerifier> cert_verifier_; |
| 784 net::SSLConfig ssl_config_; | 784 net::SSLConfig ssl_config_; |
| 785 IOBufferQueue inbound_stream_; | 785 IOBufferQueue inbound_stream_; |
| 786 IOBufferQueue outbound_stream_; | 786 IOBufferQueue outbound_stream_; |
| 787 int read_pipe_; | 787 int read_pipe_; |
| 788 int write_pipe_; | 788 int write_pipe_; |
| 789 bool is_socket_read_pending_; | 789 bool is_socket_read_pending_; |
| 790 bool is_socket_write_pending_; | 790 bool is_socket_write_pending_; |
| 791 bool is_read_pipe_blocked_; | 791 bool is_read_pipe_blocked_; |
| 792 bool is_write_pipe_blocked_; | 792 bool is_write_pipe_blocked_; |
| 793 base::WeakPtrFactory<SSLChan> weak_factory_; | 793 base::WeakPtrFactory<SSLChan> weak_factory_; |
| 794 MessageLoopForIO::FileDescriptorWatcher read_pipe_controller_; | 794 base::MessageLoopForIO::FileDescriptorWatcher read_pipe_controller_; |
| 795 MessageLoopForIO::FileDescriptorWatcher write_pipe_controller_; | 795 base::MessageLoopForIO::FileDescriptorWatcher write_pipe_controller_; |
| 796 | 796 |
| 797 friend class base::DeleteHelper<SSLChan>; | 797 friend class base::DeleteHelper<SSLChan>; |
| 798 DISALLOW_COPY_AND_ASSIGN(SSLChan); | 798 DISALLOW_COPY_AND_ASSIGN(SSLChan); |
| 799 }; | 799 }; |
| 800 | 800 |
| 801 Serv::Serv() | 801 Serv::Serv() |
| 802 : evbase_(NULL), | 802 : evbase_(NULL), |
| 803 listening_sock_(-1), | 803 listening_sock_(-1), |
| 804 extra_listening_sock_(-1), | 804 extra_listening_sock_(-1), |
| 805 shutdown_requested_(false) { | 805 shutdown_requested_(false) { |
| (...skipping 1095 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1901 | 1901 |
| 1902 void WebSocketProxy::Shutdown() { | 1902 void WebSocketProxy::Shutdown() { |
| 1903 static_cast<Serv*>(impl_)->Shutdown(); | 1903 static_cast<Serv*>(impl_)->Shutdown(); |
| 1904 } | 1904 } |
| 1905 | 1905 |
| 1906 void WebSocketProxy::OnNetworkChange() { | 1906 void WebSocketProxy::OnNetworkChange() { |
| 1907 static_cast<Serv*>(impl_)->OnNetworkChange(); | 1907 static_cast<Serv*>(impl_)->OnNetworkChange(); |
| 1908 } | 1908 } |
| 1909 | 1909 |
| 1910 } // namespace chromeos | 1910 } // namespace chromeos |
| OLD | NEW |