diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-09 18:43:55 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-09 18:43:55 +0000 |
commit | 83039bbf2f2ec0e918f7000b5212d104f60f2bb7 (patch) | |
tree | b22dbd0051b57a437a588772a874271f0d02ffdb | |
parent | e7456a206fe5b50aeb322ebabd6c26adc869a5fd (diff) | |
download | chromium_src-83039bbf2f2ec0e918f7000b5212d104f60f2bb7.zip chromium_src-83039bbf2f2ec0e918f7000b5212d104f60f2bb7.tar.gz chromium_src-83039bbf2f2ec0e918f7000b5212d104f60f2bb7.tar.bz2 |
Migrate net/socket/socket.h, net/socket/stream_socket.h to base::Bind().
This changes Socket::Read(), Socket::Write, and StreamSocket::Connect() to use CompletionCallback and fixes all users.
BUG=none
TEST=existing.
Review URL: http://codereview.chromium.org/8824006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113825 0039d316-1c4b-4281-b951-d872f2087c98
143 files changed, 1425 insertions, 3317 deletions
diff --git a/chrome/browser/chromeos/web_socket_proxy.cc b/chrome/browser/chromeos/web_socket_proxy.cc index 17f5a33..85d7d2ff 100644 --- a/chrome/browser/chromeos/web_socket_proxy.cc +++ b/chrome/browser/chromeos/web_socket_proxy.cc @@ -49,7 +49,6 @@ #include "googleurl/src/url_parse.h" #include "net/base/address_list.h" #include "net/base/cert_verifier.h" -#include "net/base/completion_callback.h" #include "net/base/host_port_pair.h" #include "net/base/io_buffer.h" #include "net/base/net_errors.h" @@ -555,15 +554,7 @@ class SSLChan : public MessageLoopForIO::Watcher { outbound_stream_(WebSocketProxy::kBufferLimit), read_pipe_(read_pipe), write_pipe_(write_pipe), - method_factory_(this), - ALLOW_THIS_IN_INITIALIZER_LIST(socket_connect_callback_( - base::Bind(&SSLChan::OnSocketConnect, base::Unretained(this)))), - ALLOW_THIS_IN_INITIALIZER_LIST(ssl_handshake_callback_( - base::Bind(&SSLChan::OnSSLHandshakeCompleted, - base::Unretained(this)))), - ALLOW_THIS_IN_INITIALIZER_LIST(socket_read_callback_( - base::Bind(&SSLChan::OnSocketRead, base::Unretained(this)))), - socket_write_callback_(NewCallback(this, &SSLChan::OnSocketWrite)) { + method_factory_(this) { if (!SetNonBlock(read_pipe_) || !SetNonBlock(write_pipe_)) { Shut(net::ERR_UNEXPECTED); return; @@ -576,7 +567,8 @@ class SSLChan : public MessageLoopForIO::Watcher { Shut(net::ERR_FAILED); return; } - int result = socket_->Connect(socket_connect_callback_); + int result = socket_->Connect(base::Bind(&SSLChan::OnSocketConnect, + base::Unretained(this))); if (result != net::ERR_IO_PENDING) OnSocketConnect(result); } @@ -636,7 +628,8 @@ class SSLChan : public MessageLoopForIO::Watcher { OnSSLHandshakeCompleted(net::ERR_UNEXPECTED); return; } - result = socket_->Connect(ssl_handshake_callback_); + result = socket_->Connect(base::Bind(&SSLChan::OnSSLHandshakeCompleted, + base::Unretained(this))); if (result != net::ERR_IO_PENDING) OnSSLHandshakeCompleted(result); } @@ -734,7 +727,9 @@ class SSLChan : public MessageLoopForIO::Watcher { scoped_refptr<net::IOBufferWithSize> buf = inbound_stream_.GetIOBufferToFill(); if (buf && buf->size() > 0) { - int rv = socket_->Read(buf, buf->size(), socket_read_callback_); + int rv = socket_->Read( + buf, buf->size(), + base::Bind(&SSLChan::OnSocketRead, base::Unretained(this))); is_socket_read_pending_ = true; if (rv != net::ERR_IO_PENDING) { MessageLoop::current()->PostTask(FROM_HERE, @@ -747,7 +742,8 @@ class SSLChan : public MessageLoopForIO::Watcher { outbound_stream_.GetIOBufferToProcess(); if (buf && buf->size() > 0) { int rv = socket_->Write( - buf, buf->size(), socket_write_callback_.get()); + buf, buf->size(), + base::Bind(&SSLChan::OnSocketWrite, base::Unretained(this))); is_socket_write_pending_ = true; if (rv != net::ERR_IO_PENDING) { MessageLoop::current()->PostTask(FROM_HERE, @@ -797,10 +793,6 @@ class SSLChan : public MessageLoopForIO::Watcher { bool is_read_pipe_blocked_; bool is_write_pipe_blocked_; ScopedRunnableMethodFactory<SSLChan> method_factory_; - net::CompletionCallback socket_connect_callback_; - net::CompletionCallback ssl_handshake_callback_; - net::CompletionCallback socket_read_callback_; - scoped_ptr<net::OldCompletionCallback> socket_write_callback_; MessageLoopForIO::FileDescriptorWatcher read_pipe_controller_; MessageLoopForIO::FileDescriptorWatcher write_pipe_controller_; diff --git a/chrome/browser/extensions/api/socket/socket_api_controller.cc b/chrome/browser/extensions/api/socket/socket_api_controller.cc index 8758719..30e3a3a 100644 --- a/chrome/browser/extensions/api/socket/socket_api_controller.cc +++ b/chrome/browser/extensions/api/socket/socket_api_controller.cc @@ -40,7 +40,6 @@ class Socket { scoped_ptr<net::UDPClientSocket> udp_client_socket_; bool is_connected_; - net::OldCompletionCallbackImpl<Socket> io_callback_; // A callback required by UDPClientSocket::Write(). void OnIOComplete(int result); @@ -56,9 +55,7 @@ Socket::Socket(const Profile* profile, const std::string& src_extension_id, RandIntCallback(), NULL, NetLog::Source())), - is_connected_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - io_callback_(this, &Socket::OnIOComplete)) {} + is_connected_(false) {} Socket::~Socket() { if (is_connected_) { @@ -88,9 +85,9 @@ int Socket::Write(const std::string message) { int bytes_sent = 0; while (buffer->BytesRemaining()) { - int rv = udp_client_socket_->Write(buffer, - buffer->BytesRemaining(), - &io_callback_); + int rv = udp_client_socket_->Write( + buffer, buffer->BytesRemaining(), + base::Bind(&Socket::OnIOComplete, base::Unretained(this))); if (rv <= 0) { // We pass all errors, including ERROR_IO_PENDING, back to the caller. return bytes_sent > 0 ? bytes_sent : rv; diff --git a/chrome/browser/net/network_stats.cc b/chrome/browser/net/network_stats.cc index 0c3d351..c365b06 100644 --- a/chrome/browser/net/network_stats.cc +++ b/chrome/browser/net/network_stats.cc @@ -82,10 +82,6 @@ NetworkStats::NetworkStats() bytes_to_read_(0), bytes_to_send_(0), encoded_message_(""), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &NetworkStats::OnReadComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_callback_(this, &NetworkStats::OnWriteComplete)), finished_callback_(NULL), start_time_(base::TimeTicks::Now()), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { @@ -246,7 +242,9 @@ void NetworkStats::ReadData() { // We release the read_buffer_ in the destructor if there is an error. read_buffer_ = new net::IOBuffer(kMaxMessage); - rv = socket_->Read(read_buffer_, kMaxMessage, &read_callback_); + rv = socket_->Read(read_buffer_, kMaxMessage, + base::Bind(&NetworkStats::OnReadComplete, + base::Unretained(this))); if (rv == net::ERR_IO_PENDING) return; // If we have read all the data then return. @@ -268,7 +266,8 @@ int NetworkStats::SendData() { return net::ERR_UNEXPECTED; int rv = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(), - &write_callback_); + base::Bind(&NetworkStats::OnWriteComplete, + base::Unretained(this))); if (rv < 0) return rv; write_buffer_->DidConsume(rv); @@ -443,10 +442,7 @@ void UDPStatsClient::Finish(Status status, int result) { } // TCPStatsClient methods and members. -TCPStatsClient::TCPStatsClient() - : NetworkStats(), - ALLOW_THIS_IN_INITIALIZER_LIST( - connect_callback_(this, &TCPStatsClient::OnConnectComplete)) { +TCPStatsClient::TCPStatsClient() { } TCPStatsClient::~TCPStatsClient() { @@ -466,7 +462,8 @@ bool TCPStatsClient::DoConnect(int result) { } set_socket(tcp_socket); - int rv = tcp_socket->Connect(&connect_callback_); + int rv = tcp_socket->Connect(base::Bind(&TCPStatsClient::OnConnectComplete, + base::Unretained(this))); if (rv == net::ERR_IO_PENDING) return true; diff --git a/chrome/browser/net/network_stats.h b/chrome/browser/net/network_stats.h index e241899..c18bf91 100644 --- a/chrome/browser/net/network_stats.h +++ b/chrome/browser/net/network_stats.h @@ -177,12 +177,6 @@ class NetworkStats { // HostResolver fills out the |addresses_| after host resolution is completed. net::AddressList addresses_; - // Callback to call when data is read from the server. - net::OldCompletionCallbackImpl<NetworkStats> read_callback_; - - // Callback to call when data is sent to the server. - net::OldCompletionCallbackImpl<NetworkStats> write_callback_; - // Callback to call when echo protocol is successefully finished or whenever // there is an error (this allows unittests to wait until echo protocol's // round trip is finished). @@ -248,9 +242,6 @@ class TCPStatsClient : public NetworkStats { // Callback that is called when connect is completed and calls // ConnectComplete() to start the echo protocol. void OnConnectComplete(int result); - - // Callback to call when connect is completed. - net::OldCompletionCallbackImpl<TCPStatsClient> connect_callback_; }; // This collects the network connectivity stats for UDP and TCP for small diff --git a/content/browser/renderer_host/p2p/socket_host_tcp.cc b/content/browser/renderer_host/p2p/socket_host_tcp.cc index f88ff4a..db94edc 100644 --- a/content/browser/renderer_host/p2p/socket_host_tcp.cc +++ b/content/browser/renderer_host/p2p/socket_host_tcp.cc @@ -21,13 +21,7 @@ namespace content { P2PSocketHostTcp::P2PSocketHostTcp(IPC::Message::Sender* message_sender, int routing_id, int id) : P2PSocketHost(message_sender, routing_id, id), - connected_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - connect_callback_(this, &P2PSocketHostTcp::OnConnected)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &P2PSocketHostTcp::OnRead)), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_callback_(this, &P2PSocketHostTcp::OnWritten)) { + connected_(false) { } P2PSocketHostTcp::~P2PSocketHostTcp() { @@ -68,7 +62,8 @@ bool P2PSocketHostTcp::Init(const net::IPEndPoint& local_address, if (socket_->SetSendBufferSize(kMaxSendBufferSize)) LOG(WARNING) << "Failed to set send buffer size for TCP socket."; - int result = socket_->Connect(&connect_callback_); + int result = socket_->Connect( + base::Bind(&P2PSocketHostTcp::OnConnected, base::Unretained(this))); if (result != net::ERR_IO_PENDING) { OnConnected(result); } @@ -125,7 +120,8 @@ void P2PSocketHostTcp::DoRead() { read_buffer_->RemainingCapacity()); } result = socket_->Read(read_buffer_, read_buffer_->RemainingCapacity(), - &read_callback_); + base::Bind(&P2PSocketHostTcp::OnRead, + base::Unretained(this))); DidCompleteRead(result); } while (result > 0); } @@ -232,7 +228,8 @@ void P2PSocketHostTcp::Send(const net::IPEndPoint& to, void P2PSocketHostTcp::DoWrite() { while (true) { int result = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(), - &write_callback_); + base::Bind(&P2PSocketHostTcp::OnWritten, + base::Unretained(this))); if (result >= 0) { write_buffer_->DidConsume(result); if (write_buffer_->BytesRemaining() == 0) { diff --git a/content/browser/renderer_host/p2p/socket_host_tcp.h b/content/browser/renderer_host/p2p/socket_host_tcp.h index 3c50f82..b24630a 100644 --- a/content/browser/renderer_host/p2p/socket_host_tcp.h +++ b/content/browser/renderer_host/p2p/socket_host_tcp.h @@ -66,10 +66,6 @@ class CONTENT_EXPORT P2PSocketHostTcp : public P2PSocketHost { bool connected_; - net::OldCompletionCallbackImpl<P2PSocketHostTcp> connect_callback_; - net::OldCompletionCallbackImpl<P2PSocketHostTcp> read_callback_; - net::OldCompletionCallbackImpl<P2PSocketHostTcp> write_callback_; - DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcp); }; diff --git a/content/browser/renderer_host/p2p/socket_host_test_utils.h b/content/browser/renderer_host/p2p/socket_host_test_utils.h index 0f1aeb4..0ea8c1c 100644 --- a/content/browser/renderer_host/p2p/socket_host_test_utils.h +++ b/content/browser/renderer_host/p2p/socket_host_test_utils.h @@ -57,14 +57,11 @@ class FakeSocket : public net::StreamSocket { // net::Socket implementation. virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; - virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -83,7 +80,6 @@ class FakeSocket : public net::StreamSocket { bool read_pending_; scoped_refptr<net::IOBuffer> read_buffer_; int read_buffer_size_; - net::OldCompletionCallback* old_read_callback_; net::CompletionCallback read_callback_; std::string* written_data_; @@ -115,15 +111,9 @@ void FakeSocket::AppendInputData(const char* data, int data_size) { memcpy(read_buffer_->data(), &input_data_[0] + input_pos_, result); input_pos_ += result; read_buffer_ = NULL; - if (old_read_callback_) { - net::OldCompletionCallback* cb = old_read_callback_; - old_read_callback_ = NULL; - cb->Run(result); - } else { - net::CompletionCallback cb = read_callback_; - read_callback_.Reset(); - cb.Run(result); - } + net::CompletionCallback cb = read_callback_; + read_callback_.Reset(); + cb.Run(result); } } @@ -136,23 +126,6 @@ void FakeSocket::SetLocalAddress(const net::IPEndPoint& local_address) { } int FakeSocket::Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { - DCHECK(buf); - if (input_pos_ < static_cast<int>(input_data_.size())){ - int result = std::min(buf_len, - static_cast<int>(input_data_.size()) - input_pos_); - memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); - input_pos_ += result; - return result; - } else { - read_pending_ = true; - read_buffer_ = buf; - read_buffer_size_ = buf_len; - old_read_callback_ = callback; - return net::ERR_IO_PENDING; - } -} -int FakeSocket::Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { DCHECK(buf); if (input_pos_ < static_cast<int>(input_data_.size())){ @@ -171,7 +144,7 @@ int FakeSocket::Read(net::IOBuffer* buf, int buf_len, } int FakeSocket::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { + const net::CompletionCallback& callback) { DCHECK(buf); if (written_data_) { written_data_->insert(written_data_->end(), @@ -190,10 +163,6 @@ bool FakeSocket::SetSendBufferSize(int32 size) { return false; } -int FakeSocket::Connect(net::OldCompletionCallback* callback) { - return 0; -} - int FakeSocket::Connect(const net::CompletionCallback& callback) { return 0; } diff --git a/content/browser/renderer_host/p2p/socket_host_udp.cc b/content/browser/renderer_host/p2p/socket_host_udp.cc index f85e80d..e5c4b01 100644 --- a/content/browser/renderer_host/p2p/socket_host_udp.cc +++ b/content/browser/renderer_host/p2p/socket_host_udp.cc @@ -4,6 +4,7 @@ #include "content/browser/renderer_host/p2p/socket_host_udp.h" +#include "base/bind.h" #include "content/common/p2p_messages.h" #include "net/base/io_buffer.h" #include "net/base/net_errors.h" @@ -34,11 +35,7 @@ P2PSocketHostUdp::P2PSocketHostUdp(IPC::Message::Sender* message_sender, : P2PSocketHost(message_sender, routing_id, id), socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), send_queue_bytes_(0), - send_pending_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - recv_callback_(this, &P2PSocketHostUdp::OnRecv)), - ALLOW_THIS_IN_INITIALIZER_LIST( - send_callback_(this, &P2PSocketHostUdp::OnSend)) { + send_pending_(false) { } P2PSocketHostUdp::~P2PSocketHostUdp() { @@ -93,7 +90,8 @@ void P2PSocketHostUdp::DoRead() { int result; do { result = socket_->RecvFrom(recv_buffer_, kReadBufferSize, &recv_address_, - &recv_callback_); + base::Bind(&P2PSocketHostUdp::OnRecv, + base::Unretained(this))); DidCompleteRead(result); } while (result > 0); } @@ -167,7 +165,8 @@ void P2PSocketHostUdp::Send(const net::IPEndPoint& to, void P2PSocketHostUdp::DoSend(const PendingPacket& packet) { int result = socket_->SendTo(packet.data, packet.size, packet.to, - &send_callback_); + base::Bind(&P2PSocketHostUdp::OnSend, + base::Unretained(this))); if (result == net::ERR_IO_PENDING) { send_pending_ = true; } else if (result < 0) { diff --git a/content/browser/renderer_host/p2p/socket_host_udp.h b/content/browser/renderer_host/p2p/socket_host_udp.h index 6925b54..d46c790 100644 --- a/content/browser/renderer_host/p2p/socket_host_udp.h +++ b/content/browser/renderer_host/p2p/socket_host_udp.h @@ -69,9 +69,6 @@ class CONTENT_EXPORT P2PSocketHostUdp : public P2PSocketHost { // response or relay allocation request or response. ConnectedPeerSet connected_peers_; - net::OldCompletionCallbackImpl<P2PSocketHostUdp> recv_callback_; - net::OldCompletionCallbackImpl<P2PSocketHostUdp> send_callback_; - DISALLOW_COPY_AND_ASSIGN(P2PSocketHostUdp); }; diff --git a/content/browser/renderer_host/p2p/socket_host_udp_unittest.cc b/content/browser/renderer_host/p2p/socket_host_udp_unittest.cc index a33bc83..02cc3b2 100644 --- a/content/browser/renderer_host/p2p/socket_host_udp_unittest.cc +++ b/content/browser/renderer_host/p2p/socket_host_udp_unittest.cc @@ -30,7 +30,7 @@ class FakeDatagramServerSocket : public net::DatagramServerSocket { // P2PSocketHostUdp destroyes a socket on errors so sent packets // need to be stored outside of this object. explicit FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets) - : sent_packets_(sent_packets), recv_callback_(NULL) { + : sent_packets_(sent_packets) { } virtual void Close() OVERRIDE { @@ -53,8 +53,8 @@ class FakeDatagramServerSocket : public net::DatagramServerSocket { virtual int RecvFrom(net::IOBuffer* buf, int buf_len, net::IPEndPoint* address, - net::OldCompletionCallback* callback) OVERRIDE { - CHECK(!recv_callback_); + const net::CompletionCallback& callback) OVERRIDE { + CHECK(recv_callback_.is_null()); if (incoming_packets_.size() > 0) { scoped_refptr<net::IOBuffer> buffer(buf); int size = std::min( @@ -74,7 +74,7 @@ class FakeDatagramServerSocket : public net::DatagramServerSocket { virtual int SendTo(net::IOBuffer* buf, int buf_len, const net::IPEndPoint& address, - net::OldCompletionCallback* callback) OVERRIDE { + const net::CompletionCallback& callback) OVERRIDE { scoped_refptr<net::IOBuffer> buffer(buf); std::vector<char> data_vector(buffer->data(), buffer->data() + buf_len); sent_packets_->push_back(UDPPacket(address, data_vector)); @@ -90,14 +90,14 @@ class FakeDatagramServerSocket : public net::DatagramServerSocket { } void ReceivePacket(const net::IPEndPoint& address, std::vector<char> data) { - if (recv_callback_) { + if (!recv_callback_.is_null()) { int size = std::min(recv_size_, static_cast<int>(data.size())); memcpy(recv_buffer_->data(), &*data.begin(), size); *recv_address_ = address; - net::OldCompletionCallback* cb = recv_callback_; - recv_callback_ = NULL; + net::CompletionCallback cb = recv_callback_; + recv_callback_.Reset(); recv_buffer_ = NULL; - cb->Run(size); + cb.Run(size); } else { incoming_packets_.push_back(UDPPacket(address, data)); } @@ -116,7 +116,7 @@ class FakeDatagramServerSocket : public net::DatagramServerSocket { scoped_refptr<net::IOBuffer> recv_buffer_; net::IPEndPoint* recv_address_; int recv_size_; - net::OldCompletionCallback* recv_callback_; + net::CompletionCallback recv_callback_; }; } // namespace diff --git a/content/browser/renderer_host/pepper_tcp_socket.cc b/content/browser/renderer_host/pepper_tcp_socket.cc index 8223f14..05a84cf 100644 --- a/content/browser/renderer_host/pepper_tcp_socket.cc +++ b/content/browser/renderer_host/pepper_tcp_socket.cc @@ -40,15 +40,7 @@ PepperTCPSocket::PepperTCPSocket( plugin_dispatcher_id_(plugin_dispatcher_id), socket_id_(socket_id), connection_state_(BEFORE_CONNECT), - end_of_file_reached_(false), - ALLOW_THIS_IN_INITIALIZER_LIST(connect_callback_( - this, &PepperTCPSocket::OnConnectCompleted)), - ALLOW_THIS_IN_INITIALIZER_LIST(ssl_handshake_callback_( - this, &PepperTCPSocket::OnSSLHandshakeCompleted)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &PepperTCPSocket::OnReadCompleted)), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_callback_(this, &PepperTCPSocket::OnWriteCompleted)) { + end_of_file_reached_(false) { DCHECK(manager); } @@ -123,7 +115,9 @@ void PepperTCPSocket::SSLHandshake(const std::string& server_name, return; } - int result = socket_->Connect(&ssl_handshake_callback_); + int result = socket_->Connect( + base::Bind(&PepperTCPSocket::OnSSLHandshakeCompleted, + base::Unretained(this))); if (result != net::ERR_IO_PENDING) OnSSLHandshakeCompleted(result); } @@ -143,7 +137,9 @@ void PepperTCPSocket::Read(int32 bytes_to_read) { } read_buffer_ = new net::IOBuffer(bytes_to_read); - int result = socket_->Read(read_buffer_, bytes_to_read, &read_callback_); + int result = socket_->Read(read_buffer_, bytes_to_read, + base::Bind(&PepperTCPSocket::OnReadCompleted, + base::Unretained(this))); if (result != net::ERR_IO_PENDING) OnReadCompleted(result); } @@ -164,7 +160,9 @@ void PepperTCPSocket::Write(const std::string& data) { write_buffer_ = new net::IOBuffer(data_size); memcpy(write_buffer_->data(), data.c_str(), data_size); - int result = socket_->Write(write_buffer_, data.size(), &write_callback_); + int result = socket_->Write(write_buffer_, data.size(), + base::Bind(&PepperTCPSocket::OnWriteCompleted, + base::Unretained(this))); if (result != net::ERR_IO_PENDING) OnWriteCompleted(result); } @@ -174,7 +172,9 @@ void PepperTCPSocket::StartConnect(const net::AddressList& addresses) { socket_.reset( new net::TCPClientSocket(addresses, NULL, net::NetLog::Source())); - int result = socket_->Connect(&connect_callback_); + int result = socket_->Connect( + base::Bind(&PepperTCPSocket::OnConnectCompleted, + base::Unretained(this))); if (result != net::ERR_IO_PENDING) OnConnectCompleted(result); } diff --git a/content/browser/renderer_host/pepper_tcp_socket.h b/content/browser/renderer_host/pepper_tcp_socket.h index 882e22e..bc9c6b6 100644 --- a/content/browser/renderer_host/pepper_tcp_socket.h +++ b/content/browser/renderer_host/pepper_tcp_socket.h @@ -80,11 +80,6 @@ class PepperTCPSocket { ConnectionState connection_state_; bool end_of_file_reached_; - net::OldCompletionCallbackImpl<PepperTCPSocket> connect_callback_; - net::OldCompletionCallbackImpl<PepperTCPSocket> ssl_handshake_callback_; - net::OldCompletionCallbackImpl<PepperTCPSocket> read_callback_; - net::OldCompletionCallbackImpl<PepperTCPSocket> write_callback_; - scoped_ptr<net::SingleRequestHostResolver> resolver_; net::AddressList address_list_; diff --git a/content/browser/renderer_host/pepper_udp_socket.cc b/content/browser/renderer_host/pepper_udp_socket.cc index ceddc2b..33f2c7f 100644 --- a/content/browser/renderer_host/pepper_udp_socket.cc +++ b/content/browser/renderer_host/pepper_udp_socket.cc @@ -27,11 +27,7 @@ PepperUDPSocket::PepperUDPSocket( : manager_(manager), routing_id_(routing_id), plugin_dispatcher_id_(plugin_dispatcher_id), - socket_id_(socket_id), - ALLOW_THIS_IN_INITIALIZER_LIST( - recvfrom_callback_(this, &PepperUDPSocket::OnRecvFromCompleted)), - ALLOW_THIS_IN_INITIALIZER_LIST( - sendto_callback_(this, &PepperUDPSocket::OnSendToCompleted)) { + socket_id_(socket_id) { DCHECK(manager); } @@ -63,10 +59,10 @@ void PepperUDPSocket::RecvFrom(int32_t num_bytes) { } recvfrom_buffer_ = new net::IOBuffer(num_bytes); - int result = socket_->RecvFrom(recvfrom_buffer_, - num_bytes, - &recvfrom_address_, - &recvfrom_callback_); + int result = socket_->RecvFrom( + recvfrom_buffer_, num_bytes, &recvfrom_address_, + base::Bind(&PepperUDPSocket::OnRecvFromCompleted, + base::Unretained(this))); if (result != net::ERR_IO_PENDING) OnRecvFromCompleted(result); @@ -89,10 +85,9 @@ void PepperUDPSocket::SendTo(const std::string& data, sendto_buffer_ = new net::IOBuffer(data_size); memcpy(sendto_buffer_->data(), data.data(), data_size); - int result = socket_->SendTo(sendto_buffer_, - data_size, - address, - &sendto_callback_); + int result = socket_->SendTo( + sendto_buffer_, data_size, address, + base::Bind(&PepperUDPSocket::OnSendToCompleted, base::Unretained(this))); if (result != net::ERR_IO_PENDING) OnSendToCompleted(result); diff --git a/content/browser/renderer_host/pepper_udp_socket.h b/content/browser/renderer_host/pepper_udp_socket.h index 83dad8c..ff3e6e8 100644 --- a/content/browser/renderer_host/pepper_udp_socket.h +++ b/content/browser/renderer_host/pepper_udp_socket.h @@ -50,9 +50,6 @@ class PepperUDPSocket { uint32 plugin_dispatcher_id_; uint32 socket_id_; - net::OldCompletionCallbackImpl<PepperUDPSocket> recvfrom_callback_; - net::OldCompletionCallbackImpl<PepperUDPSocket> sendto_callback_; - scoped_ptr<net::UDPServerSocket> socket_; scoped_refptr<net::IOBuffer> recvfrom_buffer_; diff --git a/content/renderer/p2p/p2p_transport_impl.cc b/content/renderer/p2p/p2p_transport_impl.cc index c6dc8f2..b61d616 100644 --- a/content/renderer/p2p/p2p_transport_impl.cc +++ b/content/renderer/p2p/p2p_transport_impl.cc @@ -24,9 +24,7 @@ P2PTransportImpl::P2PTransportImpl( event_handler_(NULL), state_(STATE_NONE), network_manager_(network_manager), - socket_factory_(socket_factory), - ALLOW_THIS_IN_INITIALIZER_LIST(connect_callback_( - this, &P2PTransportImpl::OnTcpConnected)) { + socket_factory_(socket_factory) { } P2PTransportImpl::P2PTransportImpl(P2PSocketDispatcher* socket_dispatcher) @@ -34,9 +32,7 @@ P2PTransportImpl::P2PTransportImpl(P2PSocketDispatcher* socket_dispatcher) event_handler_(NULL), state_(STATE_NONE), network_manager_(new IpcNetworkManager(socket_dispatcher)), - socket_factory_(new IpcPacketSocketFactory(socket_dispatcher)), - ALLOW_THIS_IN_INITIALIZER_LIST(connect_callback_( - this, &P2PTransportImpl::OnTcpConnected)) { + socket_factory_(new IpcPacketSocketFactory(socket_dispatcher)) { DCHECK(socket_dispatcher); } @@ -101,7 +97,8 @@ bool P2PTransportImpl::Init(WebKit::WebFrame* web_frame, if (config.tcp_ack_delay_ms > 0) pseudo_tcp_adapter_->SetAckDelay(config.tcp_ack_delay_ms); - int result = pseudo_tcp_adapter_->Connect(&connect_callback_); + int result = pseudo_tcp_adapter_->Connect( + base::Bind(&P2PTransportImpl::OnTcpConnected, base::Unretained(this))); if (result != net::ERR_IO_PENDING) OnTcpConnected(result); } diff --git a/content/renderer/p2p/p2p_transport_impl.h b/content/renderer/p2p/p2p_transport_impl.h index a22a991..70e8422 100644 --- a/content/renderer/p2p/p2p_transport_impl.h +++ b/content/renderer/p2p/p2p_transport_impl.h @@ -88,8 +88,6 @@ class CONTENT_EXPORT P2PTransportImpl scoped_ptr<jingle_glue::TransportChannelSocketAdapter> channel_adapter_; scoped_ptr<jingle_glue::PseudoTcpAdapter> pseudo_tcp_adapter_; - net::OldCompletionCallbackImpl<P2PTransportImpl> connect_callback_; - DISALLOW_COPY_AND_ASSIGN(P2PTransportImpl); }; diff --git a/content/renderer/p2p/p2p_transport_impl_unittest.cc b/content/renderer/p2p/p2p_transport_impl_unittest.cc index 6dabbc8d..29bc804 100644 --- a/content/renderer/p2p/p2p_transport_impl_unittest.cc +++ b/content/renderer/p2p/p2p_transport_impl_unittest.cc @@ -51,10 +51,6 @@ class UdpChannelTester : public base::RefCountedThreadSafe<UdpChannelTester> { write_socket_(write_socket), read_socket_(read_socket), done_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_cb_(this, &UdpChannelTester::OnWritten)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_cb_(this, &UdpChannelTester::OnRead)), write_errors_(0), read_errors_(0), packets_sent_(0), @@ -104,7 +100,9 @@ class UdpChannelTester : public base::RefCountedThreadSafe<UdpChannelTester> { // Put index of this packet in the beginning of the packet body. memcpy(packet->data(), &packets_sent_, sizeof(packets_sent_)); - int result = write_socket_->Write(packet, kMessageSize, &write_cb_); + int result = write_socket_->Write(packet, kMessageSize, + base::Bind(&UdpChannelTester::OnWritten, + base::Unretained(this))); HandleWriteResult(result); } @@ -132,7 +130,9 @@ class UdpChannelTester : public base::RefCountedThreadSafe<UdpChannelTester> { int kReadSize = kMessageSize * 2; read_buffer_ = new net::IOBuffer(kReadSize); - result = read_socket_->Read(read_buffer_, kReadSize, &read_cb_); + result = read_socket_->Read(read_buffer_, kReadSize, + base::Bind(&UdpChannelTester::OnRead, + base::Unretained(this))); HandleReadResult(result); }; } @@ -179,8 +179,6 @@ class UdpChannelTester : public base::RefCountedThreadSafe<UdpChannelTester> { scoped_refptr<net::IOBuffer> sent_packets_[kMessages]; scoped_refptr<net::IOBuffer> read_buffer_; - net::OldCompletionCallbackImpl<UdpChannelTester> write_cb_; - net::OldCompletionCallbackImpl<UdpChannelTester> read_cb_; int write_errors_; int read_errors_; int packets_sent_; @@ -197,10 +195,6 @@ class TcpChannelTester : public base::RefCountedThreadSafe<TcpChannelTester> { write_socket_(write_socket), read_socket_(read_socket), done_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_cb_(this, &TcpChannelTester::OnWritten)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_cb_(this, &TcpChannelTester::OnRead)), write_errors_(0), read_errors_(0) { } @@ -250,7 +244,8 @@ class TcpChannelTester : public base::RefCountedThreadSafe<TcpChannelTester> { } int result = write_socket_->Write( - send_buffer_, send_buffer_->BytesRemaining(), &write_cb_); + send_buffer_, send_buffer_->BytesRemaining(), + base::Bind(&TcpChannelTester::OnWritten, base::Unretained(this))); HandleWriteResult(result); } @@ -277,7 +272,9 @@ class TcpChannelTester : public base::RefCountedThreadSafe<TcpChannelTester> { int kReadSize = kMessageSize * 2; read_buffer_ = new net::IOBuffer(kReadSize); - result = read_socket_->Read(read_buffer_, kReadSize, &read_cb_); + result = read_socket_->Read( + read_buffer_, kReadSize, + base::Bind(&TcpChannelTester::OnRead, base::Unretained(this))); HandleReadResult(result); }; } @@ -315,8 +312,6 @@ class TcpChannelTester : public base::RefCountedThreadSafe<TcpChannelTester> { std::vector<char> sent_data_; std::vector<char> received_data_; - net::OldCompletionCallbackImpl<TcpChannelTester> write_cb_; - net::OldCompletionCallbackImpl<TcpChannelTester> read_cb_; int write_errors_; int read_errors_; }; diff --git a/jingle/glue/channel_socket_adapter.cc b/jingle/glue/channel_socket_adapter.cc index 3e22a35..ae7150a 100644 --- a/jingle/glue/channel_socket_adapter.cc +++ b/jingle/glue/channel_socket_adapter.cc @@ -18,8 +18,6 @@ TransportChannelSocketAdapter::TransportChannelSocketAdapter( cricket::TransportChannel* channel) : message_loop_(MessageLoop::current()), channel_(channel), - old_read_callback_(NULL), - write_callback_(NULL), closed_error_code_(net::OK) { DCHECK(channel_); @@ -35,30 +33,13 @@ TransportChannelSocketAdapter::~TransportChannelSocketAdapter() { } int TransportChannelSocketAdapter::Read( - net::IOBuffer* buf, int buffer_size, net::OldCompletionCallback* callback) { - DCHECK_EQ(MessageLoop::current(), message_loop_); - DCHECK(buf); - DCHECK(callback); - CHECK(!old_read_callback_ && read_callback_.is_null()); - - if (!channel_) { - DCHECK(closed_error_code_ != net::OK); - return closed_error_code_; - } - - old_read_callback_ = callback; - read_buffer_ = buf; - read_buffer_size_ = buffer_size; - - return net::ERR_IO_PENDING; -} -int TransportChannelSocketAdapter::Read( - net::IOBuffer* buf, int buffer_size, + net::IOBuffer* buf, + int buffer_size, const net::CompletionCallback& callback) { DCHECK_EQ(MessageLoop::current(), message_loop_); DCHECK(buf); DCHECK(!callback.is_null()); - CHECK(!old_read_callback_ && read_callback_.is_null()); + CHECK(read_callback_.is_null()); if (!channel_) { DCHECK(closed_error_code_ != net::OK); @@ -73,11 +54,13 @@ int TransportChannelSocketAdapter::Read( } int TransportChannelSocketAdapter::Write( - net::IOBuffer* buffer, int buffer_size, net::OldCompletionCallback* callback) { + net::IOBuffer* buffer, + int buffer_size, + const net::CompletionCallback& callback) { DCHECK_EQ(MessageLoop::current(), message_loop_); DCHECK(buffer); - DCHECK(callback); - CHECK(!write_callback_); + DCHECK(!callback.is_null()); + CHECK(write_callback_.is_null()); if (!channel_) { DCHECK(closed_error_code_ != net::OK); @@ -129,23 +112,18 @@ void TransportChannelSocketAdapter::Close(int error_code) { channel_->SignalDestroyed.disconnect(this); channel_ = NULL; - if (old_read_callback_) { - net::OldCompletionCallback* callback = old_read_callback_; - old_read_callback_ = NULL; - read_buffer_ = NULL; - callback->Run(error_code); - } else if (!read_callback_.is_null()) { + if (!read_callback_.is_null()) { net::CompletionCallback callback = read_callback_; read_callback_.Reset(); read_buffer_ = NULL; callback.Run(error_code); } - if (write_callback_) { - net::OldCompletionCallback* callback = write_callback_; - write_callback_ = NULL; + if (!write_callback_.is_null()) { + net::CompletionCallback callback = write_callback_; + write_callback_.Reset(); write_buffer_ = NULL; - callback->Run(error_code); + callback.Run(error_code); } } @@ -153,7 +131,7 @@ void TransportChannelSocketAdapter::OnNewPacket( cricket::TransportChannel* channel, const char* data, size_t data_size) { DCHECK_EQ(MessageLoop::current(), message_loop_); DCHECK_EQ(channel, channel_); - if (old_read_callback_ || !read_callback_.is_null()) { + if (!read_callback_.is_null()) { DCHECK(read_buffer_); CHECK_LT(data_size, static_cast<size_t>(std::numeric_limits<int>::max())); @@ -165,17 +143,11 @@ void TransportChannelSocketAdapter::OnNewPacket( memcpy(read_buffer_->data(), data, data_size); - if (old_read_callback_) { - net::OldCompletionCallback* callback = old_read_callback_; - old_read_callback_ = NULL; - read_buffer_ = NULL; - callback->Run(data_size); - } else { - net::CompletionCallback callback = read_callback_; - read_callback_.Reset(); - read_buffer_ = NULL; - callback.Run(data_size); - } + net::CompletionCallback callback = read_callback_; + read_callback_.Reset(); + read_buffer_ = NULL; + + callback.Run(data_size); } else { LOG(WARNING) << "Data was received without a callback. Dropping the packet."; @@ -186,17 +158,17 @@ void TransportChannelSocketAdapter::OnWritableState( cricket::TransportChannel* channel) { DCHECK_EQ(MessageLoop::current(), message_loop_); // Try to send the packet if there is a pending write. - if (write_callback_) { + if (!write_callback_.is_null()) { int result = channel_->SendPacket(write_buffer_->data(), write_buffer_size_); if (result < 0) result = net::MapSystemError(channel_->GetError()); if (result != net::ERR_IO_PENDING) { - net::OldCompletionCallback* callback = write_callback_; - write_callback_ = NULL; + net::CompletionCallback callback = write_callback_; + write_callback_.Reset(); write_buffer_ = NULL; - callback->Run(result); + callback.Run(result); } } } diff --git a/jingle/glue/channel_socket_adapter.h b/jingle/glue/channel_socket_adapter.h index 1f367e8..b90ad84 100644 --- a/jingle/glue/channel_socket_adapter.h +++ b/jingle/glue/channel_socket_adapter.h @@ -6,7 +6,6 @@ #define JINGLE_GLUE_CHANNEL_SOCKET_ADAPTER_H_ #include "base/compiler_specific.h" -#include "net/base/completion_callback.h" #include "net/socket/socket.h" #include "third_party/libjingle/source/talk/base/socketaddress.h" #include "third_party/libjingle/source/talk/base/sigslot.h" @@ -36,11 +35,9 @@ class TransportChannelSocketAdapter : public net::Socket, // Socket implementation. virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; @@ -55,12 +52,11 @@ class TransportChannelSocketAdapter : public net::Socket, cricket::TransportChannel* channel_; - net::OldCompletionCallback* old_read_callback_; // Not owned. net::CompletionCallback read_callback_; scoped_refptr<net::IOBuffer> read_buffer_; int read_buffer_size_; - net::OldCompletionCallback* write_callback_; // Not owned. + net::CompletionCallback write_callback_; scoped_refptr<net::IOBuffer> write_buffer_; int write_buffer_size_; diff --git a/jingle/glue/channel_socket_adapter_unittest.cc b/jingle/glue/channel_socket_adapter_unittest.cc index e8b34b5..7f5f3ee 100644 --- a/jingle/glue/channel_socket_adapter_unittest.cc +++ b/jingle/glue/channel_socket_adapter_unittest.cc @@ -44,7 +44,8 @@ class TransportChannelSocketAdapterTest : public testing::Test { public: TransportChannelSocketAdapterTest() : ALLOW_THIS_IN_INITIALIZER_LIST( - callback_(this, &TransportChannelSocketAdapterTest::Callback)), + callback_(base::Bind(&TransportChannelSocketAdapterTest::Callback, + base::Unretained(this)))), callback_result_(0) { } @@ -59,7 +60,7 @@ class TransportChannelSocketAdapterTest : public testing::Test { MockTransportChannel channel_; scoped_ptr<TransportChannelSocketAdapter> target_; - net::OldCompletionCallbackImpl<TransportChannelSocketAdapterTest> callback_; + net::CompletionCallback callback_; int callback_result_; MessageLoopForIO message_loop_; }; @@ -68,7 +69,7 @@ class TransportChannelSocketAdapterTest : public testing::Test { TEST_F(TransportChannelSocketAdapterTest, Read) { scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); - int result = target_->Read(buffer, kBufferSize, &callback_); + int result = target_->Read(buffer, kBufferSize, callback_); ASSERT_EQ(net::ERR_IO_PENDING, result); channel_.SignalReadPacket(&channel_, kTestData, kTestDataSize); @@ -79,14 +80,14 @@ TEST_F(TransportChannelSocketAdapterTest, Read) { TEST_F(TransportChannelSocketAdapterTest, ReadClose) { scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); - int result = target_->Read(buffer, kBufferSize, &callback_); + int result = target_->Read(buffer, kBufferSize, callback_); ASSERT_EQ(net::ERR_IO_PENDING, result); target_->Close(kTestError); EXPECT_EQ(kTestError, callback_result_); // All Read() calls after Close() should return the error. - EXPECT_EQ(kTestError, target_->Read(buffer, kBufferSize, &callback_)); + EXPECT_EQ(kTestError, target_->Read(buffer, kBufferSize, callback_)); } // Verify that Write sends the packet and returns correct result. @@ -96,7 +97,7 @@ TEST_F(TransportChannelSocketAdapterTest, Write) { EXPECT_CALL(channel_, SendPacket(buffer->data(), kTestDataSize)) .WillOnce(Return(kTestDataSize)); - int result = target_->Write(buffer, kTestDataSize, &callback_); + int result = target_->Write(buffer, kTestDataSize, callback_); EXPECT_EQ(kTestDataSize, result); } @@ -112,7 +113,7 @@ TEST_F(TransportChannelSocketAdapterTest, WritePending) { EXPECT_CALL(channel_, GetError()) .WillOnce(Return(EWOULDBLOCK)); - int result = target_->Write(buffer, kTestDataSize, &callback_); + int result = target_->Write(buffer, kTestDataSize, callback_); ASSERT_EQ(net::OK, result); } diff --git a/jingle/glue/pseudotcp_adapter.cc b/jingle/glue/pseudotcp_adapter.cc index 7807eed..cb6ed91 100644 --- a/jingle/glue/pseudotcp_adapter.cc +++ b/jingle/glue/pseudotcp_adapter.cc @@ -30,12 +30,9 @@ class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, // Functions used to implement net::StreamSocket. int Read(net::IOBuffer* buffer, int buffer_size, - net::OldCompletionCallback* callback); - int Read(net::IOBuffer* buffer, int buffer_size, const net::CompletionCallback& callback); int Write(net::IOBuffer* buffer, int buffer_size, - net::OldCompletionCallback* callback); - int Connect(net::OldCompletionCallback* callback); + const net::CompletionCallback& callback); int Connect(const net::CompletionCallback& callback); void Disconnect(); bool IsConnected() const; @@ -71,11 +68,9 @@ class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, // This re-sets |timer| without triggering callbacks. void AdjustClock(); - net::OldCompletionCallback* old_connect_callback_; net::CompletionCallback connect_callback_; - net::OldCompletionCallback* old_read_callback_; net::CompletionCallback read_callback_; - net::OldCompletionCallback* write_callback_; + net::CompletionCallback write_callback_; cricket::PseudoTcp pseudo_tcp_; scoped_ptr<net::Socket> socket_; @@ -88,9 +83,6 @@ class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, bool socket_write_pending_; scoped_refptr<net::IOBuffer> socket_read_buffer_; - net::OldCompletionCallbackImpl<Core> socket_read_callback_; - net::OldCompletionCallbackImpl<Core> socket_write_callback_; - base::OneShotTimer<Core> timer_; DISALLOW_COPY_AND_ASSIGN(Core); @@ -98,16 +90,9 @@ class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, PseudoTcpAdapter::Core::Core(net::Socket* socket) - : old_connect_callback_(NULL), - old_read_callback_(NULL), - write_callback_(NULL), - ALLOW_THIS_IN_INITIALIZER_LIST(pseudo_tcp_(this, 0)), + : ALLOW_THIS_IN_INITIALIZER_LIST(pseudo_tcp_(this, 0)), socket_(socket), - socket_write_pending_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - socket_read_callback_(this, &PseudoTcpAdapter::Core::OnRead)), - ALLOW_THIS_IN_INITIALIZER_LIST( - socket_write_callback_(this, &PseudoTcpAdapter::Core::OnWritten)) { + socket_write_pending_(false) { // Doesn't trigger callbacks. pseudo_tcp_.NotifyMTU(kDefaultMtu); } @@ -116,31 +101,8 @@ PseudoTcpAdapter::Core::~Core() { } int PseudoTcpAdapter::Core::Read(net::IOBuffer* buffer, int buffer_size, - net::OldCompletionCallback* callback) { - DCHECK(!old_read_callback_ && read_callback_.is_null()); - - // Reference the Core in case a callback deletes the adapter. - scoped_refptr<Core> core(this); - - int result = pseudo_tcp_.Recv(buffer->data(), buffer_size); - if (result < 0) { - result = net::MapSystemError(pseudo_tcp_.GetError()); - DCHECK(result < 0); - } - - if (result == net::ERR_IO_PENDING) { - read_buffer_ = buffer; - read_buffer_size_ = buffer_size; - old_read_callback_ = callback; - } - - AdjustClock(); - - return result; -} -int PseudoTcpAdapter::Core::Read(net::IOBuffer* buffer, int buffer_size, const net::CompletionCallback& callback) { - DCHECK(!old_read_callback_ && read_callback_.is_null()); + DCHECK(read_callback_.is_null()); // Reference the Core in case a callback deletes the adapter. scoped_refptr<Core> core(this); @@ -163,8 +125,8 @@ int PseudoTcpAdapter::Core::Read(net::IOBuffer* buffer, int buffer_size, } int PseudoTcpAdapter::Core::Write(net::IOBuffer* buffer, int buffer_size, - net::OldCompletionCallback* callback) { - DCHECK(!write_callback_); + const net::CompletionCallback& callback) { + DCHECK(write_callback_.is_null()); // Reference the Core in case a callback deletes the adapter. scoped_refptr<Core> core(this); @@ -186,26 +148,6 @@ int PseudoTcpAdapter::Core::Write(net::IOBuffer* buffer, int buffer_size, return result; } -int PseudoTcpAdapter::Core::Connect(net::OldCompletionCallback* callback) { - DCHECK_EQ(pseudo_tcp_.State(), cricket::PseudoTcp::TCP_LISTEN); - - // Reference the Core in case a callback deletes the adapter. - scoped_refptr<Core> core(this); - - // Start the connection attempt. - int result = pseudo_tcp_.Connect(); - if (result < 0) - return net::ERR_FAILED; - - AdjustClock(); - - old_connect_callback_ = callback; - connect_callback_.Reset(); - DoReadFromSocket(); - - return net::ERR_IO_PENDING; -} - int PseudoTcpAdapter::Core::Connect(const net::CompletionCallback& callback) { DCHECK_EQ(pseudo_tcp_.State(), cricket::PseudoTcp::TCP_LISTEN); @@ -219,7 +161,6 @@ int PseudoTcpAdapter::Core::Connect(const net::CompletionCallback& callback) { AdjustClock(); - old_connect_callback_ = NULL; connect_callback_ = callback; DoReadFromSocket(); @@ -228,12 +169,10 @@ int PseudoTcpAdapter::Core::Connect(const net::CompletionCallback& callback) { void PseudoTcpAdapter::Core::Disconnect() { // Don't dispatch outstanding callbacks, as mandated by net::StreamSocket. - old_read_callback_ = NULL; read_callback_.Reset(); read_buffer_ = NULL; - write_callback_ = NULL; + write_callback_.Reset(); write_buffer_ = NULL; - old_connect_callback_ = NULL; connect_callback_.Reset(); // TODO(wez): Connect should succeed if called after Disconnect, which @@ -253,11 +192,7 @@ bool PseudoTcpAdapter::Core::IsConnected() const { void PseudoTcpAdapter::Core::OnTcpOpen(PseudoTcp* tcp) { DCHECK(tcp == &pseudo_tcp_); - if (old_connect_callback_) { - net::OldCompletionCallback* callback = old_connect_callback_; - old_connect_callback_ = NULL; - callback->Run(net::OK); - } else if (!connect_callback_.is_null()) { + if (!connect_callback_.is_null()) { net::CompletionCallback callback = connect_callback_; connect_callback_.Reset(); callback.Run(net::OK); @@ -269,7 +204,7 @@ void PseudoTcpAdapter::Core::OnTcpOpen(PseudoTcp* tcp) { void PseudoTcpAdapter::Core::OnTcpReadable(PseudoTcp* tcp) { DCHECK_EQ(tcp, &pseudo_tcp_); - if (!old_read_callback_ && read_callback_.is_null()) + if (read_callback_.is_null()) return; int result = pseudo_tcp_.Recv(read_buffer_->data(), read_buffer_size_); @@ -282,22 +217,15 @@ void PseudoTcpAdapter::Core::OnTcpReadable(PseudoTcp* tcp) { AdjustClock(); - if (old_read_callback_) { - net::OldCompletionCallback* callback = old_read_callback_; - old_read_callback_ = NULL; - read_buffer_ = NULL; - callback->Run(result); - } else { - net::CompletionCallback callback = read_callback_; - read_callback_.Reset(); - read_buffer_ = NULL; - callback.Run(result); - } + net::CompletionCallback callback = read_callback_; + read_callback_.Reset(); + read_buffer_ = NULL; + callback.Run(result); } void PseudoTcpAdapter::Core::OnTcpWriteable(PseudoTcp* tcp) { DCHECK_EQ(tcp, &pseudo_tcp_); - if (!write_callback_) + if (write_callback_.is_null()) return; int result = pseudo_tcp_.Send(write_buffer_->data(), write_buffer_size_); @@ -310,39 +238,31 @@ void PseudoTcpAdapter::Core::OnTcpWriteable(PseudoTcp* tcp) { AdjustClock(); - net::OldCompletionCallback* callback = write_callback_; - write_callback_ = NULL; + net::CompletionCallback callback = write_callback_; + write_callback_.Reset(); write_buffer_ = NULL; - callback->Run(result); + callback.Run(result); } void PseudoTcpAdapter::Core::OnTcpClosed(PseudoTcp* tcp, uint32 error) { DCHECK_EQ(tcp, &pseudo_tcp_); - if (old_connect_callback_) { - net::OldCompletionCallback* callback = old_connect_callback_; - old_connect_callback_ = NULL; - callback->Run(net::MapSystemError(error)); - } else if (!connect_callback_.is_null()) { + if (!connect_callback_.is_null()) { net::CompletionCallback callback = connect_callback_; connect_callback_.Reset(); callback.Run(net::MapSystemError(error)); } - if (old_read_callback_) { - net::OldCompletionCallback* callback = old_read_callback_; - old_read_callback_ = NULL; - callback->Run(net::MapSystemError(error)); - } else if (!read_callback_.is_null()) { + if (!read_callback_.is_null()) { net::CompletionCallback callback = read_callback_; read_callback_.Reset(); callback.Run(net::MapSystemError(error)); } - if (write_callback_) { - net::OldCompletionCallback* callback = write_callback_; - write_callback_ = NULL; - callback->Run(net::MapSystemError(error)); + if (!write_callback_.is_null()) { + net::CompletionCallback callback = write_callback_; + write_callback_.Reset(); + callback.Run(net::MapSystemError(error)); } } @@ -379,7 +299,9 @@ cricket::IPseudoTcpNotify::WriteResult PseudoTcpAdapter::Core::TcpWritePacket( // Our underlying socket is datagram-oriented, which means it should either // send exactly as many bytes as we requested, or fail. - int result = socket_->Write(write_buffer, len, &socket_write_callback_); + int result = socket_->Write(write_buffer, len, + base::Bind(&PseudoTcpAdapter::Core::OnWritten, + base::Unretained(this))); if (result == net::ERR_IO_PENDING) { socket_write_pending_ = true; return IPseudoTcpNotify::WR_SUCCESS; @@ -398,7 +320,8 @@ void PseudoTcpAdapter::Core::DoReadFromSocket() { while (true) { int result = socket_->Read(socket_read_buffer_, kReadBufferSize, - &socket_read_callback_); + base::Bind(&PseudoTcpAdapter::Core::OnRead, + base::Unretained(this))); if (result == net::ERR_IO_PENDING) break; @@ -465,18 +388,13 @@ PseudoTcpAdapter::~PseudoTcpAdapter() { } int PseudoTcpAdapter::Read(net::IOBuffer* buffer, int buffer_size, - net::OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - return core_->Read(buffer, buffer_size, callback); -} -int PseudoTcpAdapter::Read(net::IOBuffer* buffer, int buffer_size, const net::CompletionCallback& callback) { DCHECK(CalledOnValidThread()); return core_->Read(buffer, buffer_size, callback); } int PseudoTcpAdapter::Write(net::IOBuffer* buffer, int buffer_size, - net::OldCompletionCallback* callback) { + const net::CompletionCallback& callback) { DCHECK(CalledOnValidThread()); return core_->Write(buffer, buffer_size, callback); } @@ -495,16 +413,6 @@ bool PseudoTcpAdapter::SetSendBufferSize(int32 size) { return false; } -int PseudoTcpAdapter::Connect(net::OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - - // net::StreamSocket requires that Connect return OK if already connected. - if (IsConnected()) - return net::OK; - - return core_->Connect(callback); -} - int PseudoTcpAdapter::Connect(const net::CompletionCallback& callback) { DCHECK(CalledOnValidThread()); diff --git a/jingle/glue/pseudotcp_adapter.h b/jingle/glue/pseudotcp_adapter.h index f0d27ee..91811e9 100644 --- a/jingle/glue/pseudotcp_adapter.h +++ b/jingle/glue/pseudotcp_adapter.h @@ -31,16 +31,13 @@ class PseudoTcpAdapter : public net::StreamSocket, base::NonThreadSafe { // net::Socket implementation. virtual int Read(net::IOBuffer* buffer, int buffer_size, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buffer, int buffer_size, const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buffer, int buffer_size, - net::OldCompletionCallback* callback) OVERRIDE; + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; // net::StreamSocket implementation. - virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; diff --git a/jingle/glue/pseudotcp_adapter_unittest.cc b/jingle/glue/pseudotcp_adapter_unittest.cc index 6d803a5..7ee217a 100644 --- a/jingle/glue/pseudotcp_adapter_unittest.cc +++ b/jingle/glue/pseudotcp_adapter_unittest.cc @@ -10,7 +10,6 @@ #include "base/bind_helpers.h" #include "base/compiler_specific.h" #include "jingle/glue/thread_wrapper.h" -#include "net/base/completion_callback.h" #include "net/base/io_buffer.h" #include "net/base/net_errors.h" #include "net/base/test_completion_callback.h" @@ -81,8 +80,7 @@ class LeakyBucket : public RateLimiter { class FakeSocket : public net::Socket { public: FakeSocket() - : old_read_callback_(NULL), - rate_limiter_(NULL), + : rate_limiter_(NULL), latency_ms_(0) { } virtual ~FakeSocket() { } @@ -91,20 +89,13 @@ class FakeSocket : public net::Socket { if (rate_limiter_ && rate_limiter_->DropNextPacket()) return; // Lose the packet. - if (old_read_callback_ || !read_callback_.is_null()) { + if (!read_callback_.is_null()) { int size = std::min(read_buffer_size_, static_cast<int>(data.size())); memcpy(read_buffer_->data(), &data[0], data.size()); - if (old_read_callback_) { - net::OldCompletionCallback* cb = old_read_callback_; - old_read_callback_ = NULL; - read_buffer_ = NULL; - cb->Run(size); - } else { - net::CompletionCallback cb = read_callback_; - read_callback_.Reset(); - read_buffer_ = NULL; - cb.Run(size); - } + net::CompletionCallback cb = read_callback_; + read_callback_.Reset(); + read_buffer_ = NULL; + cb.Run(size); } else { incoming_packets_.push_back(data); } @@ -120,29 +111,10 @@ class FakeSocket : public net::Socket { void set_latency(int latency_ms) { latency_ms_ = latency_ms; }; - // net::Socket implementation. - virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { - CHECK(!old_read_callback_ && read_callback_.is_null()); - CHECK(buf); - - if (incoming_packets_.size() > 0) { - scoped_refptr<net::IOBuffer> buffer(buf); - int size = std::min( - static_cast<int>(incoming_packets_.front().size()), buf_len); - memcpy(buffer->data(), &*incoming_packets_.front().begin(), size); - incoming_packets_.pop_front(); - return size; - } else { - old_read_callback_ = callback; - read_buffer_ = buf; - read_buffer_size_ = buf_len; - return net::ERR_IO_PENDING; - } - } + // net::Socket interface. virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { - CHECK(!old_read_callback_ && read_callback_.is_null()); + CHECK(read_callback_.is_null()); CHECK(buf); if (incoming_packets_.size() > 0) { @@ -161,7 +133,7 @@ class FakeSocket : public net::Socket { } virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE { + const net::CompletionCallback& callback) OVERRIDE { DCHECK(buf); if (peer_socket_) { MessageLoop::current()->PostDelayedTask( @@ -187,7 +159,6 @@ class FakeSocket : public net::Socket { private: scoped_refptr<net::IOBuffer> read_buffer_; int read_buffer_size_; - net::OldCompletionCallback* old_read_callback_; net::CompletionCallback read_callback_; std::deque<std::vector<char> > incoming_packets_; @@ -206,10 +177,6 @@ class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { host_socket_(host_socket), client_socket_(client_socket), done_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_cb_(this, &TCPChannelTester::OnWritten)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_cb_(this, &TCPChannelTester::OnRead)), write_errors_(0), read_errors_(0) { } @@ -264,8 +231,9 @@ class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { int bytes_to_write = std::min(output_buffer_->BytesRemaining(), kMessageSize); - result = client_socket_->Write(output_buffer_, bytes_to_write, - &write_cb_); + result = client_socket_->Write( + output_buffer_, bytes_to_write, + base::Bind(&TCPChannelTester::OnWritten, base::Unretained(this))); HandleWriteResult(result); } } @@ -290,7 +258,9 @@ class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { while (result > 0) { input_buffer_->set_offset(input_buffer_->capacity() - kMessageSize); - result = host_socket_->Read(input_buffer_, kMessageSize, &read_cb_); + result = host_socket_->Read(input_buffer_, kMessageSize, + base::Bind(&TCPChannelTester::OnRead, + base::Unretained(this))); HandleReadResult(result); }; } @@ -324,8 +294,6 @@ class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { scoped_refptr<net::DrainableIOBuffer> output_buffer_; scoped_refptr<net::GrowableIOBuffer> input_buffer_; - net::OldCompletionCallbackImpl<TCPChannelTester> write_cb_; - net::OldCompletionCallbackImpl<TCPChannelTester> read_cb_; int write_errors_; int read_errors_; }; @@ -354,11 +322,11 @@ class PseudoTcpAdapterTest : public testing::Test { }; TEST_F(PseudoTcpAdapterTest, DataTransfer) { - TestOldCompletionCallback host_connect_cb; - TestOldCompletionCallback client_connect_cb; + net::TestCompletionCallback host_connect_cb; + net::TestCompletionCallback client_connect_cb; - int rv1 = host_pseudotcp_->Connect(&host_connect_cb); - int rv2 = client_pseudotcp_->Connect(&client_connect_cb); + int rv1 = host_pseudotcp_->Connect(host_connect_cb.callback()); + int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); if (rv1 == net::ERR_IO_PENDING) rv1 = host_connect_cb.WaitForResult(); @@ -389,11 +357,11 @@ TEST_F(PseudoTcpAdapterTest, LimitedChannel) { host_socket_->set_latency(kLatencyMs); client_socket_->set_rate_limiter(&client_limiter); - TestOldCompletionCallback host_connect_cb; - TestOldCompletionCallback client_connect_cb; + net::TestCompletionCallback host_connect_cb; + net::TestCompletionCallback client_connect_cb; - int rv1 = host_pseudotcp_->Connect(&host_connect_cb); - int rv2 = client_pseudotcp_->Connect(&client_connect_cb); + int rv1 = host_pseudotcp_->Connect(host_connect_cb.callback()); + int rv2 = client_pseudotcp_->Connect(client_connect_cb.callback()); if (rv1 == net::ERR_IO_PENDING) rv1 = host_connect_cb.WaitForResult(); @@ -428,13 +396,12 @@ TEST_F(PseudoTcpAdapterTest, DeleteOnConnected) { // This test verifies that deleting the adapter mid-callback doesn't lead // to deleted structures being touched as the stack unrolls, so the failure // mode is a crash rather than a normal test failure. - TestOldCompletionCallback client_connect_cb; + net::TestCompletionCallback client_connect_cb; DeleteOnConnected host_delete(&message_loop_, &host_pseudotcp_); - net::OldCompletionCallbackImpl<DeleteOnConnected> - host_connect_cb(&host_delete, &DeleteOnConnected::OnConnected); - host_pseudotcp_->Connect(&host_connect_cb); - client_pseudotcp_->Connect(&client_connect_cb); + host_pseudotcp_->Connect(base::Bind(&DeleteOnConnected::OnConnected, + base::Unretained(&host_delete))); + client_pseudotcp_->Connect(client_connect_cb.callback()); message_loop_.Run(); ASSERT_EQ(NULL, host_pseudotcp_.get()); diff --git a/jingle/notifier/base/chrome_async_socket.cc b/jingle/notifier/base/chrome_async_socket.cc index 131f733..e3a243f 100644 --- a/jingle/notifier/base/chrome_async_socket.cc +++ b/jingle/notifier/base/chrome_async_socket.cc @@ -32,15 +32,7 @@ ChromeAsyncSocket::ChromeAsyncSocket( ResolvingClientSocketFactory* client_socket_factory, size_t read_buf_size, size_t write_buf_size) - : connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), - &ChromeAsyncSocket::ProcessConnectDone), - read_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), - &ChromeAsyncSocket::ProcessReadDone), - write_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), - &ChromeAsyncSocket::ProcessWriteDone), - ssl_connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), - &ChromeAsyncSocket::ProcessSSLConnectDone), - client_socket_factory_(client_socket_factory), + : client_socket_factory_(client_socket_factory), state_(STATE_CLOSED), error_(ERROR_NONE), net_error_(net::OK), @@ -119,7 +111,9 @@ bool ChromeAsyncSocket::Connect(const talk_base::SocketAddress& address) { transport_socket_.reset( client_socket_factory_->CreateTransportClientSocket( dest_host_port_pair)); - int status = transport_socket_->Connect(&connect_callback_); + int status = transport_socket_->Connect( + base::Bind(&ChromeAsyncSocket::ProcessConnectDone, + base::Unretained(this))); if (status != net::ERR_IO_PENDING) { // We defer execution of ProcessConnectDone instead of calling it // directly here as the caller may not expect an error/close to @@ -182,7 +176,9 @@ void ChromeAsyncSocket::DoRead() { // done). int status = transport_socket_->Read( - read_buf_.get(), read_buf_->size(), &read_callback_); + read_buf_.get(), read_buf_->size(), + base::Bind(&ChromeAsyncSocket::ProcessReadDone, + base::Unretained(this))); read_state_ = PENDING; if (status != net::ERR_IO_PENDING) { ProcessReadDone(status); @@ -312,7 +308,9 @@ void ChromeAsyncSocket::DoWrite() { // before we send the next message. int status = transport_socket_->Write( - write_buf_.get(), write_end_, &write_callback_); + write_buf_.get(), write_end_, + base::Bind(&ChromeAsyncSocket::ProcessWriteDone, + base::Unretained(this))); write_state_ = PENDING; if (status != net::ERR_IO_PENDING) { ProcessWriteDone(status); @@ -408,7 +406,9 @@ bool ChromeAsyncSocket::StartTls(const std::string& domain_name) { transport_socket_.reset( client_socket_factory_->CreateSSLClientSocket( socket_handle, net::HostPortPair(domain_name, 443))); - int status = transport_socket_->Connect(&ssl_connect_callback_); + int status = transport_socket_->Connect( + base::Bind(&ChromeAsyncSocket::ProcessSSLConnectDone, + base::Unretained(this))); if (status != net::ERR_IO_PENDING) { MessageLoop* message_loop = MessageLoop::current(); CHECK(message_loop); diff --git a/jingle/notifier/base/chrome_async_socket.h b/jingle/notifier/base/chrome_async_socket.h index 6e41a6c..d2df8df 100644 --- a/jingle/notifier/base/chrome_async_socket.h +++ b/jingle/notifier/base/chrome_async_socket.h @@ -176,12 +176,6 @@ class ChromeAsyncSocket : public buzz::AsyncSocket { // Close functions. void DoClose(); - // Callbacks passed to |transport_socket_|. - net::OldCompletionCallbackImpl<ChromeAsyncSocket> connect_callback_; - net::OldCompletionCallbackImpl<ChromeAsyncSocket> read_callback_; - net::OldCompletionCallbackImpl<ChromeAsyncSocket> write_callback_; - net::OldCompletionCallbackImpl<ChromeAsyncSocket> ssl_connect_callback_; - scoped_ptr<ResolvingClientSocketFactory> client_socket_factory_; // buzz::AsyncSocket state. diff --git a/jingle/notifier/base/fake_ssl_client_socket.cc b/jingle/notifier/base/fake_ssl_client_socket.cc index bdad879..922bda4 100644 --- a/jingle/notifier/base/fake_ssl_client_socket.cc +++ b/jingle/notifier/base/fake_ssl_client_socket.cc @@ -78,18 +78,9 @@ base::StringPiece FakeSSLClientSocket::GetSslServerHello() { FakeSSLClientSocket::FakeSSLClientSocket( net::StreamSocket* transport_socket) - : connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), - &FakeSSLClientSocket::OnConnectDone), - send_client_hello_callback_( - ALLOW_THIS_IN_INITIALIZER_LIST(this), - &FakeSSLClientSocket::OnSendClientHelloDone), - verify_server_hello_callback_( - ALLOW_THIS_IN_INITIALIZER_LIST(this), - &FakeSSLClientSocket::OnVerifyServerHelloDone), - transport_socket_(transport_socket), + : transport_socket_(transport_socket), next_handshake_state_(STATE_NONE), handshake_completed_(false), - old_user_connect_callback_(NULL), write_buf_(NewDrainableIOBufferWithSize(arraysize(kSslClientHello))), read_buf_(NewDrainableIOBufferWithSize(arraysize(kSslServerHello))) { CHECK(transport_socket_.get()); @@ -99,12 +90,6 @@ FakeSSLClientSocket::FakeSSLClientSocket( FakeSSLClientSocket::~FakeSSLClientSocket() {} int FakeSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { - DCHECK_EQ(next_handshake_state_, STATE_NONE); - DCHECK(handshake_completed_); - return transport_socket_->Read(buf, buf_len, callback); -} -int FakeSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { DCHECK_EQ(next_handshake_state_, STATE_NONE); DCHECK(handshake_completed_); @@ -112,7 +97,7 @@ int FakeSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, } int FakeSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { + const net::CompletionCallback& callback) { DCHECK_EQ(next_handshake_state_, STATE_NONE); DCHECK(handshake_completed_); return transport_socket_->Write(buf, buf_len, callback); @@ -126,23 +111,6 @@ bool FakeSSLClientSocket::SetSendBufferSize(int32 size) { return transport_socket_->SetSendBufferSize(size); } -int FakeSSLClientSocket::Connect(net::OldCompletionCallback* callback) { - // We don't support synchronous operation, even if - // |transport_socket_| does. - DCHECK(callback); - DCHECK_EQ(next_handshake_state_, STATE_NONE); - DCHECK(!handshake_completed_); - DCHECK(!old_user_connect_callback_); - DCHECK_EQ(write_buf_->BytesConsumed(), 0); - DCHECK_EQ(read_buf_->BytesConsumed(), 0); - - next_handshake_state_ = STATE_CONNECT; - int status = DoHandshakeLoop(); - if (status == net::ERR_IO_PENDING) { - old_user_connect_callback_ = callback; - } - return status; -} int FakeSSLClientSocket::Connect(const net::CompletionCallback& callback) { // We don't support synchronous operation, even if // |transport_socket_| does. @@ -190,16 +158,9 @@ int FakeSSLClientSocket::DoHandshakeLoop() { void FakeSSLClientSocket::RunUserConnectCallback(int status) { DCHECK_LE(status, net::OK); next_handshake_state_ = STATE_NONE; - if (old_user_connect_callback_) { - net::OldCompletionCallback* user_connect_callback = - old_user_connect_callback_; - old_user_connect_callback_ = NULL; - user_connect_callback->Run(status); - } else { - net::CompletionCallback user_connect_callback = user_connect_callback_; - user_connect_callback_.Reset(); - user_connect_callback.Run(status); - } + net::CompletionCallback user_connect_callback = user_connect_callback_; + user_connect_callback_.Reset(); + user_connect_callback.Run(status); } void FakeSSLClientSocket::DoHandshakeLoopWithUserConnectCallback() { @@ -210,7 +171,8 @@ void FakeSSLClientSocket::DoHandshakeLoopWithUserConnectCallback() { } int FakeSSLClientSocket::DoConnect() { - int status = transport_socket_->Connect(&connect_callback_); + int status = transport_socket_->Connect( + base::Bind(&FakeSSLClientSocket::OnConnectDone, base::Unretained(this))); if (status != net::OK) { return status; } @@ -221,7 +183,7 @@ int FakeSSLClientSocket::DoConnect() { void FakeSSLClientSocket::OnConnectDone(int status) { DCHECK_NE(status, net::ERR_IO_PENDING); DCHECK_LE(status, net::OK); - DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); + DCHECK(!user_connect_callback_.is_null()); if (status != net::OK) { RunUserConnectCallback(status); return; @@ -239,7 +201,8 @@ void FakeSSLClientSocket::ProcessConnectDone() { int FakeSSLClientSocket::DoSendClientHello() { int status = transport_socket_->Write( write_buf_, write_buf_->BytesRemaining(), - &send_client_hello_callback_); + base::Bind(&FakeSSLClientSocket::OnSendClientHelloDone, + base::Unretained(this))); if (status < net::OK) { return status; } @@ -249,7 +212,7 @@ int FakeSSLClientSocket::DoSendClientHello() { void FakeSSLClientSocket::OnSendClientHelloDone(int status) { DCHECK_NE(status, net::ERR_IO_PENDING); - DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); + DCHECK(!user_connect_callback_.is_null()); if (status < net::OK) { RunUserConnectCallback(status); return; @@ -272,7 +235,8 @@ void FakeSSLClientSocket::ProcessSendClientHelloDone(size_t written) { int FakeSSLClientSocket::DoVerifyServerHello() { int status = transport_socket_->Read( read_buf_, read_buf_->BytesRemaining(), - &verify_server_hello_callback_); + base::Bind(&FakeSSLClientSocket::OnVerifyServerHelloDone, + base::Unretained(this))); if (status < net::OK) { return status; } @@ -282,7 +246,7 @@ int FakeSSLClientSocket::DoVerifyServerHello() { void FakeSSLClientSocket::OnVerifyServerHelloDone(int status) { DCHECK_NE(status, net::ERR_IO_PENDING); - DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); + DCHECK(!user_connect_callback_.is_null()); if (status < net::OK) { RunUserConnectCallback(status); return; @@ -325,7 +289,6 @@ void FakeSSLClientSocket::Disconnect() { transport_socket_->Disconnect(); next_handshake_state_ = STATE_NONE; handshake_completed_ = false; - old_user_connect_callback_ = NULL; user_connect_callback_.Reset(); write_buf_->SetOffset(0); read_buf_->SetOffset(0); diff --git a/jingle/notifier/base/fake_ssl_client_socket.h b/jingle/notifier/base/fake_ssl_client_socket.h index 623e21b..3e428cd 100644 --- a/jingle/notifier/base/fake_ssl_client_socket.h +++ b/jingle/notifier/base/fake_ssl_client_socket.h @@ -47,14 +47,11 @@ class FakeSSLClientSocket : public net::StreamSocket { // net::StreamSocket implementation. virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; - virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -93,13 +90,6 @@ class FakeSSLClientSocket : public net::StreamSocket { void OnVerifyServerHelloDone(int status); net::Error ProcessVerifyServerHelloDone(size_t read); - // Callbacks passed to |transport_socket_|. - net::OldCompletionCallbackImpl<FakeSSLClientSocket> connect_callback_; - net::OldCompletionCallbackImpl<FakeSSLClientSocket> - send_client_hello_callback_; - net::OldCompletionCallbackImpl<FakeSSLClientSocket> - verify_server_hello_callback_; - scoped_ptr<net::StreamSocket> transport_socket_; // During the handshake process, holds a value from HandshakeState. @@ -110,7 +100,6 @@ class FakeSSLClientSocket : public net::StreamSocket { bool handshake_completed_; // The callback passed to Connect(). - net::OldCompletionCallback* old_user_connect_callback_; net::CompletionCallback user_connect_callback_; scoped_refptr<net::DrainableIOBuffer> write_buf_; diff --git a/jingle/notifier/base/fake_ssl_client_socket_unittest.cc b/jingle/notifier/base/fake_ssl_client_socket_unittest.cc index eb3ba5a..1a3260d 100644 --- a/jingle/notifier/base/fake_ssl_client_socket_unittest.cc +++ b/jingle/notifier/base/fake_ssl_client_socket_unittest.cc @@ -47,13 +47,12 @@ class MockClientSocket : public net::StreamSocket { public: virtual ~MockClientSocket() {} - MOCK_METHOD3(Read, int(net::IOBuffer*, int, net::OldCompletionCallback*)); MOCK_METHOD3(Read, int(net::IOBuffer*, int, const net::CompletionCallback&)); - MOCK_METHOD3(Write, int(net::IOBuffer*, int, net::OldCompletionCallback*)); + MOCK_METHOD3(Write, int(net::IOBuffer*, int, + const net::CompletionCallback&)); MOCK_METHOD1(SetReceiveBufferSize, bool(int32)); MOCK_METHOD1(SetSendBufferSize, bool(int32)); - MOCK_METHOD1(Connect, int(net::OldCompletionCallback*)); MOCK_METHOD1(Connect, int(const net::CompletionCallback&)); MOCK_METHOD0(Disconnect, void()); MOCK_CONST_METHOD0(IsConnected, bool()); @@ -111,7 +110,7 @@ class FakeSSLClientSocketTest : public testing::Test { void ExpectStatus( bool async, int expected_status, int immediate_status, - TestOldCompletionCallback* test_completion_callback) { + net::TestCompletionCallback* test_completion_callback) { if (async) { EXPECT_EQ(net::ERR_IO_PENDING, immediate_status); int status = test_completion_callback->WaitForResult(); @@ -152,8 +151,9 @@ class FakeSSLClientSocketTest : public testing::Test { for (int i = 0; i < num_resets + 1; ++i) { SCOPED_TRACE(i); - TestOldCompletionCallback test_completion_callback; - int status = fake_ssl_client_socket.Connect(&test_completion_callback); + net::TestCompletionCallback test_completion_callback; + int status = fake_ssl_client_socket.Connect( + test_completion_callback.callback()); if (async) { EXPECT_FALSE(fake_ssl_client_socket.IsConnected()); } @@ -164,13 +164,14 @@ class FakeSSLClientSocketTest : public testing::Test { scoped_refptr<net::IOBuffer> read_buf( new net::IOBuffer(read_buf_len)); int read_status = fake_ssl_client_socket.Read( - read_buf, read_buf_len, &test_completion_callback); + read_buf, read_buf_len, test_completion_callback.callback()); ExpectStatus(async, read_len, read_status, &test_completion_callback); scoped_refptr<net::IOBuffer> write_buf( new net::StringIOBuffer(kWriteTestData)); int write_status = fake_ssl_client_socket.Write( - write_buf, arraysize(kWriteTestData), &test_completion_callback); + write_buf, arraysize(kWriteTestData), + test_completion_callback.callback()); ExpectStatus(async, arraysize(kWriteTestData), write_status, &test_completion_callback); } else { @@ -245,8 +246,9 @@ class FakeSSLClientSocketTest : public testing::Test { (error == ERR_MALFORMED_SERVER_HELLO)) ? net::ERR_UNEXPECTED : error; - TestOldCompletionCallback test_completion_callback; - int status = fake_ssl_client_socket.Connect(&test_completion_callback); + net::TestCompletionCallback test_completion_callback; + int status = fake_ssl_client_socket.Connect( + test_completion_callback.callback()); EXPECT_FALSE(fake_ssl_client_socket.IsConnected()); ExpectStatus(async, expected_status, status, &test_completion_callback); EXPECT_FALSE(fake_ssl_client_socket.IsConnected()); diff --git a/jingle/notifier/base/proxy_resolving_client_socket.cc b/jingle/notifier/base/proxy_resolving_client_socket.cc index 3d46ed6b..6314bb2 100644 --- a/jingle/notifier/base/proxy_resolving_client_socket.cc +++ b/jingle/notifier/base/proxy_resolving_client_socket.cc @@ -36,8 +36,7 @@ ProxyResolvingClientSocket::ProxyResolvingClientSocket( net::BoundNetLog::Make( request_context_getter->GetURLRequestContext()->net_log(), net::NetLog::SOURCE_SOCKET)), - ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), - old_user_connect_callback_(NULL) { + ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { DCHECK(request_context_getter); net::URLRequestContext* request_context = request_context_getter->GetURLRequestContext(); @@ -68,13 +67,6 @@ ProxyResolvingClientSocket::~ProxyResolvingClientSocket() { } int ProxyResolvingClientSocket::Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { - if (transport_.get() && transport_->socket()) - return transport_->socket()->Read(buf, buf_len, callback); - NOTREACHED(); - return net::ERR_SOCKET_NOT_CONNECTED; -} -int ProxyResolvingClientSocket::Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { if (transport_.get() && transport_->socket()) return transport_->socket()->Read(buf, buf_len, callback); @@ -82,8 +74,10 @@ int ProxyResolvingClientSocket::Read(net::IOBuffer* buf, int buf_len, return net::ERR_SOCKET_NOT_CONNECTED; } -int ProxyResolvingClientSocket::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { +int ProxyResolvingClientSocket::Write( + net::IOBuffer* buf, + int buf_len, + const net::CompletionCallback& callback) { if (transport_.get() && transport_->socket()) return transport_->socket()->Write(buf, buf_len, callback); NOTREACHED(); @@ -104,36 +98,9 @@ bool ProxyResolvingClientSocket::SetSendBufferSize(int32 size) { return false; } -int ProxyResolvingClientSocket::Connect(net::OldCompletionCallback* callback) { - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); - - tried_direct_connect_fallback_ = false; - - // First we try and resolve the proxy. - GURL url("http://" + dest_host_port_pair_.ToString()); - int status = network_session_->proxy_service()->ResolveProxy( - url, - &proxy_info_, - &proxy_resolve_callback_, - &pac_request_, - bound_net_log_); - if (status != net::ERR_IO_PENDING) { - // We defer execution of ProcessProxyResolveDone instead of calling it - // directly here for simplicity. From the caller's point of view, - // the connect always happens asynchronously. - MessageLoop* message_loop = MessageLoop::current(); - CHECK(message_loop); - message_loop->PostTask( - FROM_HERE, - base::Bind(&ProxyResolvingClientSocket::ProcessProxyResolveDone, - weak_factory_.GetWeakPtr(), status)); - } - old_user_connect_callback_ = callback; - return net::ERR_IO_PENDING; -} int ProxyResolvingClientSocket::Connect( const net::CompletionCallback& callback) { - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); + DCHECK(user_connect_callback_.is_null()); tried_direct_connect_fallback_ = false; @@ -162,16 +129,9 @@ int ProxyResolvingClientSocket::Connect( void ProxyResolvingClientSocket::RunUserConnectCallback(int status) { DCHECK_LE(status, net::OK); - if (old_user_connect_callback_) { - net::OldCompletionCallback* user_connect_callback = - old_user_connect_callback_; - old_user_connect_callback_ = NULL; - user_connect_callback->Run(status); - } else { - net::CompletionCallback user_connect_callback = user_connect_callback_; - user_connect_callback_.Reset(); - user_connect_callback.Run(status); - } + net::CompletionCallback user_connect_callback = user_connect_callback_; + user_connect_callback_.Reset(); + user_connect_callback.Run(status); } // Always runs asynchronously. @@ -330,7 +290,6 @@ void ProxyResolvingClientSocket::Disconnect() { CloseTransportSocket(); if (pac_request_) network_session_->proxy_service()->CancelPacRequest(pac_request_); - old_user_connect_callback_ = NULL; user_connect_callback_.Reset(); } diff --git a/jingle/notifier/base/proxy_resolving_client_socket.h b/jingle/notifier/base/proxy_resolving_client_socket.h index 4463364..68aa718 100644 --- a/jingle/notifier/base/proxy_resolving_client_socket.h +++ b/jingle/notifier/base/proxy_resolving_client_socket.h @@ -47,14 +47,11 @@ class ProxyResolvingClientSocket : public net::StreamSocket { // net::StreamSocket implementation. virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; - virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -98,7 +95,6 @@ class ProxyResolvingClientSocket : public net::StreamSocket { base::WeakPtrFactory<ProxyResolvingClientSocket> weak_factory_; // The callback passed to Connect(). - net::OldCompletionCallback* old_user_connect_callback_; net::CompletionCallback user_connect_callback_; }; diff --git a/jingle/notifier/base/proxy_resolving_client_socket_unittest.cc b/jingle/notifier/base/proxy_resolving_client_socket_unittest.cc index acbac4a..e1f4480 100644 --- a/jingle/notifier/base/proxy_resolving_client_socket_unittest.cc +++ b/jingle/notifier/base/proxy_resolving_client_socket_unittest.cc @@ -74,8 +74,8 @@ TEST_F(ProxyResolvingClientSocketTest, DISABLED_ConnectError) { url_request_context_getter_, net::SSLConfig(), dest); - TestOldCompletionCallback callback; - int status = proxy_resolving_socket.Connect(&callback); + net::TestCompletionCallback callback; + int status = proxy_resolving_socket.Connect(callback.callback()); // Connect always returns ERR_IO_PENDING because it is always asynchronous. EXPECT_EQ(net::ERR_IO_PENDING, status); status = callback.WaitForResult(); @@ -112,8 +112,8 @@ TEST_F(ProxyResolvingClientSocketTest, ReportsBadProxies) { net::SSLConfig(), dest); - TestOldCompletionCallback callback; - int status = proxy_resolving_socket.Connect(&callback); + net::TestCompletionCallback callback; + int status = proxy_resolving_socket.Connect(callback.callback()); EXPECT_EQ(net::ERR_IO_PENDING, status); status = callback.WaitForResult(); EXPECT_EQ(net::OK, status); diff --git a/net/base/net_log.h b/net/base/net_log.h index 5cb8323..dbcf099 100644 --- a/net/base/net_log.h +++ b/net/base/net_log.h @@ -15,11 +15,8 @@ #include "net/base/net_export.h" namespace base { -class Value; -} - -namespace base { class TimeTicks; +class Value; } namespace net { diff --git a/net/curvecp/client_packetizer.cc b/net/curvecp/client_packetizer.cc index 77ddfda..9cafda2 100644 --- a/net/curvecp/client_packetizer.cc +++ b/net/curvecp/client_packetizer.cc @@ -33,12 +33,12 @@ ClientPacketizer::ClientPacketizer() : Packetizer(), next_state_(NONE), listener_(NULL), - old_user_callback_(NULL), current_address_(NULL), hello_attempts_(0), initiate_sent_(false), ALLOW_THIS_IN_INITIALIZER_LIST( - io_callback_(this, &ClientPacketizer::OnIOComplete)), + io_callback_(base::Bind(&ClientPacketizer::OnIOComplete, + base::Unretained(this)))), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { // TODO(mbelshe): Initialize our keys and such properly. // for now we use random values to keep them unique. @@ -51,23 +51,7 @@ ClientPacketizer::~ClientPacketizer() { int ClientPacketizer::Connect(const AddressList& server, Packetizer::Listener* listener, - OldCompletionCallback* callback) { - DCHECK(!old_user_callback_); - DCHECK(!socket_.get()); - DCHECK(!listener_); - - listener_ = listener; - - addresses_ = server; - - old_user_callback_ = callback; - next_state_ = LOOKUP_COOKIE; - - return DoLoop(OK); -} -int ClientPacketizer::Connect(const AddressList& server, - Packetizer::Listener* listener, - const net::CompletionCallback& callback) { + const CompletionCallback& callback) { DCHECK(user_callback_.is_null()); DCHECK(!socket_.get()); DCHECK(!listener_); @@ -85,7 +69,7 @@ int ClientPacketizer::Connect(const AddressList& server, int ClientPacketizer::SendMessage(ConnectionKey key, const char* data, size_t length, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { // We can't send messages smaller than 16 bytes. if (length < 16) return ERR_UNEXPECTED; @@ -114,7 +98,7 @@ int ClientPacketizer::SendMessage(ConnectionKey key, // TODO(mbelshe) - this is just broken to make it work with cleartext memcpy(&buffer->data()[sizeof(InitiatePacket)], data, length); int packet_length = sizeof(InitiatePacket) + length; - int rv = socket_->Write(buffer, packet_length, &io_callback_); + int rv = socket_->Write(buffer, packet_length, io_callback_); if (rv <= 0) return rv; CHECK_EQ(packet_length, rv); // We must send all data. @@ -138,7 +122,7 @@ int ClientPacketizer::SendMessage(ConnectionKey key, // TODO(mbelshe): Fill in rest of message memcpy(&buffer->data()[sizeof(ClientMessagePacket)], data, length); int packet_length = sizeof(ClientMessagePacket) + length; - int rv = socket_->Write(buffer, packet_length, &io_callback_); + int rv = socket_->Write(buffer, packet_length, io_callback_); if (rv <= 0) return rv; CHECK_EQ(packet_length, rv); // We must send all data. @@ -231,7 +215,7 @@ int ClientPacketizer::DoSendingHello() { sizeof(shortterm_public_key_)); // TODO(mbelshe): populate all other fields of the HelloPacket. - return socket_->Write(buffer, sizeof(struct HelloPacket), &io_callback_); + return socket_->Write(buffer, sizeof(struct HelloPacket), io_callback_); } int ClientPacketizer::DoSendingHelloComplete(int rv) { @@ -254,7 +238,7 @@ int ClientPacketizer::DoWaitingCookie() { StartHelloTimer(kHelloTimeoutMs[hello_attempts_++]); read_buffer_ = new IOBuffer(kMaxPacketLength); - return socket_->Read(read_buffer_, kMaxPacketLength, &io_callback_); + return socket_->Read(read_buffer_, kMaxPacketLength, io_callback_); } int ClientPacketizer::DoWaitingCookieComplete(int rv) { @@ -295,17 +279,11 @@ int ClientPacketizer::DoConnected(int rv) { void ClientPacketizer::DoCallback(int result) { DCHECK_NE(result, ERR_IO_PENDING); - DCHECK(old_user_callback_ || !user_callback_.is_null()); - - if (old_user_callback_) { - OldCompletionCallback* callback = old_user_callback_; - old_user_callback_ = NULL; - callback->Run(result); - } else { - CompletionCallback callback = user_callback_; - user_callback_.Reset(); - callback.Run(result); - } + DCHECK(!user_callback_.is_null()); + + CompletionCallback callback = user_callback_; + user_callback_.Reset(); + callback.Run(result); } int ClientPacketizer::ConnectNextAddress() { @@ -391,7 +369,7 @@ int ClientPacketizer::ReadPackets() { while (true) { rv = socket_->Read(read_buffer_, kMaxPacketLength, - &io_callback_); + io_callback_); if (rv <= 0) { if (rv != ERR_IO_PENDING) LOG(ERROR) << "Error reading socket:" << rv; diff --git a/net/curvecp/client_packetizer.h b/net/curvecp/client_packetizer.h index f8edeaf..ac27c19 100644 --- a/net/curvecp/client_packetizer.h +++ b/net/curvecp/client_packetizer.h @@ -29,16 +29,13 @@ class ClientPacketizer : public Packetizer { int Connect(const AddressList& server, Packetizer::Listener* listener, - OldCompletionCallback* callback); - int Connect(const AddressList& server, - Packetizer::Listener* listener, const CompletionCallback& callback); // Packetizer implementation. virtual int SendMessage(ConnectionKey key, const char* data, size_t length, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual void Close(ConnectionKey key) OVERRIDE; virtual int GetPeerAddress(IPEndPoint* endpoint) const OVERRIDE; virtual int max_message_payload() const OVERRIDE; @@ -86,7 +83,6 @@ class ClientPacketizer : public Packetizer { StateType next_state_; scoped_ptr<UDPClientSocket> socket_; Packetizer::Listener* listener_; - OldCompletionCallback* old_user_callback_; CompletionCallback user_callback_; AddressList addresses_; const struct addrinfo* current_address_; @@ -97,7 +93,7 @@ class ClientPacketizer : public Packetizer { uchar shortterm_public_key_[32]; - OldCompletionCallbackImpl<ClientPacketizer> io_callback_; + CompletionCallback io_callback_; base::WeakPtrFactory<ClientPacketizer> weak_factory_; DISALLOW_COPY_AND_ASSIGN(ClientPacketizer); diff --git a/net/curvecp/curvecp_client_socket.cc b/net/curvecp/curvecp_client_socket.cc index 6772e38..5271174 100644 --- a/net/curvecp/curvecp_client_socket.cc +++ b/net/curvecp/curvecp_client_socket.cc @@ -21,11 +21,7 @@ CurveCPClientSocket::CurveCPClientSocket(const AddressList& addresses, CurveCPClientSocket::~CurveCPClientSocket() { } -int CurveCPClientSocket::Connect(OldCompletionCallback* callback) { - return packetizer_.Connect(addresses_, &messenger_, callback); -} - -int CurveCPClientSocket::Connect(const net::CompletionCallback& callback) { +int CurveCPClientSocket::Connect(const CompletionCallback& callback) { return packetizer_.Connect(addresses_, &messenger_, callback); } @@ -102,18 +98,13 @@ base::TimeDelta CurveCPClientSocket::GetConnectTimeMicros() const { int CurveCPClientSocket::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - return messenger_.Read(buf, buf_len, callback); -} -int CurveCPClientSocket::Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) { return messenger_.Read(buf, buf_len, callback); } int CurveCPClientSocket::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return messenger_.Write(buf, buf_len, callback); } diff --git a/net/curvecp/curvecp_client_socket.h b/net/curvecp/curvecp_client_socket.h index 8062c87..7d7a361 100644 --- a/net/curvecp/curvecp_client_socket.h +++ b/net/curvecp/curvecp_client_socket.h @@ -25,9 +25,8 @@ class CurveCPClientSocket : public StreamSocket { const net::NetLog::Source& source); virtual ~CurveCPClientSocket(); - // ClientSocket implementation. - virtual int Connect(OldCompletionCallback* callback) OVERRIDE; - virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; + // ClientSocket methods: + virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; virtual bool IsConnectedAndIdle() const OVERRIDE; @@ -41,16 +40,13 @@ class CurveCPClientSocket : public StreamSocket { virtual int64 NumBytesRead() const OVERRIDE; virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; - // Socket implementation. - virtual int Read(IOBuffer* buf, - int buf_len, - OldCompletionCallback* callback) OVERRIDE; + // Socket methods: virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; diff --git a/net/curvecp/curvecp_server_socket.cc b/net/curvecp/curvecp_server_socket.cc index 6e7934c..d5d5b68 100644 --- a/net/curvecp/curvecp_server_socket.cc +++ b/net/curvecp/curvecp_server_socket.cc @@ -49,18 +49,13 @@ void CurveCPServerSocket::Close() { int CurveCPServerSocket::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - return messenger_.Read(buf, buf_len, callback); -} -int CurveCPServerSocket::Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) { return messenger_.Read(buf, buf_len, callback); } int CurveCPServerSocket::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return messenger_.Write(buf, buf_len, callback); } diff --git a/net/curvecp/curvecp_server_socket.h b/net/curvecp/curvecp_server_socket.h index a2ea1d7..e4df3ef 100644 --- a/net/curvecp/curvecp_server_socket.h +++ b/net/curvecp/curvecp_server_socket.h @@ -36,13 +36,10 @@ class CurveCPServerSocket : public Socket, // Socket implementation. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; diff --git a/net/curvecp/curvecp_transfer_unittest.cc b/net/curvecp/curvecp_transfer_unittest.cc index 1c092f3..11c5d3f 100644 --- a/net/curvecp/curvecp_transfer_unittest.cc +++ b/net/curvecp/curvecp_transfer_unittest.cc @@ -30,8 +30,8 @@ void RunEchoTest(int bytes) { EXPECT_TRUE(server.Start(1234)); HostPortPair server_address("localhost", 1234); - TestOldCompletionCallback cb; - EXPECT_TRUE(client.Start(server_address, bytes, &cb)); + TestCompletionCallback cb; + EXPECT_TRUE(client.Start(server_address, bytes, cb.callback())); int rv = cb.WaitForResult(); EXPECT_EQ(0, rv); diff --git a/net/curvecp/messenger.cc b/net/curvecp/messenger.cc index a99abda..355873b 100644 --- a/net/curvecp/messenger.cc +++ b/net/curvecp/messenger.cc @@ -4,6 +4,7 @@ #include "net/curvecp/messenger.h" +#include "base/bind.h" #include "base/logging.h" #include "base/message_loop.h" #include "net/base/io_buffer.h" @@ -58,39 +59,17 @@ static const size_t kReceiveBufferSize = (128 * 1024); Messenger::Messenger(Packetizer* packetizer) : packetizer_(packetizer), send_buffer_(kSendBufferSize), - send_complete_callback_(NULL), - old_receive_complete_callback_(NULL), pending_receive_length_(0), - send_message_in_progress_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - send_message_callback_(this, &Messenger::OnSendMessageComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { + send_message_in_progress_(false) { } Messenger::~Messenger() { } -int Messenger::Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - DCHECK(!old_receive_complete_callback_ && - receive_complete_callback_.is_null()); - - if (!received_list_.bytes_available()) { - old_receive_complete_callback_ = callback; - pending_receive_ = buf; - pending_receive_length_ = buf_len; - return ERR_IO_PENDING; - } - - int bytes_read = InternalRead(buf, buf_len); - DCHECK_LT(0, bytes_read); - return bytes_read; -} int Messenger::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); - DCHECK(!old_receive_complete_callback_ && - receive_complete_callback_.is_null()); + DCHECK(receive_complete_callback_.is_null()); if (!received_list_.bytes_available()) { receive_complete_callback_ = callback; @@ -104,10 +83,11 @@ int Messenger::Read(IOBuffer* buf, int buf_len, return bytes_read; } -int Messenger::Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback) { +int Messenger::Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); DCHECK(!pending_send_.get()); // Already a write pending! - DCHECK(!send_complete_callback_); + DCHECK(send_complete_callback_.is_null()); DCHECK_LT(0, buf_len); int len = send_buffer_.write(buf->data(), buf_len); @@ -168,15 +148,15 @@ IOBufferWithSize* Messenger::CreateBufferFromSendQueue() { DCHECK_EQ(bytes, length); // We consumed data, check to see if someone is waiting to write more data. - if (send_complete_callback_) { + if (!send_complete_callback_.is_null()) { DCHECK(pending_send_.get()); int len = send_buffer_.write(pending_send_->data(), pending_send_length_); if (len) { pending_send_ = NULL; - OldCompletionCallback* callback = send_complete_callback_; - send_complete_callback_ = NULL; - callback->Run(len); + CompletionCallback callback = send_complete_callback_; + send_complete_callback_.Reset(); + callback.Run(len); } } @@ -274,10 +254,9 @@ void Messenger::SendMessage(int64 position) { sent_list_.MarkBlockSent(position, id); - int rv = packetizer_->SendMessage(key_, - message->data(), - padded_size, - &send_message_callback_); + int rv = packetizer_->SendMessage( + key_, message->data(), padded_size, + base::Bind(&Messenger::OnSendMessageComplete, base::Unretained(this))); if (rv == ERR_IO_PENDING) { send_message_in_progress_ = true; return; @@ -357,19 +336,12 @@ void Messenger::RecvMessage() { // If we have data available, and a read is pending, notify the callback. if (received_list_.bytes_available() && - (old_receive_complete_callback_ || - !receive_complete_callback_.is_null())) { + !receive_complete_callback_.is_null()) { // Pass the data up to the caller. int bytes_read = InternalRead(pending_receive_, pending_receive_length_); - if (old_receive_complete_callback_) { - OldCompletionCallback* callback = old_receive_complete_callback_; - old_receive_complete_callback_ = NULL; - callback->Run(bytes_read); - } else { - CompletionCallback callback = receive_complete_callback_; - receive_complete_callback_.Reset(); - callback.Run(bytes_read); - } + CompletionCallback callback = receive_complete_callback_; + receive_complete_callback_.Reset(); + callback.Run(bytes_read); } } @@ -386,10 +358,9 @@ void Messenger::SendAck(uint32 last_message_received) { // in progress here... DCHECK(!send_message_in_progress_); - int rv = packetizer_->SendMessage(key_, - buffer->data(), - sizeof(Message), - &send_message_callback_); + int rv = packetizer_->SendMessage( + key_, buffer->data(), sizeof(Message), + base::Bind(&Messenger::OnSendMessageComplete, base::Unretained(this))); // TODO(mbelshe): Fix me! Deal with the error cases DCHECK(rv == sizeof(Message)); } diff --git a/net/curvecp/messenger.h b/net/curvecp/messenger.h index bb67946..6aedd709 100644 --- a/net/curvecp/messenger.h +++ b/net/curvecp/messenger.h @@ -33,9 +33,8 @@ class Messenger : public base::NonThreadSafe, explicit Messenger(Packetizer* packetizer); virtual ~Messenger(); - int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); - int Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); + int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); // Packetizer::Listener implementation. virtual void OnConnection(ConnectionKey key) OVERRIDE; @@ -71,13 +70,12 @@ class Messenger : public base::NonThreadSafe, // The send_buffer is a list of pending data to pack into messages and send // to the remote. CircularBuffer send_buffer_; - OldCompletionCallback* send_complete_callback_; + CompletionCallback send_complete_callback_; scoped_refptr<IOBuffer> pending_send_; int pending_send_length_; // The read_buffer is a list of pending data which has been unpacked from // messages and is awaiting delivery to the application. - OldCompletionCallback* old_receive_complete_callback_; CompletionCallback receive_complete_callback_; scoped_refptr<IOBuffer> pending_receive_; int pending_receive_length_; @@ -94,9 +92,6 @@ class Messenger : public base::NonThreadSafe, // A timer to fire when we can send data. base::OneShotTimer<Messenger> send_timer_; - OldCompletionCallbackImpl<Messenger> send_message_callback_; - - ScopedRunnableMethodFactory<Messenger> factory_; DISALLOW_COPY_AND_ASSIGN(Messenger); }; diff --git a/net/curvecp/packetizer.h b/net/curvecp/packetizer.h index 14b526a..c366095 100644 --- a/net/curvecp/packetizer.h +++ b/net/curvecp/packetizer.h @@ -34,7 +34,7 @@ class Packetizer { virtual int SendMessage(ConnectionKey key, const char* data, size_t length, - OldCompletionCallback* callback) = 0; + const CompletionCallback& callback) = 0; // Close an existing connection. virtual void Close(ConnectionKey key) = 0; diff --git a/net/curvecp/server_packetizer.cc b/net/curvecp/server_packetizer.cc index d5d9d67..d387933 100644 --- a/net/curvecp/server_packetizer.cc +++ b/net/curvecp/server_packetizer.cc @@ -15,11 +15,7 @@ ServerPacketizer::ServerPacketizer() : Packetizer(), state_(NONE), listener_(NULL), - read_buffer_(new IOBuffer(kMaxPacketLength)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &ServerPacketizer::OnReadComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_callback_(this, &ServerPacketizer::OnWriteComplete)) { + read_buffer_(new IOBuffer(kMaxPacketLength)) { } ServerPacketizer::~ServerPacketizer() { @@ -46,7 +42,7 @@ bool ServerPacketizer::Open(ConnectionKey key, Packetizer::Listener* listener) { int ServerPacketizer::SendMessage(ConnectionKey key, const char* data, size_t length, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(socket_.get()); DCHECK_LT(0u, length); DCHECK_GT(kMaxPacketLength - sizeof(ServerMessagePacket), length); @@ -137,7 +133,8 @@ void ServerPacketizer::HandleHelloPacket(Packet* packet, int length) { // XXXMB - Can't have two pending writes at the same time... int rv = socket_->SendTo(buffer, sizeof(struct CookiePacket), recv_address_, - &write_callback_); + base::Bind(&ServerPacketizer::OnWriteComplete, + base::Unretained(this))); DCHECK(rv == ERR_IO_PENDING || rv == sizeof(struct CookiePacket)); } @@ -220,7 +217,8 @@ int ServerPacketizer::ReadPackets() { rv = socket_->RecvFrom(read_buffer_, kMaxPacketLength, &recv_address_, - &read_callback_); + base::Bind(&ServerPacketizer::OnReadComplete, + base::Unretained(this))); if (rv <= 0) { if (rv != ERR_IO_PENDING) LOG(ERROR) << "Error reading listen socket: " << rv; diff --git a/net/curvecp/server_packetizer.h b/net/curvecp/server_packetizer.h index c149424..ba42586 100644 --- a/net/curvecp/server_packetizer.h +++ b/net/curvecp/server_packetizer.h @@ -38,7 +38,7 @@ class ServerPacketizer : public base::RefCounted<ServerPacketizer>, virtual int SendMessage(ConnectionKey key, const char* data, size_t length, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual void Close(ConnectionKey key) OVERRIDE; virtual int GetPeerAddress(IPEndPoint* endpoint) const OVERRIDE; virtual int max_message_payload() const OVERRIDE; @@ -87,9 +87,6 @@ class ServerPacketizer : public base::RefCounted<ServerPacketizer>, // The listener map tracks active message listeners known to the packetizer. ListenerMap listener_map_; - OldCompletionCallbackImpl<ServerPacketizer> read_callback_; - OldCompletionCallbackImpl<ServerPacketizer> write_callback_; - DISALLOW_COPY_AND_ASSIGN(ServerPacketizer); }; diff --git a/net/curvecp/test_client.cc b/net/curvecp/test_client.cc index d57a13f..f5ad5ca 100644 --- a/net/curvecp/test_client.cc +++ b/net/curvecp/test_client.cc @@ -23,14 +23,7 @@ namespace net { TestClient::TestClient() : socket_(NULL), errors_(0), - bytes_to_send_(0), - ALLOW_THIS_IN_INITIALIZER_LIST( - connect_callback_(this, &TestClient::OnConnectComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &TestClient::OnReadComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_callback_(this, &TestClient::OnWriteComplete)), - finished_callback_(NULL) { + bytes_to_send_(0) { } TestClient::~TestClient() { @@ -45,9 +38,9 @@ TestClient::~TestClient() { bool TestClient::Start(const HostPortPair& server_host_port_pair, int bytes_to_send, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(!socket_); - DCHECK(!finished_callback_); + DCHECK(finished_callback_.is_null()); finished_callback_ = callback; bytes_to_read_ = bytes_to_send_ = bytes_to_send; @@ -65,7 +58,8 @@ bool TestClient::Start(const HostPortPair& server_host_port_pair, } socket_ = new CurveCPClientSocket(addresses, NULL, NetLog::Source()); - rv = socket_->Connect(&connect_callback_); + rv = socket_->Connect( + base::Bind(&TestClient::OnConnectComplete, base::Unretained(this))); if (rv == ERR_IO_PENDING) return true; OnConnectComplete(rv); @@ -136,7 +130,9 @@ void TestClient::ReadData() { int rv; do { - rv = socket_->Read(read_buffer_, kMaxMessage, &read_callback_); + rv = socket_->Read(read_buffer_, kMaxMessage, + base::Bind(&TestClient::OnReadComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) return; OnReadComplete(rv); // Complete the read manually @@ -157,7 +153,8 @@ void TestClient::SendData() { int rv = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(), - &write_callback_); + base::Bind(&TestClient::OnWriteComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) return; @@ -169,12 +166,12 @@ void TestClient::SendData() { } void TestClient::Finish(int result) { - DCHECK(finished_callback_); + DCHECK(!finished_callback_.is_null()); LOG(ERROR) << "TestClient Done!"; - OldCompletionCallback* callback = finished_callback_; - finished_callback_ = NULL; - callback->Run(result); + CompletionCallback callback = finished_callback_; + finished_callback_.Reset(); + callback.Run(result); } } // namespace net diff --git a/net/curvecp/test_client.h b/net/curvecp/test_client.h index 59a3d59..07ae3a6 100644 --- a/net/curvecp/test_client.h +++ b/net/curvecp/test_client.h @@ -35,7 +35,7 @@ class TestClient { // Returns true if successful in starting the client. bool Start(const HostPortPair& server, int bytes_to_send, - OldCompletionCallback* callback); + const CompletionCallback& callback); // Returns the number of errors this server encountered. int error_count() { return errors_; } @@ -59,10 +59,7 @@ class TestClient { int bytes_to_send_; TestDataStream sent_stream_; TestDataStream received_stream_; - OldCompletionCallbackImpl<TestClient> connect_callback_; - OldCompletionCallbackImpl<TestClient> read_callback_; - OldCompletionCallbackImpl<TestClient> write_callback_; - OldCompletionCallback* finished_callback_; + CompletionCallback finished_callback_; }; } // namespace net diff --git a/net/curvecp/test_server.cc b/net/curvecp/test_server.cc index a1ad4b2..d8badb9 100644 --- a/net/curvecp/test_server.cc +++ b/net/curvecp/test_server.cc @@ -63,11 +63,7 @@ void TestServer::OnAccept(CurveCPServerSocket* new_socket) { EchoServer::EchoServer() : socket_(NULL), - bytes_received_(0), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &EchoServer::OnReadComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_callback_(this, &EchoServer::OnWriteComplete)) { + bytes_received_(0) { } EchoServer::~EchoServer() { @@ -100,7 +96,9 @@ void EchoServer::OnReadComplete(int result) { // Echo the read data back here. DCHECK(!write_buffer_.get()); write_buffer_ = new DrainableIOBuffer(read_buffer_, result); - int rv = socket_->Write(write_buffer_, result, &write_callback_); + int rv = socket_->Write(write_buffer_, result, + base::Bind(&EchoServer::OnWriteComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) return; OnWriteComplete(rv); @@ -116,7 +114,8 @@ void EchoServer::OnWriteComplete(int result) { while (write_buffer_->BytesRemaining()) { int rv = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(), - &write_callback_); + base::Bind(&EchoServer::OnWriteComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) return; OnWriteComplete(rv); @@ -134,7 +133,9 @@ void EchoServer::ReadData() { int rv; do { - rv = socket_->Read(read_buffer_, kMaxMessage, &read_callback_); + rv = socket_->Read(read_buffer_, kMaxMessage, + base::Bind(&EchoServer::OnReadComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) return; OnReadComplete(rv); // Complete the read manually diff --git a/net/curvecp/test_server.h b/net/curvecp/test_server.h index 68f1377..e4426ea 100644 --- a/net/curvecp/test_server.h +++ b/net/curvecp/test_server.h @@ -66,8 +66,6 @@ class EchoServer { scoped_refptr<DrainableIOBuffer> write_buffer_; TestDataStream received_stream_; int bytes_received_; - OldCompletionCallbackImpl<EchoServer> read_callback_; - OldCompletionCallbackImpl<EchoServer> write_callback_; }; } // namespace net diff --git a/net/dns/dns_transaction.cc b/net/dns/dns_transaction.cc index 5cbe39a..6d711b4 100644 --- a/net/dns/dns_transaction.cc +++ b/net/dns/dns_transaction.cc @@ -99,8 +99,6 @@ DnsTransaction::DnsTransaction(DnsSession* session, callback_(callback), attempts_(0), next_state_(STATE_NONE), - ALLOW_THIS_IN_INITIALIZER_LIST( - io_callback_(this, &DnsTransaction::OnIOComplete)), net_log_(BoundNetLog::Make(session->net_log(), NetLog::SOURCE_DNS_TRANSACTION)) { net_log_.BeginEvent( @@ -206,7 +204,8 @@ int DnsTransaction::DoSendQuery() { next_state_ = STATE_SEND_QUERY_COMPLETE; return socket_->Write(query_->io_buffer(), query_->io_buffer()->size(), - &io_callback_); + base::Bind(&DnsTransaction::OnIOComplete, + base::Unretained(this))); } int DnsTransaction::DoSendQueryComplete(int rv) { @@ -226,7 +225,8 @@ int DnsTransaction::DoReadResponse() { response_.reset(new DnsResponse()); return socket_->Read(response_->io_buffer(), response_->io_buffer()->size(), - &io_callback_); + base::Bind(&DnsTransaction::OnIOComplete, + base::Unretained(this))); } int DnsTransaction::DoReadResponseComplete(int rv) { diff --git a/net/dns/dns_transaction.h b/net/dns/dns_transaction.h index d4078f0..e6a1d02 100644 --- a/net/dns/dns_transaction.h +++ b/net/dns/dns_transaction.h @@ -93,7 +93,6 @@ class NET_EXPORT_PRIVATE DnsTransaction : State next_state_; base::OneShotTimer<DnsTransaction> timer_; - OldCompletionCallbackImpl<DnsTransaction> io_callback_; BoundNetLog net_log_; diff --git a/net/ftp/ftp_network_transaction.cc b/net/ftp/ftp_network_transaction.cc index f5ef45e..322764f 100644 --- a/net/ftp/ftp_network_transaction.cc +++ b/net/ftp/ftp_network_transaction.cc @@ -206,8 +206,8 @@ FtpNetworkTransaction::FtpNetworkTransaction( ClientSocketFactory* socket_factory) : command_sent_(COMMAND_NONE), ALLOW_THIS_IN_INITIALIZER_LIST( - io_callback_(this, &FtpNetworkTransaction::OnIOComplete)), - user_callback_(NULL), + io_callback_(base::Bind(&FtpNetworkTransaction::OnIOComplete, + base::Unretained(this)))), session_(session), request_(NULL), resolver_(session->host_resolver()), @@ -239,12 +239,12 @@ int FtpNetworkTransaction::Stop(int error) { } int FtpNetworkTransaction::RestartIgnoringLastError( - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return ERR_NOT_IMPLEMENTED; } int FtpNetworkTransaction::Start(const FtpRequestInfo* request_info, - OldCompletionCallback* callback, + const CompletionCallback& callback, const BoundNetLog& net_log) { net_log_ = net_log; request_ = request_info; @@ -269,7 +269,7 @@ int FtpNetworkTransaction::Start(const FtpRequestInfo* request_info, } int FtpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { ResetStateForRestart(); credentials_ = credentials; @@ -283,7 +283,7 @@ int FtpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials, int FtpNetworkTransaction::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(buf); DCHECK_GT(buf_len, 0); @@ -330,7 +330,7 @@ uint64 FtpNetworkTransaction::GetUploadProgress() const { void FtpNetworkTransaction::ResetStateForRestart() { command_sent_ = COMMAND_NONE; - user_callback_ = NULL; + user_callback_.Reset(); response_ = FtpResponseInfo(); read_ctrl_buf_ = new IOBuffer(kCtrlBufLen); ctrl_response_buffer_.reset(new FtpCtrlResponseBuffer()); @@ -347,12 +347,12 @@ void FtpNetworkTransaction::ResetStateForRestart() { void FtpNetworkTransaction::DoCallback(int rv) { DCHECK(rv != ERR_IO_PENDING); - DCHECK(user_callback_); + DCHECK(!user_callback_.is_null()); // Since Run may result in Read being called, clear callback_ up front. - OldCompletionCallback* c = user_callback_; - user_callback_ = NULL; - c->Run(rv); + CompletionCallback c = user_callback_; + user_callback_.Reset(); + c.Run(rv); } void FtpNetworkTransaction::OnIOComplete(int result) { @@ -645,7 +645,7 @@ int FtpNetworkTransaction::DoCtrlConnect() { next_state_ = STATE_CTRL_CONNECT_COMPLETE; ctrl_socket_.reset(socket_factory_->CreateTransportClientSocket( addresses_, net_log_.net_log(), net_log_.source())); - return ctrl_socket_->Connect(&io_callback_); + return ctrl_socket_->Connect(io_callback_); } int FtpNetworkTransaction::DoCtrlConnectComplete(int result) { @@ -663,10 +663,7 @@ int FtpNetworkTransaction::DoCtrlConnectComplete(int result) { int FtpNetworkTransaction::DoCtrlRead() { next_state_ = STATE_CTRL_READ_COMPLETE; - return ctrl_socket_->Read( - read_ctrl_buf_, - kCtrlBufLen, - &io_callback_); + return ctrl_socket_->Read(read_ctrl_buf_, kCtrlBufLen, io_callback_); } int FtpNetworkTransaction::DoCtrlReadComplete(int result) { @@ -699,7 +696,7 @@ int FtpNetworkTransaction::DoCtrlWrite() { return ctrl_socket_->Write(write_buf_, write_buf_->BytesRemaining(), - &io_callback_); + io_callback_); } int FtpNetworkTransaction::DoCtrlWriteComplete(int result) { @@ -1200,7 +1197,7 @@ int FtpNetworkTransaction::DoDataConnect() { data_address.SetPort(data_connection_port_); data_socket_.reset(socket_factory_->CreateTransportClientSocket( data_address, net_log_.net_log(), net_log_.source())); - return data_socket_->Connect(&io_callback_); + return data_socket_->Connect(io_callback_); } int FtpNetworkTransaction::DoDataConnectComplete(int result) { @@ -1247,8 +1244,7 @@ int FtpNetworkTransaction::DoDataRead() { next_state_ = STATE_DATA_READ_COMPLETE; read_data_buf_->data()[0] = 0; - return data_socket_->Read(read_data_buf_, read_data_buf_len_, - &io_callback_); + return data_socket_->Read(read_data_buf_, read_data_buf_len_, io_callback_); } int FtpNetworkTransaction::DoDataReadComplete(int result) { diff --git a/net/ftp/ftp_network_transaction.h b/net/ftp/ftp_network_transaction.h index 7d32d55..3fec79d 100644 --- a/net/ftp/ftp_network_transaction.h +++ b/net/ftp/ftp_network_transaction.h @@ -34,16 +34,16 @@ class NET_EXPORT_PRIVATE FtpNetworkTransaction : public FtpTransaction { virtual ~FtpNetworkTransaction(); virtual int Stop(int error); - virtual int RestartIgnoringLastError(OldCompletionCallback* callback); + virtual int RestartIgnoringLastError(const CompletionCallback& callback); // FtpTransaction methods: virtual int Start(const FtpRequestInfo* request_info, - OldCompletionCallback* callback, + const CompletionCallback& callback, const BoundNetLog& net_log) OVERRIDE; virtual int RestartWithAuth(const AuthCredentials& credentials, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback) - OVERRIDE; + const CompletionCallback& callback) OVERRIDE; + virtual int Read(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) OVERRIDE; virtual const FtpResponseInfo* GetResponseInfo() const OVERRIDE; virtual LoadState GetLoadState() const OVERRIDE; virtual uint64 GetUploadProgress() const OVERRIDE; @@ -189,8 +189,8 @@ class NET_EXPORT_PRIVATE FtpNetworkTransaction : public FtpTransaction { Command command_sent_; - OldCompletionCallbackImpl<FtpNetworkTransaction> io_callback_; - OldCompletionCallback* user_callback_; + CompletionCallback io_callback_; + CompletionCallback user_callback_; scoped_refptr<FtpNetworkSession> session_; diff --git a/net/ftp/ftp_network_transaction_unittest.cc b/net/ftp/ftp_network_transaction_unittest.cc index 5d686e0..08be329 100644 --- a/net/ftp/ftp_network_transaction_unittest.cc +++ b/net/ftp/ftp_network_transaction_unittest.cc @@ -782,20 +782,23 @@ class FtpNetworkTransactionTest : public PlatformTest { FtpRequestInfo request_info = GetRequestInfo(request); EXPECT_EQ(LOAD_STATE_IDLE, transaction_.GetLoadState()); ASSERT_EQ(ERR_IO_PENDING, - transaction_.Start(&request_info, &callback_, BoundNetLog())); + transaction_.Start(&request_info, callback_.callback(), + BoundNetLog())); EXPECT_NE(LOAD_STATE_IDLE, transaction_.GetLoadState()); ASSERT_EQ(expected_result, callback_.WaitForResult()); if (expected_result == OK) { scoped_refptr<IOBuffer> io_buffer(new IOBuffer(kBufferSize)); memset(io_buffer->data(), 0, kBufferSize); ASSERT_EQ(ERR_IO_PENDING, - transaction_.Read(io_buffer.get(), kBufferSize, &callback_)); + transaction_.Read(io_buffer.get(), kBufferSize, + callback_.callback())); ASSERT_EQ(static_cast<int>(mock_data.length()), callback_.WaitForResult()); EXPECT_EQ(mock_data, std::string(io_buffer->data(), mock_data.length())); // Do another Read to detect that the data socket is now closed. - int rv = transaction_.Read(io_buffer.get(), kBufferSize, &callback_); + int rv = transaction_.Read(io_buffer.get(), kBufferSize, + callback_.callback()); if (rv == ERR_IO_PENDING) { EXPECT_EQ(0, callback_.WaitForResult()); } else { @@ -820,7 +823,7 @@ class FtpNetworkTransactionTest : public PlatformTest { scoped_refptr<FtpNetworkSession> session_; MockClientSocketFactory mock_socket_factory_; FtpNetworkTransaction transaction_; - TestOldCompletionCallback callback_; + TestCompletionCallback callback_; }; TEST_F(FtpNetworkTransactionTest, FailedLookup) { @@ -828,7 +831,8 @@ TEST_F(FtpNetworkTransactionTest, FailedLookup) { host_resolver_->rules()->AddSimulatedFailure("badhost"); EXPECT_EQ(LOAD_STATE_IDLE, transaction_.GetLoadState()); ASSERT_EQ(ERR_IO_PENDING, - transaction_.Start(&request_info, &callback_, BoundNetLog())); + transaction_.Start(&request_info, callback_.callback(), + BoundNetLog())); ASSERT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult()); EXPECT_EQ(LOAD_STATE_IDLE, transaction_.GetLoadState()); } @@ -1051,7 +1055,8 @@ TEST_F(FtpNetworkTransactionTest, DownloadTransactionEvilPasvUnsafeHost) { // Start the transaction. ASSERT_EQ(ERR_IO_PENDING, - transaction_.Start(&request_info, &callback_, BoundNetLog())); + transaction_.Start(&request_info, callback_.callback(), + BoundNetLog())); ASSERT_EQ(OK, callback_.WaitForResult()); // The transaction fires the callback when we can start reading data. That @@ -1175,7 +1180,8 @@ TEST_F(FtpNetworkTransactionTest, EvilRestartUser) { FtpRequestInfo request_info = GetRequestInfo("ftp://host/file"); ASSERT_EQ(ERR_IO_PENDING, - transaction_.Start(&request_info, &callback_, BoundNetLog())); + transaction_.Start(&request_info, callback_.callback(), + BoundNetLog())); ASSERT_EQ(ERR_FTP_FAILED, callback_.WaitForResult()); MockRead ctrl_reads[] = { @@ -1194,7 +1200,7 @@ TEST_F(FtpNetworkTransactionTest, EvilRestartUser) { AuthCredentials( ASCIIToUTF16("foo\nownz0red"), ASCIIToUTF16("innocent")), - &callback_)); + callback_.callback())); EXPECT_EQ(ERR_MALFORMED_IDENTITY, callback_.WaitForResult()); } @@ -1208,7 +1214,8 @@ TEST_F(FtpNetworkTransactionTest, EvilRestartPassword) { FtpRequestInfo request_info = GetRequestInfo("ftp://host/file"); ASSERT_EQ(ERR_IO_PENDING, - transaction_.Start(&request_info, &callback_, BoundNetLog())); + transaction_.Start(&request_info, callback_.callback(), + BoundNetLog())); ASSERT_EQ(ERR_FTP_FAILED, callback_.WaitForResult()); MockRead ctrl_reads[] = { @@ -1228,7 +1235,7 @@ TEST_F(FtpNetworkTransactionTest, EvilRestartPassword) { transaction_.RestartWithAuth( AuthCredentials(ASCIIToUTF16("innocent"), ASCIIToUTF16("foo\nownz0red")), - &callback_)); + callback_.callback())); EXPECT_EQ(ERR_MALFORMED_IDENTITY, callback_.WaitForResult()); } diff --git a/net/ftp/ftp_transaction.h b/net/ftp/ftp_transaction.h index 269c8bb..4fec784 100644 --- a/net/ftp/ftp_transaction.h +++ b/net/ftp/ftp_transaction.h @@ -29,7 +29,7 @@ class NET_EXPORT_PRIVATE FtpTransaction { // Returns OK if the transaction could be started synchronously, which means // that the request was served from the cache (only supported for directory // listings). ERR_IO_PENDING is returned to indicate that the - // OldCompletionCallback will be notified once response info is available or if + // CompletionCallback will be notified once response info is available or if // an IO error occurs. Any other return value indicates that the transaction // could not be started. // @@ -40,21 +40,21 @@ class NET_EXPORT_PRIVATE FtpTransaction { // // Profiling information for the request is saved to |net_log| if non-NULL. virtual int Start(const FtpRequestInfo* request_info, - OldCompletionCallback* callback, + const CompletionCallback& callback, const BoundNetLog& net_log) = 0; // Restarts the FTP transaction with authentication credentials. virtual int RestartWithAuth(const AuthCredentials& credentials, - OldCompletionCallback* callback) = 0; + const CompletionCallback& callback) = 0; // Once response info is available for the transaction, response data may be // read by calling this method. // // Response data is copied into the given buffer and the number of bytes // copied is returned. ERR_IO_PENDING is returned if response data is not - // yet available. The OldCompletionCallback is notified when the data copy + // yet available. The CompletionCallback is notified when the data copy // completes, and it is passed the number of bytes that were successfully - // copied. Or, if a read error occurs, the OldCompletionCallback is notified of + // copied. Or, if a read error occurs, the CompletionCallback is notified of // the error. Any other negative return value indicates that the transaction // could not be read. // @@ -62,7 +62,7 @@ class NET_EXPORT_PRIVATE FtpTransaction { // virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) = 0; + const CompletionCallback& callback) = 0; // Returns the response info for this transaction or NULL if the response // info is not available. diff --git a/net/http/http_network_transaction_unittest.cc b/net/http/http_network_transaction_unittest.cc index d6f966c..0a6a163 100644 --- a/net/http/http_network_transaction_unittest.cc +++ b/net/http/http_network_transaction_unittest.cc @@ -7648,15 +7648,16 @@ TEST_F(HttpNetworkTransactionTest, spdy_writes, arraysize(spdy_writes))); session_deps.socket_factory.AddSocketDataProvider(spdy_data); - TestOldCompletionCallback callback; + TestOldCompletionCallback callback_old; + TestCompletionCallback callback; scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); - int rv = trans->Start(&request, &callback, BoundNetLog()); + int rv = trans->Start(&request, &callback_old, BoundNetLog()); EXPECT_EQ(ERR_IO_PENDING, rv); - EXPECT_EQ(OK, callback.WaitForResult()); + EXPECT_EQ(OK, callback_old.WaitForResult()); const HttpResponseInfo* response = trans->GetResponseInfo(); ASSERT_TRUE(response != NULL); @@ -7680,10 +7681,10 @@ TEST_F(HttpNetworkTransactionTest, connection->Init(host_port_pair.ToString(), transport_params, LOWEST, - &callback, + &callback_old, session->GetTransportSocketPool(), BoundNetLog())); - EXPECT_EQ(OK, callback.WaitForResult()); + EXPECT_EQ(OK, callback_old.WaitForResult()); SSLConfig ssl_config; session->ssl_config_service()->GetSSLConfig(&ssl_config); @@ -7693,7 +7694,8 @@ TEST_F(HttpNetworkTransactionTest, ssl_connection->set_socket(session_deps.socket_factory.CreateSSLClientSocket( connection.release(), HostPortPair("" , 443), ssl_config, NULL /* ssl_host_info */, context)); - EXPECT_EQ(ERR_IO_PENDING, ssl_connection->socket()->Connect(&callback)); + EXPECT_EQ(ERR_IO_PENDING, + ssl_connection->socket()->Connect(callback.callback())); EXPECT_EQ(OK, callback.WaitForResult()); EXPECT_EQ(OK, spdy_session->InitializeWithSocket(ssl_connection.release(), @@ -7701,9 +7703,9 @@ TEST_F(HttpNetworkTransactionTest, trans.reset(new HttpNetworkTransaction(session)); - rv = trans->Start(&request, &callback, BoundNetLog()); + rv = trans->Start(&request, &callback_old, BoundNetLog()); EXPECT_EQ(ERR_IO_PENDING, rv); - EXPECT_EQ(OK, callback.WaitForResult()); + EXPECT_EQ(OK, callback_old.WaitForResult()); response = trans->GetResponseInfo(); ASSERT_TRUE(response != NULL); diff --git a/net/http/http_proxy_client_socket.cc b/net/http/http_proxy_client_socket.cc index ef9fb82..6356da7 100644 --- a/net/http/http_proxy_client_socket.cc +++ b/net/http/http_proxy_client_socket.cc @@ -37,7 +37,6 @@ HttpProxyClientSocket::HttpProxyClientSocket( : ALLOW_THIS_IN_INITIALIZER_LIST( io_callback_(this, &HttpProxyClientSocket::OnIOComplete)), next_state_(STATE_NONE), - old_user_callback_(NULL), transport_(transport_socket), endpoint_(endpoint), auth_(tunnel ? @@ -65,7 +64,7 @@ HttpProxyClientSocket::~HttpProxyClientSocket() { int HttpProxyClientSocket::RestartWithAuth(OldCompletionCallback* callback) { DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_ && user_callback_.is_null()); + DCHECK(user_callback_.is_null()); int rv = PrepareForAuthRestart(); if (rv != OK || next_state_ == STATE_NONE) @@ -73,7 +72,10 @@ int HttpProxyClientSocket::RestartWithAuth(OldCompletionCallback* callback) { rv = DoLoop(OK); if (rv == ERR_IO_PENDING) - old_user_callback_ = callback; + if (callback) { + user_callback_ = base::Bind(&OldCompletionCallback::Run<int>, + base::Unretained(callback)); + } return rv; } @@ -92,34 +94,10 @@ HttpStream* HttpProxyClientSocket::CreateConnectResponseStream() { } -int HttpProxyClientSocket::Connect(OldCompletionCallback* callback) { - DCHECK(transport_.get()); - DCHECK(transport_->socket()); - DCHECK(!old_user_callback_ && user_callback_.is_null()); - - // TODO(rch): figure out the right way to set up a tunnel with SPDY. - // This approach sends the complete HTTPS request to the proxy - // which allows the proxy to see "private" data. Instead, we should - // create an SSL tunnel to the origin server using the CONNECT method - // inside a single SPDY stream. - if (using_spdy_ || !tunnel_) - next_state_ = STATE_DONE; - if (next_state_ == STATE_DONE) - return OK; - - DCHECK_EQ(STATE_NONE, next_state_); - next_state_ = STATE_GENERATE_AUTH_TOKEN; - - int rv = DoLoop(OK); - if (rv == ERR_IO_PENDING) - old_user_callback_ = callback; - return rv; -} - int HttpProxyClientSocket::Connect(const CompletionCallback& callback) { DCHECK(transport_.get()); DCHECK(transport_->socket()); - DCHECK(!old_user_callback_ && user_callback_.is_null()); + DCHECK(user_callback_.is_null()); // TODO(rch): figure out the right way to set up a tunnel with SPDY. // This approach sends the complete HTTPS request to the proxy @@ -147,7 +125,6 @@ void HttpProxyClientSocket::Disconnect() { // Reset other states to make sure they aren't mistakenly used later. // These are the states initialized by Connect(). next_state_ = STATE_NONE; - old_user_callback_ = NULL; user_callback_.Reset(); } @@ -213,27 +190,8 @@ base::TimeDelta HttpProxyClientSocket::GetConnectTimeMicros() const { } int HttpProxyClientSocket::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(!old_user_callback_ && user_callback_.is_null()); - if (next_state_ != STATE_DONE) { - // We're trying to read the body of the response but we're still trying - // to establish an SSL tunnel through the proxy. We can't read these - // bytes when establishing a tunnel because they might be controlled by - // an active network attacker. We don't worry about this for HTTP - // because an active network attacker can already control HTTP sessions. - // We reach this case when the user cancels a 407 proxy auth prompt. - // See http://crbug.com/8473. - DCHECK_EQ(407, response_.headers->response_code()); - LogBlockedTunnelResponse(response_.headers->response_code()); - - return ERR_TUNNEL_CONNECTION_FAILED; - } - - return transport_->socket()->Read(buf, buf_len, callback); -} -int HttpProxyClientSocket::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { - DCHECK(!old_user_callback_ && user_callback_.is_null()); + DCHECK(user_callback_.is_null()); if (next_state_ != STATE_DONE) { // We're trying to read the body of the response but we're still trying // to establish an SSL tunnel through the proxy. We can't read these @@ -252,9 +210,9 @@ int HttpProxyClientSocket::Read(IOBuffer* buf, int buf_len, } int HttpProxyClientSocket::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK_EQ(STATE_DONE, next_state_); - DCHECK(!old_user_callback_); + DCHECK(user_callback_.is_null()); return transport_->socket()->Write(buf, buf_len, callback); } @@ -321,19 +279,13 @@ void HttpProxyClientSocket::LogBlockedTunnelResponse(int response_code) const { void HttpProxyClientSocket::DoCallback(int result) { DCHECK_NE(ERR_IO_PENDING, result); - DCHECK(old_user_callback_ || !user_callback_.is_null()); + DCHECK(!user_callback_.is_null()); // Since Run() may result in Read being called, - // clear old_user_callback_ up front. - if (old_user_callback_) { - OldCompletionCallback* c = old_user_callback_; - old_user_callback_ = NULL; - c->Run(result); - } else { - CompletionCallback c = user_callback_; - user_callback_.Reset(); - c.Run(result); - } + // clear user_callback_ up front. + CompletionCallback c = user_callback_; + user_callback_.Reset(); + c.Run(result); } void HttpProxyClientSocket::OnIOComplete(int result) { diff --git a/net/http/http_proxy_client_socket.h b/net/http/http_proxy_client_socket.h index 662c305..6bb36fb 100644 --- a/net/http/http_proxy_client_socket.h +++ b/net/http/http_proxy_client_socket.h @@ -61,7 +61,6 @@ class HttpProxyClientSocket : public ProxyClientSocket { virtual const scoped_refptr<HttpAuthController>& auth_controller() OVERRIDE; // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -77,13 +76,10 @@ class HttpProxyClientSocket : public ProxyClientSocket { // Socket implementation. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; virtual int GetPeerAddress(AddressList* address) const OVERRIDE; @@ -130,7 +126,6 @@ class HttpProxyClientSocket : public ProxyClientSocket { State next_state_; // Stores the callback to the layer above, called on completing Connect(). - OldCompletionCallback* old_user_callback_; CompletionCallback user_callback_; HttpRequestInfo request_; diff --git a/net/http/http_proxy_client_socket_pool.cc b/net/http/http_proxy_client_socket_pool.cc index f7d3206..f210cc8 100644 --- a/net/http/http_proxy_client_socket_pool.cc +++ b/net/http/http_proxy_client_socket_pool.cc @@ -84,7 +84,10 @@ HttpProxyConnectJob::HttpProxyConnectJob( ssl_pool_(ssl_pool), resolver_(host_resolver), ALLOW_THIS_IN_INITIALIZER_LIST( - callback_(this, &HttpProxyConnectJob::OnIOComplete)), + callback_(base::Bind(&HttpProxyConnectJob::OnIOComplete, + base::Unretained(this)))), + ALLOW_THIS_IN_INITIALIZER_LIST( + callback_old_(this, &HttpProxyConnectJob::OnIOComplete)), using_spdy_(false) { } @@ -174,7 +177,7 @@ int HttpProxyConnectJob::DoTransportConnect() { group_name(), params_->transport_params(), params_->transport_params()->destination().priority(), - &callback_, + &callback_old_, transport_pool_, net_log()); } @@ -208,7 +211,7 @@ int HttpProxyConnectJob::DoSSLConnect() { return transport_socket_handle_->Init( group_name(), params_->ssl_params(), params_->ssl_params()->transport_params()->destination().priority(), - &callback_, ssl_pool_, net_log()); + &callback_old_, ssl_pool_, net_log()); } int HttpProxyConnectJob::DoSSLConnectComplete(int result) { @@ -271,7 +274,7 @@ int HttpProxyConnectJob::DoHttpProxyConnect() { params_->tunnel(), using_spdy_, params_->ssl_params() != NULL)); - return transport_socket_->Connect(&callback_); + return transport_socket_->Connect(callback_); } int HttpProxyConnectJob::DoHttpProxyConnectComplete(int result) { @@ -312,7 +315,7 @@ int HttpProxyConnectJob::DoSpdyProxyCreateStream() { return spdy_session->CreateStream(params_->request_url(), params_->destination().priority(), &spdy_stream_, spdy_session->net_log(), - &callback_); + &callback_old_); } int HttpProxyConnectJob::DoSpdyProxyCreateStreamComplete(int result) { @@ -328,7 +331,7 @@ int HttpProxyConnectJob::DoSpdyProxyCreateStreamComplete(int result) { params_->destination().host_port_pair(), params_->http_auth_cache(), params_->http_auth_handler_factory())); - return transport_socket_->Connect(&callback_); + return transport_socket_->Connect(callback_); } int HttpProxyConnectJob::ConnectInternal() { diff --git a/net/http/http_proxy_client_socket_pool.h b/net/http/http_proxy_client_socket_pool.h index 31d21cb..68a466b 100644 --- a/net/http/http_proxy_client_socket_pool.h +++ b/net/http/http_proxy_client_socket_pool.h @@ -155,7 +155,8 @@ class HttpProxyConnectJob : public ConnectJob { HostResolver* const resolver_; State next_state_; - OldCompletionCallbackImpl<HttpProxyConnectJob> callback_; + CompletionCallback callback_; + OldCompletionCallbackImpl<HttpProxyConnectJob> callback_old_; scoped_ptr<ClientSocketHandle> transport_socket_handle_; scoped_ptr<ProxyClientSocket> transport_socket_; bool using_spdy_; diff --git a/net/http/http_stream_parser.cc b/net/http/http_stream_parser.cc index cd3c9fa..8969d73 100644 --- a/net/http/http_stream_parser.cc +++ b/net/http/http_stream_parser.cc @@ -81,7 +81,9 @@ HttpStreamParser::HttpStreamParser(ClientSocketHandle* connection, connection_(connection), net_log_(net_log), ALLOW_THIS_IN_INITIALIZER_LIST( - io_callback_(this, &HttpStreamParser::OnIOComplete)), + io_callback_( + base::Bind(&HttpStreamParser::OnIOComplete, + base::Unretained(this)))), chunk_length_(0), chunk_length_without_encoding_(0), sent_last_chunk_(false) { @@ -314,7 +316,7 @@ int HttpStreamParser::DoSendHeaders(int result) { } result = connection_->socket()->Write(request_headers_, bytes_remaining, - &io_callback_); + io_callback_); } else if (request_body_ != NULL && (request_body_->is_chunked() || request_body_->size())) { io_state_ = STATE_SENDING_BODY; @@ -331,7 +333,7 @@ int HttpStreamParser::DoSendBody(int result) { if (chunk_length_) { memmove(chunk_buf_->data(), chunk_buf_->data() + result, chunk_length_); return connection_->socket()->Write(chunk_buf_, chunk_length_, - &io_callback_); + io_callback_); } if (sent_last_chunk_) { @@ -366,7 +368,7 @@ int HttpStreamParser::DoSendBody(int result) { return ERR_IO_PENDING; return connection_->socket()->Write(chunk_buf_, chunk_length_, - &io_callback_); + io_callback_); } // Non-chunked request body. @@ -375,7 +377,7 @@ int HttpStreamParser::DoSendBody(int result) { if (!request_body_->eof()) { int buf_len = static_cast<int>(request_body_->buf_len()); result = connection_->socket()->Write(request_body_->buf(), buf_len, - &io_callback_); + io_callback_); } else { io_state_ = STATE_REQUEST_SENT; } @@ -395,7 +397,7 @@ int HttpStreamParser::DoReadHeaders() { return connection_->socket()->Read(read_buf_, read_buf_->RemainingCapacity(), - &io_callback_); + io_callback_); } int HttpStreamParser::DoReadHeadersComplete(int result) { @@ -526,7 +528,7 @@ int HttpStreamParser::DoReadBody() { DCHECK_EQ(0, read_buf_->offset()); return connection_->socket()->Read(user_read_buf_, user_read_buf_len_, - &io_callback_); + io_callback_); } int HttpStreamParser::DoReadBodyComplete(int result) { diff --git a/net/http/http_stream_parser.h b/net/http/http_stream_parser.h index aa218f8..b80145f 100644 --- a/net/http/http_stream_parser.h +++ b/net/http/http_stream_parser.h @@ -189,7 +189,7 @@ class HttpStreamParser : public ChunkCallback { BoundNetLog net_log_; // Callback to be used when doing IO. - OldCompletionCallbackImpl<HttpStreamParser> io_callback_; + CompletionCallback io_callback_; // Stores an encoded chunk for chunked uploads. // Note: This should perhaps be improved to not create copies of the data. diff --git a/net/socket/client_socket_pool_base_unittest.cc b/net/socket/client_socket_pool_base_unittest.cc index 779b8dee..1aac330 100644 --- a/net/socket/client_socket_pool_base_unittest.cc +++ b/net/socket/client_socket_pool_base_unittest.cc @@ -63,18 +63,15 @@ class MockClientSocket : public StreamSocket { // Socket implementation. virtual int Read( - IOBuffer* /* buf */, int len, OldCompletionCallback* /* callback */) { - num_bytes_read_ += len; - return len; - } - virtual int Read( - IOBuffer* /* buf */, int len, const CompletionCallback& /* callback */) { + IOBuffer* /* buf */, int len, + const CompletionCallback& /* callback */) OVERRIDE { num_bytes_read_ += len; return len; } virtual int Write( - IOBuffer* /* buf */, int len, OldCompletionCallback* /* callback */) { + IOBuffer* /* buf */, int len, + const CompletionCallback& /* callback */) OVERRIDE { was_used_to_convey_data_ = true; return len; } @@ -82,11 +79,7 @@ class MockClientSocket : public StreamSocket { virtual bool SetSendBufferSize(int32 size) { return true; } // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback) { - connected_ = true; - return OK; - } - virtual int Connect(const net::CompletionCallback& callback) { + virtual int Connect(const CompletionCallback& callback) OVERRIDE { connected_ = true; return OK; } @@ -328,7 +321,7 @@ class TestConnectJob : public ConnectJob { int DoConnect(bool succeed, bool was_async, bool recoverable) { int result = OK; if (succeed) { - socket()->Connect(NULL); + socket()->Connect(CompletionCallback()); } else if (recoverable) { result = ERR_PROXY_AUTH_REQUESTED; } else { @@ -384,6 +377,7 @@ class TestConnectJobFactory } // ConnectJobFactory implementation. + virtual ConnectJob* NewConnectJob( const std::string& group_name, const TestClientSocketPoolBase::Request& request, @@ -677,7 +671,7 @@ TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_WarmestSocket) { MockClientSocket* sock = static_cast<MockClientSocket*>(s); CHECK(sock); sockets_[i] = sock; - sock->Read(NULL, 1024 - i, NULL); + sock->Read(NULL, 1024 - i, CompletionCallback()); } ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); @@ -713,7 +707,7 @@ TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_LastAccessedSocket) { MockClientSocket* sock = static_cast<MockClientSocket*>(s); CHECK(sock); sockets_[i] = sock; - sock->Read(NULL, 1024 - i, NULL); + sock->Read(NULL, 1024 - i, CompletionCallback()); } ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); @@ -2037,7 +2031,7 @@ TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimer) { handle.Reset(); EXPECT_EQ(OK, callback2.WaitForResult()); // Use the socket. - EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL)); + EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); handle2.Reset(); // The idle socket timeout value was set to 10 milliseconds. Wait 100 @@ -2111,7 +2105,7 @@ TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { handle.Reset(); EXPECT_EQ(OK, callback2.WaitForResult()); // Use the socket. - EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL)); + EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); handle2.Reset(); // We post all of our delayed tasks with a 2ms delay. I.e. they don't @@ -2872,8 +2866,8 @@ TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { EXPECT_EQ(OK, callback3.WaitForResult()); // Use the socket. - EXPECT_EQ(1, handle1.socket()->Write(NULL, 1, NULL)); - EXPECT_EQ(1, handle3.socket()->Write(NULL, 1, NULL)); + EXPECT_EQ(1, handle1.socket()->Write(NULL, 1, CompletionCallback())); + EXPECT_EQ(1, handle3.socket()->Write(NULL, 1, CompletionCallback())); handle1.Reset(); handle2.Reset(); diff --git a/net/socket/deterministic_socket_data_unittest.cc b/net/socket/deterministic_socket_data_unittest.cc index cb6ca32..8740c8d 100644 --- a/net/socket/deterministic_socket_data_unittest.cc +++ b/net/socket/deterministic_socket_data_unittest.cc @@ -41,8 +41,8 @@ class DeterministicSocketDataTest : public PlatformTest { void AssertAsyncWriteEquals(const char* data, int len); void AssertWriteReturns(const char* data, int len, int rv); - TestOldCompletionCallback read_callback_; - TestOldCompletionCallback write_callback_; + TestCompletionCallback read_callback_; + TestCompletionCallback write_callback_; StreamSocket* sock_; scoped_refptr<DeterministicSocketData> data_; @@ -61,9 +61,7 @@ class DeterministicSocketDataTest : public PlatformTest { }; DeterministicSocketDataTest::DeterministicSocketDataTest() - : read_callback_(), - write_callback_(), - sock_(NULL), + : sock_(NULL), data_(NULL), read_buf_(NULL), connect_data_(false, OK), @@ -125,7 +123,7 @@ void DeterministicSocketDataTest::AssertAsyncReadEquals(const char* data, void DeterministicSocketDataTest::AssertReadReturns(const char* data, int len, int rv) { read_buf_ = new IOBuffer(len); - ASSERT_EQ(rv, sock_->Read(read_buf_, len, &read_callback_)); + ASSERT_EQ(rv, sock_->Read(read_buf_, len, read_callback_.callback())); } void DeterministicSocketDataTest::AssertReadBufferEquals(const char* data, @@ -139,7 +137,7 @@ void DeterministicSocketDataTest::AssertSyncWriteEquals(const char* data, memcpy(buf->data(), data, len); // Issue the write, which will complete immediately - ASSERT_EQ(len, sock_->Write(buf, len, &write_callback_)); + ASSERT_EQ(len, sock_->Write(buf, len, write_callback_.callback())); } void DeterministicSocketDataTest::AssertAsyncWriteEquals(const char* data, @@ -160,7 +158,7 @@ void DeterministicSocketDataTest::AssertWriteReturns(const char* data, memcpy(buf->data(), data, len); // Issue the read, which will complete asynchronously - ASSERT_EQ(rv, sock_->Write(buf, len, &write_callback_)); + ASSERT_EQ(rv, sock_->Write(buf, len, write_callback_.callback())); } // ----------- Read diff --git a/net/socket/socket.h b/net/socket/socket.h index c185c44..2f1fe50 100644 --- a/net/socket/socket.h +++ b/net/socket/socket.h @@ -30,8 +30,6 @@ class NET_EXPORT Socket { // closed. If the socket is Disconnected before the read completes, the // callback will not be invoked. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) = 0; - virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) = 0; // Writes data, up to |buf_len| bytes, to the socket. Note: data may be @@ -47,7 +45,7 @@ class NET_EXPORT Socket { // of the actual buffer that is written to the socket. If the socket is // Disconnected before the write completes, the callback will not be invoked. virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) = 0; + const CompletionCallback& callback) = 0; // Set the receive buffer size (in bytes) for the socket. // Note: changing this value can affect the TCP window size on some platforms. diff --git a/net/socket/socket_test_util.cc b/net/socket/socket_test_util.cc index 376d542..6b4e00f 100644 --- a/net/socket/socket_test_util.cc +++ b/net/socket/socket_test_util.cc @@ -176,7 +176,7 @@ MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { // Check that what we are writing matches the expectation. // Then give the mocked return value. - net::MockWrite* w = &writes_[write_index_++]; + MockWrite* w = &writes_[write_index_++]; w->time_stamp = base::Time::Now(); int result = w->result; if (w->data) { @@ -191,8 +191,8 @@ MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { std::string actual_data(data.substr(0, w->data_len)); EXPECT_EQ(expected_data, actual_data); if (expected_data != actual_data) - return MockWriteResult(false, net::ERR_UNEXPECTED); - if (result == net::OK) + return MockWriteResult(false, ERR_UNEXPECTED); + if (result == OK) result = w->data_len; } return MockWriteResult(w->async, result); @@ -252,7 +252,7 @@ DelayedSocketData::DelayedSocketData( MockWrite* writes, size_t writes_count) : StaticSocketDataProvider(reads, reads_count, writes, writes_count), write_delay_(write_delay), - ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { + ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { DCHECK_GE(write_delay_, 0); } @@ -261,7 +261,7 @@ DelayedSocketData::DelayedSocketData( size_t reads_count, MockWrite* writes, size_t writes_count) : StaticSocketDataProvider(reads, reads_count, writes, writes_count), write_delay_(write_delay), - ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { + ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { DCHECK_GE(write_delay_, 0); set_connect_data(connect); } @@ -284,14 +284,17 @@ MockWriteResult DelayedSocketData::OnWrite(const std::string& data) { MockWriteResult rv = StaticSocketDataProvider::OnWrite(data); // Now that our write has completed, we can allow reads to continue. if (!--write_delay_) - MessageLoop::current()->PostDelayedTask(FROM_HERE, - factory_.NewRunnableMethod(&DelayedSocketData::CompleteRead), 100); + MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::Bind(&DelayedSocketData::CompleteRead, + weak_factory_.GetWeakPtr()), + 100); return rv; } void DelayedSocketData::Reset() { set_socket(NULL); - factory_.RevokeAll(); + weak_factory_.InvalidateWeakPtrs(); StaticSocketDataProvider::Reset(); } @@ -303,8 +306,8 @@ void DelayedSocketData::CompleteRead() { OrderedSocketData::OrderedSocketData( MockRead* reads, size_t reads_count, MockWrite* writes, size_t writes_count) : StaticSocketDataProvider(reads, reads_count, writes, writes_count), - sequence_number_(0), loop_stop_stage_(0), callback_(NULL), - blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { + sequence_number_(0), loop_stop_stage_(0), + blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { } OrderedSocketData::OrderedSocketData( @@ -312,8 +315,8 @@ OrderedSocketData::OrderedSocketData( MockRead* reads, size_t reads_count, MockWrite* writes, size_t writes_count) : StaticSocketDataProvider(reads, reads_count, writes, writes_count), - sequence_number_(0), loop_stop_stage_(0), callback_(NULL), - blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { + sequence_number_(0), loop_stop_stage_(0), + blocked_(false), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { set_connect_data(connect); } @@ -336,12 +339,12 @@ void OrderedSocketData::EndLoop() { NET_TRACE(INFO, " *** ") << "Stage " << sequence_number_ << ": Posting Quit at read " << read_index(); loop_stop_stage_ = sequence_number_; - if (callback_) - callback_->RunWithParams(Tuple1<int>(ERR_IO_PENDING)); + if (!callback_.is_null()) + callback_.Run(ERR_IO_PENDING); } MockRead OrderedSocketData::GetNextRead() { - factory_.RevokeAll(); + weak_factory_.InvalidateWeakPtrs(); blocked_ = false; const MockRead& next_read = StaticSocketDataProvider::PeekRead(); if (next_read.sequence_number & MockRead::STOPLOOP) @@ -374,7 +377,9 @@ MockWriteResult OrderedSocketData::OnWrite(const std::string& data) { // SpdyStream::ReadResponseHeaders() is called, we hit a NOTREACHED(). MessageLoop::current()->PostDelayedTask( FROM_HERE, - factory_.NewRunnableMethod(&OrderedSocketData::CompleteRead), 100); + base::Bind(&OrderedSocketData::CompleteRead, + weak_factory_.GetWeakPtr()), + 100); } return StaticSocketDataProvider::OnWrite(data); } @@ -385,7 +390,7 @@ void OrderedSocketData::Reset() { sequence_number_ = 0; loop_stop_stage_ = 0; set_socket(NULL); - factory_.RevokeAll(); + weak_factory_.InvalidateWeakPtrs(); StaticSocketDataProvider::Reset(); } @@ -593,8 +598,8 @@ MockSSLClientSocket* MockClientSocketFactory::GetMockSSLClientSocket( DatagramClientSocket* MockClientSocketFactory::CreateDatagramClientSocket( DatagramSocket::BindType bind_type, const RandIntCallback& rand_int_cb, - NetLog* net_log, - const NetLog::Source& source) { + net::NetLog* net_log, + const net::NetLog::Source& source) { SocketDataProvider* data_provider = mock_data_.GetNext(); MockUDPClientSocket* socket = new MockUDPClientSocket(data_provider, net_log); data_provider->set_socket(socket); @@ -605,7 +610,7 @@ DatagramClientSocket* MockClientSocketFactory::CreateDatagramClientSocket( StreamSocket* MockClientSocketFactory::CreateTransportClientSocket( const AddressList& addresses, net::NetLog* net_log, - const NetLog::Source& source) { + const net::NetLog::Source& source) { SocketDataProvider* data_provider = mock_data_.GetNext(); MockTCPClientSocket* socket = new MockTCPClientSocket(addresses, net_log, data_provider); @@ -633,7 +638,7 @@ void MockClientSocketFactory::ClearSSLSessionCache() { MockClientSocket::MockClientSocket(net::NetLog* net_log) : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), connected_(false), - net_log_(NetLog::Source(), net_log) { + net_log_(net::NetLog::Source(), net_log) { } bool MockClientSocket::SetReceiveBufferSize(int32 size) { @@ -676,12 +681,12 @@ const BoundNetLog& MockClientSocket::NetLog() const { return net_log_; } -void MockClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { +void MockClientSocket::GetSSLInfo(SSLInfo* ssl_info) { NOTREACHED(); } void MockClientSocket::GetSSLCertRequestInfo( - net::SSLCertRequestInfo* cert_request_info) { + SSLCertRequestInfo* cert_request_info) { } int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, @@ -701,44 +706,32 @@ MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { MockClientSocket::~MockClientSocket() {} -void MockClientSocket::RunCallbackAsync(net::OldCompletionCallback* callback, - int result) { - MessageLoop::current()->PostTask(FROM_HERE, - base::Bind(&MockClientSocket::RunOldCallback, weak_factory_.GetWeakPtr(), - callback, result)); -} -void MockClientSocket::RunCallbackAsync(const net::CompletionCallback& callback, +void MockClientSocket::RunCallbackAsync(const CompletionCallback& callback, int result) { MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&MockClientSocket::RunCallback, weak_factory_.GetWeakPtr(), callback, result)); } -void MockClientSocket::RunOldCallback(net::OldCompletionCallback* callback, - int result) { - if (callback) - callback->Run(result); -} void MockClientSocket::RunCallback(const net::CompletionCallback& callback, int result) { if (!callback.is_null()) callback.Run(result); } -MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses, +MockTCPClientSocket::MockTCPClientSocket(const AddressList& addresses, net::NetLog* net_log, - net::SocketDataProvider* data) + SocketDataProvider* data) : MockClientSocket(net_log), addresses_(addresses), data_(data), read_offset_(0), num_bytes_read_(0), - read_data_(false, net::ERR_UNEXPECTED), + read_data_(false, ERR_UNEXPECTED), need_read_data_(true), peer_closed_connection_(false), pending_buf_(NULL), pending_buf_len_(0), - old_pending_callback_(NULL), was_used_to_convey_data_(false) { DCHECK(data_); data_->Reset(); @@ -746,42 +739,10 @@ MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses, MockTCPClientSocket::~MockTCPClientSocket() {} -int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { +int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) { if (!connected_) - return net::ERR_UNEXPECTED; - - // If the buffer is already in use, a read is already in progress! - DCHECK(pending_buf_ == NULL); - - // Store our async IO data. - pending_buf_ = buf; - pending_buf_len_ = buf_len; - old_pending_callback_ = callback; - - if (need_read_data_) { - read_data_ = data_->GetNextRead(); - if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { - // This MockRead is just a marker to instruct us to set - // peer_closed_connection_. Skip it and get the next one. - read_data_ = data_->GetNextRead(); - peer_closed_connection_ = true; - } - // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility - // to complete the async IO manually later (via OnReadComplete). - if (read_data_.result == ERR_IO_PENDING) { - DCHECK(callback); // We need to be using async IO in this case. - return ERR_IO_PENDING; - } - need_read_data_ = false; - } - - return CompleteRead(); -} -int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, - const net::CompletionCallback& callback) { - if (!connected_) - return net::ERR_UNEXPECTED; + return ERR_UNEXPECTED; // If the buffer is already in use, a read is already in progress! DCHECK(pending_buf_ == NULL); @@ -812,55 +773,41 @@ int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, return CompleteRead(); } -int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { +int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) { DCHECK(buf); DCHECK_GT(buf_len, 0); if (!connected_) - return net::ERR_UNEXPECTED; + return ERR_UNEXPECTED; std::string data(buf->data(), buf_len); - net::MockWriteResult write_result = data_->OnWrite(data); + MockWriteResult write_result = data_->OnWrite(data); was_used_to_convey_data_ = true; if (write_result.async) { RunCallbackAsync(callback, write_result.result); - return net::ERR_IO_PENDING; + return ERR_IO_PENDING; } return write_result.result; } -int MockTCPClientSocket::Connect(net::OldCompletionCallback* callback) { - if (connected_) - return net::OK; - connected_ = true; - peer_closed_connection_ = false; - if (data_->connect_data().async) { - RunCallbackAsync(callback, data_->connect_data().result); - return net::ERR_IO_PENDING; - } - return data_->connect_data().result; -} -int MockTCPClientSocket::Connect(const net::CompletionCallback& callback) { +int MockTCPClientSocket::Connect(const CompletionCallback& callback) { if (connected_) - return net::OK; - + return OK; connected_ = true; peer_closed_connection_ = false; if (data_->connect_data().async) { RunCallbackAsync(callback, data_->connect_data().result); - return net::ERR_IO_PENDING; + return ERR_IO_PENDING; } - return data_->connect_data().result; } void MockTCPClientSocket::Disconnect() { MockClientSocket::Disconnect(); - old_pending_callback_ = NULL; pending_callback_.Reset(); } @@ -912,15 +859,9 @@ void MockTCPClientSocket::OnReadComplete(const MockRead& data) { // let CompleteRead() schedule a callback. read_data_.async = false; - if (old_pending_callback_) { - net::OldCompletionCallback* callback = old_pending_callback_; - int rv = CompleteRead(); - RunOldCallback(callback, rv); - } else { - net::CompletionCallback callback = pending_callback_; - int rv = CompleteRead(); - RunCallback(callback, rv); - } + CompletionCallback callback = pending_callback_; + int rv = CompleteRead(); + RunCallback(callback, rv); } int MockTCPClientSocket::CompleteRead() { @@ -930,13 +871,11 @@ int MockTCPClientSocket::CompleteRead() { was_used_to_convey_data_ = true; // Save the pending async IO data and reset our |pending_| state. - net::IOBuffer* buf = pending_buf_; + IOBuffer* buf = pending_buf_; int buf_len = pending_buf_len_; - net::OldCompletionCallback* old_callback = old_pending_callback_; - net::CompletionCallback callback = pending_callback_; + CompletionCallback callback = pending_callback_; pending_buf_ = NULL; pending_buf_len_ = 0; - old_pending_callback_ = NULL; pending_callback_.Reset(); int result = read_data_.result; @@ -958,27 +897,22 @@ int MockTCPClientSocket::CompleteRead() { } if (read_data_.async) { - DCHECK(old_callback || !callback.is_null()); - if (old_callback) - RunCallbackAsync(old_callback, result); - else - RunCallbackAsync(callback, result); - return net::ERR_IO_PENDING; + DCHECK(!callback.is_null()); + RunCallbackAsync(callback, result); + return ERR_IO_PENDING; } return result; } DeterministicMockTCPClientSocket::DeterministicMockTCPClientSocket( - net::NetLog* net_log, net::DeterministicSocketData* data) + net::NetLog* net_log, DeterministicSocketData* data) : MockClientSocket(net_log), write_pending_(false), - write_callback_(NULL), write_result_(0), read_data_(), read_buf_(NULL), read_buf_len_(0), read_pending_(false), - old_read_callback_(NULL), data_(data), was_used_to_convey_data_(false) {} @@ -987,7 +921,7 @@ DeterministicMockTCPClientSocket::~DeterministicMockTCPClientSocket() {} void DeterministicMockTCPClientSocket::CompleteWrite() { was_used_to_convey_data_ = true; write_pending_ = false; - write_callback_->Run(write_result_); + write_callback_.Run(write_result_); } int DeterministicMockTCPClientSocket::CompleteRead() { @@ -1012,32 +946,29 @@ int DeterministicMockTCPClientSocket::CompleteRead() { if (read_pending_) { read_pending_ = false; - if (old_read_callback_) - old_read_callback_->Run(result); - else - read_callback_.Run(result); + read_callback_.Run(result); } return result; } int DeterministicMockTCPClientSocket::Write( - net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { + IOBuffer* buf, int buf_len, const CompletionCallback& callback) { DCHECK(buf); DCHECK_GT(buf_len, 0); if (!connected_) - return net::ERR_UNEXPECTED; + return ERR_UNEXPECTED; std::string data(buf->data(), buf_len); - net::MockWriteResult write_result = data_->OnWrite(data); + MockWriteResult write_result = data_->OnWrite(data); if (write_result.async) { write_callback_ = callback; write_result_ = write_result.result; - DCHECK(write_callback_ != NULL); + DCHECK(!write_callback_.is_null()); write_pending_ = true; - return net::ERR_IO_PENDING; + return ERR_IO_PENDING; } was_used_to_convey_data_ = true; @@ -1046,32 +977,9 @@ int DeterministicMockTCPClientSocket::Write( } int DeterministicMockTCPClientSocket::Read( - net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) { - if (!connected_) - return net::ERR_UNEXPECTED; - - read_data_ = data_->GetNextRead(); - // The buffer should always be big enough to contain all the MockRead data. To - // use small buffers, split the data into multiple MockReads. - DCHECK_LE(read_data_.data_len, buf_len); - - read_buf_ = buf; - read_buf_len_ = buf_len; - old_read_callback_ = callback; - - if (read_data_.async || (read_data_.result == ERR_IO_PENDING)) { - read_pending_ = true; - DCHECK(old_read_callback_); - return ERR_IO_PENDING; - } - - was_used_to_convey_data_ = true; - return CompleteRead(); -} -int DeterministicMockTCPClientSocket::Read( - net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { + IOBuffer* buf, int buf_len, const CompletionCallback& callback) { if (!connected_) - return net::ERR_UNEXPECTED; + return ERR_UNEXPECTED; read_data_ = data_->GetNextRead(); // The buffer should always be big enough to contain all the MockRead data. To @@ -1094,27 +1002,14 @@ int DeterministicMockTCPClientSocket::Read( // TODO(erikchen): Support connect sequencing. int DeterministicMockTCPClientSocket::Connect( - net::OldCompletionCallback* callback) { - if (connected_) - return net::OK; - connected_ = true; - if (data_->connect_data().async) { - RunCallbackAsync(callback, data_->connect_data().result); - return net::ERR_IO_PENDING; - } - return data_->connect_data().result; -} -int DeterministicMockTCPClientSocket::Connect( - const net::CompletionCallback& callback) { + const CompletionCallback& callback) { if (connected_) - return net::OK; - + return OK; connected_ = true; if (data_->connect_data().async) { RunCallbackAsync(callback, data_->connect_data().result); - return net::ERR_IO_PENDING; + return ERR_IO_PENDING; } - return data_->connect_data().result; } @@ -1148,67 +1043,22 @@ base::TimeDelta DeterministicMockTCPClientSocket::GetConnectTimeMicros() const { void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} -class MockSSLClientSocket::OldConnectCallback - : public net::OldCompletionCallbackImpl< - MockSSLClientSocket::OldConnectCallback> { - public: - OldConnectCallback(MockSSLClientSocket *ssl_client_socket, - net::OldCompletionCallback* user_callback, - int rv) - : ALLOW_THIS_IN_INITIALIZER_LIST( - net::OldCompletionCallbackImpl< - MockSSLClientSocket::OldConnectCallback>( - this, &OldConnectCallback::Wrapper)), - ssl_client_socket_(ssl_client_socket), - user_callback_(user_callback), - rv_(rv) { - } - - private: - void Wrapper(int rv) { - if (rv_ == net::OK) - ssl_client_socket_->connected_ = true; - user_callback_->Run(rv_); - delete this; - } - - MockSSLClientSocket* ssl_client_socket_; - net::OldCompletionCallback* user_callback_; - int rv_; -}; -class MockSSLClientSocket::ConnectCallback { - public: - ConnectCallback(MockSSLClientSocket *ssl_client_socket, - const CompletionCallback& user_callback, - int rv) - : ALLOW_THIS_IN_INITIALIZER_LIST(callback_( - base::Bind(&ConnectCallback::Wrapper, base::Unretained(this)))), - ssl_client_socket_(ssl_client_socket), - user_callback_(user_callback), - rv_(rv) { - } - - const CompletionCallback& callback() const { return callback_; } - - private: - void Wrapper(int rv) { - if (rv_ == net::OK) - ssl_client_socket_->connected_ = true; - user_callback_.Run(rv_); - } - - CompletionCallback callback_; - MockSSLClientSocket* ssl_client_socket_; - CompletionCallback user_callback_; - int rv_; -}; +// static +void MockSSLClientSocket::ConnectCallback( + MockSSLClientSocket *ssl_client_socket, + const CompletionCallback& callback, + int rv) { + if (rv == OK) + ssl_client_socket->connected_ = true; + callback.Run(rv); +} MockSSLClientSocket::MockSSLClientSocket( - net::ClientSocketHandle* transport_socket, + ClientSocketHandle* transport_socket, const HostPortPair& host_port_pair, - const net::SSLConfig& ssl_config, + const SSLConfig& ssl_config, SSLHostInfo* ssl_host_info, - net::SSLSocketDataProvider* data) + SSLSocketDataProvider* data) : MockClientSocket(transport_socket->socket()->NetLog().net_log()), transport_(transport_socket), data_(data), @@ -1222,45 +1072,25 @@ MockSSLClientSocket::~MockSSLClientSocket() { Disconnect(); } -int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { - return transport_->socket()->Read(buf, buf_len, callback); -} -int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, - const net::CompletionCallback& callback) { +int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) { return transport_->socket()->Read(buf, buf_len, callback); } -int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { +int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) { return transport_->socket()->Write(buf, buf_len, callback); } -int MockSSLClientSocket::Connect(net::OldCompletionCallback* callback) { - OldConnectCallback* connect_callback = new OldConnectCallback( - this, callback, data_->connect.result); - int rv = transport_->socket()->Connect(connect_callback); - if (rv == net::OK) { - delete connect_callback; - if (data_->connect.result == net::OK) - connected_ = true; - if (data_->connect.async) { - RunCallbackAsync(callback, data_->connect.result); - return net::ERR_IO_PENDING; - } - return data_->connect.result; - } - return rv; -} -int MockSSLClientSocket::Connect(const net::CompletionCallback& callback) { - ConnectCallback connect_callback(this, callback, data_->connect.result); - int rv = transport_->socket()->Connect(connect_callback.callback()); - if (rv == net::OK) { - if (data_->connect.result == net::OK) +int MockSSLClientSocket::Connect(const CompletionCallback& callback) { + int rv = transport_->socket()->Connect( + base::Bind(&ConnectCallback, base::Unretained(this), callback)); + if (rv == OK) { + if (data_->connect.result == OK) connected_ = true; if (data_->connect.async) { RunCallbackAsync(callback, data_->connect.result); - return net::ERR_IO_PENDING; + return ERR_IO_PENDING; } return data_->connect.result; } @@ -1293,14 +1123,14 @@ base::TimeDelta MockSSLClientSocket::GetConnectTimeMicros() const { return base::TimeDelta::FromMicroseconds(-1); } -void MockSSLClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { +void MockSSLClientSocket::GetSSLInfo(SSLInfo* ssl_info) { ssl_info->Reset(); ssl_info->cert = data_->cert; ssl_info->client_cert_sent = data_->client_cert_sent; } void MockSSLClientSocket::GetSSLCertRequestInfo( - net::SSLCertRequestInfo* cert_request_info) { + SSLCertRequestInfo* cert_request_info) { DCHECK(cert_request_info); if (data_->cert_request_info) { cert_request_info->host_and_port = @@ -1338,12 +1168,11 @@ MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, : connected_(false), data_(data), read_offset_(0), - read_data_(false, net::ERR_UNEXPECTED), + read_data_(false, ERR_UNEXPECTED), need_read_data_(true), pending_buf_(NULL), pending_buf_len_(0), - old_pending_callback_(NULL), - net_log_(NetLog::Source(), net_log), + net_log_(net::NetLog::Source(), net_log), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { DCHECK(data_); data_->Reset(); @@ -1351,36 +1180,10 @@ MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, MockUDPClientSocket::~MockUDPClientSocket() {} -int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { +int MockUDPClientSocket::Read(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) { if (!connected_) - return net::ERR_UNEXPECTED; - - // If the buffer is already in use, a read is already in progress! - DCHECK(pending_buf_ == NULL); - - // Store our async IO data. - pending_buf_ = buf; - pending_buf_len_ = buf_len; - old_pending_callback_ = callback; - - if (need_read_data_) { - read_data_ = data_->GetNextRead(); - // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility - // to complete the async IO manually later (via OnReadComplete). - if (read_data_.result == ERR_IO_PENDING) { - DCHECK(callback); // We need to be using async IO in this case. - return ERR_IO_PENDING; - } - need_read_data_ = false; - } - - return CompleteRead(); -} -int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, - const net::CompletionCallback& callback) { - if (!connected_) - return net::ERR_UNEXPECTED; + return ERR_UNEXPECTED; // If the buffer is already in use, a read is already in progress! DCHECK(pending_buf_ == NULL); @@ -1405,8 +1208,8 @@ int MockUDPClientSocket::Read(net::IOBuffer* buf, int buf_len, return CompleteRead(); } -int MockUDPClientSocket::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { +int MockUDPClientSocket::Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) { DCHECK(buf); DCHECK_GT(buf_len, 0); @@ -1469,15 +1272,9 @@ void MockUDPClientSocket::OnReadComplete(const MockRead& data) { // let CompleteRead() schedule a callback. read_data_.async = false; - if (old_pending_callback_) { - net::OldCompletionCallback* callback = old_pending_callback_; - int rv = CompleteRead(); - RunOldCallback(callback, rv); - } else { - net::CompletionCallback callback = pending_callback_; - int rv = CompleteRead(); - RunCallback(callback, rv); - } + net::CompletionCallback callback = pending_callback_; + int rv = CompleteRead(); + RunCallback(callback, rv); } int MockUDPClientSocket::CompleteRead() { @@ -1485,14 +1282,11 @@ int MockUDPClientSocket::CompleteRead() { DCHECK(pending_buf_len_ > 0); // Save the pending async IO data and reset our |pending_| state. - net::IOBuffer* buf = pending_buf_; + IOBuffer* buf = pending_buf_; int buf_len = pending_buf_len_; - net::OldCompletionCallback* old_callback = old_pending_callback_; - net::CompletionCallback callback = pending_callback_; + CompletionCallback callback = pending_callback_; pending_buf_ = NULL; pending_buf_len_ = 0; - old_pending_callback_ = NULL; - pending_callback_.Reset(); pending_callback_.Reset(); int result = read_data_.result; @@ -1513,35 +1307,22 @@ int MockUDPClientSocket::CompleteRead() { } if (read_data_.async) { - DCHECK(old_callback || !callback.is_null()); - if (old_callback) - RunCallbackAsync(old_callback, result); - else - RunCallbackAsync(callback, result); - return net::ERR_IO_PENDING; + DCHECK(!callback.is_null()); + RunCallbackAsync(callback, result); + return ERR_IO_PENDING; } return result; } -void MockUDPClientSocket::RunCallbackAsync(net::OldCompletionCallback* callback, +void MockUDPClientSocket::RunCallbackAsync(const CompletionCallback& callback, int result) { - MessageLoop::current()->PostTask(FROM_HERE, - base::Bind(&MockUDPClientSocket::RunOldCallback, - weak_factory_.GetWeakPtr(), callback, result)); -} -void MockUDPClientSocket::RunCallbackAsync( - const net::CompletionCallback& callback, int result) { - MessageLoop::current()->PostTask(FROM_HERE, + MessageLoop::current()->PostTask( + FROM_HERE, base::Bind(&MockUDPClientSocket::RunCallback, weak_factory_.GetWeakPtr(), callback, result)); } -void MockUDPClientSocket::RunOldCallback(net::OldCompletionCallback* callback, - int result) { - if (callback) - callback->Run(result); -} -void MockUDPClientSocket::RunCallback(const net::CompletionCallback& callback, +void MockUDPClientSocket::RunCallback(const CompletionCallback& callback, int result) { if (!callback.is_null()) callback.Run(result); @@ -1614,20 +1395,19 @@ void ClientSocketPoolTest::ReleaseAllConnections(KeepAlive keep_alive) { MockTransportClientSocketPool::MockConnectJob::MockConnectJob( StreamSocket* socket, ClientSocketHandle* handle, - OldCompletionCallback* callback) + const CompletionCallback& callback) : socket_(socket), handle_(handle), - user_callback_(callback), - ALLOW_THIS_IN_INITIALIZER_LIST( - connect_callback_(this, &MockConnectJob::OnConnect)) { + user_callback_(callback) { } MockTransportClientSocketPool::MockConnectJob::~MockConnectJob() {} int MockTransportClientSocketPool::MockConnectJob::Connect() { - int rv = socket_->Connect(&connect_callback_); + int rv = socket_->Connect(base::Bind(&MockConnectJob::OnConnect, + base::Unretained(this))); if (rv == OK) { - user_callback_ = NULL; + user_callback_.Reset(); OnConnect(OK); } return rv; @@ -1639,7 +1419,7 @@ bool MockTransportClientSocketPool::MockConnectJob::CancelHandle( return false; socket_.reset(); handle_ = NULL; - user_callback_ = NULL; + user_callback_.Reset(); return true; } @@ -1654,10 +1434,10 @@ void MockTransportClientSocketPool::MockConnectJob::OnConnect(int rv) { handle_ = NULL; - if (user_callback_) { - OldCompletionCallback* callback = user_callback_; - user_callback_ = NULL; - callback->Run(rv); + if (!user_callback_.is_null()) { + CompletionCallback callback = user_callback_; + user_callback_.Reset(); + callback.Run(rv); } } @@ -1675,15 +1455,21 @@ MockTransportClientSocketPool::MockTransportClientSocketPool( MockTransportClientSocketPool::~MockTransportClientSocketPool() {} -int MockTransportClientSocketPool::RequestSocket(const std::string& group_name, - const void* socket_params, - RequestPriority priority, - ClientSocketHandle* handle, - OldCompletionCallback* callback, - const BoundNetLog& net_log) { +int MockTransportClientSocketPool::RequestSocket( + const std::string& group_name, + const void* socket_params, + RequestPriority priority, + ClientSocketHandle* handle, + OldCompletionCallback* callback, + const BoundNetLog& net_log) { StreamSocket* socket = client_socket_factory_->CreateTransportClientSocket( AddressList(), net_log.net_log(), net::NetLog::Source()); - MockConnectJob* job = new MockConnectJob(socket, handle, callback); + CompletionCallback cb; + if (callback) { + cb = base::Bind(&OldCompletionCallback::Run<int>, + base::Unretained(callback)); + } + MockConnectJob* job = new MockConnectJob(socket, handle, cb); job_list_.push_back(job); handle->set_pool_id(1); return job->Connect(); @@ -1736,7 +1522,7 @@ DatagramClientSocket* DeterministicMockClientSocketFactory::CreateDatagramClientSocket( DatagramSocket::BindType bind_type, const RandIntCallback& rand_int_cb, - NetLog* net_log, + net::NetLog* net_log, const NetLog::Source& source) { NOTREACHED(); return NULL; diff --git a/net/socket/socket_test_util.h b/net/socket/socket_test_util.h index 73ffc3d..84c9c05 100644 --- a/net/socket/socket_test_util.h +++ b/net/socket/socket_test_util.h @@ -19,7 +19,6 @@ #include "base/memory/weak_ptr.h" #include "base/string16.h" #include "net/base/address_list.h" -#include "net/base/completion_callback.h" #include "net/base/io_buffer.h" #include "net/base/net_errors.h" #include "net/base/net_log.h" @@ -267,7 +266,7 @@ struct SSLSocketDataProvider { std::string server_protos; bool was_npn_negotiated; bool client_cert_sent; - net::SSLCertRequestInfo* cert_request_info; + SSLCertRequestInfo* cert_request_info; scoped_refptr<X509Certificate> cert; }; @@ -310,7 +309,7 @@ class DelayedSocketData : public StaticSocketDataProvider, private: int write_delay_; - ScopedRunnableMethodFactory<DelayedSocketData> factory_; + base::WeakPtrFactory<DelayedSocketData> weak_factory_; }; // A DataProvider where the reads are ordered. @@ -345,7 +344,7 @@ class OrderedSocketData : public StaticSocketDataProvider, MockRead* reads, size_t reads_count, MockWrite* writes, size_t writes_count); - void SetOldCompletionCallback(OldCompletionCallback* callback) { + void SetCompletionCallback(const CompletionCallback& callback) { callback_ = callback; } @@ -364,9 +363,9 @@ class OrderedSocketData : public StaticSocketDataProvider, int sequence_number_; int loop_stop_stage_; - OldCompletionCallback* callback_; + CompletionCallback callback_; bool blocked_; - ScopedRunnableMethodFactory<OrderedSocketData> factory_; + base::WeakPtrFactory<OrderedSocketData> weak_factory_; }; class DeterministicMockTCPClientSocket; @@ -581,23 +580,21 @@ class MockClientSocketFactory : public ClientSocketFactory { std::vector<MockSSLClientSocket*> ssl_client_sockets_; }; -class MockClientSocket : public net::SSLClientSocket { +class MockClientSocket : public SSLClientSocket { public: + // TODO(ajwong): Why do we need net::NetLog? explicit MockClientSocket(net::NetLog* net_log); // Socket implementation. - virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) = 0; - virtual int Read(net::IOBuffer* buf, int buf_len, - const net::CompletionCallback& callback) = 0; - virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) = 0; + virtual int Read(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) = 0; + virtual int Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) = 0; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; // StreamSocket implementation. - virtual int Connect(net::OldCompletionCallback* callback) = 0; - virtual int Connect(const net::CompletionCallback& callback) = 0; + virtual int Connect(const CompletionCallback& callback) = 0; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; virtual bool IsConnectedAndIdle() const OVERRIDE; @@ -608,9 +605,9 @@ class MockClientSocket : public net::SSLClientSocket { virtual void SetOmniboxSpeculation() OVERRIDE {} // SSLClientSocket implementation. - virtual void GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE; + virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; virtual void GetSSLCertRequestInfo( - net::SSLCertRequestInfo* cert_request_info) OVERRIDE; + SSLCertRequestInfo* cert_request_info) OVERRIDE; virtual int ExportKeyingMaterial(const base::StringPiece& label, const base::StringPiece& context, unsigned char *out, @@ -620,38 +617,33 @@ class MockClientSocket : public net::SSLClientSocket { protected: virtual ~MockClientSocket(); - void RunCallbackAsync(net::OldCompletionCallback* callback, int result); - void RunCallbackAsync(const net::CompletionCallback& callback, int result); - void RunOldCallback(net::OldCompletionCallback*, int result); - void RunCallback(const net::CompletionCallback&, int result); + void RunCallbackAsync(const CompletionCallback& callback, int result); + void RunCallback(const CompletionCallback& callback, int result); base::WeakPtrFactory<MockClientSocket> weak_factory_; // True if Connect completed successfully and Disconnect hasn't been called. bool connected_; - net::BoundNetLog net_log_; + BoundNetLog net_log_; }; class MockTCPClientSocket : public MockClientSocket, public AsyncSocket { public: - MockTCPClientSocket(const net::AddressList& addresses, net::NetLog* net_log, - net::SocketDataProvider* socket); + MockTCPClientSocket(const AddressList& addresses, net::NetLog* net_log, + SocketDataProvider* socket); virtual ~MockTCPClientSocket(); - net::AddressList addresses() const { return addresses_; } + AddressList addresses() const { return addresses_; } // Socket implementation. - virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, - const net::CompletionCallback& callback) OVERRIDE; - virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + virtual int Read(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) OVERRIDE; + virtual int Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) OVERRIDE; // StreamSocket implementation. - virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; - virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; + virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; virtual bool IsConnectedAndIdle() const OVERRIDE; @@ -667,12 +659,12 @@ class MockTCPClientSocket : public MockClientSocket, public AsyncSocket { private: int CompleteRead(); - net::AddressList addresses_; + AddressList addresses_; - net::SocketDataProvider* data_; + SocketDataProvider* data_; int read_offset_; int num_bytes_read_; - net::MockRead read_data_; + MockRead read_data_; bool need_read_data_; // True if the peer has closed the connection. This allows us to simulate @@ -681,10 +673,9 @@ class MockTCPClientSocket : public MockClientSocket, public AsyncSocket { bool peer_closed_connection_; // While an asynchronous IO is pending, we save our user-buffer state. - net::IOBuffer* pending_buf_; + IOBuffer* pending_buf_; int pending_buf_len_; - net::OldCompletionCallback* old_pending_callback_; - net::CompletionCallback pending_callback_; + CompletionCallback pending_callback_; bool was_used_to_convey_data_; }; @@ -693,7 +684,7 @@ class DeterministicMockTCPClientSocket : public MockClientSocket, public base::SupportsWeakPtr<DeterministicMockTCPClientSocket> { public: DeterministicMockTCPClientSocket(net::NetLog* net_log, - net::DeterministicSocketData* data); + DeterministicSocketData* data); virtual ~DeterministicMockTCPClientSocket(); bool write_pending() const { return write_pending_; } @@ -703,16 +694,14 @@ class DeterministicMockTCPClientSocket : public MockClientSocket, int CompleteRead(); // Socket implementation. - virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, - const net::CompletionCallback& callback) OVERRIDE; + // Socket: + virtual int Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) OVERRIDE; + virtual int Read(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) OVERRIDE; // StreamSocket implementation. - virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; - virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; + virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; virtual bool IsConnectedAndIdle() const OVERRIDE; @@ -726,41 +715,37 @@ class DeterministicMockTCPClientSocket : public MockClientSocket, private: bool write_pending_; - net::OldCompletionCallback* write_callback_; + CompletionCallback write_callback_; int write_result_; - net::MockRead read_data_; + MockRead read_data_; - net::IOBuffer* read_buf_; + IOBuffer* read_buf_; int read_buf_len_; bool read_pending_; - net::OldCompletionCallback* old_read_callback_; - net::CompletionCallback read_callback_; - net::DeterministicSocketData* data_; + CompletionCallback read_callback_; + DeterministicSocketData* data_; bool was_used_to_convey_data_; }; class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { public: MockSSLClientSocket( - net::ClientSocketHandle* transport_socket, + ClientSocketHandle* transport_socket, const HostPortPair& host_and_port, - const net::SSLConfig& ssl_config, + const SSLConfig& ssl_config, SSLHostInfo* ssl_host_info, - net::SSLSocketDataProvider* socket); + SSLSocketDataProvider* socket); virtual ~MockSSLClientSocket(); // Socket implementation. - virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, - const net::CompletionCallback& callback) OVERRIDE; - virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + virtual int Read(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) OVERRIDE; + virtual int Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) OVERRIDE; // StreamSocket implementation. - virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; - virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; + virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; virtual bool WasEverUsed() const OVERRIDE; @@ -769,9 +754,9 @@ class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; // SSLClientSocket implementation. - virtual void GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE; + virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; virtual void GetSSLCertRequestInfo( - net::SSLCertRequestInfo* cert_request_info) OVERRIDE; + SSLCertRequestInfo* cert_request_info) OVERRIDE; virtual NextProtoStatus GetNextProto(std::string* proto, std::string* server_protos) OVERRIDE; virtual bool was_npn_negotiated() const OVERRIDE; @@ -781,11 +766,12 @@ class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { virtual void OnReadComplete(const MockRead& data) OVERRIDE; private: - class OldConnectCallback; - class ConnectCallback; + static void ConnectCallback(MockSSLClientSocket *ssl_client_socket, + const CompletionCallback& callback, + int rv); scoped_ptr<ClientSocketHandle> transport_; - net::SSLSocketDataProvider* data_; + SSLSocketDataProvider* data_; bool is_npn_state_set_; bool new_npn_value_; bool was_used_to_convey_data_; @@ -798,12 +784,10 @@ class MockUDPClientSocket : public DatagramClientSocket, virtual ~MockUDPClientSocket(); // Socket implementation. - virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, - const net::CompletionCallback& callback) OVERRIDE; - virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + virtual int Read(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) OVERRIDE; + virtual int Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; @@ -822,22 +806,19 @@ class MockUDPClientSocket : public DatagramClientSocket, private: int CompleteRead(); - void RunCallbackAsync(net::OldCompletionCallback* callback, int result); - void RunCallbackAsync(const net::CompletionCallback& callback, int result); - void RunOldCallback(net::OldCompletionCallback* callback, int result); - void RunCallback(const net::CompletionCallback& callback, int result); + void RunCallbackAsync(const CompletionCallback& callback, int result); + void RunCallback(const CompletionCallback& callback, int result); bool connected_; SocketDataProvider* data_; int read_offset_; - net::MockRead read_data_; + MockRead read_data_; bool need_read_data_; // While an asynchronous IO is pending, we save our user-buffer state. - net::IOBuffer* pending_buf_; + IOBuffer* pending_buf_; int pending_buf_len_; - net::OldCompletionCallback* old_pending_callback_; - net::CompletionCallback pending_callback_; + CompletionCallback pending_callback_; BoundNetLog net_log_; @@ -926,7 +907,7 @@ class MockTransportClientSocketPool : public TransportClientSocketPool { class MockConnectJob { public: MockConnectJob(StreamSocket* socket, ClientSocketHandle* handle, - OldCompletionCallback* callback); + const CompletionCallback& callback); ~MockConnectJob(); int Connect(); @@ -937,8 +918,7 @@ class MockTransportClientSocketPool : public TransportClientSocketPool { scoped_ptr<StreamSocket> socket_; ClientSocketHandle* handle_; - OldCompletionCallback* user_callback_; - OldCompletionCallbackImpl<MockConnectJob> connect_callback_; + CompletionCallback user_callback_; DISALLOW_COPY_AND_ASSIGN(MockConnectJob); }; diff --git a/net/socket/socks5_client_socket.cc b/net/socket/socks5_client_socket.cc index ea5fc7a..b8b3439 100644 --- a/net/socket/socks5_client_socket.cc +++ b/net/socket/socks5_client_socket.cc @@ -31,10 +31,10 @@ SOCKS5ClientSocket::SOCKS5ClientSocket( ClientSocketHandle* transport_socket, const HostResolver::RequestInfo& req_info) : ALLOW_THIS_IN_INITIALIZER_LIST( - io_callback_(this, &SOCKS5ClientSocket::OnIOComplete)), + io_callback_(base::Bind(&SOCKS5ClientSocket::OnIOComplete, + base::Unretained(this)))), transport_(transport_socket), next_state_(STATE_NONE), - old_user_callback_(NULL), completed_handshake_(false), bytes_sent_(0), bytes_received_(0), @@ -47,10 +47,10 @@ SOCKS5ClientSocket::SOCKS5ClientSocket( StreamSocket* transport_socket, const HostResolver::RequestInfo& req_info) : ALLOW_THIS_IN_INITIALIZER_LIST( - io_callback_(this, &SOCKS5ClientSocket::OnIOComplete)), + io_callback_(base::Bind(&SOCKS5ClientSocket::OnIOComplete, + base::Unretained(this)))), transport_(new ClientSocketHandle()), next_state_(STATE_NONE), - old_user_callback_(NULL), completed_handshake_(false), bytes_sent_(0), bytes_received_(0), @@ -64,11 +64,11 @@ SOCKS5ClientSocket::~SOCKS5ClientSocket() { Disconnect(); } -int SOCKS5ClientSocket::Connect(OldCompletionCallback* callback) { +int SOCKS5ClientSocket::Connect(const CompletionCallback& callback) { DCHECK(transport_.get()); DCHECK(transport_->socket()); DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_ && user_callback_.is_null()); + DCHECK(user_callback_.is_null()); // If already connected, then just return OK. if (completed_handshake_) @@ -81,35 +81,12 @@ int SOCKS5ClientSocket::Connect(OldCompletionCallback* callback) { int rv = DoLoop(OK); if (rv == ERR_IO_PENDING) { - old_user_callback_ = callback; + user_callback_ = callback; } else { net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS5_CONNECT, rv); } return rv; } -int SOCKS5ClientSocket::Connect(const CompletionCallback& callback) { - DCHECK(transport_.get()); - DCHECK(transport_->socket()); - DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_ && user_callback_.is_null()); - - // If already connected, then just return OK. - if (completed_handshake_) - return OK; - - net_log_.BeginEvent(NetLog::TYPE_SOCKS5_CONNECT, NULL); - - next_state_ = STATE_GREET_WRITE; - buffer_.clear(); - - int rv = DoLoop(OK); - if (rv == ERR_IO_PENDING) - user_callback_ = callback; - else - net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS5_CONNECT, rv); - - return rv; -} void SOCKS5ClientSocket::Disconnect() { completed_handshake_ = false; @@ -118,7 +95,6 @@ void SOCKS5ClientSocket::Disconnect() { // Reset other states to make sure they aren't mistakenly used later. // These are the states initialized by Connect(). next_state_ = STATE_NONE; - old_user_callback_ = NULL; user_callback_.Reset(); } @@ -185,18 +161,10 @@ base::TimeDelta SOCKS5ClientSocket::GetConnectTimeMicros() const { // Read is called by the transport layer above to read. This can only be done // if the SOCKS handshake is complete. int SOCKS5ClientSocket::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(completed_handshake_); - DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_ && user_callback_.is_null()); - - return transport_->socket()->Read(buf, buf_len, callback); -} -int SOCKS5ClientSocket::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { DCHECK(completed_handshake_); DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_ && user_callback_.is_null()); + DCHECK(user_callback_.is_null()); return transport_->socket()->Read(buf, buf_len, callback); } @@ -204,10 +172,10 @@ int SOCKS5ClientSocket::Read(IOBuffer* buf, int buf_len, // Write is called by the transport layer. This can only be done if the // SOCKS handshake is complete. int SOCKS5ClientSocket::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(completed_handshake_); DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_); + DCHECK(user_callback_.is_null()); return transport_->socket()->Write(buf, buf_len, callback); } @@ -222,19 +190,13 @@ bool SOCKS5ClientSocket::SetSendBufferSize(int32 size) { void SOCKS5ClientSocket::DoCallback(int result) { DCHECK_NE(ERR_IO_PENDING, result); - DCHECK(old_user_callback_ || !user_callback_.is_null()); + DCHECK(!user_callback_.is_null()); // Since Run() may result in Read being called, // clear user_callback_ up front. - if (old_user_callback_) { - OldCompletionCallback* c = old_user_callback_; - old_user_callback_ = NULL; - c->Run(result); - } else { - CompletionCallback c = user_callback_; - user_callback_.Reset(); - c.Run(result); - } + CompletionCallback c = user_callback_; + user_callback_.Reset(); + c.Run(result); } void SOCKS5ClientSocket::OnIOComplete(int result) { @@ -323,7 +285,7 @@ int SOCKS5ClientSocket::DoGreetWrite() { memcpy(handshake_buf_->data(), &buffer_.data()[bytes_sent_], handshake_buf_len); return transport_->socket()->Write(handshake_buf_, handshake_buf_len, - &io_callback_); + io_callback_); } int SOCKS5ClientSocket::DoGreetWriteComplete(int result) { @@ -346,7 +308,7 @@ int SOCKS5ClientSocket::DoGreetRead() { size_t handshake_buf_len = kGreetReadHeaderSize - bytes_received_; handshake_buf_ = new IOBuffer(handshake_buf_len); return transport_->socket()->Read(handshake_buf_, handshake_buf_len, - &io_callback_); + io_callback_); } int SOCKS5ClientSocket::DoGreetReadComplete(int result) { @@ -424,7 +386,7 @@ int SOCKS5ClientSocket::DoHandshakeWrite() { memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], handshake_buf_len); return transport_->socket()->Write(handshake_buf_, handshake_buf_len, - &io_callback_); + io_callback_); } int SOCKS5ClientSocket::DoHandshakeWriteComplete(int result) { @@ -458,7 +420,7 @@ int SOCKS5ClientSocket::DoHandshakeRead() { int handshake_buf_len = read_header_size - bytes_received_; handshake_buf_ = new IOBuffer(handshake_buf_len); return transport_->socket()->Read(handshake_buf_, handshake_buf_len, - &io_callback_); + io_callback_); } int SOCKS5ClientSocket::DoHandshakeReadComplete(int result) { diff --git a/net/socket/socks5_client_socket.h b/net/socket/socks5_client_socket.h index b83a347..84bb325 100644 --- a/net/socket/socks5_client_socket.h +++ b/net/socket/socks5_client_socket.h @@ -51,7 +51,6 @@ class NET_EXPORT_PRIVATE SOCKS5ClientSocket : public StreamSocket { // StreamSocket implementation. // Does the SOCKS handshake and completes the protocol. - virtual int Connect(OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -67,13 +66,10 @@ class NET_EXPORT_PRIVATE SOCKS5ClientSocket : public StreamSocket { // Socket implementation. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; @@ -125,7 +121,7 @@ class NET_EXPORT_PRIVATE SOCKS5ClientSocket : public StreamSocket { // and return OK on success. int BuildHandshakeWriteBuffer(std::string* handshake) const; - OldCompletionCallbackImpl<SOCKS5ClientSocket> io_callback_; + CompletionCallback io_callback_; // Stores the underlying socket. scoped_ptr<ClientSocketHandle> transport_; @@ -133,7 +129,6 @@ class NET_EXPORT_PRIVATE SOCKS5ClientSocket : public StreamSocket { State next_state_; // Stores the callback to the layer above, called on completing Connect(). - OldCompletionCallback* old_user_callback_; CompletionCallback user_callback_; // This IOBuffer is used by the class to read and write diff --git a/net/socket/socks5_client_socket_unittest.cc b/net/socket/socks5_client_socket_unittest.cc index 85b07b8..72f015a 100644 --- a/net/socket/socks5_client_socket_unittest.cc +++ b/net/socket/socks5_client_socket_unittest.cc @@ -47,7 +47,7 @@ class SOCKS5ClientSocketTest : public PlatformTest { scoped_ptr<SOCKS5ClientSocket> user_sock_; AddressList address_list_; StreamSocket* tcp_sock_; - TestOldCompletionCallback callback_; + TestCompletionCallback callback_; scoped_ptr<MockHostResolver> host_resolver_; scoped_ptr<SocketDataProvider> data_; @@ -83,12 +83,12 @@ SOCKS5ClientSocket* SOCKS5ClientSocketTest::BuildMockSocket( const std::string& hostname, int port, NetLog* net_log) { - TestOldCompletionCallback callback; + TestCompletionCallback callback; data_.reset(new StaticSocketDataProvider(reads, reads_count, writes, writes_count)); tcp_sock_ = new MockTCPClientSocket(address_list_, net_log, data_.get()); - int rv = tcp_sock_->Connect(&callback); + int rv = tcp_sock_->Connect(callback.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); rv = callback.WaitForResult(); EXPECT_EQ(OK, rv); @@ -131,7 +131,7 @@ TEST_F(SOCKS5ClientSocketTest, CompleteHandshake) { EXPECT_TRUE(tcp_sock_->IsConnected()); EXPECT_FALSE(user_sock_->IsConnected()); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_FALSE(user_sock_->IsConnected()); @@ -151,13 +151,13 @@ TEST_F(SOCKS5ClientSocketTest, CompleteHandshake) { scoped_refptr<IOBuffer> buffer(new IOBuffer(payload_write.size())); memcpy(buffer->data(), payload_write.data(), payload_write.size()); - rv = user_sock_->Write(buffer, payload_write.size(), &callback_); + rv = user_sock_->Write(buffer, payload_write.size(), callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); rv = callback_.WaitForResult(); EXPECT_EQ(static_cast<int>(payload_write.size()), rv); buffer = new IOBuffer(payload_read.size()); - rv = user_sock_->Read(buffer, payload_read.size(), &callback_); + rv = user_sock_->Read(buffer, payload_read.size(), callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); rv = callback_.WaitForResult(); EXPECT_EQ(static_cast<int>(payload_read.size()), rv); @@ -197,7 +197,7 @@ TEST_F(SOCKS5ClientSocketTest, ConnectAndDisconnectTwice) { data_writes, arraysize(data_writes), hostname, 80, NULL)); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(OK, rv); EXPECT_TRUE(user_sock_->IsConnected()); @@ -221,8 +221,8 @@ TEST_F(SOCKS5ClientSocketTest, LargeHostNameFails) { // Try to connect -- should fail (without having read/written anything to // the transport socket first) because the hostname is too long. - TestOldCompletionCallback callback; - int rv = user_sock_->Connect(&callback); + TestCompletionCallback callback; + int rv = user_sock_->Connect(callback.callback()); EXPECT_EQ(ERR_SOCKS_CONNECTION_FAILED, rv); } @@ -254,7 +254,7 @@ TEST_F(SOCKS5ClientSocketTest, PartialReadWrites) { user_sock_.reset(BuildMockSocket(data_reads, arraysize(data_reads), data_writes, arraysize(data_writes), hostname, 80, &net_log_)); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); net::CapturingNetLog::EntryList net_log_entries; @@ -285,7 +285,7 @@ TEST_F(SOCKS5ClientSocketTest, PartialReadWrites) { user_sock_.reset(BuildMockSocket(data_reads, arraysize(data_reads), data_writes, arraysize(data_writes), hostname, 80, &net_log_)); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); net::CapturingNetLog::EntryList net_log_entries; @@ -315,7 +315,7 @@ TEST_F(SOCKS5ClientSocketTest, PartialReadWrites) { user_sock_.reset(BuildMockSocket(data_reads, arraysize(data_reads), data_writes, arraysize(data_writes), hostname, 80, &net_log_)); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); net::CapturingNetLog::EntryList net_log_entries; net_log_.GetEntries(&net_log_entries); @@ -346,7 +346,7 @@ TEST_F(SOCKS5ClientSocketTest, PartialReadWrites) { user_sock_.reset(BuildMockSocket(data_reads, arraysize(data_reads), data_writes, arraysize(data_writes), hostname, 80, &net_log_)); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); net::CapturingNetLog::EntryList net_log_entries; net_log_.GetEntries(&net_log_entries); diff --git a/net/socket/socks_client_socket.cc b/net/socket/socks_client_socket.cc index 623f202..4c368c1 100644 --- a/net/socket/socks_client_socket.cc +++ b/net/socket/socks_client_socket.cc @@ -58,11 +58,8 @@ COMPILE_ASSERT(sizeof(SOCKS4ServerResponse) == kReadHeaderSize, SOCKSClientSocket::SOCKSClientSocket(ClientSocketHandle* transport_socket, const HostResolver::RequestInfo& req_info, HostResolver* host_resolver) - : ALLOW_THIS_IN_INITIALIZER_LIST( - io_callback_(this, &SOCKSClientSocket::OnIOComplete)), - transport_(transport_socket), + : transport_(transport_socket), next_state_(STATE_NONE), - old_user_callback_(NULL), completed_handshake_(false), bytes_sent_(0), bytes_received_(0), @@ -74,11 +71,8 @@ SOCKSClientSocket::SOCKSClientSocket(ClientSocketHandle* transport_socket, SOCKSClientSocket::SOCKSClientSocket(StreamSocket* transport_socket, const HostResolver::RequestInfo& req_info, HostResolver* host_resolver) - : ALLOW_THIS_IN_INITIALIZER_LIST( - io_callback_(this, &SOCKSClientSocket::OnIOComplete)), - transport_(new ClientSocketHandle()), + : transport_(new ClientSocketHandle()), next_state_(STATE_NONE), - old_user_callback_(NULL), completed_handshake_(false), bytes_sent_(0), bytes_received_(0), @@ -92,11 +86,11 @@ SOCKSClientSocket::~SOCKSClientSocket() { Disconnect(); } -int SOCKSClientSocket::Connect(OldCompletionCallback* callback) { +int SOCKSClientSocket::Connect(const CompletionCallback& callback) { DCHECK(transport_.get()); DCHECK(transport_->socket()); DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_ && user_callback_.is_null()); + DCHECK(user_callback_.is_null()); // If already connected, then just return OK. if (completed_handshake_) @@ -108,34 +102,12 @@ int SOCKSClientSocket::Connect(OldCompletionCallback* callback) { int rv = DoLoop(OK); if (rv == ERR_IO_PENDING) { - old_user_callback_ = callback; + user_callback_ = callback; } else { net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv); } return rv; } -int SOCKSClientSocket::Connect(const net::CompletionCallback& callback) { - DCHECK(transport_.get()); - DCHECK(transport_->socket()); - DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_ && user_callback_.is_null()); - - // If already connected, then just return OK. - if (completed_handshake_) - return OK; - - next_state_ = STATE_RESOLVE_HOST; - - net_log_.BeginEvent(NetLog::TYPE_SOCKS_CONNECT, NULL); - - int rv = DoLoop(OK); - if (rv == ERR_IO_PENDING) - user_callback_ = callback; - else - net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv); - - return rv; -} void SOCKSClientSocket::Disconnect() { completed_handshake_ = false; @@ -145,7 +117,6 @@ void SOCKSClientSocket::Disconnect() { // Reset other states to make sure they aren't mistakenly used later. // These are the states initialized by Connect(). next_state_ = STATE_NONE; - old_user_callback_ = NULL; user_callback_.Reset(); } @@ -213,18 +184,10 @@ base::TimeDelta SOCKSClientSocket::GetConnectTimeMicros() const { // Read is called by the transport layer above to read. This can only be done // if the SOCKS handshake is complete. int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(completed_handshake_); - DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_ && user_callback_.is_null()); - - return transport_->socket()->Read(buf, buf_len, callback); -} -int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { DCHECK(completed_handshake_); DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_ && user_callback_.is_null()); + DCHECK(user_callback_.is_null()); return transport_->socket()->Read(buf, buf_len, callback); } @@ -232,10 +195,10 @@ int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len, // Write is called by the transport layer. This can only be done if the // SOCKS handshake is complete. int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(completed_handshake_); DCHECK_EQ(STATE_NONE, next_state_); - DCHECK(!old_user_callback_); + DCHECK(user_callback_.is_null()); return transport_->socket()->Write(buf, buf_len, callback); } @@ -250,21 +213,14 @@ bool SOCKSClientSocket::SetSendBufferSize(int32 size) { void SOCKSClientSocket::DoCallback(int result) { DCHECK_NE(ERR_IO_PENDING, result); - DCHECK(old_user_callback_ || !user_callback_.is_null()); + DCHECK(!user_callback_.is_null()); // Since Run() may result in Read being called, // clear user_callback_ up front. - if (old_user_callback_) { - OldCompletionCallback* c = old_user_callback_; - old_user_callback_ = NULL; - DVLOG(1) << "Finished setting up SOCKS handshake"; - c->Run(result); - } else { - CompletionCallback c = user_callback_; - user_callback_.Reset(); - DVLOG(1) << "Finished setting up SOCKS handshake"; - c.Run(result); - } + CompletionCallback c = user_callback_; + user_callback_.Reset(); + DVLOG(1) << "Finished setting up SOCKS handshake"; + c.Run(result); } void SOCKSClientSocket::OnIOComplete(int result) { @@ -379,8 +335,9 @@ int SOCKSClientSocket::DoHandshakeWrite() { handshake_buf_ = new IOBuffer(handshake_buf_len); memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], handshake_buf_len); - return transport_->socket()->Write(handshake_buf_, handshake_buf_len, - &io_callback_); + return transport_->socket()->Write( + handshake_buf_, handshake_buf_len, + base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this))); } int SOCKSClientSocket::DoHandshakeWriteComplete(int result) { @@ -413,7 +370,8 @@ int SOCKSClientSocket::DoHandshakeRead() { int handshake_buf_len = kReadHeaderSize - bytes_received_; handshake_buf_ = new IOBuffer(handshake_buf_len); return transport_->socket()->Read(handshake_buf_, handshake_buf_len, - &io_callback_); + base::Bind(&SOCKSClientSocket::OnIOComplete, + base::Unretained(this))); } int SOCKSClientSocket::DoHandshakeReadComplete(int result) { diff --git a/net/socket/socks_client_socket.h b/net/socket/socks_client_socket.h index 1a4a75c..fb88cd2 100644 --- a/net/socket/socks_client_socket.h +++ b/net/socket/socks_client_socket.h @@ -48,8 +48,7 @@ class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket { // StreamSocket implementation. // Does the SOCKS handshake and completes the protocol. - virtual int Connect(OldCompletionCallback* callback) OVERRIDE; - virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; + virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; virtual bool IsConnectedAndIdle() const OVERRIDE; @@ -64,13 +63,10 @@ class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket { // Socket implementation. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; @@ -106,15 +102,12 @@ class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket { const std::string BuildHandshakeWriteBuffer() const; - OldCompletionCallbackImpl<SOCKSClientSocket> io_callback_; - // Stores the underlying socket. scoped_ptr<ClientSocketHandle> transport_; State next_state_; // Stores the callback to the layer above, called on completing Connect(). - OldCompletionCallback* old_user_callback_; CompletionCallback user_callback_; // This IOBuffer is used by the class to read and write diff --git a/net/socket/socks_client_socket_pool.cc b/net/socket/socks_client_socket_pool.cc index 5fa52bf..2602a3e 100644 --- a/net/socket/socks_client_socket_pool.cc +++ b/net/socket/socks_client_socket_pool.cc @@ -51,7 +51,7 @@ SOCKSConnectJob::SOCKSConnectJob( transport_pool_(transport_pool), resolver_(host_resolver), ALLOW_THIS_IN_INITIALIZER_LIST( - callback_(this, &SOCKSConnectJob::OnIOComplete)) { + callback_old_(this, &SOCKSConnectJob::OnIOComplete)) { } SOCKSConnectJob::~SOCKSConnectJob() { @@ -118,7 +118,7 @@ int SOCKSConnectJob::DoTransportConnect() { return transport_socket_handle_->Init(group_name(), socks_params_->transport_params(), socks_params_->destination().priority(), - &callback_, + &callback_old_, transport_pool_, net_log()); } @@ -147,7 +147,8 @@ int SOCKSConnectJob::DoSOCKSConnect() { socks_params_->destination(), resolver_)); } - return socket_->Connect(&callback_); + return socket_->Connect( + base::Bind(&SOCKSConnectJob::OnIOComplete, base::Unretained(this))); } int SOCKSConnectJob::DoSOCKSConnectComplete(int result) { diff --git a/net/socket/socks_client_socket_pool.h b/net/socket/socks_client_socket_pool.h index 501f3bf..422b772 100644 --- a/net/socket/socks_client_socket_pool.h +++ b/net/socket/socks_client_socket_pool.h @@ -98,7 +98,7 @@ class SOCKSConnectJob : public ConnectJob { HostResolver* const resolver_; State next_state_; - OldCompletionCallbackImpl<SOCKSConnectJob> callback_; + OldCompletionCallbackImpl<SOCKSConnectJob> callback_old_; scoped_ptr<ClientSocketHandle> transport_socket_handle_; scoped_ptr<StreamSocket> socket_; diff --git a/net/socket/socks_client_socket_unittest.cc b/net/socket/socks_client_socket_unittest.cc index 6762e1bb..1a6db28 100644 --- a/net/socket/socks_client_socket_unittest.cc +++ b/net/socket/socks_client_socket_unittest.cc @@ -38,7 +38,7 @@ class SOCKSClientSocketTest : public PlatformTest { scoped_ptr<SOCKSClientSocket> user_sock_; AddressList address_list_; StreamSocket* tcp_sock_; - TestOldCompletionCallback callback_; + TestCompletionCallback callback_; scoped_ptr<MockHostResolver> host_resolver_; scoped_ptr<SocketDataProvider> data_; }; @@ -62,12 +62,12 @@ SOCKSClientSocket* SOCKSClientSocketTest::BuildMockSocket( int port, NetLog* net_log) { - TestOldCompletionCallback callback; + TestCompletionCallback callback; data_.reset(new StaticSocketDataProvider(reads, reads_count, writes, writes_count)); tcp_sock_ = new MockTCPClientSocket(address_list_, net_log, data_.get()); - int rv = tcp_sock_->Connect(&callback); + int rv = tcp_sock_->Connect(callback.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); rv = callback.WaitForResult(); EXPECT_EQ(OK, rv); @@ -144,7 +144,7 @@ TEST_F(SOCKSClientSocketTest, CompleteHandshake) { EXPECT_TRUE(tcp_sock_->IsConnected()); EXPECT_FALSE(user_sock_->IsConnected()); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); net::CapturingNetLog::EntryList entries; @@ -162,13 +162,13 @@ TEST_F(SOCKSClientSocketTest, CompleteHandshake) { scoped_refptr<IOBuffer> buffer(new IOBuffer(payload_write.size())); memcpy(buffer->data(), payload_write.data(), payload_write.size()); - rv = user_sock_->Write(buffer, payload_write.size(), &callback_); + rv = user_sock_->Write(buffer, payload_write.size(), callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); rv = callback_.WaitForResult(); EXPECT_EQ(static_cast<int>(payload_write.size()), rv); buffer = new IOBuffer(payload_read.size()); - rv = user_sock_->Read(buffer, payload_read.size(), &callback_); + rv = user_sock_->Read(buffer, payload_read.size(), callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); rv = callback_.WaitForResult(); EXPECT_EQ(static_cast<int>(payload_read.size()), rv); @@ -213,7 +213,7 @@ TEST_F(SOCKSClientSocketTest, HandshakeFailures) { "localhost", 80, &log)); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); net::CapturingNetLog::EntryList entries; @@ -250,7 +250,7 @@ TEST_F(SOCKSClientSocketTest, PartialServerReads) { "localhost", 80, &log)); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); net::CapturingNetLog::EntryList entries; log.GetEntries(&entries); @@ -288,7 +288,7 @@ TEST_F(SOCKSClientSocketTest, PartialClientWrites) { "localhost", 80, &log)); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); net::CapturingNetLog::EntryList entries; log.GetEntries(&entries); @@ -320,7 +320,7 @@ TEST_F(SOCKSClientSocketTest, FailedSocketRead) { "localhost", 80, &log)); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); net::CapturingNetLog::EntryList entries; log.GetEntries(&entries); @@ -350,7 +350,7 @@ TEST_F(SOCKSClientSocketTest, FailedDNS) { hostname, 80, &log)); - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); net::CapturingNetLog::EntryList entries; log.GetEntries(&entries); @@ -382,7 +382,7 @@ TEST_F(SOCKSClientSocketTest, DisconnectWhileHostResolveInProgress) { NULL)); // Start connecting (will get stuck waiting for the host to resolve). - int rv = user_sock_->Connect(&callback_); + int rv = user_sock_->Connect(callback_.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_FALSE(user_sock_->IsConnected()); diff --git a/net/socket/ssl_client_socket_mac.cc b/net/socket/ssl_client_socket_mac.cc index f58d340..b6e03f7 100644 --- a/net/socket/ssl_client_socket_mac.cc +++ b/net/socket/ssl_client_socket_mac.cc @@ -524,16 +524,9 @@ SSLClientSocketMac::SSLClientSocketMac(ClientSocketHandle* transport_socket, const HostPortPair& host_and_port, const SSLConfig& ssl_config, const SSLClientSocketContext& context) - : transport_read_callback_(this, - &SSLClientSocketMac::OnTransportReadComplete), - transport_write_callback_(this, - &SSLClientSocketMac::OnTransportWriteComplete), - transport_(transport_socket), + : transport_(transport_socket), host_and_port_(host_and_port), ssl_config_(ssl_config), - old_user_connect_callback_(NULL), - old_user_read_callback_(NULL), - user_write_callback_(NULL), user_read_buf_len_(0), user_write_buf_len_(0), next_handshake_state_(STATE_NONE), @@ -555,32 +548,10 @@ SSLClientSocketMac::~SSLClientSocketMac() { Disconnect(); } -int SSLClientSocketMac::Connect(OldCompletionCallback* callback) { - DCHECK(transport_.get()); - DCHECK(next_handshake_state_ == STATE_NONE); - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); - - net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT, NULL); - - int rv = InitializeSSLContext(); - if (rv != OK) { - net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); - return rv; - } - - next_handshake_state_ = STATE_HANDSHAKE; - rv = DoHandshakeLoop(OK); - if (rv == ERR_IO_PENDING) { - old_user_connect_callback_ = callback; - } else { - net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); - } - return rv; -} int SSLClientSocketMac::Connect(const CompletionCallback& callback) { DCHECK(transport_.get()); DCHECK(next_handshake_state_ == STATE_NONE); - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); + DCHECK(user_connect_callback_.is_null()); net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT, NULL); @@ -698,27 +669,9 @@ base::TimeDelta SSLClientSocketMac::GetConnectTimeMicros() const { } int SSLClientSocketMac::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(completed_handshake()); - DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); - DCHECK(!user_read_buf_); - - user_read_buf_ = buf; - user_read_buf_len_ = buf_len; - - int rv = DoPayloadRead(); - if (rv == ERR_IO_PENDING) { - old_user_read_callback_ = callback; - } else { - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - } - return rv; -} -int SSLClientSocketMac::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { DCHECK(completed_handshake()); - DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); + DCHECK(user_read_callback_.is_null()); DCHECK(!user_read_buf_); user_read_buf_ = buf; @@ -735,9 +688,9 @@ int SSLClientSocketMac::Read(IOBuffer* buf, int buf_len, } int SSLClientSocketMac::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(completed_handshake()); - DCHECK(!user_write_callback_); + DCHECK(user_write_callback_.is_null()); DCHECK(!user_write_buf_); user_write_buf_ = buf; @@ -936,51 +889,37 @@ int SSLClientSocketMac::InitializeSSLContext() { void SSLClientSocketMac::DoConnectCallback(int rv) { DCHECK(rv != ERR_IO_PENDING); - DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); + DCHECK(!user_connect_callback_.is_null()); - if (old_user_connect_callback_) { - OldCompletionCallback* c = old_user_connect_callback_; - old_user_connect_callback_ = NULL; - c->Run(rv > OK ? OK : rv); - } else { - CompletionCallback c = user_connect_callback_; - user_connect_callback_.Reset(); - c.Run(rv > OK ? OK : rv); - } + CompletionCallback c = user_connect_callback_; + user_connect_callback_.Reset(); + c.Run(rv > OK ? OK : rv); } void SSLClientSocketMac::DoReadCallback(int rv) { DCHECK(rv != ERR_IO_PENDING); - DCHECK(old_user_read_callback_ || !user_read_callback_.is_null()); + DCHECK(!user_read_callback_.is_null()); // Since Run may result in Read being called, clear user_read_callback_ up // front. - if (old_user_read_callback_) { - OldCompletionCallback* c = old_user_read_callback_; - old_user_read_callback_ = NULL; - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c->Run(rv); - } else { - CompletionCallback c = user_read_callback_; - user_read_callback_.Reset(); - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c.Run(rv); - } + CompletionCallback c = user_read_callback_; + user_read_callback_.Reset(); + user_read_buf_ = NULL; + user_read_buf_len_ = 0; + c.Run(rv); } void SSLClientSocketMac::DoWriteCallback(int rv) { DCHECK(rv != ERR_IO_PENDING); - DCHECK(user_write_callback_); + DCHECK(!user_write_callback_.is_null()); // Since Run may result in Write being called, clear user_write_callback_ up // front. - OldCompletionCallback* c = user_write_callback_; - user_write_callback_ = NULL; + CompletionCallback c = user_write_callback_; + user_write_callback_.Reset(); user_write_buf_ = NULL; user_write_buf_len_ = 0; - c->Run(rv); + c.Run(rv); } void SSLClientSocketMac::OnHandshakeIOComplete(int result) { @@ -990,7 +929,7 @@ void SSLClientSocketMac::OnHandshakeIOComplete(int result) { // renegotiating (which occurs because we are in the middle of a Read // when the renegotiation process starts). So we complete the Read // here. - if (!old_user_connect_callback_ && user_connect_callback_.is_null()) { + if (user_connect_callback_.is_null()) { DoReadCallback(rv); return; } @@ -1328,7 +1267,7 @@ int SSLClientSocketMac::DoCompletedRenegotiation(int result) { } void SSLClientSocketMac::DidCompleteRenegotiation() { - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); + DCHECK(user_connect_callback_.is_null()); renegotiating_ = false; next_handshake_state_ = STATE_COMPLETED_RENEGOTIATION; } @@ -1408,9 +1347,11 @@ OSStatus SSLClientSocketMac::SSLReadCallback(SSLConnectionRef connection, int rv = 1; // any old value to spin the loop below while (rv > 0 && total_read < *data_length) { us->read_io_buf_ = new IOBuffer(*data_length - total_read); - rv = us->transport_->socket()->Read(us->read_io_buf_, - *data_length - total_read, - &us->transport_read_callback_); + rv = us->transport_->socket()->Read( + us->read_io_buf_, + *data_length - total_read, + base::Bind(&SSLClientSocketMac::OnTransportReadComplete, + base::Unretained(us))); if (rv >= 0) { us->recv_buffer_.insert(us->recv_buffer_.end(), @@ -1470,9 +1411,11 @@ OSStatus SSLClientSocketMac::SSLWriteCallback(SSLConnectionRef connection, us->write_io_buf_ = new IOBuffer(us->send_buffer_.size()); memcpy(us->write_io_buf_->data(), &us->send_buffer_[0], us->send_buffer_.size()); - rv = us->transport_->socket()->Write(us->write_io_buf_, - us->send_buffer_.size(), - &us->transport_write_callback_); + rv = us->transport_->socket()->Write( + us->write_io_buf_, + us->send_buffer_.size(), + base::Bind(&SSLClientSocketMac::OnTransportWriteComplete, + base::Unretained(us))); if (rv > 0) { us->send_buffer_.erase(us->send_buffer_.begin(), us->send_buffer_.begin() + rv); diff --git a/net/socket/ssl_client_socket_mac.h b/net/socket/ssl_client_socket_mac.h index 7792cb3..3a10ae3 100644 --- a/net/socket/ssl_client_socket_mac.h +++ b/net/socket/ssl_client_socket_mac.h @@ -52,7 +52,6 @@ class SSLClientSocketMac : public SSLClientSocket { std::string* server_protos) OVERRIDE; // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -70,13 +69,10 @@ class SSLClientSocketMac : public SSLClientSocket { // Socket implementation. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; @@ -115,18 +111,13 @@ class SSLClientSocketMac : public SSLClientSocket { const void* data, size_t* data_length); - OldCompletionCallbackImpl<SSLClientSocketMac> transport_read_callback_; - OldCompletionCallbackImpl<SSLClientSocketMac> transport_write_callback_; - scoped_ptr<ClientSocketHandle> transport_; HostPortPair host_and_port_; SSLConfig ssl_config_; - OldCompletionCallback* old_user_connect_callback_; CompletionCallback user_connect_callback_; - OldCompletionCallback* old_user_read_callback_; CompletionCallback user_read_callback_; - OldCompletionCallback* user_write_callback_; + CompletionCallback user_write_callback_; // Used by Read function. scoped_refptr<IOBuffer> user_read_buf_; diff --git a/net/socket/ssl_client_socket_nss.cc b/net/socket/ssl_client_socket_nss.cc index 5279cf9..eb8c662 100644 --- a/net/socket/ssl_client_socket_nss.cc +++ b/net/socket/ssl_client_socket_nss.cc @@ -432,22 +432,12 @@ SSLClientSocketNSS::SSLClientSocketNSS(ClientSocketHandle* transport_socket, const SSLConfig& ssl_config, SSLHostInfo* ssl_host_info, const SSLClientSocketContext& context) - : ALLOW_THIS_IN_INITIALIZER_LIST(buffer_send_callback_( - this, &SSLClientSocketNSS::BufferSendComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST(buffer_recv_callback_( - this, &SSLClientSocketNSS::BufferRecvComplete)), - transport_send_busy_(false), + : transport_send_busy_(false), transport_recv_busy_(false), corked_(false), - ALLOW_THIS_IN_INITIALIZER_LIST(handshake_io_callback_( - base::Bind(&SSLClientSocketNSS::OnHandshakeIOComplete, - base::Unretained(this)))), transport_(transport_socket), host_and_port_(host_and_port), ssl_config_(ssl_config), - old_user_connect_callback_(NULL), - old_user_read_callback_(NULL), - user_write_callback_(NULL), user_read_buf_len_(0), user_write_buf_len_(0), server_cert_nss_(NULL), @@ -570,61 +560,13 @@ SSLClientSocketNSS::GetNextProto(std::string* proto, return next_proto_status_; } -int SSLClientSocketNSS::Connect(OldCompletionCallback* callback) { - EnterFunction(""); - DCHECK(transport_.get()); - DCHECK(next_handshake_state_ == STATE_NONE); - DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); - DCHECK(!user_write_callback_); - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); - DCHECK(!user_read_buf_); - DCHECK(!user_write_buf_); - - EnsureThreadIdAssigned(); - - net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT, NULL); - - int rv = Init(); - if (rv != OK) { - net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); - return rv; - } - - rv = InitializeSSLOptions(); - if (rv != OK) { - net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); - return rv; - } - - rv = InitializeSSLPeerName(); - if (rv != OK) { - net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); - return rv; - } - - if (ssl_config_.cached_info_enabled && ssl_host_info_.get()) { - GotoState(STATE_LOAD_SSL_HOST_INFO); - } else { - GotoState(STATE_HANDSHAKE); - } - - rv = DoHandshakeLoop(OK); - if (rv == ERR_IO_PENDING) { - old_user_connect_callback_ = callback; - } else { - net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); - } - - LeaveFunction(""); - return rv > OK ? OK : rv; -} int SSLClientSocketNSS::Connect(const CompletionCallback& callback) { EnterFunction(""); DCHECK(transport_.get()); DCHECK(next_handshake_state_ == STATE_NONE); - DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); - DCHECK(!user_write_callback_); - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); + DCHECK(user_read_callback_.is_null()); + DCHECK(user_write_callback_.is_null()); + DCHECK(user_connect_callback_.is_null()); DCHECK(!user_read_buf_); DCHECK(!user_write_buf_); @@ -672,8 +614,7 @@ void SSLClientSocketNSS::Disconnect() { CHECK(CalledOnValidThread()); - // Shut down anything that may call us back (through buffer_send_callback_, - // buffer_recv_callback, or handshake_io_callback_). + // Shut down anything that may call us back. verifier_.reset(); transport_->socket()->Disconnect(); @@ -688,14 +629,12 @@ void SSLClientSocketNSS::Disconnect() { nss_fd_ = NULL; } - // Reset object state - transport_send_busy_ = false; - transport_recv_busy_ = false; - old_user_connect_callback_ = NULL; + // Reset object state. user_connect_callback_.Reset(); - old_user_read_callback_ = NULL; user_read_callback_.Reset(); - user_write_callback_ = NULL; + user_write_callback_.Reset(); + transport_send_busy_ = false; + transport_recv_busy_ = false; user_read_buf_ = NULL; user_read_buf_len_ = 0; user_write_buf_ = NULL; @@ -810,36 +749,12 @@ base::TimeDelta SSLClientSocketNSS::GetConnectTimeMicros() const { } int SSLClientSocketNSS::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - EnterFunction(buf_len); - DCHECK(completed_handshake_); - DCHECK(next_handshake_state_ == STATE_NONE); - DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); - DCHECK(!user_read_buf_); - DCHECK(nss_bufs_); - - user_read_buf_ = buf; - user_read_buf_len_ = buf_len; - - int rv = DoReadLoop(OK); - - if (rv == ERR_IO_PENDING) { - old_user_read_callback_ = callback; - } else { - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - } - LeaveFunction(rv); - return rv; -} -int SSLClientSocketNSS::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { EnterFunction(buf_len); DCHECK(completed_handshake_); DCHECK(next_handshake_state_ == STATE_NONE); - DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); + DCHECK(user_read_callback_.is_null()); + DCHECK(user_connect_callback_.is_null()); DCHECK(!user_read_buf_); DCHECK(nss_bufs_); @@ -859,12 +774,12 @@ int SSLClientSocketNSS::Read(IOBuffer* buf, int buf_len, } int SSLClientSocketNSS::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { EnterFunction(buf_len); DCHECK(completed_handshake_); DCHECK(next_handshake_state_ == STATE_NONE); - DCHECK(!user_write_callback_); - DCHECK(!old_user_connect_callback_); + DCHECK(user_write_callback_.is_null()); + DCHECK(user_connect_callback_.is_null()); DCHECK(!user_write_buf_); DCHECK(nss_bufs_); @@ -1223,38 +1138,30 @@ void SSLClientSocketNSS::UpdateConnectionStatus() { void SSLClientSocketNSS::DoReadCallback(int rv) { EnterFunction(rv); DCHECK(rv != ERR_IO_PENDING); - DCHECK(old_user_read_callback_ || user_read_callback_.is_null()); + DCHECK(!user_read_callback_.is_null()); - // Since Run may result in Read being called, clear |old_user_read_callback_| + // Since Run may result in Read being called, clear |user_read_callback_| // up front. - if (old_user_read_callback_) { - OldCompletionCallback* c = old_user_read_callback_; - old_user_read_callback_ = NULL; - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c->Run(rv); - } else { - CompletionCallback c = user_read_callback_; - user_read_callback_.Reset(); - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c.Run(rv); - } + CompletionCallback c = user_read_callback_; + user_read_callback_.Reset(); + user_read_buf_ = NULL; + user_read_buf_len_ = 0; + c.Run(rv); LeaveFunction(""); } void SSLClientSocketNSS::DoWriteCallback(int rv) { EnterFunction(rv); DCHECK(rv != ERR_IO_PENDING); - DCHECK(user_write_callback_); + DCHECK(!user_write_callback_.is_null()); // Since Run may result in Write being called, clear |user_write_callback_| // up front. - OldCompletionCallback* c = user_write_callback_; - user_write_callback_ = NULL; + CompletionCallback c = user_write_callback_; + user_write_callback_.Reset(); user_write_buf_ = NULL; user_write_buf_len_ = 0; - c->Run(rv); + c.Run(rv); LeaveFunction(""); } @@ -1268,17 +1175,11 @@ void SSLClientSocketNSS::DoWriteCallback(int rv) { void SSLClientSocketNSS::DoConnectCallback(int rv) { EnterFunction(rv); DCHECK_NE(rv, ERR_IO_PENDING); - DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); + DCHECK(!user_connect_callback_.is_null()); - if (old_user_connect_callback_) { - OldCompletionCallback* c = old_user_connect_callback_; - old_user_connect_callback_ = NULL; - c->Run(rv > OK ? OK : rv); - } else { - CompletionCallback c = user_connect_callback_; - user_connect_callback_.Reset(); - c.Run(rv > OK ? OK : rv); - } + CompletionCallback c = user_connect_callback_; + user_connect_callback_.Reset(); + c.Run(rv > OK ? OK : rv); LeaveFunction(""); } @@ -1483,7 +1384,9 @@ bool SSLClientSocketNSS::LoadSSLHostInfo() { int SSLClientSocketNSS::DoLoadSSLHostInfo() { EnterFunction(""); - int rv = ssl_host_info_->WaitForDataReady(handshake_io_callback_); + int rv = ssl_host_info_->WaitForDataReady( + base::Bind(&SSLClientSocketNSS::OnHandshakeIOComplete, + base::Unretained(this))); GotoState(STATE_HANDSHAKE); if (rv == OK) { @@ -1786,7 +1689,9 @@ int SSLClientSocketNSS::DoVerifyCert(int result) { UMA_HISTOGRAM_TIMES("Net.SSLVerificationMergedMsSaved", end_time - ssl_host_info_->verification_start_time()); server_cert_verify_result_ = &ssl_host_info_->cert_verify_result(); - return ssl_host_info_->WaitForCertVerification(handshake_io_callback_); + return ssl_host_info_->WaitForCertVerification( + base::Bind(&SSLClientSocketNSS::OnHandshakeIOComplete, + base::Unretained(this))); } else { UMA_HISTOGRAM_ENUMERATION("Net.SSLVerificationMerged", 0 /* false */, 2); } @@ -1889,7 +1794,7 @@ int SSLClientSocketNSS::DoVerifyCertComplete(int result) { completed_handshake_ = true; - if (old_user_read_callback_ || !user_read_callback_.is_null()) { + if (!user_read_callback_.is_null()) { int rv = DoReadLoop(OK); if (rv != ERR_IO_PENDING) DoReadCallback(rv); @@ -2106,8 +2011,10 @@ int SSLClientSocketNSS::BufferSend(void) { scoped_refptr<IOBuffer> send_buffer(new IOBuffer(len)); memcpy(send_buffer->data(), buf1, len1); memcpy(send_buffer->data() + len1, buf2, len2); - rv = transport_->socket()->Write(send_buffer, len, - &buffer_send_callback_); + rv = transport_->socket()->Write( + send_buffer, len, + base::Bind(&SSLClientSocketNSS::BufferSendComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) { transport_send_busy_ = true; } else { @@ -2139,7 +2046,10 @@ int SSLClientSocketNSS::BufferRecv(void) { rv = ERR_IO_PENDING; } else { recv_buffer_ = new IOBuffer(nb); - rv = transport_->socket()->Read(recv_buffer_, nb, &buffer_recv_callback_); + rv = transport_->socket()->Read( + recv_buffer_, nb, + base::Bind(&SSLClientSocketNSS::BufferRecvComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) { transport_recv_busy_ = true; } else { diff --git a/net/socket/ssl_client_socket_nss.h b/net/socket/ssl_client_socket_nss.h index 5c566e9..366aa7f 100644 --- a/net/socket/ssl_client_socket_nss.h +++ b/net/socket/ssl_client_socket_nss.h @@ -71,7 +71,6 @@ class SSLClientSocketNSS : public SSLClientSocket { std::string* server_protos) OVERRIDE; // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -89,13 +88,10 @@ class SSLClientSocketNSS : public SSLClientSocket { // Socket implementation. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; @@ -212,8 +208,6 @@ class SSLClientSocketNSS : public SSLClientSocket { void EnsureThreadIdAssigned() const; bool CalledOnValidThread() const; - OldCompletionCallbackImpl<SSLClientSocketNSS> buffer_send_callback_; - OldCompletionCallbackImpl<SSLClientSocketNSS> buffer_recv_callback_; bool transport_send_busy_; bool transport_recv_busy_; // corked_ is true if we are currently suspending writes to the network. This @@ -224,16 +218,13 @@ class SSLClientSocketNSS : public SSLClientSocket { base::OneShotTimer<SSLClientSocketNSS> uncork_timer_; scoped_refptr<IOBuffer> recv_buffer_; - CompletionCallback handshake_io_callback_; scoped_ptr<ClientSocketHandle> transport_; HostPortPair host_and_port_; SSLConfig ssl_config_; - OldCompletionCallback* old_user_connect_callback_; CompletionCallback user_connect_callback_; - OldCompletionCallback* old_user_read_callback_; CompletionCallback user_read_callback_; - OldCompletionCallback* user_write_callback_; + CompletionCallback user_write_callback_; // Used by Read function. scoped_refptr<IOBuffer> user_read_buf_; diff --git a/net/socket/ssl_client_socket_openssl.cc b/net/socket/ssl_client_socket_openssl.cc index 1237348..f933031 100644 --- a/net/socket/ssl_client_socket_openssl.cc +++ b/net/socket/ssl_client_socket_openssl.cc @@ -384,15 +384,8 @@ SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( const HostPortPair& host_and_port, const SSLConfig& ssl_config, const SSLClientSocketContext& context) - : ALLOW_THIS_IN_INITIALIZER_LIST(buffer_send_callback_( - this, &SSLClientSocketOpenSSL::BufferSendComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST(buffer_recv_callback_( - this, &SSLClientSocketOpenSSL::BufferRecvComplete)), - transport_send_busy_(false), + : transport_send_busy_(false), transport_recv_busy_(false), - old_user_connect_callback_(NULL), - old_user_read_callback_(NULL), - user_write_callback_(NULL), completed_handshake_(false), client_auth_cert_needed_(false), cert_verifier_(context.cert_verifier), @@ -614,56 +607,24 @@ SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( void SSLClientSocketOpenSSL::DoReadCallback(int rv) { // Since Run may result in Read being called, clear |user_read_callback_| // up front. - if (old_user_read_callback_) { - OldCompletionCallback* c = old_user_read_callback_; - old_user_read_callback_ = NULL; - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c->Run(rv); - } else { - CompletionCallback c = user_read_callback_; - user_read_callback_.Reset(); - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c.Run(rv); - } + CompletionCallback c = user_read_callback_; + user_read_callback_.Reset(); + user_read_buf_ = NULL; + user_read_buf_len_ = 0; + c.Run(rv); } void SSLClientSocketOpenSSL::DoWriteCallback(int rv) { // Since Run may result in Write being called, clear |user_write_callback_| // up front. - OldCompletionCallback* c = user_write_callback_; - user_write_callback_ = NULL; + CompletionCallback c = user_write_callback_; + user_write_callback_.Reset(); user_write_buf_ = NULL; user_write_buf_len_ = 0; - c->Run(rv); + c.Run(rv); } -// StreamSocket methods - -int SSLClientSocketOpenSSL::Connect(OldCompletionCallback* callback) { - net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT, NULL); - - // Set up new ssl object. - if (!Init()) { - int result = ERR_UNEXPECTED; - net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, result); - return result; - } - - // Set SSL to client mode. Handshake happens in the loop below. - SSL_set_connect_state(ssl_); - - GotoState(STATE_HANDSHAKE); - int rv = DoHandshakeLoop(net::OK); - if (rv == ERR_IO_PENDING) { - old_user_connect_callback_ = callback; - } else { - net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); - } - - return rv > OK ? OK : rv; -} +// StreamSocket implementation. int SSLClientSocketOpenSSL::Connect(const CompletionCallback& callback) { net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT, NULL); @@ -708,11 +669,9 @@ void SSLClientSocketOpenSSL::Disconnect() { transport_recv_busy_ = false; recv_buffer_ = NULL; - old_user_connect_callback_ = NULL; user_connect_callback_.Reset(); - old_user_read_callback_ = NULL; user_read_callback_.Reset(); - user_write_callback_ = NULL; + user_write_callback_.Reset(); user_read_buf_ = NULL; user_read_buf_len_ = 0; user_write_buf_ = NULL; @@ -971,9 +930,11 @@ int SSLClientSocketOpenSSL::BufferSend(void) { int rv = 0; while (send_buffer_) { - rv = transport_->socket()->Write(send_buffer_, - send_buffer_->BytesRemaining(), - &buffer_send_callback_); + rv = transport_->socket()->Write( + send_buffer_, + send_buffer_->BytesRemaining(), + base::Bind(&SSLClientSocketOpenSSL::BufferSendComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) { transport_send_busy_ = true; return rv; @@ -1017,8 +978,10 @@ int SSLClientSocketOpenSSL::BufferRecv(void) { return ERR_IO_PENDING; recv_buffer_ = new IOBuffer(max_write); - int rv = transport_->socket()->Read(recv_buffer_, max_write, - &buffer_recv_callback_); + int rv = transport_->socket()->Read( + recv_buffer_, max_write, + base::Bind(&SSLClientSocketOpenSSL::BufferRecvComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) { transport_recv_busy_ = true; } else { @@ -1052,11 +1015,7 @@ void SSLClientSocketOpenSSL::TransportReadComplete(int result) { } void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { - if (old_user_connect_callback_) { - OldCompletionCallback* c = old_user_connect_callback_; - old_user_connect_callback_ = NULL; - c->Run(rv > OK ? OK : rv); - } else { + if (!user_connect_callback_.is_null()) { CompletionCallback c = user_connect_callback_; user_connect_callback_.Reset(); c.Run(rv > OK ? OK : rv); @@ -1190,23 +1149,6 @@ base::TimeDelta SSLClientSocketOpenSSL::GetConnectTimeMicros() const { int SSLClientSocketOpenSSL::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - user_read_buf_ = buf; - user_read_buf_len_ = buf_len; - - int rv = DoReadLoop(OK); - - if (rv == ERR_IO_PENDING) { - old_user_read_callback_ = callback; - } else { - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - } - - return rv; -} -int SSLClientSocketOpenSSL::Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) { user_read_buf_ = buf; user_read_buf_len_ = buf_len; @@ -1239,7 +1181,7 @@ int SSLClientSocketOpenSSL::DoReadLoop(int result) { int SSLClientSocketOpenSSL::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { user_write_buf_ = buf; user_write_buf_len_ = buf_len; diff --git a/net/socket/ssl_client_socket_openssl.h b/net/socket/ssl_client_socket_openssl.h index a15c0e3..e1e1778 100644 --- a/net/socket/ssl_client_socket_openssl.h +++ b/net/socket/ssl_client_socket_openssl.h @@ -63,7 +63,6 @@ class SSLClientSocketOpenSSL : public SSLClientSocket { std::string* server_protos); // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback); virtual int Connect(const CompletionCallback& callback); virtual void Disconnect(); virtual bool IsConnected() const; @@ -79,10 +78,10 @@ class SSLClientSocketOpenSSL : public SSLClientSocket { virtual base::TimeDelta GetConnectTimeMicros() const; // Socket implementation. - virtual int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); - virtual int Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); + virtual int Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback); virtual bool SetReceiveBufferSize(int32 size); virtual bool SetSendBufferSize(int32 size); @@ -115,18 +114,14 @@ class SSLClientSocketOpenSSL : public SSLClientSocket { void TransportWriteComplete(int result); void TransportReadComplete(int result); - OldCompletionCallbackImpl<SSLClientSocketOpenSSL> buffer_send_callback_; - OldCompletionCallbackImpl<SSLClientSocketOpenSSL> buffer_recv_callback_; bool transport_send_busy_; scoped_refptr<DrainableIOBuffer> send_buffer_; bool transport_recv_busy_; scoped_refptr<IOBuffer> recv_buffer_; - OldCompletionCallback* old_user_connect_callback_; CompletionCallback user_connect_callback_; - OldCompletionCallback* old_user_read_callback_; CompletionCallback user_read_callback_; - OldCompletionCallback* user_write_callback_; + CompletionCallback user_write_callback_; // Used by Read function. scoped_refptr<IOBuffer> user_read_buf_; diff --git a/net/socket/ssl_client_socket_pool.cc b/net/socket/ssl_client_socket_pool.cc index dffe962..2cc1cde 100644 --- a/net/socket/ssl_client_socket_pool.cc +++ b/net/socket/ssl_client_socket_pool.cc @@ -94,7 +94,10 @@ SSLConnectJob::SSLConnectJob(const std::string& group_name, host_resolver_(host_resolver), context_(context), ALLOW_THIS_IN_INITIALIZER_LIST( - callback_(this, &SSLConnectJob::OnIOComplete)) {} + callback_(base::Bind(&SSLConnectJob::OnIOComplete, + base::Unretained(this)))), + ALLOW_THIS_IN_INITIALIZER_LIST( + callback_old_(this, &SSLConnectJob::OnIOComplete)) {} SSLConnectJob::~SSLConnectJob() {} @@ -119,7 +122,7 @@ LoadState SSLConnectJob::GetLoadState() const { } } -void SSLConnectJob::GetAdditionalErrorState(ClientSocketHandle * handle) { +void SSLConnectJob::GetAdditionalErrorState(ClientSocketHandle* handle) { // Headers in |error_response_info_| indicate a proxy tunnel setup // problem. See DoTunnelConnectComplete. if (error_response_info_.headers) { @@ -207,7 +210,7 @@ int SSLConnectJob::DoTransportConnect() { group_name(), transport_params, transport_params->destination().priority(), - &callback_, transport_pool_, net_log()); + &callback_old_, transport_pool_, net_log()); } int SSLConnectJob::DoTransportConnectComplete(int result) { @@ -224,7 +227,7 @@ int SSLConnectJob::DoSOCKSConnect() { scoped_refptr<SOCKSSocketParams> socks_params = params_->socks_params(); return transport_socket_handle_->Init(group_name(), socks_params, socks_params->destination().priority(), - &callback_, socks_pool_, net_log()); + &callback_old_, socks_pool_, net_log()); } int SSLConnectJob::DoSOCKSConnectComplete(int result) { @@ -243,7 +246,7 @@ int SSLConnectJob::DoTunnelConnect() { params_->http_proxy_params(); return transport_socket_handle_->Init( group_name(), http_proxy_params, - http_proxy_params->destination().priority(), &callback_, + http_proxy_params->destination().priority(), &callback_old_, http_proxy_pool_, net_log()); } @@ -276,7 +279,7 @@ int SSLConnectJob::DoSSLConnect() { ssl_socket_.reset(client_socket_factory_->CreateSSLClientSocket( transport_socket_handle_.release(), params_->host_and_port(), params_->ssl_config(), ssl_host_info_.release(), context_)); - return ssl_socket_->Connect(&callback_); + return ssl_socket_->Connect(callback_); } int SSLConnectJob::DoSSLConnectComplete(int result) { diff --git a/net/socket/ssl_client_socket_pool.h b/net/socket/ssl_client_socket_pool.h index 2ca42b5..cec2e25 100644 --- a/net/socket/ssl_client_socket_pool.h +++ b/net/socket/ssl_client_socket_pool.h @@ -152,7 +152,8 @@ class SSLConnectJob : public ConnectJob { const SSLClientSocketContext context_; State next_state_; - OldCompletionCallbackImpl<SSLConnectJob> callback_; + CompletionCallback callback_; + OldCompletionCallbackImpl<SSLConnectJob> callback_old_; scoped_ptr<ClientSocketHandle> transport_socket_handle_; scoped_ptr<SSLClientSocket> ssl_socket_; scoped_ptr<SSLHostInfo> ssl_host_info_; diff --git a/net/socket/ssl_client_socket_unittest.cc b/net/socket/ssl_client_socket_unittest.cc index 88af817..aadffe1 100644 --- a/net/socket/ssl_client_socket_unittest.cc +++ b/net/socket/ssl_client_socket_unittest.cc @@ -73,11 +73,11 @@ TEST_F(SSLClientSocketTest, Connect) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); net::StreamSocket* transport = new net::TCPClientSocket( addr, &log, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -91,7 +91,7 @@ TEST_F(SSLClientSocketTest, Connect) { EXPECT_FALSE(sock->IsConnected()); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); net::CapturingNetLog::EntryList entries; log.GetEntries(&entries); @@ -117,11 +117,11 @@ TEST_F(SSLClientSocketTest, ConnectExpired) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); net::StreamSocket* transport = new net::TCPClientSocket( addr, &log, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -132,7 +132,7 @@ TEST_F(SSLClientSocketTest, ConnectExpired) { EXPECT_FALSE(sock->IsConnected()); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); net::CapturingNetLog::EntryList entries; log.GetEntries(&entries); @@ -160,11 +160,11 @@ TEST_F(SSLClientSocketTest, ConnectMismatched) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); net::StreamSocket* transport = new net::TCPClientSocket( addr, &log, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -175,7 +175,7 @@ TEST_F(SSLClientSocketTest, ConnectMismatched) { EXPECT_FALSE(sock->IsConnected()); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); net::CapturingNetLog::EntryList entries; log.GetEntries(&entries); @@ -205,11 +205,11 @@ TEST_F(SSLClientSocketTest, ConnectClientAuthCertRequested) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); net::StreamSocket* transport = new net::TCPClientSocket( addr, &log, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -220,7 +220,7 @@ TEST_F(SSLClientSocketTest, ConnectClientAuthCertRequested) { EXPECT_FALSE(sock->IsConnected()); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); net::CapturingNetLog::EntryList entries; log.GetEntries(&entries); @@ -265,11 +265,11 @@ TEST_F(SSLClientSocketTest, ConnectClientAuthSendNullCert) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); net::StreamSocket* transport = new net::TCPClientSocket( addr, &log, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -286,7 +286,7 @@ TEST_F(SSLClientSocketTest, ConnectClientAuthSendNullCert) { // Our test server accepts certificate-less connections. // TODO(davidben): Add a test which requires them and verify the error. - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); net::CapturingNetLog::EntryList entries; log.GetEntries(&entries); @@ -323,10 +323,10 @@ TEST_F(SSLClientSocketTest, Read) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::StreamSocket* transport = new net::TCPClientSocket( addr, NULL, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -335,7 +335,7 @@ TEST_F(SSLClientSocketTest, Read) { CreateSSLClientSocket(transport, test_server.host_port_pair(), kDefaultSSLConfig)); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -346,7 +346,8 @@ TEST_F(SSLClientSocketTest, Read) { new net::IOBuffer(arraysize(request_text) - 1)); memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); - rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); + rv = sock->Write(request_buffer, arraysize(request_text) - 1, + callback.callback()); EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); if (rv == net::ERR_IO_PENDING) @@ -355,7 +356,7 @@ TEST_F(SSLClientSocketTest, Read) { scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(4096)); for (;;) { - rv = sock->Read(buf, 4096, &callback); + rv = sock->Read(buf, 4096, callback.callback()); EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); if (rv == net::ERR_IO_PENDING) @@ -376,12 +377,11 @@ TEST_F(SSLClientSocketTest, Read_FullDuplex) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; // Used for everything except Write. - TestOldCompletionCallback callback2; // Used for Write only. + net::TestCompletionCallback callback; // Used for everything except Write. net::StreamSocket* transport = new net::TCPClientSocket( addr, NULL, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -393,7 +393,7 @@ TEST_F(SSLClientSocketTest, Read_FullDuplex) { transport, test_server.host_port_pair(), kDefaultSSLConfig, NULL, context)); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -401,7 +401,7 @@ TEST_F(SSLClientSocketTest, Read_FullDuplex) { // Issue a "hanging" Read first. scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(4096)); - rv = sock->Read(buf, 4096, &callback); + rv = sock->Read(buf, 4096, callback.callback()); // We haven't written the request, so there should be no response yet. ASSERT_EQ(net::ERR_IO_PENDING, rv); @@ -416,7 +416,8 @@ TEST_F(SSLClientSocketTest, Read_FullDuplex) { scoped_refptr<net::IOBuffer> request_buffer( new net::StringIOBuffer(request_text)); - rv = sock->Write(request_buffer, request_text.size(), &callback2); + net::TestCompletionCallback callback2; // Used for Write only. + rv = sock->Write(request_buffer, request_text.size(), callback2.callback()); EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); if (rv == net::ERR_IO_PENDING) @@ -435,10 +436,10 @@ TEST_F(SSLClientSocketTest, Read_SmallChunks) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::StreamSocket* transport = new net::TCPClientSocket( addr, NULL, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -447,7 +448,7 @@ TEST_F(SSLClientSocketTest, Read_SmallChunks) { CreateSSLClientSocket(transport, test_server.host_port_pair(), kDefaultSSLConfig)); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -457,7 +458,8 @@ TEST_F(SSLClientSocketTest, Read_SmallChunks) { new net::IOBuffer(arraysize(request_text) - 1)); memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); - rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); + rv = sock->Write(request_buffer, arraysize(request_text) - 1, + callback.callback()); EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); if (rv == net::ERR_IO_PENDING) @@ -466,7 +468,7 @@ TEST_F(SSLClientSocketTest, Read_SmallChunks) { scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(1)); for (;;) { - rv = sock->Read(buf, 1, &callback); + rv = sock->Read(buf, 1, callback.callback()); EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); if (rv == net::ERR_IO_PENDING) @@ -485,10 +487,10 @@ TEST_F(SSLClientSocketTest, Read_Interrupted) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::StreamSocket* transport = new net::TCPClientSocket( addr, NULL, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -497,7 +499,7 @@ TEST_F(SSLClientSocketTest, Read_Interrupted) { CreateSSLClientSocket(transport, test_server.host_port_pair(), kDefaultSSLConfig)); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -507,7 +509,8 @@ TEST_F(SSLClientSocketTest, Read_Interrupted) { new net::IOBuffer(arraysize(request_text) - 1)); memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); - rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); + rv = sock->Write(request_buffer, arraysize(request_text) - 1, + callback.callback()); EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); if (rv == net::ERR_IO_PENDING) @@ -516,7 +519,7 @@ TEST_F(SSLClientSocketTest, Read_Interrupted) { // Do a partial read and then exit. This test should not crash! scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(512)); - rv = sock->Read(buf, 512, &callback); + rv = sock->Read(buf, 512, callback.callback()); EXPECT_TRUE(rv > 0 || rv == net::ERR_IO_PENDING); if (rv == net::ERR_IO_PENDING) @@ -532,12 +535,12 @@ TEST_F(SSLClientSocketTest, Read_FullLogging) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); log.SetLogLevel(net::NetLog::LOG_ALL); net::StreamSocket* transport = new net::TCPClientSocket( addr, &log, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -546,7 +549,7 @@ TEST_F(SSLClientSocketTest, Read_FullLogging) { CreateSSLClientSocket(transport, test_server.host_port_pair(), kDefaultSSLConfig)); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -557,7 +560,8 @@ TEST_F(SSLClientSocketTest, Read_FullLogging) { new net::IOBuffer(arraysize(request_text) - 1)); memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); - rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); + rv = sock->Write(request_buffer, arraysize(request_text) - 1, + callback.callback()); EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); if (rv == net::ERR_IO_PENDING) @@ -572,7 +576,7 @@ TEST_F(SSLClientSocketTest, Read_FullLogging) { scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(4096)); for (;;) { - rv = sock->Read(buf, 4096, &callback); + rv = sock->Read(buf, 4096, callback.callback()); EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); if (rv == net::ERR_IO_PENDING) @@ -595,7 +599,7 @@ TEST_F(SSLClientSocketTest, PrematureApplicationData) { ASSERT_TRUE(test_server.Start()); net::AddressList addr; - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; static const unsigned char application_data[] = { 0x17, 0x03, 0x01, 0x00, 0x4a, 0x02, 0x00, 0x00, 0x46, 0x03, 0x01, 0x4b, @@ -622,7 +626,7 @@ TEST_F(SSLClientSocketTest, PrematureApplicationData) { net::StreamSocket* transport = new net::MockTCPClientSocket(addr, NULL, &data); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -631,7 +635,7 @@ TEST_F(SSLClientSocketTest, PrematureApplicationData) { CreateSSLClientSocket(transport, test_server.host_port_pair(), kDefaultSSLConfig)); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); EXPECT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); } @@ -656,11 +660,11 @@ TEST_F(SSLClientSocketTest, CipherSuiteDisables) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); net::StreamSocket* transport = new net::TCPClientSocket( addr, &log, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -675,7 +679,7 @@ TEST_F(SSLClientSocketTest, CipherSuiteDisables) { EXPECT_FALSE(sock->IsConnected()); - rv = sock->Connect(&callback); + rv = sock->Connect(callback.callback()); net::CapturingNetLog::EntryList entries; log.GetEntries(&entries); EXPECT_TRUE(net::LogContainsBeginEvent( @@ -725,10 +729,10 @@ TEST_F(SSLClientSocketTest, ClientSocketHandleNotFromPool) { net::AddressList addr; ASSERT_TRUE(test_server.GetAddressList(&addr)); - TestOldCompletionCallback callback; + net::TestCompletionCallback callback; net::StreamSocket* transport = new net::TCPClientSocket( addr, NULL, net::NetLog::Source()); - int rv = transport->Connect(&callback); + int rv = transport->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); @@ -744,7 +748,7 @@ TEST_F(SSLClientSocketTest, ClientSocketHandleNotFromPool) { NULL, context)); EXPECT_FALSE(ssl_socket->IsConnected()); - rv = ssl_socket->Connect(&callback); + rv = ssl_socket->Connect(callback.callback()); if (rv == net::ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_EQ(net::OK, rv); diff --git a/net/socket/ssl_client_socket_win.cc b/net/socket/ssl_client_socket_win.cc index 30f599d..e2509f2 100644 --- a/net/socket/ssl_client_socket_win.cc +++ b/net/socket/ssl_client_socket_win.cc @@ -387,20 +387,10 @@ SSLClientSocketWin::SSLClientSocketWin(ClientSocketHandle* transport_socket, const HostPortPair& host_and_port, const SSLConfig& ssl_config, const SSLClientSocketContext& context) - : ALLOW_THIS_IN_INITIALIZER_LIST( - handshake_io_callback_(this, - &SSLClientSocketWin::OnHandshakeIOComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &SSLClientSocketWin::OnReadComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_callback_(this, &SSLClientSocketWin::OnWriteComplete)), - transport_(transport_socket), + : transport_(transport_socket), host_and_port_(host_and_port), ssl_config_(ssl_config), - old_user_connect_callback_(NULL), - old_user_read_callback_(NULL), user_read_buf_len_(0), - user_write_callback_(NULL), user_write_buf_len_(0), next_state_(STATE_NONE), cert_verifier_(context.cert_verifier), @@ -562,33 +552,10 @@ SSLClientSocketWin::GetNextProto(std::string* proto, return kNextProtoUnsupported; } -int SSLClientSocketWin::Connect(OldCompletionCallback* callback) { - DCHECK(transport_.get()); - DCHECK(next_state_ == STATE_NONE); - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); - - net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT, NULL); - - int rv = InitializeSSLContext(); - if (rv != OK) { - net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT, NULL); - return rv; - } - - writing_first_token_ = true; - next_state_ = STATE_HANDSHAKE_WRITE; - rv = DoLoop(OK); - if (rv == ERR_IO_PENDING) { - old_user_connect_callback_ = callback; - } else { - net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT, NULL); - } - return rv; -} int SSLClientSocketWin::Connect(const CompletionCallback& callback) { DCHECK(transport_.get()); DCHECK(next_state_ == STATE_NONE); - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); + DCHECK(user_connect_callback_.is_null()); net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT, NULL); @@ -784,50 +751,9 @@ base::TimeDelta SSLClientSocketWin::GetConnectTimeMicros() const { } int SSLClientSocketWin::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(completed_handshake()); - DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); - - // If we have surplus decrypted plaintext, satisfy the Read with it without - // reading more ciphertext from the transport socket. - if (bytes_decrypted_ != 0) { - int len = std::min(buf_len, bytes_decrypted_); - net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, len, - decrypted_ptr_); - memcpy(buf->data(), decrypted_ptr_, len); - decrypted_ptr_ += len; - bytes_decrypted_ -= len; - if (bytes_decrypted_ == 0) { - decrypted_ptr_ = NULL; - if (bytes_received_ != 0) { - memmove(recv_buffer_.get(), received_ptr_, bytes_received_); - received_ptr_ = recv_buffer_.get(); - } - } - return len; - } - - DCHECK(!user_read_buf_); - // http://crbug.com/16371: We're seeing |buf->data()| return NULL. See if the - // user is passing in an IOBuffer with a NULL |data_|. - CHECK(buf); - CHECK(buf->data()); - user_read_buf_ = buf; - user_read_buf_len_ = buf_len; - - int rv = DoPayloadRead(); - if (rv == ERR_IO_PENDING) { - old_user_read_callback_ = callback; - } else { - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - } - return rv; -} -int SSLClientSocketWin::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { DCHECK(completed_handshake()); - DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); + DCHECK(user_read_callback_.is_null()); // If we have surplus decrypted plaintext, satisfy the Read with it without // reading more ciphertext from the transport socket. @@ -867,9 +793,9 @@ int SSLClientSocketWin::Read(IOBuffer* buf, int buf_len, } int SSLClientSocketWin::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(completed_handshake()); - DCHECK(!user_write_callback_); + DCHECK(user_write_callback_.is_null()); DCHECK(!user_write_buf_); user_write_buf_ = buf; @@ -906,32 +832,18 @@ void SSLClientSocketWin::OnHandshakeIOComplete(int result) { // If there is no connect callback available to call, we are renegotiating // (which occurs because we are in the middle of a Read when the // renegotiation process starts). So we complete the Read here. - if (!old_user_connect_callback_ && user_connect_callback_.is_null()) { - if (old_user_read_callback_) { - OldCompletionCallback* c = old_user_read_callback_; - old_user_read_callback_ = NULL; - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c->Run(rv); - } else { - CompletionCallback c = user_read_callback_; - user_read_callback_.Reset(); - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c.Run(rv); - } + if (user_connect_callback_.is_null()) { + CompletionCallback c = user_read_callback_; + user_read_callback_.Reset(); + user_read_buf_ = NULL; + user_read_buf_len_ = 0; + c.Run(rv); return; } net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT, NULL); - if (old_user_connect_callback_) { - OldCompletionCallback* c = old_user_connect_callback_; - old_user_connect_callback_ = NULL; - c->Run(rv); - } else { - CompletionCallback c = user_connect_callback_; - user_connect_callback_.Reset(); - c.Run(rv); - } + CompletionCallback c = user_connect_callback_; + user_connect_callback_.Reset(); + c.Run(rv); } } @@ -942,20 +854,12 @@ void SSLClientSocketWin::OnReadComplete(int result) { if (result > 0) result = DoPayloadDecrypt(); if (result != ERR_IO_PENDING) { - DCHECK(old_user_read_callback_ || !user_read_callback_.is_null()); - if (old_user_read_callback_) { - OldCompletionCallback* c = old_user_read_callback_; - old_user_read_callback_ = NULL; - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c->Run(result); - } else { - CompletionCallback c = user_read_callback_; - user_read_callback_.Reset(); - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c.Run(result); - } + DCHECK(!user_read_callback_.is_null()); + CompletionCallback c = user_read_callback_; + user_read_callback_.Reset(); + user_read_buf_ = NULL; + user_read_buf_len_ = 0; + c.Run(result); } } @@ -964,12 +868,12 @@ void SSLClientSocketWin::OnWriteComplete(int result) { int rv = DoPayloadWriteComplete(result); if (rv != ERR_IO_PENDING) { - DCHECK(user_write_callback_); - OldCompletionCallback* c = user_write_callback_; - user_write_callback_ = NULL; + DCHECK(!user_write_callback_.is_null()); + CompletionCallback c = user_write_callback_; + user_write_callback_.Reset(); user_write_buf_ = NULL; user_write_buf_len_ = 0; - c->Run(rv); + c.Run(rv); } } @@ -1031,8 +935,10 @@ int SSLClientSocketWin::DoHandshakeRead() { DCHECK(!transport_read_buf_); transport_read_buf_ = new IOBuffer(buf_len); - return transport_->socket()->Read(transport_read_buf_, buf_len, - &handshake_io_callback_); + return transport_->socket()->Read( + transport_read_buf_, buf_len, + base::Bind(&SSLClientSocketWin::OnHandshakeIOComplete, + base::Unretained(this))); } int SSLClientSocketWin::DoHandshakeReadComplete(int result) { @@ -1209,8 +1115,10 @@ int SSLClientSocketWin::DoHandshakeWrite() { transport_write_buf_ = new IOBuffer(buf_len); memcpy(transport_write_buf_->data(), buf, buf_len); - return transport_->socket()->Write(transport_write_buf_, buf_len, - &handshake_io_callback_); + return transport_->socket()->Write( + transport_write_buf_, buf_len, + base::Bind(&SSLClientSocketWin::OnHandshakeIOComplete, + base::Unretained(this))); } int SSLClientSocketWin::DoHandshakeWriteComplete(int result) { @@ -1308,8 +1216,10 @@ int SSLClientSocketWin::DoPayloadRead() { DCHECK(!transport_read_buf_); transport_read_buf_ = new IOBuffer(buf_len); - rv = transport_->socket()->Read(transport_read_buf_, buf_len, - &read_callback_); + rv = transport_->socket()->Read( + transport_read_buf_, buf_len, + base::Bind(&SSLClientSocketWin::OnReadComplete, + base::Unretained(this))); if (rv != ERR_IO_PENDING) rv = DoPayloadReadComplete(rv); if (rv <= 0) @@ -1551,8 +1461,10 @@ int SSLClientSocketWin::DoPayloadWrite() { transport_write_buf_ = new IOBuffer(buf_len); memcpy(transport_write_buf_->data(), buf, buf_len); - int rv = transport_->socket()->Write(transport_write_buf_, buf_len, - &write_callback_); + int rv = transport_->socket()->Write( + transport_write_buf_, buf_len, + base::Bind(&SSLClientSocketWin::OnWriteComplete, + base::Unretained(this))); if (rv != ERR_IO_PENDING) rv = DoPayloadWriteComplete(rv); return rv; @@ -1635,8 +1547,8 @@ int SSLClientSocketWin::DidCompleteHandshake() { // Called when a renegotiation is completed. |result| is the verification // result of the server certificate received during renegotiation. void SSLClientSocketWin::DidCompleteRenegotiation() { - DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); - DCHECK(old_user_read_callback_ || !user_read_callback_.is_null()); + DCHECK(user_connect_callback_.is_null()); + DCHECK(!user_read_callback_.is_null()); renegotiating_ = false; next_state_ = STATE_COMPLETED_RENEGOTIATION; } diff --git a/net/socket/ssl_client_socket_win.h b/net/socket/ssl_client_socket_win.h index 27ce300..c59decf 100644 --- a/net/socket/ssl_client_socket_win.h +++ b/net/socket/ssl_client_socket_win.h @@ -56,29 +56,28 @@ class SSLClientSocketWin : public SSLClientSocket { std::string* server_protos); // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback); - virtual int Connect(const CompletionCallback& callback); - virtual void Disconnect(); - virtual bool IsConnected() const; - virtual bool IsConnectedAndIdle() const; - virtual int GetPeerAddress(AddressList* address) const; - virtual int GetLocalAddress(IPEndPoint* address) const; - virtual const BoundNetLog& NetLog() const { return net_log_; } - virtual void SetSubresourceSpeculation(); - virtual void SetOmniboxSpeculation(); - virtual bool WasEverUsed() const; - virtual bool UsingTCPFastOpen() const; - virtual int64 NumBytesRead() const; - virtual base::TimeDelta GetConnectTimeMicros() const; + virtual int Connect(const CompletionCallback& callback) OVERRIDE; + virtual void Disconnect() OVERRIDE; + virtual bool IsConnected() const OVERRIDE; + virtual bool IsConnectedAndIdle() const OVERRIDE; + virtual int GetPeerAddress(AddressList* address) const OVERRIDE; + virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; + virtual const BoundNetLog& NetLog() const OVERRIDE{ return net_log_; } + virtual void SetSubresourceSpeculation() OVERRIDE; + virtual void SetOmniboxSpeculation() OVERRIDE; + virtual bool WasEverUsed() const OVERRIDE; + virtual bool UsingTCPFastOpen() const OVERRIDE; + virtual int64 NumBytesRead() const OVERRIDE; + virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; // Socket implementation. - virtual int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); virtual int Read(IOBuffer* buf, int buf_len, - const CompletionCallback& callback); - virtual int Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); + const CompletionCallback& callback) OVERRIDE; + virtual int Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback) OVERRIDE; - virtual bool SetReceiveBufferSize(int32 size); - virtual bool SetSendBufferSize(int32 size); + virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; + virtual bool SetSendBufferSize(int32 size) OVERRIDE; private: bool completed_handshake() const { @@ -114,27 +113,20 @@ class SSLClientSocketWin : public SSLClientSocket { void LogConnectionTypeMetrics() const; void FreeSendBuffer(); - // Internal callbacks as async operations complete. - OldCompletionCallbackImpl<SSLClientSocketWin> handshake_io_callback_; - OldCompletionCallbackImpl<SSLClientSocketWin> read_callback_; - OldCompletionCallbackImpl<SSLClientSocketWin> write_callback_; - scoped_ptr<ClientSocketHandle> transport_; HostPortPair host_and_port_; SSLConfig ssl_config_; // User function to callback when the Connect() completes. - OldCompletionCallback* old_user_connect_callback_; CompletionCallback user_connect_callback_; // User function to callback when a Read() completes. - OldCompletionCallback* old_user_read_callback_; CompletionCallback user_read_callback_; scoped_refptr<IOBuffer> user_read_buf_; int user_read_buf_len_; // User function to callback when a Write() completes. - OldCompletionCallback* user_write_callback_; + CompletionCallback user_write_callback_; scoped_refptr<IOBuffer> user_write_buf_; int user_write_buf_len_; diff --git a/net/socket/ssl_server_socket_nss.cc b/net/socket/ssl_server_socket_nss.cc index 0785dd7d..5b57492 100644 --- a/net/socket/ssl_server_socket_nss.cc +++ b/net/socket/ssl_server_socket_nss.cc @@ -58,15 +58,8 @@ SSLServerSocketNSS::SSLServerSocketNSS( scoped_refptr<X509Certificate> cert, crypto::RSAPrivateKey* key, const SSLConfig& ssl_config) - : ALLOW_THIS_IN_INITIALIZER_LIST(buffer_send_callback_( - this, &SSLServerSocketNSS::BufferSendComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST(buffer_recv_callback_( - this, &SSLServerSocketNSS::BufferRecvComplete)), - transport_send_busy_(false), + : transport_send_busy_(false), transport_recv_busy_(false), - user_handshake_callback_(NULL), - old_user_read_callback_(NULL), - user_write_callback_(NULL), nss_fd_(NULL), nss_bufs_(NULL), transport_socket_(transport_socket), @@ -143,40 +136,14 @@ int SSLServerSocketNSS::ExportKeyingMaterial(const base::StringPiece& label, return OK; } -int SSLServerSocketNSS::Connect(OldCompletionCallback* callback) { - NOTIMPLEMENTED(); - return ERR_NOT_IMPLEMENTED; -} int SSLServerSocketNSS::Connect(const CompletionCallback& callback) { NOTIMPLEMENTED(); return ERR_NOT_IMPLEMENTED; } int SSLServerSocketNSS::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); - DCHECK(!user_handshake_callback_); - DCHECK(!user_read_buf_); - DCHECK(nss_bufs_); - - user_read_buf_ = buf; - user_read_buf_len_ = buf_len; - - DCHECK(completed_handshake_); - - int rv = DoReadLoop(OK); - - if (rv == ERR_IO_PENDING) { - old_user_read_callback_ = callback; - } else { - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - } - return rv; -} -int SSLServerSocketNSS::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { - DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); + DCHECK(user_read_callback_.is_null()); DCHECK(!user_handshake_callback_); DCHECK(!user_read_buf_); DCHECK(nss_bufs_); @@ -198,8 +165,8 @@ int SSLServerSocketNSS::Read(IOBuffer* buf, int buf_len, } int SSLServerSocketNSS::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(!user_write_callback_); + const CompletionCallback& callback) { + DCHECK(user_write_callback_.is_null()); DCHECK(!user_write_buf_); DCHECK(nss_bufs_); @@ -521,8 +488,10 @@ int SSLServerSocketNSS::BufferSend(void) { scoped_refptr<IOBuffer> send_buffer(new IOBuffer(len)); memcpy(send_buffer->data(), buf1, len1); memcpy(send_buffer->data() + len1, buf2, len2); - rv = transport_socket_->Write(send_buffer, len, - &buffer_send_callback_); + rv = transport_socket_->Write( + send_buffer, len, + base::Bind(&SSLServerSocketNSS::BufferSendComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) { transport_send_busy_ = true; } else { @@ -550,7 +519,10 @@ int SSLServerSocketNSS::BufferRecv(void) { rv = ERR_IO_PENDING; } else { recv_buffer_ = new IOBuffer(nb); - rv = transport_socket_->Read(recv_buffer_, nb, &buffer_recv_callback_); + rv = transport_socket_->Read( + recv_buffer_, nb, + base::Bind(&SSLServerSocketNSS::BufferRecvComplete, + base::Unretained(this))); if (rv == ERR_IO_PENDING) { transport_recv_busy_ = true; } else { @@ -739,36 +711,28 @@ void SSLServerSocketNSS::DoHandshakeCallback(int rv) { void SSLServerSocketNSS::DoReadCallback(int rv) { DCHECK(rv != ERR_IO_PENDING); - DCHECK(old_user_read_callback_ || !user_read_callback_.is_null()); + DCHECK(!user_read_callback_.is_null()); // Since Run may result in Read being called, clear |user_read_callback_| // up front. - if (old_user_read_callback_) { - OldCompletionCallback* c = old_user_read_callback_; - old_user_read_callback_ = NULL; - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c->Run(rv); - } else { - CompletionCallback c = user_read_callback_; - user_read_callback_.Reset(); - user_read_buf_ = NULL; - user_read_buf_len_ = 0; - c.Run(rv); - } + CompletionCallback c = user_read_callback_; + user_read_callback_.Reset(); + user_read_buf_ = NULL; + user_read_buf_len_ = 0; + c.Run(rv); } void SSLServerSocketNSS::DoWriteCallback(int rv) { DCHECK(rv != ERR_IO_PENDING); - DCHECK(user_write_callback_); + DCHECK(!user_write_callback_.is_null()); // Since Run may result in Write being called, clear |user_write_callback_| // up front. - OldCompletionCallback* c = user_write_callback_; - user_write_callback_ = NULL; + CompletionCallback c = user_write_callback_; + user_write_callback_.Reset(); user_write_buf_ = NULL; user_write_buf_len_ = 0; - c->Run(rv); + c.Run(rv); } // static diff --git a/net/socket/ssl_server_socket_nss.h b/net/socket/ssl_server_socket_nss.h index 39283f6..1d716a1 100644 --- a/net/socket/ssl_server_socket_nss.h +++ b/net/socket/ssl_server_socket_nss.h @@ -40,16 +40,13 @@ class SSLServerSocketNSS : public SSLServerSocket { // Socket interface (via StreamSocket). virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -101,8 +98,6 @@ class SSLServerSocketNSS : public SSLServerSocket { virtual int Init(); // Members used to send and receive buffer. - OldCompletionCallbackImpl<SSLServerSocketNSS> buffer_send_callback_; - OldCompletionCallbackImpl<SSLServerSocketNSS> buffer_recv_callback_; bool transport_send_busy_; bool transport_recv_busy_; @@ -111,9 +106,8 @@ class SSLServerSocketNSS : public SSLServerSocket { BoundNetLog net_log_; OldCompletionCallback* user_handshake_callback_; - OldCompletionCallback* old_user_read_callback_; CompletionCallback user_read_callback_; - OldCompletionCallback* user_write_callback_; + CompletionCallback user_write_callback_; // Used by Read function. scoped_refptr<IOBuffer> user_read_buf_; diff --git a/net/socket/ssl_server_socket_unittest.cc b/net/socket/ssl_server_socket_unittest.cc index eb9dc7c..f5034c2 100644 --- a/net/socket/ssl_server_socket_unittest.cc +++ b/net/socket/ssl_server_socket_unittest.cc @@ -51,22 +51,11 @@ namespace { class FakeDataChannel { public: FakeDataChannel() - : old_read_callback_(NULL), - read_buf_len_(0), - ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { + : read_buf_len_(0), + ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { } virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - if (data_.empty()) { - old_read_callback_ = callback; - read_buf_ = buf; - read_buf_len_ = buf_len; - return net::ERR_IO_PENDING; - } - return PropogateData(buf, buf_len); - } - virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { if (data_.empty()) { read_callback_ = callback; @@ -78,33 +67,25 @@ class FakeDataChannel { } virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { data_.push(new net::DrainableIOBuffer(buf, buf_len)); MessageLoop::current()->PostTask( - FROM_HERE, task_factory_.NewRunnableMethod( - &FakeDataChannel::DoReadCallback)); + FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, + weak_factory_.GetWeakPtr())); return buf_len; } private: void DoReadCallback() { - if ((!old_read_callback_ && read_callback_.is_null()) || data_.empty()) + if (read_callback_.is_null() || data_.empty()) return; int copied = PropogateData(read_buf_, read_buf_len_); - if (old_read_callback_) { - net::OldCompletionCallback* callback = old_read_callback_; - old_read_callback_ = NULL; - read_buf_ = NULL; - read_buf_len_ = 0; - callback->Run(copied); - } else { - net::CompletionCallback callback = read_callback_; - read_callback_.Reset(); - read_buf_ = NULL; - read_buf_len_ = 0; - callback.Run(copied); - } + CompletionCallback callback = read_callback_; + read_callback_.Reset(); + read_buf_ = NULL; + read_buf_len_ = 0; + callback.Run(copied); } int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) { @@ -118,14 +99,13 @@ class FakeDataChannel { return copied; } - net::OldCompletionCallback* old_read_callback_; - net::CompletionCallback read_callback_; + CompletionCallback read_callback_; scoped_refptr<net::IOBuffer> read_buf_; int read_buf_len_; std::queue<scoped_refptr<net::DrainableIOBuffer> > data_; - ScopedRunnableMethodFactory<FakeDataChannel> task_factory_; + base::WeakPtrFactory<FakeDataChannel> weak_factory_; DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); }; @@ -142,82 +122,73 @@ class FakeSocket : public StreamSocket { } virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - // Read random number of bytes. - buf_len = rand() % buf_len + 1; - return incoming_->Read(buf, buf_len, callback); - } - virtual int Read(IOBuffer* buf, int buf_len, - const CompletionCallback& callback) { + const CompletionCallback& callback) OVERRIDE { // Read random number of bytes. buf_len = rand() % buf_len + 1; return incoming_->Read(buf, buf_len, callback); } virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) OVERRIDE { // Write random number of bytes. buf_len = rand() % buf_len + 1; return outgoing_->Write(buf, buf_len, callback); } - virtual bool SetReceiveBufferSize(int32 size) { + virtual bool SetReceiveBufferSize(int32 size) OVERRIDE { return true; } - virtual bool SetSendBufferSize(int32 size) { + virtual bool SetSendBufferSize(int32 size) OVERRIDE { return true; } - virtual int Connect(OldCompletionCallback* callback) { - return net::OK; - } - virtual int Connect(const CompletionCallback& callback) { + virtual int Connect(const CompletionCallback& callback) OVERRIDE { return net::OK; } - virtual void Disconnect() {} + virtual void Disconnect() OVERRIDE {} - virtual bool IsConnected() const { + virtual bool IsConnected() const OVERRIDE { return true; } - virtual bool IsConnectedAndIdle() const { + virtual bool IsConnectedAndIdle() const OVERRIDE { return true; } - virtual int GetPeerAddress(AddressList* address) const { + virtual int GetPeerAddress(AddressList* address) const OVERRIDE { net::IPAddressNumber ip_address(4); *address = net::AddressList::CreateFromIPAddress(ip_address, 0 /*port*/); return net::OK; } - virtual int GetLocalAddress(IPEndPoint* address) const { + virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { net::IPAddressNumber ip_address(4); *address = net::IPEndPoint(ip_address, 0); return net::OK; } - virtual const BoundNetLog& NetLog() const { + virtual const BoundNetLog& NetLog() const OVERRIDE { return net_log_; } - virtual void SetSubresourceSpeculation() {} - virtual void SetOmniboxSpeculation() {} + virtual void SetSubresourceSpeculation() OVERRIDE {} + virtual void SetOmniboxSpeculation() OVERRIDE {} - virtual bool WasEverUsed() const { + virtual bool WasEverUsed() const OVERRIDE { return true; } - virtual bool UsingTCPFastOpen() const { + virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } - virtual int64 NumBytesRead() const { + virtual int64 NumBytesRead() const OVERRIDE { return -1; } - virtual base::TimeDelta GetConnectTimeMicros() const { + virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE { return base::TimeDelta::FromMicroseconds(-1); } @@ -246,21 +217,21 @@ TEST(FakeSocketTest, DataTransfer) { scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize); // Write then read. - int written = server.Write(write_buf, kTestDataSize, NULL); + int written = server.Write(write_buf, kTestDataSize, CompletionCallback()); EXPECT_GT(written, 0); EXPECT_LE(written, kTestDataSize); - int read = client.Read(read_buf, kReadBufSize, NULL); + int read = client.Read(read_buf, kReadBufSize, CompletionCallback()); EXPECT_GT(read, 0); EXPECT_LE(read, written); EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); // Read then write. - TestOldCompletionCallback callback; + TestCompletionCallback callback; EXPECT_EQ(net::ERR_IO_PENDING, - server.Read(read_buf, kReadBufSize, &callback)); + server.Read(read_buf, kReadBufSize, callback.callback())); - written = client.Write(write_buf, kTestDataSize, NULL); + written = client.Write(write_buf, kTestDataSize, CompletionCallback()); EXPECT_GT(written, 0); EXPECT_LE(written, kTestDataSize); @@ -354,13 +325,13 @@ TEST_F(SSLServerSocketTest, Initialize) { TEST_F(SSLServerSocketTest, Handshake) { Initialize(); - TestOldCompletionCallback connect_callback; + TestCompletionCallback connect_callback; TestOldCompletionCallback handshake_callback; int server_ret = server_socket_->Handshake(&handshake_callback); EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING); - int client_ret = client_socket_->Connect(&connect_callback); + int client_ret = client_socket_->Connect(connect_callback.callback()); EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING); if (client_ret == net::ERR_IO_PENDING) { @@ -379,11 +350,11 @@ TEST_F(SSLServerSocketTest, Handshake) { TEST_F(SSLServerSocketTest, DataTransfer) { Initialize(); - TestOldCompletionCallback connect_callback; + TestCompletionCallback connect_callback; TestOldCompletionCallback handshake_callback; // Establish connection. - int client_ret = client_socket_->Connect(&connect_callback); + int client_ret = client_socket_->Connect(connect_callback.callback()); ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING); int server_ret = server_socket_->Handshake(&handshake_callback); @@ -402,13 +373,13 @@ TEST_F(SSLServerSocketTest, DataTransfer) { kReadBufSize); // Write then read. - TestOldCompletionCallback write_callback; - TestOldCompletionCallback read_callback; + TestCompletionCallback write_callback; + TestCompletionCallback read_callback; server_ret = server_socket_->Write(write_buf, write_buf->size(), - &write_callback); + write_callback.callback()); EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); client_ret = client_socket_->Read(read_buf, read_buf->BytesRemaining(), - &read_callback); + read_callback.callback()); EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); server_ret = write_callback.GetResult(server_ret); @@ -419,7 +390,7 @@ TEST_F(SSLServerSocketTest, DataTransfer) { read_buf->DidConsume(client_ret); while (read_buf->BytesConsumed() < write_buf->size()) { client_ret = client_socket_->Read(read_buf, read_buf->BytesRemaining(), - &read_callback); + read_callback.callback()); EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); client_ret = read_callback.GetResult(client_ret); ASSERT_GT(client_ret, 0); @@ -432,10 +403,10 @@ TEST_F(SSLServerSocketTest, DataTransfer) { // Read then write. write_buf = new net::StringIOBuffer("hello123"); server_ret = server_socket_->Read(read_buf, read_buf->BytesRemaining(), - &read_callback); + read_callback.callback()); EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); client_ret = client_socket_->Write(write_buf, write_buf->size(), - &write_callback); + write_callback.callback()); EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); server_ret = read_callback.GetResult(server_ret); @@ -446,7 +417,7 @@ TEST_F(SSLServerSocketTest, DataTransfer) { read_buf->DidConsume(server_ret); while (read_buf->BytesConsumed() < write_buf->size()) { server_ret = server_socket_->Read(read_buf, read_buf->BytesRemaining(), - &read_callback); + read_callback.callback()); EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); server_ret = read_callback.GetResult(server_ret); ASSERT_GT(server_ret, 0); @@ -463,10 +434,10 @@ TEST_F(SSLServerSocketTest, DataTransfer) { TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { Initialize(); - TestOldCompletionCallback connect_callback; + TestCompletionCallback connect_callback; TestOldCompletionCallback handshake_callback; - int client_ret = client_socket_->Connect(&connect_callback); + int client_ret = client_socket_->Connect(connect_callback.callback()); ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING); int server_ret = server_socket_->Handshake(&handshake_callback); diff --git a/net/socket/stream_socket.h b/net/socket/stream_socket.h index 3ba5b42..a0e6f86 100644 --- a/net/socket/stream_socket.h +++ b/net/socket/stream_socket.h @@ -33,7 +33,6 @@ class NET_EXPORT_PRIVATE StreamSocket : public Socket { // // Connect may also be called again after a call to the Disconnect method. // - virtual int Connect(OldCompletionCallback* callback) = 0; virtual int Connect(const CompletionCallback& callback) = 0; // Called to disconnect a socket. Does nothing if the socket is already diff --git a/net/socket/tcp_client_socket_libevent.cc b/net/socket/tcp_client_socket_libevent.cc index 3c99ae5..9b47249 100644 --- a/net/socket/tcp_client_socket_libevent.cc +++ b/net/socket/tcp_client_socket_libevent.cc @@ -130,8 +130,6 @@ TCPClientSocketLibevent::TCPClientSocketLibevent( current_ai_(NULL), read_watcher_(this), write_watcher_(this), - old_read_callback_(NULL), - old_write_callback_(NULL), next_connect_state_(CONNECT_STATE_NONE), connect_os_error_(0), net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)), @@ -202,38 +200,6 @@ int TCPClientSocketLibevent::Bind(const IPEndPoint& address) { return 0; } -int TCPClientSocketLibevent::Connect(OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - - // If already connected, then just return OK. - if (socket_ != kInvalidSocket) - return OK; - - base::StatsCounter connects("tcp.connect"); - connects.Increment(); - - DCHECK(!waiting_connect()); - - net_log_.BeginEvent( - NetLog::TYPE_TCP_CONNECT, - make_scoped_refptr(new AddressListNetLogParam(addresses_))); - - // We will try to connect to each address in addresses_. Start with the - // first one in the list. - next_connect_state_ = CONNECT_STATE_CONNECT; - current_ai_ = addresses_.head(); - - int rv = DoConnectLoop(OK); - if (rv == ERR_IO_PENDING) { - // Synchronous operation not supported. - DCHECK(callback); - old_write_callback_ = callback; - } else { - LogConnectCompletion(rv); - } - - return rv; -} int TCPClientSocketLibevent::Connect(const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); @@ -461,50 +427,11 @@ bool TCPClientSocketLibevent::IsConnectedAndIdle() const { int TCPClientSocketLibevent::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - DCHECK_NE(kInvalidSocket, socket_); - DCHECK(!waiting_connect()); - DCHECK(!old_read_callback_ && read_callback_.is_null()); - // Synchronous operation not supported - DCHECK(callback); - DCHECK_GT(buf_len, 0); - - int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); - if (nread >= 0) { - base::StatsCounter read_bytes("tcp.read_bytes"); - read_bytes.Add(nread); - num_bytes_read_ += static_cast<int64>(nread); - if (nread > 0) - use_history_.set_was_used_to_convey_data(); - net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, - buf->data()); - return nread; - } - if (errno != EAGAIN && errno != EWOULDBLOCK) { - DVLOG(1) << "read failed, errno " << errno; - return MapSystemError(errno); - } - - if (!MessageLoopForIO::current()->WatchFileDescriptor( - socket_, true, MessageLoopForIO::WATCH_READ, - &read_socket_watcher_, &read_watcher_)) { - DVLOG(1) << "WatchFileDescriptor failed on read, errno " << errno; - return MapSystemError(errno); - } - - read_buf_ = buf; - read_buf_len_ = buf_len; - old_read_callback_ = callback; - return ERR_IO_PENDING; -} -int TCPClientSocketLibevent::Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); DCHECK_NE(kInvalidSocket, socket_); DCHECK(!waiting_connect()); - DCHECK(!old_read_callback_ && read_callback_.is_null()); + DCHECK(read_callback_.is_null()); // Synchronous operation not supported DCHECK(!callback.is_null()); DCHECK_GT(buf_len, 0); @@ -540,13 +467,13 @@ int TCPClientSocketLibevent::Read(IOBuffer* buf, int TCPClientSocketLibevent::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); DCHECK_NE(kInvalidSocket, socket_); DCHECK(!waiting_connect()); - DCHECK(!old_write_callback_ && write_callback_.is_null()); + DCHECK(write_callback_.is_null()); // Synchronous operation not supported - DCHECK(callback); + DCHECK(!callback.is_null()); DCHECK_GT(buf_len, 0); int nwrite = InternalWrite(buf, buf_len); @@ -571,7 +498,7 @@ int TCPClientSocketLibevent::Write(IOBuffer* buf, write_buf_ = buf; write_buf_len_ = buf_len; - old_write_callback_ = callback; + write_callback_ = callback; return ERR_IO_PENDING; } @@ -657,34 +584,22 @@ void TCPClientSocketLibevent::LogConnectCompletion(int net_error) { void TCPClientSocketLibevent::DoReadCallback(int rv) { DCHECK_NE(rv, ERR_IO_PENDING); - DCHECK(old_read_callback_ || !read_callback_.is_null()); + DCHECK(!read_callback_.is_null()); // since Run may result in Read being called, clear read_callback_ up front. - if (old_read_callback_) { - OldCompletionCallback* c = old_read_callback_; - old_read_callback_ = NULL; - c->Run(rv); - } else { - CompletionCallback c = read_callback_; - read_callback_.Reset(); - c.Run(rv); - } + CompletionCallback c = read_callback_; + read_callback_.Reset(); + c.Run(rv); } void TCPClientSocketLibevent::DoWriteCallback(int rv) { DCHECK_NE(rv, ERR_IO_PENDING); - DCHECK(old_write_callback_ || !write_callback_.is_null()); + DCHECK(!write_callback_.is_null()); // since Run may result in Write being called, clear write_callback_ up front. - if (old_write_callback_) { - OldCompletionCallback* c = old_write_callback_; - old_write_callback_ = NULL; - c->Run(rv); - } else { - CompletionCallback c = write_callback_; - write_callback_.Reset(); - c.Run(rv); - } + CompletionCallback c = write_callback_; + write_callback_.Reset(); + c.Run(rv); } void TCPClientSocketLibevent::DidCompleteConnect() { diff --git a/net/socket/tcp_client_socket_libevent.h b/net/socket/tcp_client_socket_libevent.h index 47f19a0..a6aa241 100644 --- a/net/socket/tcp_client_socket_libevent.h +++ b/net/socket/tcp_client_socket_libevent.h @@ -43,7 +43,6 @@ class NET_EXPORT_PRIVATE TCPClientSocketLibevent : public StreamSocket, int Bind(const IPEndPoint& address); // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -63,13 +62,10 @@ class NET_EXPORT_PRIVATE TCPClientSocketLibevent : public StreamSocket, // Full duplex mode (reading and writing at the same time) is supported virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; @@ -88,7 +84,7 @@ class NET_EXPORT_PRIVATE TCPClientSocketLibevent : public StreamSocket, // MessageLoopForIO::Watcher methods virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { - if (socket_->old_read_callback_) + if (!socket_->read_callback_.is_null()) socket_->DidCompleteRead(); } @@ -109,8 +105,7 @@ class NET_EXPORT_PRIVATE TCPClientSocketLibevent : public StreamSocket, virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { if (socket_->waiting_connect()) { socket_->DidCompleteConnect(); - } else if (socket_->old_write_callback_ || - !socket_->write_callback_.is_null()) { + } else if (!socket_->write_callback_.is_null()) { socket_->DidCompleteWrite(); } } @@ -179,11 +174,9 @@ class NET_EXPORT_PRIVATE TCPClientSocketLibevent : public StreamSocket, int write_buf_len_; // External callback; called when read is complete. - OldCompletionCallback* old_read_callback_; CompletionCallback read_callback_; // External callback; called when write is complete. - OldCompletionCallback* old_write_callback_; CompletionCallback write_callback_; // The next state for the Connect() state machine. diff --git a/net/socket/tcp_client_socket_unittest.cc b/net/socket/tcp_client_socket_unittest.cc index 50a319b..991b645 100644 --- a/net/socket/tcp_client_socket_unittest.cc +++ b/net/socket/tcp_client_socket_unittest.cc @@ -38,8 +38,8 @@ TEST(TCPClientSocketTest, BindLoopbackToLoopback) { EXPECT_EQ(OK, socket.Bind(IPEndPoint(lo_address, 0))); - TestOldCompletionCallback connect_callback; - EXPECT_EQ(ERR_IO_PENDING, socket.Connect(&connect_callback)); + TestCompletionCallback connect_callback; + EXPECT_EQ(ERR_IO_PENDING, socket.Connect(connect_callback.callback())); TestCompletionCallback accept_callback; scoped_ptr<StreamSocket> accepted_socket; @@ -63,8 +63,8 @@ TEST(TCPClientSocketTest, BindLoopbackToExternal) { ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &lo_address)); EXPECT_EQ(OK, socket.Bind(IPEndPoint(lo_address, 0))); - TestOldCompletionCallback connect_callback; - int result = socket.Connect(&connect_callback); + TestCompletionCallback connect_callback; + int result = socket.Connect(connect_callback.callback()); if (result == ERR_IO_PENDING) result = connect_callback.WaitForResult(); @@ -97,8 +97,8 @@ TEST(TCPClientSocketTest, BindLoopbackToIPv6) { ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &ipv4_lo_ip)); EXPECT_EQ(OK, socket.Bind(IPEndPoint(ipv4_lo_ip, 0))); - TestOldCompletionCallback connect_callback; - int result = socket.Connect(&connect_callback); + TestCompletionCallback connect_callback; + int result = socket.Connect(connect_callback.callback()); if (result == ERR_IO_PENDING) result = connect_callback.WaitForResult(); diff --git a/net/socket/tcp_client_socket_win.cc b/net/socket/tcp_client_socket_win.cc index cb59a98..b5921b9 100644 --- a/net/socket/tcp_client_socket_win.cc +++ b/net/socket/tcp_client_socket_win.cc @@ -317,8 +317,6 @@ TCPClientSocketWin::TCPClientSocketWin(const AddressList& addresses, current_ai_(NULL), waiting_read_(false), waiting_write_(false), - old_read_callback_(NULL), - write_callback_(NULL), next_connect_state_(CONNECT_STATE_NONE), connect_os_error_(0), net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)), @@ -382,35 +380,6 @@ int TCPClientSocketWin::Bind(const IPEndPoint& address) { } -int TCPClientSocketWin::Connect(OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - - // If already connected, then just return OK. - if (socket_ != INVALID_SOCKET) - return OK; - - base::StatsCounter connects("tcp.connect"); - connects.Increment(); - - net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT, - new AddressListNetLogParam(addresses_)); - - // We will try to connect to each address in addresses_. Start with the - // first one in the list. - next_connect_state_ = CONNECT_STATE_CONNECT; - current_ai_ = addresses_.head(); - - int rv = DoConnectLoop(OK); - if (rv == ERR_IO_PENDING) { - // Synchronous operation not supported. - DCHECK(callback); - old_read_callback_ = callback; - } else { - LogConnectCompletion(rv); - } - - return rv; -} int TCPClientSocketWin::Connect(const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); @@ -433,6 +402,7 @@ int TCPClientSocketWin::Connect(const CompletionCallback& callback) { if (rv == ERR_IO_PENDING) { // Synchronous operation not supported. DCHECK(!callback.is_null()); + // TODO(ajwong): Is setting read_callback_ the right thing to do here?? read_callback_ = callback; } else { LogConnectCompletion(rv); @@ -705,52 +675,11 @@ base::TimeDelta TCPClientSocketWin::GetConnectTimeMicros() const { int TCPClientSocketWin::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - DCHECK_NE(socket_, INVALID_SOCKET); - DCHECK(!waiting_read_); - DCHECK(!old_read_callback_ && read_callback_.is_null()); - DCHECK(!core_->read_iobuffer_); - - buf_len = core_->ThrottleReadSize(buf_len); - - core_->read_buffer_.len = buf_len; - core_->read_buffer_.buf = buf->data(); - - // TODO(wtc): Remove the assertion after enough testing. - AssertEventNotSignaled(core_->read_overlapped_.hEvent); - DWORD num, flags = 0; - int rv = WSARecv(socket_, &core_->read_buffer_, 1, &num, &flags, - &core_->read_overlapped_, NULL); - if (rv == 0) { - if (ResetEventIfSignaled(core_->read_overlapped_.hEvent)) { - base::StatsCounter read_bytes("tcp.read_bytes"); - read_bytes.Add(num); - num_bytes_read_ += num; - if (num > 0) - use_history_.set_was_used_to_convey_data(); - net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, num, - core_->read_buffer_.buf); - return static_cast<int>(num); - } - } else { - int os_error = WSAGetLastError(); - if (os_error != WSA_IO_PENDING) - return MapSystemError(os_error); - } - core_->WatchForRead(); - waiting_read_ = true; - old_read_callback_ = callback; - core_->read_iobuffer_ = buf; - return ERR_IO_PENDING; -} -int TCPClientSocketWin::Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); DCHECK_NE(socket_, INVALID_SOCKET); DCHECK(!waiting_read_); - DCHECK(!old_read_callback_ && read_callback_.is_null()); + DCHECK(read_callback_.is_null()); DCHECK(!core_->read_iobuffer_); buf_len = core_->ThrottleReadSize(buf_len); @@ -788,11 +717,11 @@ int TCPClientSocketWin::Read(IOBuffer* buf, int TCPClientSocketWin::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); DCHECK_NE(socket_, INVALID_SOCKET); DCHECK(!waiting_write_); - DCHECK(!write_callback_); + DCHECK(write_callback_.is_null()); DCHECK_GT(buf_len, 0); DCHECK(!core_->write_iobuffer_); @@ -881,28 +810,22 @@ void TCPClientSocketWin::LogConnectCompletion(int net_error) { void TCPClientSocketWin::DoReadCallback(int rv) { DCHECK_NE(rv, ERR_IO_PENDING); - DCHECK(old_read_callback_ || !read_callback_.is_null()); + DCHECK(!read_callback_.is_null()); - // since Run may result in Read being called, clear read_callback_ up front. - if (old_read_callback_) { - OldCompletionCallback* c = old_read_callback_; - old_read_callback_ = NULL; - c->Run(rv); - } else { - CompletionCallback c = read_callback_; - read_callback_.Reset(); - c.Run(rv); - } + // Since Run may result in Read being called, clear read_callback_ up front. + CompletionCallback c = read_callback_; + read_callback_.Reset(); + c.Run(rv); } void TCPClientSocketWin::DoWriteCallback(int rv) { DCHECK_NE(rv, ERR_IO_PENDING); - DCHECK(write_callback_); + DCHECK(!write_callback_.is_null()); // since Run may result in Write being called, clear write_callback_ up front. - OldCompletionCallback* c = write_callback_; - write_callback_ = NULL; - c->Run(rv); + CompletionCallback c = write_callback_; + write_callback_.Reset(); + c.Run(rv); } void TCPClientSocketWin::DidCompleteConnect() { diff --git a/net/socket/tcp_client_socket_win.h b/net/socket/tcp_client_socket_win.h index 1e75933..7f681fa 100644 --- a/net/socket/tcp_client_socket_win.h +++ b/net/socket/tcp_client_socket_win.h @@ -42,7 +42,6 @@ class NET_EXPORT TCPClientSocketWin : public StreamSocket, int Bind(const IPEndPoint& address); // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback); virtual int Connect(const CompletionCallback& callback); virtual void Disconnect(); virtual bool IsConnected() const; @@ -60,10 +59,10 @@ class NET_EXPORT TCPClientSocketWin : public StreamSocket, // Socket implementation. // Multiple outstanding requests are not supported. // Full duplex mode (reading and writing at the same time) is supported - virtual int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); - virtual int Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); + virtual int Write(IOBuffer* buf, int buf_len, + const CompletionCallback& callback); virtual bool SetReceiveBufferSize(int32 size); virtual bool SetSendBufferSize(int32 size); @@ -126,11 +125,10 @@ class NET_EXPORT TCPClientSocketWin : public StreamSocket, scoped_refptr<Core> core_; // External callback; called when connect or read is complete. - OldCompletionCallback* old_read_callback_; CompletionCallback read_callback_; // External callback; called when write is complete. - OldCompletionCallback* write_callback_; + CompletionCallback write_callback_; // The next state for the Connect() state machine. ConnectState next_connect_state_; diff --git a/net/socket/tcp_server_socket_unittest.cc b/net/socket/tcp_server_socket_unittest.cc index 573ff4c..2ccc70e 100644 --- a/net/socket/tcp_server_socket_unittest.cc +++ b/net/socket/tcp_server_socket_unittest.cc @@ -77,10 +77,10 @@ class TCPServerSocketTest : public PlatformTest { TEST_F(TCPServerSocketTest, Accept) { ASSERT_NO_FATAL_FAILURE(SetUpIPv4()); - TestOldCompletionCallback connect_callback; + TestCompletionCallback connect_callback; TCPClientSocket connecting_socket(local_address_list(), NULL, NetLog::Source()); - connecting_socket.Connect(&connect_callback); + connecting_socket.Connect(connect_callback.callback()); TestCompletionCallback accept_callback; scoped_ptr<StreamSocket> accepted_socket; @@ -108,10 +108,10 @@ TEST_F(TCPServerSocketTest, AcceptAsync) { ASSERT_EQ(ERR_IO_PENDING, socket_.Accept(&accepted_socket, accept_callback.callback())); - TestOldCompletionCallback connect_callback; + TestCompletionCallback connect_callback; TCPClientSocket connecting_socket(local_address_list(), NULL, NetLog::Source()); - connecting_socket.Connect(&connect_callback); + connecting_socket.Connect(connect_callback.callback()); EXPECT_EQ(OK, connect_callback.WaitForResult()); EXPECT_EQ(OK, accept_callback.WaitForResult()); @@ -133,15 +133,15 @@ TEST_F(TCPServerSocketTest, Accept2Connections) { ASSERT_EQ(ERR_IO_PENDING, socket_.Accept(&accepted_socket, accept_callback.callback())); - TestOldCompletionCallback connect_callback; + TestCompletionCallback connect_callback; TCPClientSocket connecting_socket(local_address_list(), NULL, NetLog::Source()); - connecting_socket.Connect(&connect_callback); + connecting_socket.Connect(connect_callback.callback()); - TestOldCompletionCallback connect_callback2; + TestCompletionCallback connect_callback2; TCPClientSocket connecting_socket2(local_address_list(), NULL, NetLog::Source()); - connecting_socket2.Connect(&connect_callback2); + connecting_socket2.Connect(connect_callback2.callback()); EXPECT_EQ(OK, accept_callback.WaitForResult()); @@ -170,10 +170,10 @@ TEST_F(TCPServerSocketTest, AcceptIPv6) { if (!initialized) return; - TestOldCompletionCallback connect_callback; + TestCompletionCallback connect_callback; TCPClientSocket connecting_socket(local_address_list(), NULL, NetLog::Source()); - connecting_socket.Connect(&connect_callback); + connecting_socket.Connect(connect_callback.callback()); TestCompletionCallback accept_callback; scoped_ptr<StreamSocket> accepted_socket; diff --git a/net/socket/transport_client_socket_pool.cc b/net/socket/transport_client_socket_pool.cc index a423ebd..faf12df 100644 --- a/net/socket/transport_client_socket_pool.cc +++ b/net/socket/transport_client_socket_pool.cc @@ -99,14 +99,7 @@ TransportConnectJob::TransportConnectJob( BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)), params_(params), client_socket_factory_(client_socket_factory), - ALLOW_THIS_IN_INITIALIZER_LIST( - callback_(this, - &TransportConnectJob::OnIOComplete)), - resolver_(host_resolver), - ALLOW_THIS_IN_INITIALIZER_LIST( - fallback_callback_( - this, - &TransportConnectJob::DoIPv6FallbackTransportConnectComplete)) {} + resolver_(host_resolver) {} TransportConnectJob::~TransportConnectJob() { // We don't worry about cancelling the host resolution and TCP connect, since @@ -216,7 +209,8 @@ int TransportConnectJob::DoTransportConnect() { transport_socket_.reset(client_socket_factory_->CreateTransportClientSocket( addresses_, net_log().net_log(), net_log().source())); connect_start_time_ = base::TimeTicks::Now(); - int rv = transport_socket_->Connect(&callback_); + int rv = transport_socket_->Connect( + base::Bind(&TransportConnectJob::OnIOComplete, base::Unretained(this))); if (rv == ERR_IO_PENDING && AddressListStartsWithIPv6AndHasAnIPv4Addr(addresses_)) { fallback_timer_.Start(FROM_HERE, @@ -296,7 +290,10 @@ void TransportConnectJob::DoIPv6FallbackTransportConnect() { client_socket_factory_->CreateTransportClientSocket( *fallback_addresses_, net_log().net_log(), net_log().source())); fallback_connect_start_time_ = base::TimeTicks::Now(); - int rv = fallback_transport_socket_->Connect(&fallback_callback_); + int rv = fallback_transport_socket_->Connect( + base::Bind( + &TransportConnectJob::DoIPv6FallbackTransportConnectComplete, + base::Unretained(this))); if (rv != ERR_IO_PENDING) DoIPv6FallbackTransportConnectComplete(rv); } diff --git a/net/socket/transport_client_socket_pool.h b/net/socket/transport_client_socket_pool.h index e3e6f02..44309d4 100644 --- a/net/socket/transport_client_socket_pool.h +++ b/net/socket/transport_client_socket_pool.h @@ -108,7 +108,6 @@ class NET_EXPORT_PRIVATE TransportConnectJob : public ConnectJob { scoped_refptr<TransportSocketParams> params_; ClientSocketFactory* const client_socket_factory_; - OldCompletionCallbackImpl<TransportConnectJob> callback_; SingleRequestHostResolver resolver_; AddressList addresses_; State next_state_; @@ -123,7 +122,6 @@ class NET_EXPORT_PRIVATE TransportConnectJob : public ConnectJob { scoped_ptr<StreamSocket> fallback_transport_socket_; scoped_ptr<AddressList> fallback_addresses_; - OldCompletionCallbackImpl<TransportConnectJob> fallback_callback_; base::TimeTicks fallback_connect_start_time_; base::OneShotTimer<TransportConnectJob> fallback_timer_; diff --git a/net/socket/transport_client_socket_pool_unittest.cc b/net/socket/transport_client_socket_pool_unittest.cc index 56b1fa9c..c5d1080 100644 --- a/net/socket/transport_client_socket_pool_unittest.cc +++ b/net/socket/transport_client_socket_pool_unittest.cc @@ -52,10 +52,6 @@ class MockClientSocket : public StreamSocket { addrlist_(addrlist) {} // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback) { - connected_ = true; - return OK; - } virtual int Connect(const CompletionCallback& callback) { connected_ = true; return OK; @@ -96,15 +92,11 @@ class MockClientSocket : public StreamSocket { // Socket implementation. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - return ERR_FAILED; - } - virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { return ERR_FAILED; } virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return ERR_FAILED; } virtual bool SetReceiveBufferSize(int32 size) { return true; } @@ -121,10 +113,7 @@ class MockFailingClientSocket : public StreamSocket { MockFailingClientSocket(const AddressList& addrlist) : addrlist_(addrlist) {} // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback) { - return ERR_CONNECTION_FAILED; - } - virtual int Connect(const net::CompletionCallback& callback) { + virtual int Connect(const CompletionCallback& callback) { return ERR_CONNECTION_FAILED; } @@ -157,16 +146,12 @@ class MockFailingClientSocket : public StreamSocket { // Socket implementation. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - return ERR_FAILED; - } - virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { return ERR_FAILED; } virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return ERR_FAILED; } virtual bool SetReceiveBufferSize(int32 size) { return true; } @@ -196,14 +181,6 @@ class MockPendingClientSocket : public StreamSocket { addrlist_(addrlist) {} // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback) { - MessageLoop::current()->PostDelayedTask( - FROM_HERE, - base::Bind(&MockPendingClientSocket::DoOldCallback, - weak_factory_.GetWeakPtr(), callback), - delay_ms_); - return ERR_IO_PENDING; - } virtual int Connect(const CompletionCallback& callback) { MessageLoop::current()->PostDelayedTask( FROM_HERE, @@ -248,34 +225,18 @@ class MockPendingClientSocket : public StreamSocket { // Socket implementation. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - return ERR_FAILED; - } - virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { return ERR_FAILED; } virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return ERR_FAILED; } virtual bool SetReceiveBufferSize(int32 size) { return true; } virtual bool SetSendBufferSize(int32 size) { return true; } private: - void DoOldCallback(OldCompletionCallback* callback) { - if (should_stall_) - return; - - if (should_connect_) { - is_connected_ = true; - callback->Run(OK); - } else { - is_connected_ = false; - callback->Run(ERR_CONNECTION_FAILED); - } - } void DoCallback(const CompletionCallback& callback) { if (should_stall_) return; diff --git a/net/socket/transport_client_socket_unittest.cc b/net/socket/transport_client_socket_unittest.cc index d661e02..4ba4c000 100644 --- a/net/socket/transport_client_socket_unittest.cc +++ b/net/socket/transport_client_socket_unittest.cc @@ -75,7 +75,7 @@ class TransportClientSocketTest int DrainClientSocket(IOBuffer* buf, uint32 buf_len, uint32 bytes_to_read, - TestOldCompletionCallback* callback); + TestCompletionCallback* callback); void SendClientRequest(); @@ -136,12 +136,12 @@ void TransportClientSocketTest::SetUp() { int TransportClientSocketTest::DrainClientSocket( IOBuffer* buf, uint32 buf_len, - uint32 bytes_to_read, TestOldCompletionCallback* callback) { + uint32 bytes_to_read, TestCompletionCallback* callback) { int rv = OK; uint32 bytes_read = 0; while (bytes_read < bytes_to_read) { - rv = sock_->Read(buf, buf_len, callback); + rv = sock_->Read(buf, buf_len, callback->callback()); EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); if (rv == ERR_IO_PENDING) @@ -158,11 +158,12 @@ void TransportClientSocketTest::SendClientRequest() { const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; scoped_refptr<IOBuffer> request_buffer( new IOBuffer(arraysize(request_text) - 1)); - TestOldCompletionCallback callback; + TestCompletionCallback callback; int rv; memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); - rv = sock_->Write(request_buffer, arraysize(request_text) - 1, &callback); + rv = sock_->Write(request_buffer, arraysize(request_text) - 1, + callback.callback()); EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); if (rv == ERR_IO_PENDING) @@ -176,10 +177,10 @@ INSTANTIATE_TEST_CASE_P(StreamSocket, ::testing::Values(TCP)); TEST_P(TransportClientSocketTest, Connect) { - TestOldCompletionCallback callback; + TestCompletionCallback callback; EXPECT_FALSE(sock_->IsConnected()); - int rv = sock_->Connect(&callback); + int rv = sock_->Connect(callback.callback()); net::CapturingNetLog::EntryList net_log_entries; net_log_.GetEntries(&net_log_entries); @@ -204,12 +205,12 @@ TEST_P(TransportClientSocketTest, Connect) { TEST_P(TransportClientSocketTest, IsConnected) { scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); - TestOldCompletionCallback callback; + TestCompletionCallback callback; uint32 bytes_read; EXPECT_FALSE(sock_->IsConnected()); EXPECT_FALSE(sock_->IsConnectedAndIdle()); - int rv = sock_->Connect(&callback); + int rv = sock_->Connect(callback.callback()); if (rv != OK) { ASSERT_EQ(rv, ERR_IO_PENDING); rv = callback.WaitForResult(); @@ -261,8 +262,8 @@ TEST_P(TransportClientSocketTest, IsConnected) { } TEST_P(TransportClientSocketTest, Read) { - TestOldCompletionCallback callback; - int rv = sock_->Connect(&callback); + TestCompletionCallback callback; + int rv = sock_->Connect(callback.callback()); if (rv != OK) { ASSERT_EQ(rv, ERR_IO_PENDING); @@ -279,7 +280,7 @@ TEST_P(TransportClientSocketTest, Read) { // All data has been read now. Read once more to force an ERR_IO_PENDING, and // then close the server socket, and note the close. - rv = sock_->Read(buf, 4096, &callback); + rv = sock_->Read(buf, 4096, callback.callback()); ASSERT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(static_cast<int64>(std::string(kServerReply).size()), sock_->NumBytesRead()); @@ -288,8 +289,8 @@ TEST_P(TransportClientSocketTest, Read) { } TEST_P(TransportClientSocketTest, Read_SmallChunks) { - TestOldCompletionCallback callback; - int rv = sock_->Connect(&callback); + TestCompletionCallback callback; + int rv = sock_->Connect(callback.callback()); if (rv != OK) { ASSERT_EQ(rv, ERR_IO_PENDING); @@ -301,7 +302,7 @@ TEST_P(TransportClientSocketTest, Read_SmallChunks) { scoped_refptr<IOBuffer> buf(new IOBuffer(1)); uint32 bytes_read = 0; while (bytes_read < arraysize(kServerReply) - 1) { - rv = sock_->Read(buf, 1, &callback); + rv = sock_->Read(buf, 1, callback.callback()); EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); if (rv == ERR_IO_PENDING) @@ -314,7 +315,7 @@ TEST_P(TransportClientSocketTest, Read_SmallChunks) { // All data has been read now. Read once more to force an ERR_IO_PENDING, and // then close the server socket, and note the close. - rv = sock_->Read(buf, 1, &callback); + rv = sock_->Read(buf, 1, callback.callback()); EXPECT_EQ(static_cast<int64>(std::string(kServerReply).size()), sock_->NumBytesRead()); ASSERT_EQ(ERR_IO_PENDING, rv); @@ -323,8 +324,8 @@ TEST_P(TransportClientSocketTest, Read_SmallChunks) { } TEST_P(TransportClientSocketTest, Read_Interrupted) { - TestOldCompletionCallback callback; - int rv = sock_->Connect(&callback); + TestCompletionCallback callback; + int rv = sock_->Connect(callback.callback()); if (rv != OK) { ASSERT_EQ(ERR_IO_PENDING, rv); @@ -335,7 +336,7 @@ TEST_P(TransportClientSocketTest, Read_Interrupted) { // Do a partial read and then exit. This test should not crash! scoped_refptr<IOBuffer> buf(new IOBuffer(16)); - rv = sock_->Read(buf, 16, &callback); + rv = sock_->Read(buf, 16, callback.callback()); EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); EXPECT_EQ(0, sock_->NumBytesRead()); @@ -348,8 +349,8 @@ TEST_P(TransportClientSocketTest, Read_Interrupted) { } TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_ReadFirst) { - TestOldCompletionCallback callback; - int rv = sock_->Connect(&callback); + TestCompletionCallback callback; + int rv = sock_->Connect(callback.callback()); if (rv != OK) { ASSERT_EQ(rv, ERR_IO_PENDING); @@ -360,7 +361,7 @@ TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_ReadFirst) { // Read first. There's no data, so it should return ERR_IO_PENDING. const int kBufLen = 4096; scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); - rv = sock_->Read(buf, kBufLen, &callback); + rv = sock_->Read(buf, kBufLen, callback.callback()); EXPECT_EQ(ERR_IO_PENDING, rv); PauseServerReads(); @@ -368,10 +369,10 @@ TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_ReadFirst) { scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); char* request_data = request_buffer->data(); memset(request_data, 'A', kWriteBufLen); - TestOldCompletionCallback write_callback; + TestCompletionCallback write_callback; while (true) { - rv = sock_->Write(request_buffer, kWriteBufLen, &write_callback); + rv = sock_->Write(request_buffer, kWriteBufLen, write_callback.callback()); ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); if (rv == ERR_IO_PENDING) { @@ -390,8 +391,8 @@ TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_ReadFirst) { } TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_WriteFirst) { - TestOldCompletionCallback callback; - int rv = sock_->Connect(&callback); + TestCompletionCallback callback; + int rv = sock_->Connect(callback.callback()); if (rv != OK) { ASSERT_EQ(ERR_IO_PENDING, rv); @@ -404,10 +405,10 @@ TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_WriteFirst) { scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); char* request_data = request_buffer->data(); memset(request_data, 'A', kWriteBufLen); - TestOldCompletionCallback write_callback; + TestCompletionCallback write_callback; while (true) { - rv = sock_->Write(request_buffer, kWriteBufLen, &write_callback); + rv = sock_->Write(request_buffer, kWriteBufLen, write_callback.callback()); ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); if (rv == ERR_IO_PENDING) @@ -420,7 +421,7 @@ TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_WriteFirst) { const int kBufLen = 4096; scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); while (true) { - rv = sock_->Read(buf, kBufLen, &callback); + rv = sock_->Read(buf, kBufLen, callback.callback()); ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); if (rv == ERR_IO_PENDING) break; diff --git a/net/socket/web_socket_server_socket.cc b/net/socket/web_socket_server_socket.cc index d792689..08b7788 100644 --- a/net/socket/web_socket_server_socket.cc +++ b/net/socket/web_socket_server_socket.cc @@ -118,10 +118,6 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { handshake_buf_, kHandshakeLimitBytes)), process_handshake_buf_(new net::DrainableIOBuffer( handshake_buf_, kHandshakeLimitBytes)), - transport_read_callback_(NewCallback( - this, &WebSocketServerSocketImpl::OnRead)), - transport_write_callback_(NewCallback( - this, &WebSocketServerSocketImpl::OnWrite)), is_transport_read_pending_(false), is_transport_write_pending_(false), method_factory_(this) { @@ -135,11 +131,8 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { it->type == PendingReq::TYPE_READ && it->io_buf != NULL && it->io_buf->data() != NULL && - (it->old_callback || !it->callback.is_null())) { - if (it->old_callback) - it->old_callback->Run(0); // Report EOF. - else - it->callback.Run(0); + !it->callback.is_null()) { + it->callback.Run(0); // Report EOF. } } @@ -175,29 +168,9 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { }; PendingReq(Type type, net::DrainableIOBuffer* io_buf, - net::OldCompletionCallback* callback) - : type(type), - io_buf(io_buf), - old_callback(callback) { - switch (type) { - case PendingReq::TYPE_READ: - case PendingReq::TYPE_WRITE: - case PendingReq::TYPE_READ_METADATA: - case PendingReq::TYPE_WRITE_METADATA: { - DCHECK(io_buf); - break; - } - default: { - NOTREACHED(); - break; - } - } - } - PendingReq(Type type, net::DrainableIOBuffer* io_buf, const net::CompletionCallback& callback) : type(type), io_buf(io_buf), - old_callback(NULL), callback(callback) { switch (type) { case PendingReq::TYPE_READ: @@ -216,76 +189,11 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { Type type; scoped_refptr<net::DrainableIOBuffer> io_buf; - net::OldCompletionCallback* old_callback; net::CompletionCallback callback; }; // Socket implementation. virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE { - if (buf_len == 0) - return 0; - if (buf == NULL || buf_len < 0) { - NOTREACHED(); - return net::ERR_INVALID_ARGUMENT; - } - while (int bytes_remaining = fill_handshake_buf_->BytesConsumed() - - process_handshake_buf_->BytesConsumed()) { - DCHECK(!is_transport_read_pending_); - DCHECK(GetPendingReq(PendingReq::TYPE_READ) == pending_reqs_.end()); - switch (phase_) { - case PHASE_FRAME_OUTSIDE: - case PHASE_FRAME_INSIDE: - case PHASE_FRAME_LENGTH: - case PHASE_FRAME_SKIP: { - int n = std::min(bytes_remaining, buf_len); - int rv = ProcessDataFrames( - process_handshake_buf_->data(), n, buf->data(), buf_len); - process_handshake_buf_->DidConsume(n); - if (rv == 0) { - // ProcessDataFrames may return zero for non-empty buffer if it - // contains only frame delimiters without real data. In this case: - // try again and do not just return zero (zero stands for EOF). - continue; - } - return rv; - } - case PHASE_SHUT: { - return 0; - } - case PHASE_NYMPH: - case PHASE_HANDSHAKE: - default: { - NOTREACHED(); - return net::ERR_UNEXPECTED; - } - } - } - switch (phase_) { - case PHASE_FRAME_OUTSIDE: - case PHASE_FRAME_INSIDE: - case PHASE_FRAME_LENGTH: - case PHASE_FRAME_SKIP: { - pending_reqs_.push_back(PendingReq( - PendingReq::TYPE_READ, - new net::DrainableIOBuffer(buf, buf_len), - callback)); - ConsiderTransportRead(); - break; - } - case PHASE_SHUT: { - return 0; - } - case PHASE_NYMPH: - case PHASE_HANDSHAKE: - default: { - NOTREACHED(); - return net::ERR_UNEXPECTED; - } - } - return net::ERR_IO_PENDING; - } - virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) OVERRIDE { if (buf_len == 0) return 0; @@ -351,7 +259,7 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { } virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE { + const net::CompletionCallback& callback) OVERRIDE { if (buf_len == 0) return 0; if (buf == NULL || buf_len < 0) { @@ -382,7 +290,7 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { frame_start->data()[0] = '\x00'; pending_reqs_.push_back(PendingReq(PendingReq::TYPE_WRITE_METADATA, new net::DrainableIOBuffer(frame_start, 1), - NULL)); + net::CompletionCallback())); pending_reqs_.push_back(PendingReq(PendingReq::TYPE_WRITE, new net::DrainableIOBuffer(buf, buf_len), @@ -392,7 +300,7 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { frame_end->data()[0] = '\xff'; pending_reqs_.push_back(PendingReq(PendingReq::TYPE_WRITE_METADATA, new net::DrainableIOBuffer(frame_end, 1), - NULL)); + net::CompletionCallback())); ConsiderTransportWrite(); return net::ERR_IO_PENDING; @@ -411,8 +319,14 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { if (phase_ != PHASE_NYMPH) return net::ERR_UNEXPECTED; phase_ = PHASE_HANDSHAKE; + net::CompletionCallback cb; + if (callback) { + cb = base::Bind(&net::OldCompletionCallback::Run<int>, + base::Unretained(callback)); + } pending_reqs_.push_front(PendingReq( - PendingReq::TYPE_READ_METADATA, fill_handshake_buf_.get(), callback)); + PendingReq::TYPE_READ_METADATA, fill_handshake_buf_.get(), + cb)); ConsiderTransportRead(); return net::ERR_IO_PENDING; } @@ -441,7 +355,8 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { is_transport_read_pending_ = true; int rv = transport_socket_->Read( it->io_buf.get(), it->io_buf->BytesRemaining(), - transport_read_callback_.get()); + base::Bind(&WebSocketServerSocketImpl::OnRead, + base::Unretained(this))); if (rv != net::ERR_IO_PENDING) { // PostTask rather than direct call in order to: // (1) guarantee calling callback after returning from Read(); @@ -468,7 +383,8 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { is_transport_write_pending_ = true; int rv = transport_socket_->Write( it->io_buf.get(), it->io_buf->BytesRemaining(), - transport_write_callback_.get()); + base::Bind(&WebSocketServerSocketImpl::OnWrite, + base::Unretained(this))); if (rv != net::ERR_IO_PENDING) { // PostTask rather than direct call in order to: // (1) guarantee calling callback after returning from Read(); @@ -485,9 +401,7 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { if (result != 0) { while (!pending_reqs_.empty()) { PendingReq& req = pending_reqs_.front(); - if (req.old_callback) - req.old_callback->Run(result); - else if (!req.callback.is_null()) + if (!req.callback.is_null()) req.callback.Run(result); pending_reqs_.pop_front(); } @@ -537,14 +451,10 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { if (rv > 0) { process_handshake_buf_->DidConsume(rv); phase_ = PHASE_FRAME_OUTSIDE; - net::OldCompletionCallback* old_cb = - pending_reqs_.front().old_callback; net::CompletionCallback cb = pending_reqs_.front().callback; pending_reqs_.pop_front(); ConsiderTransportWrite(); // Schedule answer handshake. - if (old_cb) - old_cb->Run(0); - else if (!cb.is_null()) + if (!cb.is_null()) cb.Run(0); } else if (rv == net::ERR_IO_PENDING) { if (fill_handshake_buf_->BytesRemaining() < 1) @@ -568,12 +478,9 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { return; } if (rv > 0 || phase_ == PHASE_SHUT) { - net::OldCompletionCallback* old_cb = it->old_callback; net::CompletionCallback cb = it->callback; pending_reqs_.erase(it); - if (old_cb) - old_cb->Run(rv); - else if (!cb.is_null()) + if (!cb.is_null()) cb.Run(rv); } break; @@ -612,14 +519,11 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { DCHECK_LE(result, it->io_buf->BytesRemaining()); it->io_buf->DidConsume(result); if (it->io_buf->BytesRemaining() == 0) { - net::OldCompletionCallback* old_cb = it->old_callback; net::CompletionCallback cb = it->callback; int bytes_written = it->io_buf->BytesConsumed(); DCHECK_GT(bytes_written, 0); pending_reqs_.erase(it); - if (old_cb) - old_cb->Run(bytes_written); - else if (!cb.is_null()) + if (!cb.is_null()) cb.Run(bytes_written); } ConsiderTransportWrite(); @@ -885,7 +789,7 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { return net::ERR_LIMIT_VIOLATION; pending_reqs_.push_back(PendingReq( - PendingReq::TYPE_WRITE_METADATA, buffer, NULL)); + PendingReq::TYPE_WRITE_METADATA, buffer, net::CompletionCallback())); DCHECK_GT(term_pos - buf, 0); return term_pos - buf; } @@ -977,10 +881,6 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket { // Pending io requests we need to complete. std::deque<PendingReq> pending_reqs_; - // Callbacks from transport to us. - scoped_ptr<net::OldCompletionCallback> transport_read_callback_; - scoped_ptr<net::OldCompletionCallback> transport_write_callback_; - // Whether transport requests are pending. bool is_transport_read_pending_; bool is_transport_write_pending_; diff --git a/net/socket/web_socket_server_socket_unittest.cc b/net/socket/web_socket_server_socket_unittest.cc index cabb4b9..d64cf19 100644 --- a/net/socket/web_socket_server_socket_unittest.cc +++ b/net/socket/web_socket_server_socket_unittest.cc @@ -79,53 +79,25 @@ class TestingTransportSocket : public net::Socket { net::DrainableIOBuffer* sample, net::DrainableIOBuffer* answer) : sample_(sample), answer_(answer), - old_final_read_callback_(NULL), - method_factory_(this) { + ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { } ~TestingTransportSocket() { - if (old_final_read_callback_) { + if (!final_read_callback_.is_null()) { MessageLoop::current()->PostTask(FROM_HERE, - method_factory_.NewRunnableMethod( - &TestingTransportSocket::DoOldReadCallback, - old_final_read_callback_, 0)); - } else if (!final_read_callback_.is_null()) { - MessageLoop::current()->PostTask( - FROM_HERE, - method_factory_.NewRunnableMethod( - &TestingTransportSocket::DoReadCallback, - final_read_callback_, 0)); + base::Bind(&TestingTransportSocket::DoReadCallback, + weak_factory_.GetWeakPtr(), + final_read_callback_, 0)); } } // Socket implementation. virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { - CHECK_GT(buf_len, 0); - int remaining = sample_->BytesRemaining(); - if (remaining < 1) { - if (old_final_read_callback_ || !final_read_callback_.is_null()) - return 0; - old_final_read_callback_ = callback; - return net::ERR_IO_PENDING; - } - int lot = GetRand(1, std::min(remaining, buf_len)); - std::copy(sample_->data(), sample_->data() + lot, buf->data()); - sample_->DidConsume(lot); - if (GetRand(0, 1)) { - return lot; - } - MessageLoop::current()->PostTask(FROM_HERE, - method_factory_.NewRunnableMethod( - &TestingTransportSocket::DoOldReadCallback, callback, lot)); - return net::ERR_IO_PENDING; - } - virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { CHECK_GT(buf_len, 0); int remaining = sample_->BytesRemaining(); if (remaining < 1) { - if (old_final_read_callback_ || !final_read_callback_.is_null()) + if (!final_read_callback_.is_null()) return 0; final_read_callback_ = callback; return net::ERR_IO_PENDING; @@ -136,14 +108,15 @@ class TestingTransportSocket : public net::Socket { if (GetRand(0, 1)) { return lot; } - MessageLoop::current()->PostTask(FROM_HERE, - method_factory_.NewRunnableMethod( - &TestingTransportSocket::DoReadCallback, callback, lot)); + MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(&TestingTransportSocket::DoReadCallback, + weak_factory_.GetWeakPtr(), callback, lot)); return net::ERR_IO_PENDING; } virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { + const net::CompletionCallback& callback) { CHECK_GT(buf_len, 0); int remaining = answer_->BytesRemaining(); CHECK_GE(remaining, buf_len); @@ -155,9 +128,10 @@ class TestingTransportSocket : public net::Socket { if (GetRand(0, 1)) { return lot; } - MessageLoop::current()->PostTask(FROM_HERE, - method_factory_.NewRunnableMethod( - &TestingTransportSocket::DoWriteCallback, callback, lot)); + MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(&TestingTransportSocket::DoWriteCallback, + weak_factory_.GetWeakPtr(), callback, lot)); return net::ERR_IO_PENDING; } @@ -171,30 +145,22 @@ class TestingTransportSocket : public net::Socket { net::DrainableIOBuffer* answer() { return answer_.get(); } - void DoOldReadCallback(net::OldCompletionCallback* callback, int result) { - if (result == 0 && !is_closed_) { - MessageLoop::current()->PostTask(FROM_HERE, - method_factory_.NewRunnableMethod( - &TestingTransportSocket::DoOldReadCallback, callback, 0)); - } else { - if (callback) - callback->Run(result); - } - } void DoReadCallback(const net::CompletionCallback& callback, int result) { if (result == 0 && !is_closed_) { - MessageLoop::current()->PostTask(FROM_HERE, - method_factory_.NewRunnableMethod( - &TestingTransportSocket::DoReadCallback, callback, 0)); + MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind( + &TestingTransportSocket::DoReadCallback, + weak_factory_.GetWeakPtr(), callback, 0)); } else { if (!callback.is_null()) callback.Run(result); } } - void DoWriteCallback(net::OldCompletionCallback* callback, int result) { - if (callback) - callback->Run(result); + void DoWriteCallback(const net::CompletionCallback& callback, int result) { + if (!callback.is_null()) + callback.Run(result); } bool is_closed_; @@ -206,10 +172,9 @@ class TestingTransportSocket : public net::Socket { scoped_refptr<net::DrainableIOBuffer> answer_; // Final read callback to report zero (zero stands for EOF). - net::OldCompletionCallback* old_final_read_callback_; net::CompletionCallback final_read_callback_; - ScopedRunnableMethodFactory<TestingTransportSocket> method_factory_; + base::WeakPtrFactory<TestingTransportSocket> weak_factory_; }; class Validator : public net::WebSocketServerSocket::Delegate { @@ -256,9 +221,8 @@ class ReadWriteTracker { net::WebSocketServerSocket* ws, int bytes_to_read, int bytes_to_write) : ws_(ws), buf_size_(1 << 14), - accept_callback_(NewCallback(this, &ReadWriteTracker::OnAccept)), - read_callback_(NewCallback(this, &ReadWriteTracker::OnRead)), - write_callback_(NewCallback(this, &ReadWriteTracker::OnWrite)), + ALLOW_THIS_IN_INITIALIZER_LIST( + accept_callback_(this, &ReadWriteTracker::OnAccept)), read_buf_(new net::IOBuffer(buf_size_)), write_buf_(new net::IOBuffer(buf_size_)), bytes_remaining_to_read_(bytes_to_read), @@ -266,7 +230,7 @@ class ReadWriteTracker { read_initiated_(false), write_initiated_(false), got_final_zero_(false) { - int rv = ws_->Accept(accept_callback_.get()); + int rv = ws_->Accept(&accept_callback_); if (rv != net::ERR_IO_PENDING) OnAccept(rv); } @@ -295,7 +259,8 @@ class ReadWriteTracker { for (int i = 0; i < lot; ++i) write_buf_->data()[i] = ReferenceSeq( bytes_remaining_to_write_ - i - 1, kWriteSalt); - int rv = ws_->Write(write_buf_, lot, write_callback_.get()); + int rv = ws_->Write(write_buf_, lot, base::Bind(&ReadWriteTracker::OnWrite, + base::Unretained(this))); if (rv != net::ERR_IO_PENDING) OnWrite(rv); } @@ -309,7 +274,8 @@ class ReadWriteTracker { lot = GetRand(1, bytes_remaining_to_read_); lot = std::min(lot, buf_size_); } - int rv = ws_->Read(read_buf_, lot, read_callback_.get()); + int rv = ws_->Read(read_buf_, lot, base::Bind(&ReadWriteTracker::OnRead, + base::Unretained(this))); if (rv != net::ERR_IO_PENDING) OnRead(rv); } @@ -340,9 +306,7 @@ class ReadWriteTracker { private: net::WebSocketServerSocket* const ws_; int const buf_size_; - scoped_ptr<net::OldCompletionCallback> accept_callback_; - scoped_ptr<net::OldCompletionCallback> read_callback_; - scoped_ptr<net::OldCompletionCallback> write_callback_; + net::OldCompletionCallbackImpl<ReadWriteTracker> accept_callback_; scoped_refptr<net::IOBuffer> read_buf_; scoped_refptr<net::IOBuffer> write_buf_; int bytes_remaining_to_read_; diff --git a/net/socket_stream/socket_stream.cc b/net/socket_stream/socket_stream.cc index e948570..0e9f6b0 100644 --- a/net/socket_stream/socket_stream.cc +++ b/net/socket_stream/socket_stream.cc @@ -71,10 +71,6 @@ SocketStream::SocketStream(const GURL& url, Delegate* delegate) base::Unretained(this)))), ALLOW_THIS_IN_INITIALIZER_LIST( io_callback_old_(this, &SocketStream::OnIOCompleted)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_old_(this, &SocketStream::OnReadCompleted)), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_callback_old_(this, &SocketStream::OnWriteCompleted)), read_buf_(NULL), write_buf_(NULL), current_write_buf_(NULL), @@ -668,7 +664,7 @@ int SocketStream::DoTcpConnect(int result) { net_log_.net_log(), net_log_.source())); metrics_->OnStartConnection(); - return socket_->Connect(&io_callback_old_); + return socket_->Connect(io_callback_); } int SocketStream::DoTcpConnectComplete(int result) { @@ -761,7 +757,7 @@ int SocketStream::DoWriteTunnelHeaders() { int buf_len = static_cast<int>(tunnel_request_headers_->headers_.size() - tunnel_request_headers_bytes_sent_); DCHECK_GT(buf_len, 0); - return socket_->Write(tunnel_request_headers_, buf_len, &io_callback_old_); + return socket_->Write(tunnel_request_headers_, buf_len, io_callback_); } int SocketStream::DoWriteTunnelHeadersComplete(int result) { @@ -798,7 +794,7 @@ int SocketStream::DoReadTunnelHeaders() { tunnel_response_headers_->SetDataOffset(tunnel_response_headers_len_); CHECK(tunnel_response_headers_->data()); - return socket_->Read(tunnel_response_headers_, buf_len, &io_callback_old_); + return socket_->Read(tunnel_response_headers_, buf_len, io_callback_); } int SocketStream::DoReadTunnelHeadersComplete(int result) { @@ -895,7 +891,7 @@ int SocketStream::DoSOCKSConnect() { s = new SOCKSClientSocket(s, req_info, host_resolver_); socket_.reset(s); metrics_->OnCountConnectionType(SocketStreamMetrics::SOCKS_CONNECTION); - return socket_->Connect(&io_callback_old_); + return socket_->Connect(io_callback_); } int SocketStream::DoSOCKSConnectComplete(int result) { @@ -926,7 +922,7 @@ int SocketStream::DoSecureProxyConnect() { ssl_context)); next_state_ = STATE_SECURE_PROXY_CONNECT_COMPLETE; metrics_->OnCountConnectionType(SocketStreamMetrics::SECURE_PROXY_CONNECTION); - return socket_->Connect(&io_callback_old_); + return socket_->Connect(io_callback_); } int SocketStream::DoSecureProxyConnectComplete(int result) { @@ -956,7 +952,7 @@ int SocketStream::DoSSLConnect() { ssl_context)); next_state_ = STATE_SSL_CONNECT_COMPLETE; metrics_->OnCountConnectionType(SocketStreamMetrics::SSL_CONNECTION); - return socket_->Connect(&io_callback_old_); + return socket_->Connect(io_callback_); } int SocketStream::DoSSLConnectComplete(int result) { @@ -1001,7 +997,9 @@ int SocketStream::DoReadWrite(int result) { if (!read_buf_) { // No read pending and server didn't close the socket. read_buf_ = new IOBuffer(kReadBufferSize); - result = socket_->Read(read_buf_, kReadBufferSize, &read_callback_old_); + result = socket_->Read(read_buf_, kReadBufferSize, + base::Bind(&SocketStream::OnReadCompleted, + base::Unretained(this))); if (result > 0) { return DidReceiveData(result); } else if (result == 0) { @@ -1029,7 +1027,8 @@ int SocketStream::DoReadWrite(int result) { current_write_buf_->SetOffset(write_buf_offset_); result = socket_->Write(current_write_buf_, current_write_buf_->BytesRemaining(), - &write_callback_old_); + base::Bind(&SocketStream::OnWriteCompleted, + base::Unretained(this))); if (result > 0) { return DidSendData(result); } diff --git a/net/socket_stream/socket_stream.h b/net/socket_stream/socket_stream.h index 501c44b..663091a 100644 --- a/net/socket_stream/socket_stream.h +++ b/net/socket_stream/socket_stream.h @@ -338,8 +338,6 @@ class NET_EXPORT SocketStream const CompletionCallback io_callback_; OldCompletionCallbackImpl<SocketStream> io_callback_old_; - OldCompletionCallbackImpl<SocketStream> read_callback_old_; - OldCompletionCallbackImpl<SocketStream> write_callback_old_; scoped_refptr<IOBuffer> read_buf_; int read_buf_size_; diff --git a/net/spdy/spdy_proxy_client_socket.cc b/net/spdy/spdy_proxy_client_socket.cc index 7b2c83b..77ea05b 100644 --- a/net/spdy/spdy_proxy_client_socket.cc +++ b/net/spdy/spdy_proxy_client_socket.cc @@ -33,8 +33,6 @@ SpdyProxyClientSocket::SpdyProxyClientSocket( io_callback_(this, &SpdyProxyClientSocket::OnIOComplete)), next_state_(STATE_DISCONNECTED), spdy_stream_(spdy_stream), - old_read_callback_(NULL), - write_callback_(NULL), endpoint_(endpoint), auth_( new HttpAuthController(HttpAuth::AUTH_PROXY, @@ -91,21 +89,8 @@ HttpStream* SpdyProxyClientSocket::CreateConnectResponseStream() { // by creating a new stream for the subsequent request. // TODO(rch): create a more appropriate error code to disambiguate // the HTTPS Proxy tunnel failure from an HTTP Proxy tunnel failure. -int SpdyProxyClientSocket::Connect(OldCompletionCallback* callback) { - DCHECK(!old_read_callback_ && read_callback_.is_null()); - if (next_state_ == STATE_OPEN) - return OK; - - DCHECK_EQ(STATE_DISCONNECTED, next_state_); - next_state_ = STATE_GENERATE_AUTH_TOKEN; - - int rv = DoLoop(OK); - if (rv == ERR_IO_PENDING) - old_read_callback_ = callback; - return rv; -} int SpdyProxyClientSocket::Connect(const CompletionCallback& callback) { - DCHECK(!old_read_callback_ && read_callback_.is_null()); + DCHECK(read_callback_.is_null()); if (next_state_ == STATE_OPEN) return OK; @@ -121,12 +106,11 @@ int SpdyProxyClientSocket::Connect(const CompletionCallback& callback) { void SpdyProxyClientSocket::Disconnect() { read_buffer_.clear(); user_buffer_ = NULL; - old_read_callback_ = NULL; read_callback_.Reset(); write_buffer_len_ = 0; write_bytes_outstanding_ = 0; - write_callback_ = NULL; + write_callback_.Reset(); next_state_ = STATE_DISCONNECTED; @@ -173,32 +157,8 @@ base::TimeDelta SpdyProxyClientSocket::GetConnectTimeMicros() const { } int SpdyProxyClientSocket::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(!old_read_callback_ && read_callback_.is_null()); - DCHECK(!user_buffer_); - - if (next_state_ == STATE_DISCONNECTED) - return ERR_SOCKET_NOT_CONNECTED; - - if (next_state_ == STATE_CLOSED && read_buffer_.empty()) { - return 0; - } - - DCHECK(next_state_ == STATE_OPEN || next_state_ == STATE_CLOSED); - DCHECK(buf); - user_buffer_ = new DrainableIOBuffer(buf, buf_len); - int result = PopulateUserReadBuffer(); - if (result == 0) { - DCHECK(callback); - old_read_callback_ = callback; - return ERR_IO_PENDING; - } - user_buffer_ = NULL; - return result; -} -int SpdyProxyClientSocket::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { - DCHECK(!old_read_callback_ && read_callback_.is_null()); + DCHECK(read_callback_.is_null()); DCHECK(!user_buffer_); if (next_state_ == STATE_DISCONNECTED) @@ -243,8 +203,8 @@ int SpdyProxyClientSocket::PopulateUserReadBuffer() { } int SpdyProxyClientSocket::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(!write_callback_); + const CompletionCallback& callback) { + DCHECK(write_callback_.is_null()); if (next_state_ != STATE_OPEN) return ERR_SOCKET_NOT_CONNECTED; @@ -309,15 +269,9 @@ void SpdyProxyClientSocket::OnIOComplete(int result) { DCHECK_NE(STATE_DISCONNECTED, next_state_); int rv = DoLoop(result); if (rv != ERR_IO_PENDING) { - if (old_read_callback_) { - OldCompletionCallback* c = old_read_callback_; - old_read_callback_ = NULL; - c->Run(rv); - } else { - CompletionCallback c = read_callback_; - read_callback_.Reset(); - c.Run(rv); - } + CompletionCallback c = read_callback_; + read_callback_.Reset(); + c.Run(rv); } } @@ -516,13 +470,7 @@ void SpdyProxyClientSocket::OnDataReceived(const char* data, int length) { make_scoped_refptr(new DrainableIOBuffer(io_buffer, length))); } - if (old_read_callback_) { - int rv = PopulateUserReadBuffer(); - OldCompletionCallback* c = old_read_callback_; - old_read_callback_ = NULL; - user_buffer_ = NULL; - c->Run(rv); - } else if (!read_callback_.is_null()) { + if (!read_callback_.is_null()) { int rv = PopulateUserReadBuffer(); CompletionCallback c = read_callback_; read_callback_.Reset(); @@ -532,7 +480,7 @@ void SpdyProxyClientSocket::OnDataReceived(const char* data, int length) { } void SpdyProxyClientSocket::OnDataSent(int length) { - DCHECK(write_callback_); + DCHECK(!write_callback_.is_null()); write_bytes_outstanding_ -= length; @@ -542,9 +490,9 @@ void SpdyProxyClientSocket::OnDataSent(int length) { int rv = write_buffer_len_; write_buffer_len_ = 0; write_bytes_outstanding_ = 0; - OldCompletionCallback* c = write_callback_; - write_callback_ = NULL; - c->Run(rv); + CompletionCallback c = write_callback_; + write_callback_.Reset(); + c.Run(rv); } } @@ -561,31 +509,25 @@ void SpdyProxyClientSocket::OnClose(int status) { next_state_ = STATE_DISCONNECTED; base::WeakPtr<SpdyProxyClientSocket> weak_ptr = weak_factory_.GetWeakPtr(); - OldCompletionCallback* write_callback = write_callback_; - write_callback_ = NULL; + CompletionCallback write_callback = write_callback_; + write_callback_.Reset(); write_buffer_len_ = 0; write_bytes_outstanding_ = 0; // If we're in the middle of connecting, we need to make sure // we invoke the connect callback. if (connecting) { - DCHECK(old_read_callback_ || !read_callback_.is_null()); - if (old_read_callback_) { - OldCompletionCallback* read_callback = old_read_callback_; - old_read_callback_ = NULL; - read_callback->Run(status); - } else { - CompletionCallback read_callback = read_callback_; - read_callback_.Reset(); - read_callback.Run(status); - } - } else if (old_read_callback_ || !read_callback_.is_null()) { + DCHECK(!read_callback_.is_null()); + CompletionCallback read_callback = read_callback_; + read_callback_.Reset(); + read_callback.Run(status); + } else if (!read_callback_.is_null()) { // If we have a read_callback_, the we need to make sure we call it back. OnDataReceived(NULL, 0); } // This may have been deleted by read_callback_, so check first. - if (weak_ptr && write_callback) - write_callback->Run(ERR_CONNECTION_CLOSED); + if (weak_ptr && !write_callback.is_null()) + write_callback.Run(ERR_CONNECTION_CLOSED); } void SpdyProxyClientSocket::set_chunk_callback(ChunkCallback* /*callback*/) { diff --git a/net/spdy/spdy_proxy_client_socket.h b/net/spdy/spdy_proxy_client_socket.h index fb2d9a3..9c210b2 100644 --- a/net/spdy/spdy_proxy_client_socket.h +++ b/net/spdy/spdy_proxy_client_socket.h @@ -61,7 +61,6 @@ class NET_EXPORT_PRIVATE SpdyProxyClientSocket : public ProxyClientSocket, virtual const scoped_refptr<HttpAuthController>& auth_controller() OVERRIDE; // StreamSocket implementation. - virtual int Connect(OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -77,13 +76,10 @@ class NET_EXPORT_PRIVATE SpdyProxyClientSocket : public ProxyClientSocket, // Socket implementation. virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; virtual int GetPeerAddress(AddressList* address) const OVERRIDE; @@ -134,10 +130,9 @@ class NET_EXPORT_PRIVATE SpdyProxyClientSocket : public ProxyClientSocket, // Stores the callback to the layer above, called on completing Read() or // Connect(). - OldCompletionCallback* old_read_callback_; CompletionCallback read_callback_; // Stores the callback to the layer above, called on completing Write(). - OldCompletionCallback* write_callback_; + CompletionCallback write_callback_; // CONNECT request and response. HttpRequestInfo request_; diff --git a/net/spdy/spdy_proxy_client_socket_unittest.cc b/net/spdy/spdy_proxy_client_socket_unittest.cc index 4ec8a48..25be93a 100644 --- a/net/spdy/spdy_proxy_client_socket_unittest.cc +++ b/net/spdy/spdy_proxy_client_socket_unittest.cc @@ -100,8 +100,9 @@ class SpdyProxyClientSocketTest : public PlatformTest { } scoped_ptr<SpdyProxyClientSocket> sock_; - TestOldCompletionCallback read_callback_; - TestOldCompletionCallback write_callback_; + TestCompletionCallback read_callback_; + TestOldCompletionCallback read_callback_old_; + TestCompletionCallback write_callback_; scoped_refptr<DeterministicSocketData> data_; private: @@ -126,8 +127,6 @@ class SpdyProxyClientSocketTest : public PlatformTest { SpdyProxyClientSocketTest::SpdyProxyClientSocketTest() : sock_(NULL), - read_callback_(), - write_callback_(), data_(NULL), session_(NULL), read_buf_(NULL), @@ -212,13 +211,13 @@ scoped_refptr<IOBufferWithSize> SpdyProxyClientSocketTest::CreateBuffer( } void SpdyProxyClientSocketTest::AssertConnectSucceeds() { - ASSERT_EQ(ERR_IO_PENDING, sock_->Connect(&read_callback_)); + ASSERT_EQ(ERR_IO_PENDING, sock_->Connect(read_callback_.callback())); data_->Run(); ASSERT_EQ(OK, read_callback_.WaitForResult()); } void SpdyProxyClientSocketTest::AssertConnectFails(int result) { - ASSERT_EQ(ERR_IO_PENDING, sock_->Connect(&read_callback_)); + ASSERT_EQ(ERR_IO_PENDING, sock_->Connect(read_callback_.callback())); data_->Run(); ASSERT_EQ(result, read_callback_.WaitForResult()); } @@ -233,7 +232,7 @@ void SpdyProxyClientSocketTest::AssertConnectionEstablished() { void SpdyProxyClientSocketTest::AssertSyncReadEquals(const char* data, int len) { scoped_refptr<IOBuffer> buf(new IOBuffer(len)); - ASSERT_EQ(len, sock_->Read(buf, len, NULL)); + ASSERT_EQ(len, sock_->Read(buf, len, CompletionCallback())); ASSERT_EQ(std::string(data, len), std::string(buf->data(), len)); ASSERT_TRUE(sock_->IsConnected()); } @@ -243,7 +242,7 @@ void SpdyProxyClientSocketTest::AssertAsyncReadEquals(const char* data, data_->StopAfter(1); // Issue the read, which will be completed asynchronously scoped_refptr<IOBuffer> buf(new IOBuffer(len)); - ASSERT_EQ(ERR_IO_PENDING, sock_->Read(buf, len, &read_callback_)); + ASSERT_EQ(ERR_IO_PENDING, sock_->Read(buf, len, read_callback_.callback())); EXPECT_TRUE(sock_->IsConnected()); data_->Run(); @@ -258,7 +257,8 @@ void SpdyProxyClientSocketTest::AssertReadStarts(const char* data, int len) { data_->StopAfter(1); // Issue the read, which will be completed asynchronously read_buf_ = new IOBuffer(len); - ASSERT_EQ(ERR_IO_PENDING, sock_->Read(read_buf_, len, &read_callback_)); + ASSERT_EQ(ERR_IO_PENDING, + sock_->Read(read_buf_, len, read_callback_.callback())); EXPECT_TRUE(sock_->IsConnected()); } @@ -280,7 +280,7 @@ void SpdyProxyClientSocketTest::AssertAsyncWriteSucceeds(const char* data, void SpdyProxyClientSocketTest::AssertWriteReturns(const char* data, int len, int rv) { scoped_refptr<IOBufferWithSize> buf(CreateBuffer(data, len)); - EXPECT_EQ(rv, sock_->Write(buf, buf->size(), &write_callback_)); + EXPECT_EQ(rv, sock_->Write(buf, buf->size(), write_callback_.callback())); } void SpdyProxyClientSocketTest::AssertWriteLength(int len) { @@ -291,7 +291,8 @@ void SpdyProxyClientSocketTest::AssertAsyncWriteWithReadsSucceeds( const char* data, int len, int num_reads) { scoped_refptr<IOBufferWithSize> buf(CreateBuffer(data, len)); - EXPECT_EQ(ERR_IO_PENDING, sock_->Write(buf, buf->size(), &write_callback_)); + EXPECT_EQ(ERR_IO_PENDING, sock_->Write(buf, buf->size(), + write_callback_.callback())); for (int i = 0; i < num_reads; i++) { Run(1); @@ -541,7 +542,7 @@ TEST_F(SpdyProxyClientSocketTest, ConnectWithAuthRestart) { AddAuthToCache(); - ASSERT_EQ(OK, sock_->RestartWithAuth(&read_callback_)); + ASSERT_EQ(OK, sock_->RestartWithAuth(&read_callback_old_)); // A SpdyProxyClientSocket sits on a single SPDY stream which can // only be used for a single request/response. ASSERT_FALSE(sock_->IsConnectedAndIdle()); @@ -675,7 +676,8 @@ TEST_F(SpdyProxyClientSocketTest, WriteSplitsLargeDataIntoMultipleFrames) { scoped_refptr<IOBufferWithSize> buf(CreateBuffer(big_data.data(), big_data.length())); - EXPECT_EQ(ERR_IO_PENDING, sock_->Write(buf, buf->size(), &write_callback_)); + EXPECT_EQ(ERR_IO_PENDING, sock_->Write(buf, buf->size(), + write_callback_.callback())); data_->RunFor(3); EXPECT_EQ(buf->size(), write_callback_.WaitForResult()); @@ -875,7 +877,7 @@ TEST_F(SpdyProxyClientSocketTest, MultipleReadsFromSameLargeFrame) { // Now attempt to do a read of more data than remains buffered scoped_refptr<IOBuffer> buf(new IOBuffer(kLen33)); - ASSERT_EQ(kLen3, sock_->Read(buf, kLen33, &read_callback_)); + ASSERT_EQ(kLen3, sock_->Read(buf, kLen33, read_callback_.callback())); ASSERT_EQ(std::string(kMsg3, kLen3), std::string(buf->data(), kLen3)); ASSERT_TRUE(sock_->IsConnected()); } @@ -928,10 +930,11 @@ TEST_F(SpdyProxyClientSocketTest, ReadErrorResponseBody) { Run(2); // SpdySession consumes the next two reads and sends then to // sock_ to be buffered. - EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, sock_->Read(NULL, 1, NULL)); + EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, + sock_->Read(NULL, 1, CompletionCallback())); scoped_refptr<IOBuffer> buf(new IOBuffer(kLen1 + kLen2)); scoped_ptr<HttpStream> stream(sock_->CreateConnectResponseStream()); - stream->ReadResponseBody(buf, kLen1 + kLen2, &read_callback_); + stream->ReadResponseBody(buf, kLen1 + kLen2, &read_callback_old_); } // ----------- Reads and Writes @@ -1030,9 +1033,9 @@ TEST_F(SpdyProxyClientSocketTest, ReadOnClosedSocketReturnsZero) { Run(1); ASSERT_FALSE(sock_->IsConnected()); - ASSERT_EQ(0, sock_->Read(NULL, 1, NULL)); - ASSERT_EQ(0, sock_->Read(NULL, 1, NULL)); - ASSERT_EQ(0, sock_->Read(NULL, 1, NULL)); + ASSERT_EQ(0, sock_->Read(NULL, 1, CompletionCallback())); + ASSERT_EQ(0, sock_->Read(NULL, 1, CompletionCallback())); + ASSERT_EQ(0, sock_->Read(NULL, 1, CompletionCallback())); ASSERT_FALSE(sock_->IsConnectedAndIdle()); } @@ -1079,7 +1082,8 @@ TEST_F(SpdyProxyClientSocketTest, ReadOnDisconnectSocketReturnsNotConnected) { sock_->Disconnect(); - ASSERT_EQ(ERR_SOCKET_NOT_CONNECTED, sock_->Read(NULL, 1, NULL)); + ASSERT_EQ(ERR_SOCKET_NOT_CONNECTED, + sock_->Read(NULL, 1, CompletionCallback())); } // Reading buffered data from an already closed socket should return @@ -1106,13 +1110,14 @@ TEST_F(SpdyProxyClientSocketTest, ReadOnClosedSocketReturnsBufferedData) { ASSERT_FALSE(sock_->IsConnected()); scoped_refptr<IOBuffer> buf(new IOBuffer(kLen1)); - ASSERT_EQ(kLen1, sock_->Read(buf, kLen1, NULL)); + ASSERT_EQ(kLen1, sock_->Read(buf, kLen1, CompletionCallback())); ASSERT_EQ(std::string(kMsg1, kLen1), std::string(buf->data(), kLen1)); - ASSERT_EQ(0, sock_->Read(NULL, 1, NULL)); - ASSERT_EQ(0, sock_->Read(NULL, 1, NULL)); + ASSERT_EQ(0, sock_->Read(NULL, 1, CompletionCallback())); + ASSERT_EQ(0, sock_->Read(NULL, 1, CompletionCallback())); sock_->Disconnect(); - ASSERT_EQ(ERR_SOCKET_NOT_CONNECTED, sock_->Read(NULL, 1, NULL)); + ASSERT_EQ(ERR_SOCKET_NOT_CONNECTED, + sock_->Read(NULL, 1, CompletionCallback())); } // Calling Write() on a closed socket is an error @@ -1135,7 +1140,8 @@ TEST_F(SpdyProxyClientSocketTest, WriteOnClosedStream) { Run(1); // Read EOF which will close the stream scoped_refptr<IOBufferWithSize> buf(CreateBuffer(kMsg1, kLen1)); - EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, sock_->Write(buf, buf->size(), NULL)); + EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, + sock_->Write(buf, buf->size(), CompletionCallback())); } // Calling Write() on a disconnected socket is an error @@ -1159,7 +1165,8 @@ TEST_F(SpdyProxyClientSocketTest, WriteOnDisconnectedSocket) { sock_->Disconnect(); scoped_refptr<IOBufferWithSize> buf(CreateBuffer(kMsg1, kLen1)); - EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, sock_->Write(buf, buf->size(), NULL)); + EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, + sock_->Write(buf, buf->size(), CompletionCallback())); } // If the socket is closed with a pending Write(), the callback @@ -1184,7 +1191,8 @@ TEST_F(SpdyProxyClientSocketTest, WritePendingOnClose) { EXPECT_TRUE(sock_->IsConnected()); scoped_refptr<IOBufferWithSize> buf(CreateBuffer(kMsg1, kLen1)); - EXPECT_EQ(ERR_IO_PENDING, sock_->Write(buf, buf->size(), &write_callback_)); + EXPECT_EQ(ERR_IO_PENDING, + sock_->Write(buf, buf->size(), write_callback_.callback())); Run(1); @@ -1213,7 +1221,8 @@ TEST_F(SpdyProxyClientSocketTest, DisconnectWithWritePending) { EXPECT_TRUE(sock_->IsConnected()); scoped_refptr<IOBufferWithSize> buf(CreateBuffer(kMsg1, kLen1)); - EXPECT_EQ(ERR_IO_PENDING, sock_->Write(buf, buf->size(), &write_callback_)); + EXPECT_EQ(ERR_IO_PENDING, + sock_->Write(buf, buf->size(), write_callback_.callback())); sock_->Disconnect(); @@ -1242,7 +1251,8 @@ TEST_F(SpdyProxyClientSocketTest, DisconnectWithReadPending) { EXPECT_TRUE(sock_->IsConnected()); scoped_refptr<IOBuffer> buf(new IOBuffer(kLen1)); - ASSERT_EQ(ERR_IO_PENDING, sock_->Read(buf, kLen1, &read_callback_)); + ASSERT_EQ(ERR_IO_PENDING, + sock_->Read(buf, kLen1, read_callback_.callback())); sock_->Disconnect(); @@ -1273,11 +1283,13 @@ TEST_F(SpdyProxyClientSocketTest, RstWithReadAndWritePending) { EXPECT_TRUE(sock_->IsConnected()); scoped_refptr<IOBuffer> read_buf(new IOBuffer(kLen1)); - ASSERT_EQ(ERR_IO_PENDING, sock_->Read(read_buf, kLen1, &read_callback_)); + ASSERT_EQ(ERR_IO_PENDING, + sock_->Read(read_buf, kLen1, read_callback_.callback())); scoped_refptr<IOBufferWithSize> write_buf(CreateBuffer(kMsg1, kLen1)); - EXPECT_EQ(ERR_IO_PENDING, sock_->Write(write_buf, write_buf->size(), - &write_callback_)); + EXPECT_EQ(ERR_IO_PENDING, + sock_->Write(write_buf, write_buf->size(), + write_callback_.callback())); Run(2); @@ -1332,11 +1344,14 @@ TEST_F(SpdyProxyClientSocketTest, RstWithReadAndWritePendingDelete) { DeleteSockCallback read_callback(&sock_); scoped_refptr<IOBuffer> read_buf(new IOBuffer(kLen1)); - ASSERT_EQ(ERR_IO_PENDING, sock_->Read(read_buf, kLen1, &read_callback)); + ASSERT_EQ(ERR_IO_PENDING, + sock_->Read(read_buf, kLen1, + base::Bind(&DeleteSockCallback::Run<int>, + base::Unretained(&read_callback)))); scoped_refptr<IOBufferWithSize> write_buf(CreateBuffer(kMsg1, kLen1)); EXPECT_EQ(ERR_IO_PENDING, sock_->Write(write_buf, write_buf->size(), - &write_callback_)); + write_callback_.callback())); Run(2); diff --git a/net/spdy/spdy_session.cc b/net/spdy/spdy_session.cc index 686f4ae..17c18eb 100644 --- a/net/spdy/spdy_session.cc +++ b/net/spdy/spdy_session.cc @@ -252,11 +252,7 @@ SpdySession::SpdySession(const HostPortProxyPair& host_port_proxy_pair, HttpServerProperties* http_server_properties, bool verify_domain_authentication, NetLog* net_log) - : ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &SpdySession::OnReadComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_callback_(this, &SpdySession::OnWriteComplete)), - ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), + : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), host_port_proxy_pair_(host_port_proxy_pair), spdy_session_pool_(spdy_session_pool), http_server_properties_(http_server_properties), @@ -428,8 +424,8 @@ void SpdySession::ProcessPendingCreateStreams() { CallbackResultPair(pending_create.callback, error); MessageLoop::current()->PostTask( FROM_HERE, - method_factory_.NewRunnableMethod( - &SpdySession::InvokeUserStreamCreationCallback, stream)); + base::Bind(&SpdySession::InvokeUserStreamCreationCallback, + weak_factory_.GetWeakPtr(), stream)); break; } } @@ -757,9 +753,10 @@ net::Error SpdySession::ReadSocket() { CHECK(connection_.get()); CHECK(connection_->socket()); - int bytes_read = connection_->socket()->Read(read_buffer_.get(), - kReadBufferSize, - &read_callback_); + int bytes_read = connection_->socket()->Read( + read_buffer_.get(), + kReadBufferSize, + base::Bind(&SpdySession::OnReadComplete, base::Unretained(this))); switch (bytes_read) { case 0: // Socket is closed! @@ -776,8 +773,8 @@ net::Error SpdySession::ReadSocket() { read_pending_ = true; MessageLoop::current()->PostTask( FROM_HERE, - method_factory_.NewRunnableMethod( - &SpdySession::OnReadComplete, bytes_read)); + base::Bind(&SpdySession::OnReadComplete, + weak_factory_.GetWeakPtr(), bytes_read)); break; } return OK; @@ -793,7 +790,7 @@ void SpdySession::WriteSocketLater() { delayed_write_pending_ = true; MessageLoop::current()->PostTask( FROM_HERE, - method_factory_.NewRunnableMethod(&SpdySession::WriteSocket)); + base::Bind(&SpdySession::WriteSocket, weak_factory_.GetWeakPtr())); } void SpdySession::WriteSocket() { @@ -850,8 +847,10 @@ void SpdySession::WriteSocket() { } write_pending_ = true; - int rv = connection_->socket()->Write(in_flight_write_.buffer(), - in_flight_write_.buffer()->BytesRemaining(), &write_callback_); + int rv = connection_->socket()->Write( + in_flight_write_.buffer(), + in_flight_write_.buffer()->BytesRemaining(), + base::Bind(&SpdySession::OnWriteComplete, base::Unretained(this))); if (rv == net::ERR_IO_PENDING) break; @@ -1596,7 +1595,7 @@ void SpdySession::PlanToSendTrailingPing() { trailing_ping_pending_ = true; MessageLoop::current()->PostDelayedTask( FROM_HERE, - method_factory_.NewRunnableMethod(&SpdySession::SendTrailingPing), + base::Bind(&SpdySession::SendTrailingPing, weak_factory_.GetWeakPtr()), trailing_ping_delay_time_ms_); } @@ -1632,8 +1631,8 @@ void SpdySession::PlanToCheckPingStatus() { check_ping_status_pending_ = true; MessageLoop::current()->PostDelayedTask( FROM_HERE, - method_factory_.NewRunnableMethod( - &SpdySession::CheckPingStatus, base::TimeTicks::Now()), + base::Bind(&SpdySession::CheckPingStatus, weak_factory_.GetWeakPtr(), + base::TimeTicks::Now()), hung_interval_ms_); } @@ -1664,7 +1663,8 @@ void SpdySession::CheckPingStatus(base::TimeTicks last_check_time) { // Check the status of connection after a delay. MessageLoop::current()->PostDelayedTask( FROM_HERE, - method_factory_.NewRunnableMethod(&SpdySession::CheckPingStatus, now), + base::Bind(&SpdySession::CheckPingStatus, weak_factory_.GetWeakPtr(), + now), delay.InMilliseconds()); } diff --git a/net/spdy/spdy_session.h b/net/spdy/spdy_session.h index ce02d5e..a05bd3c 100644 --- a/net/spdy/spdy_session.h +++ b/net/spdy/spdy_session.h @@ -16,7 +16,6 @@ #include "base/memory/linked_ptr.h" #include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" -#include "base/task.h" #include "net/base/io_buffer.h" #include "net/base/load_states.h" #include "net/base/net_errors.h" @@ -262,7 +261,7 @@ class NET_EXPORT SpdySession : public base::RefCounted<SpdySession>, typedef std::priority_queue<SpdyIOBuffer> OutputQueue; struct CallbackResultPair { - CallbackResultPair() : callback(NULL), result(OK) {} + CallbackResultPair() : result(OK) {} CallbackResultPair(OldCompletionCallback* callback_in, int result_in) : callback(callback_in), result(result_in) {} @@ -431,15 +430,11 @@ class NET_EXPORT SpdySession : public base::RefCounted<SpdySession>, bool check_ping_status_pending() const { return check_ping_status_pending_; } - // Callbacks for the Spdy session. - OldCompletionCallbackImpl<SpdySession> read_callback_; - OldCompletionCallbackImpl<SpdySession> write_callback_; - // Used for posting asynchronous IO tasks. We use this even though // SpdySession is refcounted because we don't need to keep the SpdySession // alive if the last reference is within a RunnableMethod. Just revoke the // method. - ScopedRunnableMethodFactory<SpdySession> method_factory_; + base::WeakPtrFactory<SpdySession> weak_factory_; // Map of the SpdyStreams for which we have a pending Task to invoke a // callback. This is necessary since, before we invoke said callback, it's diff --git a/net/udp/datagram_server_socket.h b/net/udp/datagram_server_socket.h index cd20191..c83481c 100644 --- a/net/udp/datagram_server_socket.h +++ b/net/udp/datagram_server_socket.h @@ -39,7 +39,7 @@ class NET_EXPORT DatagramServerSocket : public DatagramSocket { virtual int RecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address, - OldCompletionCallback* callback) = 0; + const CompletionCallback& callback) = 0; // Send to a socket with a particular destination. // |buf| is the buffer to send @@ -53,7 +53,7 @@ class NET_EXPORT DatagramServerSocket : public DatagramSocket { virtual int SendTo(IOBuffer* buf, int buf_len, const IPEndPoint& address, - OldCompletionCallback* callback) = 0; + const CompletionCallback& callback) = 0; // Set the receive buffer size (in bytes) for the socket. virtual bool SetReceiveBufferSize(int32 size) = 0; diff --git a/net/udp/udp_client_socket.cc b/net/udp/udp_client_socket.cc index 4bb3885..bbc32d4 100644 --- a/net/udp/udp_client_socket.cc +++ b/net/udp/udp_client_socket.cc @@ -24,18 +24,13 @@ int UDPClientSocket::Connect(const IPEndPoint& address) { int UDPClientSocket::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - return socket_.Read(buf, buf_len, callback); -} -int UDPClientSocket::Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) { return socket_.Read(buf, buf_len, callback); } int UDPClientSocket::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return socket_.Write(buf, buf_len, callback); } diff --git a/net/udp/udp_client_socket.h b/net/udp/udp_client_socket.h index b25df63..e28e6a4 100644 --- a/net/udp/udp_client_socket.h +++ b/net/udp/udp_client_socket.h @@ -27,11 +27,9 @@ class NET_EXPORT_PRIVATE UDPClientSocket : public DatagramClientSocket { // DatagramClientSocket implementation. virtual int Connect(const IPEndPoint& address) OVERRIDE; virtual int Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; - virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual void Close() OVERRIDE; virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; diff --git a/net/udp/udp_server_socket.cc b/net/udp/udp_server_socket.cc index 7958463..b4395d2 100644 --- a/net/udp/udp_server_socket.cc +++ b/net/udp/udp_server_socket.cc @@ -26,14 +26,14 @@ int UDPServerSocket::Listen(const IPEndPoint& address) { int UDPServerSocket::RecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return socket_.RecvFrom(buf, buf_len, address, callback); } int UDPServerSocket::SendTo(IOBuffer* buf, int buf_len, const IPEndPoint& address, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return socket_.SendTo(buf, buf_len, address, callback); } diff --git a/net/udp/udp_server_socket.h b/net/udp/udp_server_socket.h index bb6ac5c..89acb4f 100644 --- a/net/udp/udp_server_socket.h +++ b/net/udp/udp_server_socket.h @@ -27,11 +27,11 @@ class NET_EXPORT UDPServerSocket : public DatagramServerSocket { virtual int RecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual int SendTo(IOBuffer* buf, int buf_len, const IPEndPoint& address, - OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; virtual void Close() OVERRIDE; diff --git a/net/udp/udp_socket_libevent.cc b/net/udp/udp_socket_libevent.cc index a669c37..f8ef7e3 100644 --- a/net/udp/udp_socket_libevent.cc +++ b/net/udp/udp_socket_libevent.cc @@ -47,8 +47,6 @@ UDPSocketLibevent::UDPSocketLibevent( read_buf_len_(0), recv_from_address_(NULL), write_buf_len_(0), - old_read_callback_(NULL), - write_callback_(NULL), net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { scoped_refptr<NetLog::EventParameters> params; if (source.is_valid()) @@ -72,12 +70,11 @@ void UDPSocketLibevent::Close() { // Zero out any pending read/write callback state. read_buf_ = NULL; read_buf_len_ = 0; - old_read_callback_ = NULL; read_callback_.Reset(); recv_from_address_ = NULL; write_buf_ = NULL; write_buf_len_ = 0; - write_callback_ = NULL; + write_callback_.Reset(); send_to_address_.reset(); bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); @@ -137,11 +134,6 @@ int UDPSocketLibevent::GetLocalAddress(IPEndPoint* address) const { int UDPSocketLibevent::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - return RecvFrom(buf, buf_len, NULL, callback); -} -int UDPSocketLibevent::Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) { return RecvFrom(buf, buf_len, NULL, callback); } @@ -149,40 +141,10 @@ int UDPSocketLibevent::Read(IOBuffer* buf, int UDPSocketLibevent::RecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address, - OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - DCHECK_NE(kInvalidSocket, socket_); - DCHECK(!old_read_callback_ && read_callback_.is_null()); - DCHECK(!recv_from_address_); - DCHECK(callback); // Synchronous operation not supported - DCHECK_GT(buf_len, 0); - - int nread = InternalRecvFrom(buf, buf_len, address); - if (nread != ERR_IO_PENDING) - return nread; - - if (!MessageLoopForIO::current()->WatchFileDescriptor( - socket_, true, MessageLoopForIO::WATCH_READ, - &read_socket_watcher_, &read_watcher_)) { - PLOG(ERROR) << "WatchFileDescriptor failed on read"; - int result = MapSystemError(errno); - LogRead(result, NULL, 0, NULL); - return result; - } - - read_buf_ = buf; - read_buf_len_ = buf_len; - recv_from_address_ = address; - old_read_callback_ = callback; - return ERR_IO_PENDING; -} -int UDPSocketLibevent::RecvFrom(IOBuffer* buf, - int buf_len, - IPEndPoint* address, const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); DCHECK_NE(kInvalidSocket, socket_); - DCHECK(!old_read_callback_ && read_callback_.is_null()); + DCHECK(read_callback_.is_null()); DCHECK(!recv_from_address_); DCHECK(!callback.is_null()); // Synchronous operation not supported DCHECK_GT(buf_len, 0); @@ -209,25 +171,25 @@ int UDPSocketLibevent::RecvFrom(IOBuffer* buf, int UDPSocketLibevent::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return SendToOrWrite(buf, buf_len, NULL, callback); } int UDPSocketLibevent::SendTo(IOBuffer* buf, int buf_len, const IPEndPoint& address, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return SendToOrWrite(buf, buf_len, &address, callback); } int UDPSocketLibevent::SendToOrWrite(IOBuffer* buf, int buf_len, const IPEndPoint* address, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); DCHECK_NE(kInvalidSocket, socket_); - DCHECK(!write_callback_); - DCHECK(callback); // Synchronous operation not supported + DCHECK(write_callback_.is_null()); + DCHECK(!callback.is_null()); // Synchronous operation not supported DCHECK_GT(buf_len, 0); int result = InternalSendTo(buf, buf_len, address); @@ -323,28 +285,22 @@ bool UDPSocketLibevent::SetSendBufferSize(int32 size) { void UDPSocketLibevent::DoReadCallback(int rv) { DCHECK_NE(rv, ERR_IO_PENDING); - DCHECK(old_read_callback_ || !read_callback_.is_null()); + DCHECK(!read_callback_.is_null()); - // Since Run may result in Read being called, clear read_callback_ up front. - if (old_read_callback_) { - OldCompletionCallback* c = old_read_callback_; - old_read_callback_ = NULL; - c->Run(rv); - } else { - CompletionCallback c = read_callback_; - read_callback_.Reset(); - c.Run(rv); - } + // since Run may result in Read being called, clear read_callback_ up front. + CompletionCallback c = read_callback_; + read_callback_.Reset(); + c.Run(rv); } void UDPSocketLibevent::DoWriteCallback(int rv) { DCHECK_NE(rv, ERR_IO_PENDING); - DCHECK(write_callback_); + DCHECK(!write_callback_.is_null()); // since Run may result in Write being called, clear write_callback_ up front. - OldCompletionCallback* c = write_callback_; - write_callback_ = NULL; - c->Run(rv); + CompletionCallback c = write_callback_; + write_callback_.Reset(); + c.Run(rv); } void UDPSocketLibevent::DidCompleteRead() { diff --git a/net/udp/udp_socket_libevent.h b/net/udp/udp_socket_libevent.h index 6cef1c0..4317740 100644 --- a/net/udp/udp_socket_libevent.h +++ b/net/udp/udp_socket_libevent.h @@ -54,13 +54,12 @@ class UDPSocketLibevent : public base::NonThreadSafe { // Read from the socket. // Only usable from the client-side of a UDP socket, after the socket // has been connected. - int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); // Write to the socket. // Only usable from the client-side of a UDP socket, after the socket // has been connected. - int Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); + int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); // Read from a socket and receive sender address information. // |buf| is the buffer to read data into. @@ -78,10 +77,6 @@ class UDPSocketLibevent : public base::NonThreadSafe { int RecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address, - OldCompletionCallback* callback); - int RecvFrom(IOBuffer* buf, - int buf_len, - IPEndPoint* address, const CompletionCallback& callback); // Send to a socket with a particular destination. @@ -96,7 +91,7 @@ class UDPSocketLibevent : public base::NonThreadSafe { int SendTo(IOBuffer* buf, int buf_len, const IPEndPoint& address, - OldCompletionCallback* callback); + const CompletionCallback& callback); // Set the receive buffer size (in bytes) for the socket. bool SetReceiveBufferSize(int32 size); @@ -119,7 +114,7 @@ class UDPSocketLibevent : public base::NonThreadSafe { // MessageLoopForIO::Watcher methods virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { - if (socket_->old_read_callback_ || !socket_->read_callback_.is_null()) + if (!socket_->read_callback_.is_null()) socket_->DidCompleteRead(); } @@ -140,7 +135,7 @@ class UDPSocketLibevent : public base::NonThreadSafe { virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {} virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { - if (socket_->write_callback_) + if (!socket_->write_callback_.is_null()) socket_->DidCompleteWrite(); } @@ -172,7 +167,7 @@ class UDPSocketLibevent : public base::NonThreadSafe { int SendToOrWrite(IOBuffer* buf, int buf_len, const IPEndPoint* address, - OldCompletionCallback* callback); + const CompletionCallback& callback); int InternalConnect(const IPEndPoint& address); int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); @@ -214,11 +209,10 @@ class UDPSocketLibevent : public base::NonThreadSafe { scoped_ptr<IPEndPoint> send_to_address_; // External callback; called when read is complete. - OldCompletionCallback* old_read_callback_; CompletionCallback read_callback_; // External callback; called when write is complete. - OldCompletionCallback* write_callback_; + CompletionCallback write_callback_; BoundNetLog net_log_; diff --git a/net/udp/udp_socket_unittest.cc b/net/udp/udp_socket_unittest.cc index 066b47c..5165d3d 100644 --- a/net/udp/udp_socket_unittest.cc +++ b/net/udp/udp_socket_unittest.cc @@ -32,10 +32,10 @@ class UDPSocketTest : public PlatformTest { // Blocks until data is read from the socket. std::string RecvFromSocket(UDPServerSocket* socket) { - TestOldCompletionCallback callback; + TestCompletionCallback callback; int rv = socket->RecvFrom(buffer_, kMaxRead, &recv_from_address_, - &callback); + callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); if (rv < 0) @@ -55,7 +55,7 @@ class UDPSocketTest : public PlatformTest { int SendToSocket(UDPServerSocket* socket, std::string msg, const IPEndPoint& address) { - TestOldCompletionCallback callback; + TestCompletionCallback callback; int length = msg.length(); scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); @@ -65,7 +65,7 @@ class UDPSocketTest : public PlatformTest { int bytes_sent = 0; while (buffer->BytesRemaining()) { int rv = socket->SendTo(buffer, buffer->BytesRemaining(), - address, &callback); + address, callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); if (rv <= 0) @@ -77,9 +77,9 @@ class UDPSocketTest : public PlatformTest { } std::string ReadSocket(UDPClientSocket* socket) { - TestOldCompletionCallback callback; + TestCompletionCallback callback; - int rv = socket->Read(buffer_, kMaxRead, &callback); + int rv = socket->Read(buffer_, kMaxRead, callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); if (rv < 0) @@ -90,7 +90,7 @@ class UDPSocketTest : public PlatformTest { // Loop until |msg| has been written to the socket or until an // error occurs. int WriteSocket(UDPClientSocket* socket, std::string msg) { - TestOldCompletionCallback callback; + TestCompletionCallback callback; int length = msg.length(); scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); @@ -99,7 +99,8 @@ class UDPSocketTest : public PlatformTest { int bytes_sent = 0; while (buffer->BytesRemaining()) { - int rv = socket->Write(buffer, buffer->BytesRemaining(), &callback); + int rv = socket->Write(buffer, buffer->BytesRemaining(), + callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); if (rv <= 0) @@ -433,9 +434,9 @@ TEST_F(UDPSocketTest, CloseWithPendingRead) { int rv = server.Listen(bind_address); EXPECT_EQ(OK, rv); - TestOldCompletionCallback callback; + TestCompletionCallback callback; IPEndPoint from; - rv = server.RecvFrom(buffer_, kMaxRead, &from, &callback); + rv = server.RecvFrom(buffer_, kMaxRead, &from, callback.callback()); EXPECT_EQ(rv, ERR_IO_PENDING); server.Close(); diff --git a/net/udp/udp_socket_win.cc b/net/udp/udp_socket_win.cc index 88aa23b..06f5e3d 100644 --- a/net/udp/udp_socket_win.cc +++ b/net/udp/udp_socket_win.cc @@ -51,8 +51,6 @@ UDPSocketWin::UDPSocketWin(DatagramSocket::BindType bind_type, ALLOW_THIS_IN_INITIALIZER_LIST(read_delegate_(this)), ALLOW_THIS_IN_INITIALIZER_LIST(write_delegate_(this)), recv_from_address_(NULL), - old_read_callback_(NULL), - write_callback_(NULL), net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { EnsureWinsockInit(); scoped_refptr<NetLog::EventParameters> params; @@ -79,10 +77,9 @@ void UDPSocketWin::Close() { return; // Zero out any pending read/write callback state. - old_read_callback_ = NULL; read_callback_.Reset(); recv_from_address_ = NULL; - write_callback_ = NULL; + write_callback_.Reset(); read_watcher_.StopWatching(); write_watcher_.StopWatching(); @@ -137,11 +134,6 @@ int UDPSocketWin::GetLocalAddress(IPEndPoint* address) const { int UDPSocketWin::Read(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { - return RecvFrom(buf, buf_len, NULL, callback); -} -int UDPSocketWin::Read(IOBuffer* buf, - int buf_len, const CompletionCallback& callback) { return RecvFrom(buf, buf_len, NULL, callback); } @@ -149,30 +141,10 @@ int UDPSocketWin::Read(IOBuffer* buf, int UDPSocketWin::RecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address, - OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - DCHECK_NE(INVALID_SOCKET, socket_); - DCHECK(!old_read_callback_ && read_callback_.is_null()); - DCHECK(!recv_from_address_); - DCHECK(callback); // Synchronous operation not supported. - DCHECK_GT(buf_len, 0); - - int nread = InternalRecvFrom(buf, buf_len, address); - if (nread != ERR_IO_PENDING) - return nread; - - read_iobuffer_ = buf; - old_read_callback_ = callback; - recv_from_address_ = address; - return ERR_IO_PENDING; -} -int UDPSocketWin::RecvFrom(IOBuffer* buf, - int buf_len, - IPEndPoint* address, const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); DCHECK_NE(INVALID_SOCKET, socket_); - DCHECK(!old_read_callback_ && read_callback_.is_null()); + DCHECK(read_callback_.is_null()); DCHECK(!recv_from_address_); DCHECK(!callback.is_null()); // Synchronous operation not supported. DCHECK_GT(buf_len, 0); @@ -189,25 +161,25 @@ int UDPSocketWin::RecvFrom(IOBuffer* buf, int UDPSocketWin::Write(IOBuffer* buf, int buf_len, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return SendToOrWrite(buf, buf_len, NULL, callback); } int UDPSocketWin::SendTo(IOBuffer* buf, int buf_len, const IPEndPoint& address, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { return SendToOrWrite(buf, buf_len, &address, callback); } int UDPSocketWin::SendToOrWrite(IOBuffer* buf, int buf_len, const IPEndPoint* address, - OldCompletionCallback* callback) { + const CompletionCallback& callback) { DCHECK(CalledOnValidThread()); DCHECK_NE(INVALID_SOCKET, socket_); - DCHECK(!write_callback_); - DCHECK(callback); // Synchronous operation not supported. + DCHECK(write_callback_.is_null()); + DCHECK(!callback.is_null()); // Synchronous operation not supported. DCHECK_GT(buf_len, 0); DCHECK(!send_to_address_.get()); @@ -298,28 +270,22 @@ bool UDPSocketWin::SetSendBufferSize(int32 size) { void UDPSocketWin::DoReadCallback(int rv) { DCHECK_NE(rv, ERR_IO_PENDING); - DCHECK(old_read_callback_ || !read_callback_.is_null()); + DCHECK(!read_callback_.is_null()); // since Run may result in Read being called, clear read_callback_ up front. - if (old_read_callback_) { - OldCompletionCallback* c = old_read_callback_; - old_read_callback_ = NULL; - c->Run(rv); - } else { - CompletionCallback c = read_callback_; - read_callback_.Reset(); - c.Run(rv); - } + CompletionCallback c = read_callback_; + read_callback_.Reset(); + c.Run(rv); } void UDPSocketWin::DoWriteCallback(int rv) { DCHECK_NE(rv, ERR_IO_PENDING); - DCHECK(write_callback_); + DCHECK(!write_callback_.is_null()); // since Run may result in Write being called, clear write_callback_ up front. - OldCompletionCallback* c = write_callback_; - write_callback_ = NULL; - c->Run(rv); + CompletionCallback c = write_callback_; + write_callback_.Reset(); + c.Run(rv); } void UDPSocketWin::DidCompleteRead() { diff --git a/net/udp/udp_socket_win.h b/net/udp/udp_socket_win.h index 6aace29..d49f64d 100644 --- a/net/udp/udp_socket_win.h +++ b/net/udp/udp_socket_win.h @@ -55,13 +55,12 @@ class UDPSocketWin : public base::NonThreadSafe { // Read from the socket. // Only usable from the client-side of a UDP socket, after the socket // has been connected. - int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); // Write to the socket. // Only usable from the client-side of a UDP socket, after the socket // has been connected. - int Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); + int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); // Read from a socket and receive sender address information. // |buf| is the buffer to read data into. @@ -79,10 +78,6 @@ class UDPSocketWin : public base::NonThreadSafe { int RecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address, - OldCompletionCallback* callback); - int RecvFrom(IOBuffer* buf, - int buf_len, - IPEndPoint* address, const CompletionCallback& callback); // Send to a socket with a particular destination. @@ -97,7 +92,7 @@ class UDPSocketWin : public base::NonThreadSafe { int SendTo(IOBuffer* buf, int buf_len, const IPEndPoint& address, - OldCompletionCallback* callback); + const CompletionCallback& callback); // Set the receive buffer size (in bytes) for the socket. bool SetReceiveBufferSize(int32 size); @@ -155,7 +150,7 @@ class UDPSocketWin : public base::NonThreadSafe { int SendToOrWrite(IOBuffer* buf, int buf_len, const IPEndPoint* address, - OldCompletionCallback* callback); + const CompletionCallback& callback); int InternalConnect(const IPEndPoint& address); int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); @@ -208,11 +203,10 @@ class UDPSocketWin : public base::NonThreadSafe { scoped_refptr<IOBuffer> write_iobuffer_; // External callback; called when read is complete. - OldCompletionCallback* old_read_callback_; CompletionCallback read_callback_; // External callback; called when write is complete. - OldCompletionCallback* write_callback_; + CompletionCallback write_callback_; BoundNetLog net_log_; diff --git a/net/url_request/url_request_ftp_job.cc b/net/url_request/url_request_ftp_job.cc index 669a889..a07b94e 100644 --- a/net/url_request/url_request_ftp_job.cc +++ b/net/url_request/url_request_ftp_job.cc @@ -21,13 +21,9 @@ namespace net { URLRequestFtpJob::URLRequestFtpJob(URLRequest* request) : URLRequestJob(request), - ALLOW_THIS_IN_INITIALIZER_LIST( - start_callback_(this, &URLRequestFtpJob::OnStartCompleted)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &URLRequestFtpJob::OnReadCompleted)), read_in_progress_(false), context_(request->context()), - ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { + ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { } // static @@ -78,7 +74,10 @@ void URLRequestFtpJob::StartTransaction() { int rv; if (transaction_.get()) { rv = transaction_->Start( - &request_info_, &start_callback_, request_->net_log()); + &request_info_, + base::Bind(&URLRequestFtpJob::OnStartCompleted, + base::Unretained(this)), + request_->net_log()); if (rv == ERR_IO_PENDING) return; } else { @@ -88,8 +87,8 @@ void URLRequestFtpJob::StartTransaction() { // URLRequest delegate via the message loop. MessageLoop::current()->PostTask( FROM_HERE, - method_factory_.NewRunnableMethod( - &URLRequestFtpJob::OnStartCompleted, rv)); + base::Bind(&URLRequestFtpJob::OnStartCompleted, + weak_factory_.GetWeakPtr(), rv)); } void URLRequestFtpJob::OnStartCompleted(int result) { @@ -148,15 +147,17 @@ void URLRequestFtpJob::RestartTransactionWithAuth() { // be notifying our consumer asynchronously via OnStartCompleted. SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); - int rv = transaction_->RestartWithAuth(server_auth_->credentials, - &start_callback_); + int rv = transaction_->RestartWithAuth( + server_auth_->credentials, + base::Bind(&URLRequestFtpJob::OnStartCompleted, + base::Unretained(this))); if (rv == ERR_IO_PENDING) return; MessageLoop::current()->PostTask( FROM_HERE, - method_factory_.NewRunnableMethod( - &URLRequestFtpJob::OnStartCompleted, rv)); + base::Bind(&URLRequestFtpJob::OnStartCompleted, + weak_factory_.GetWeakPtr(), rv)); } void URLRequestFtpJob::Start() { @@ -170,7 +171,7 @@ void URLRequestFtpJob::Kill() { return; transaction_.reset(); URLRequestJob::Kill(); - method_factory_.RevokeAll(); + weak_factory_.InvalidateWeakPtrs(); } LoadState URLRequestFtpJob::GetLoadState() const { @@ -219,8 +220,8 @@ void URLRequestFtpJob::CancelAuth() { // any recursing into the caller as a result of this call. MessageLoop::current()->PostTask( FROM_HERE, - method_factory_.NewRunnableMethod( - &URLRequestFtpJob::OnStartCompleted, OK)); + base::Bind(&URLRequestFtpJob::OnStartCompleted, + weak_factory_.GetWeakPtr(), OK)); } uint64 URLRequestFtpJob::GetUploadProgress() const { @@ -234,7 +235,9 @@ bool URLRequestFtpJob::ReadRawData(IOBuffer* buf, DCHECK(bytes_read); DCHECK(!read_in_progress_); - int rv = transaction_->Read(buf, buf_size, &read_callback_); + int rv = transaction_->Read(buf, buf_size, + base::Bind(&URLRequestFtpJob::OnReadCompleted, + base::Unretained(this))); if (rv >= 0) { *bytes_read = rv; return true; diff --git a/net/url_request/url_request_ftp_job.h b/net/url_request/url_request_ftp_job.h index bbec2b2..05def33 100644 --- a/net/url_request/url_request_ftp_job.h +++ b/net/url_request/url_request_ftp_job.h @@ -63,9 +63,6 @@ class URLRequestFtpJob : public URLRequestJob { FtpRequestInfo request_info_; scoped_ptr<FtpTransaction> transaction_; - OldCompletionCallbackImpl<URLRequestFtpJob> start_callback_; - OldCompletionCallbackImpl<URLRequestFtpJob> read_callback_; - bool read_in_progress_; scoped_refptr<AuthData> server_auth_; @@ -74,7 +71,7 @@ class URLRequestFtpJob : public URLRequestJob { // before us. scoped_refptr<const URLRequestContext> context_; - ScopedRunnableMethodFactory<URLRequestFtpJob> method_factory_; + base::WeakPtrFactory<URLRequestFtpJob> weak_factory_; DISALLOW_COPY_AND_ASSIGN(URLRequestFtpJob); }; diff --git a/remoting/jingle_glue/ssl_socket_adapter.cc b/remoting/jingle_glue/ssl_socket_adapter.cc index 7414c4b..5bf2f73 100644 --- a/remoting/jingle_glue/ssl_socket_adapter.cc +++ b/remoting/jingle_glue/ssl_socket_adapter.cc @@ -27,12 +27,6 @@ SSLSocketAdapter::SSLSocketAdapter(AsyncSocket* socket) : SSLAdapter(socket), ignore_bad_cert_(false), cert_verifier_(new net::CertVerifier()), - ALLOW_THIS_IN_INITIALIZER_LIST( - connected_callback_(this, &SSLSocketAdapter::OnConnected)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &SSLSocketAdapter::OnRead)), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_callback_(this, &SSLSocketAdapter::OnWrite)), ssl_state_(SSLSTATE_NONE), read_state_(IOSTATE_NONE), write_state_(IOSTATE_NONE) { @@ -77,7 +71,8 @@ int SSLSocketAdapter::BeginSSL() { transport_socket_, net::HostPortPair(hostname_, 443), ssl_config, NULL /* ssl_host_info */, context)); - int result = ssl_socket_->Connect(&connected_callback_); + int result = ssl_socket_->Connect( + base::Bind(&SSLSocketAdapter::OnConnected, base::Unretained(this))); if (result == net::ERR_IO_PENDING || result == net::OK) { return 0; @@ -94,7 +89,8 @@ int SSLSocketAdapter::Send(const void* buf, size_t len) { scoped_refptr<net::IOBuffer> transport_buf(new net::IOBuffer(len)); memcpy(transport_buf->data(), buf, len); - int result = ssl_socket_->Write(transport_buf, len, NULL); + int result = ssl_socket_->Write(transport_buf, len, + net::CompletionCallback()); if (result == net::ERR_IO_PENDING) { SetError(EWOULDBLOCK); } @@ -116,7 +112,9 @@ int SSLSocketAdapter::Recv(void* buf, size_t len) { switch (read_state_) { case IOSTATE_NONE: { transport_buf_ = new net::IOBuffer(len); - int result = ssl_socket_->Read(transport_buf_, len, &read_callback_); + int result = ssl_socket_->Read( + transport_buf_, len, + base::Bind(&SSLSocketAdapter::OnRead, base::Unretained(this))); if (result >= 0) { memcpy(buf, transport_buf_->data(), len); } @@ -187,9 +185,7 @@ void SSLSocketAdapter::OnConnectEvent(talk_base::AsyncSocket* socket) { TransportSocket::TransportSocket(talk_base::AsyncSocket* socket, SSLSocketAdapter *ssl_adapter) - : old_read_callback_(NULL), - write_callback_(NULL), - read_buffer_len_(0), + : read_buffer_len_(0), write_buffer_len_(0), socket_(socket), was_used_to_convey_data_(false) { @@ -200,12 +196,6 @@ TransportSocket::TransportSocket(talk_base::AsyncSocket* socket, TransportSocket::~TransportSocket() { } -int TransportSocket::Connect(net::OldCompletionCallback* callback) { - // Connect is never called by SSLClientSocket, instead SSLSocketAdapter - // calls Connect() on socket_ directly. - NOTREACHED(); - return false; -} int TransportSocket::Connect(const net::CompletionCallback& callback) { // Connect is never called by SSLClientSocket, instead SSLSocketAdapter // calls Connect() on socket_ directly. @@ -288,27 +278,9 @@ base::TimeDelta TransportSocket::GetConnectTimeMicros() const { } int TransportSocket::Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { - DCHECK(buf); - DCHECK(!old_read_callback_ && read_callback_.is_null()); - DCHECK(!read_buffer_.get()); - int result = socket_->Recv(buf->data(), buf_len); - if (result < 0) { - result = net::MapSystemError(socket_->GetError()); - if (result == net::ERR_IO_PENDING) { - old_read_callback_ = callback; - read_buffer_ = buf; - read_buffer_len_ = buf_len; - } - } - if (result != net::ERR_IO_PENDING) - was_used_to_convey_data_ = true; - return result; -} -int TransportSocket::Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { DCHECK(buf); - DCHECK(!old_read_callback_ && read_callback_.is_null()); + DCHECK(read_callback_.is_null()); DCHECK(!read_buffer_.get()); int result = socket_->Recv(buf->data(), buf_len); if (result < 0) { @@ -325,9 +297,9 @@ int TransportSocket::Read(net::IOBuffer* buf, int buf_len, } int TransportSocket::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { + const net::CompletionCallback& callback) { DCHECK(buf); - DCHECK(!write_callback_); + DCHECK(write_callback_.is_null()); DCHECK(!write_buffer_.get()); int result = socket_->Send(buf->data(), buf_len); if (result < 0) { @@ -354,14 +326,12 @@ bool TransportSocket::SetSendBufferSize(int32 size) { } void TransportSocket::OnReadEvent(talk_base::AsyncSocket* socket) { - if (old_read_callback_ || !read_callback_.is_null()) { + if (!read_callback_.is_null()) { DCHECK(read_buffer_.get()); - net::OldCompletionCallback* old_callback = old_read_callback_; net::CompletionCallback callback = read_callback_; scoped_refptr<net::IOBuffer> buffer = read_buffer_; int buffer_len = read_buffer_len_; - old_read_callback_ = NULL; read_callback_.Reset(); read_buffer_ = NULL; read_buffer_len_ = 0; @@ -370,7 +340,6 @@ void TransportSocket::OnReadEvent(talk_base::AsyncSocket* socket) { if (result < 0) { result = net::MapSystemError(socket_->GetError()); if (result == net::ERR_IO_PENDING) { - old_read_callback_ = old_callback; read_callback_ = callback; read_buffer_ = buffer; read_buffer_len_ = buffer_len; @@ -378,21 +347,18 @@ void TransportSocket::OnReadEvent(talk_base::AsyncSocket* socket) { } } was_used_to_convey_data_ = true; - if (old_callback) - old_callback->RunWithParams(Tuple1<int>(result)); - else - callback.Run(result); + callback.Run(result); } } void TransportSocket::OnWriteEvent(talk_base::AsyncSocket* socket) { - if (write_callback_) { + if (!write_callback_.is_null()) { DCHECK(write_buffer_.get()); - net::OldCompletionCallback* callback = write_callback_; + net::CompletionCallback callback = write_callback_; scoped_refptr<net::IOBuffer> buffer = write_buffer_; int buffer_len = write_buffer_len_; - write_callback_ = NULL; + write_callback_.Reset(); write_buffer_ = NULL; write_buffer_len_ = 0; @@ -407,7 +373,7 @@ void TransportSocket::OnWriteEvent(talk_base::AsyncSocket* socket) { } } was_used_to_convey_data_ = true; - callback->RunWithParams(Tuple1<int>(result)); + callback.Run(result); } } diff --git a/remoting/jingle_glue/ssl_socket_adapter.h b/remoting/jingle_glue/ssl_socket_adapter.h index f929bf8..ea8cff1 100644 --- a/remoting/jingle_glue/ssl_socket_adapter.h +++ b/remoting/jingle_glue/ssl_socket_adapter.h @@ -40,7 +40,6 @@ class TransportSocket : public net::StreamSocket, public sigslot::has_slots<> { } // net::StreamSocket implementation. - virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -57,11 +56,9 @@ class TransportSocket : public net::StreamSocket, public sigslot::has_slots<> { // net::Socket implementation. virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; @@ -71,9 +68,9 @@ class TransportSocket : public net::StreamSocket, public sigslot::has_slots<> { void OnReadEvent(talk_base::AsyncSocket* socket); void OnWriteEvent(talk_base::AsyncSocket* socket); - net::OldCompletionCallback* old_read_callback_; + // Holds the user's completion callback when Write and Read are called. net::CompletionCallback read_callback_; - net::OldCompletionCallback* write_callback_; + net::CompletionCallback write_callback_; scoped_refptr<net::IOBuffer> read_buffer_; int read_buffer_len_; @@ -143,9 +140,6 @@ class SSLSocketAdapter : public talk_base::SSLAdapter { scoped_ptr<net::CertVerifier> cert_verifier_; scoped_ptr<net::SSLClientSocket> ssl_socket_; - net::OldCompletionCallbackImpl<SSLSocketAdapter> connected_callback_; - net::OldCompletionCallbackImpl<SSLSocketAdapter> read_callback_; - net::OldCompletionCallbackImpl<SSLSocketAdapter> write_callback_; SSLState ssl_state_; IOState read_state_; IOState write_state_; diff --git a/remoting/protocol/buffered_socket_writer.cc b/remoting/protocol/buffered_socket_writer.cc index 2edba0b..b05f467 100644 --- a/remoting/protocol/buffered_socket_writer.cc +++ b/remoting/protocol/buffered_socket_writer.cc @@ -42,8 +42,6 @@ BufferedSocketWriterBase::BufferedSocketWriterBase( socket_(NULL), message_loop_(message_loop), write_pending_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - written_callback_(this, &BufferedSocketWriterBase::OnWritten)), closed_(false) { } @@ -93,8 +91,10 @@ void BufferedSocketWriterBase::DoWrite() { if (!current_packet) return; - int result = socket_->Write(current_packet, current_packet_size, - &written_callback_); + int result = socket_->Write( + current_packet, current_packet_size, + base::Bind(&BufferedSocketWriterBase::OnWritten, + base::Unretained(this))); if (result >= 0) { base::AutoLock auto_lock(lock_); AdvanceBufferPosition_Locked(result); diff --git a/remoting/protocol/buffered_socket_writer.h b/remoting/protocol/buffered_socket_writer.h index 762e045..7bc1b09 100644 --- a/remoting/protocol/buffered_socket_writer.h +++ b/remoting/protocol/buffered_socket_writer.h @@ -99,8 +99,6 @@ class BufferedSocketWriterBase bool write_pending_; - net::OldCompletionCallbackImpl<BufferedSocketWriterBase> written_callback_; - bool closed_; }; diff --git a/remoting/protocol/fake_session.cc b/remoting/protocol/fake_session.cc index f1a890a..c43b863 100644 --- a/remoting/protocol/fake_session.cc +++ b/remoting/protocol/fake_session.cc @@ -36,32 +36,12 @@ void FakeSocket::AppendInputData(const char* data, int data_size) { memcpy(read_buffer_->data(), &(*input_data_.begin()) + input_pos_, result); input_pos_ += result; - if (old_read_callback_) - old_read_callback_->Run(result); - else - read_callback_.Run(result); + read_callback_.Run(result); read_buffer_ = NULL; } } int FakeSocket::Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { - EXPECT_EQ(message_loop_, MessageLoop::current()); - if (input_pos_ < static_cast<int>(input_data_.size())) { - int result = std::min(buf_len, - static_cast<int>(input_data_.size()) - input_pos_); - memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result); - input_pos_ += result; - return result; - } else { - read_pending_ = true; - read_buffer_ = buf; - read_buffer_size_ = buf_len; - old_read_callback_ = callback; - return net::ERR_IO_PENDING; - } -} -int FakeSocket::Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { EXPECT_EQ(message_loop_, MessageLoop::current()); if (input_pos_ < static_cast<int>(input_data_.size())) { @@ -80,7 +60,7 @@ int FakeSocket::Read(net::IOBuffer* buf, int buf_len, } int FakeSocket::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { + const net::CompletionCallback& callback) { EXPECT_EQ(message_loop_, MessageLoop::current()); written_data_.insert(written_data_.end(), buf->data(), buf->data() + buf_len); @@ -96,10 +76,6 @@ bool FakeSocket::SetSendBufferSize(int32 size) { return false; } -int FakeSocket::Connect(net::OldCompletionCallback* callback) { - EXPECT_EQ(message_loop_, MessageLoop::current()); - return net::OK; -} int FakeSocket::Connect(const net::CompletionCallback& callback) { EXPECT_EQ(message_loop_, MessageLoop::current()); return net::OK; @@ -166,7 +142,6 @@ base::TimeDelta FakeSocket::GetConnectTimeMicros() const { FakeUdpSocket::FakeUdpSocket() : read_pending_(false), - old_read_callback_(NULL), input_pos_(0), message_loop_(MessageLoop::current()) { } @@ -186,32 +161,12 @@ void FakeUdpSocket::AppendInputPacket(const char* data, int data_size) { int result = std::min(data_size, read_buffer_size_); memcpy(read_buffer_->data(), data, result); input_pos_ = input_packets_.size(); - if (old_read_callback_) - old_read_callback_->Run(result); - else - old_read_callback_->Run(result); + read_callback_.Run(result); read_buffer_ = NULL; } } int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { - EXPECT_EQ(message_loop_, MessageLoop::current()); - if (input_pos_ < static_cast<int>(input_packets_.size())) { - int result = std::min( - buf_len, static_cast<int>(input_packets_[input_pos_].size())); - memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), result); - ++input_pos_; - return result; - } else { - read_pending_ = true; - read_buffer_ = buf; - read_buffer_size_ = buf_len; - old_read_callback_ = callback; - return net::ERR_IO_PENDING; - } -} -int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { EXPECT_EQ(message_loop_, MessageLoop::current()); if (input_pos_ < static_cast<int>(input_packets_.size())) { @@ -230,7 +185,7 @@ int FakeUdpSocket::Read(net::IOBuffer* buf, int buf_len, } int FakeUdpSocket::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { + const net::CompletionCallback& callback) { EXPECT_EQ(message_loop_, MessageLoop::current()); written_packets_.push_back(std::string()); written_packets_.back().assign(buf->data(), buf->data() + buf_len); diff --git a/remoting/protocol/fake_session.h b/remoting/protocol/fake_session.h index a8239f5..90fe0c8 100644 --- a/remoting/protocol/fake_session.h +++ b/remoting/protocol/fake_session.h @@ -40,17 +40,14 @@ class FakeSocket : public net::StreamSocket { // net::Socket implementation. virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; // net::StreamSocket implementation. - virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -69,7 +66,6 @@ class FakeSocket : public net::StreamSocket { bool read_pending_; scoped_refptr<net::IOBuffer> read_buffer_; int read_buffer_size_; - net::OldCompletionCallback* old_read_callback_; net::CompletionCallback read_callback_; std::string written_data_; @@ -100,11 +96,9 @@ class FakeUdpSocket : public net::Socket { // net::Socket implementation. virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; @@ -113,7 +107,6 @@ class FakeUdpSocket : public net::Socket { bool read_pending_; scoped_refptr<net::IOBuffer> read_buffer_; int read_buffer_size_; - net::OldCompletionCallback* old_read_callback_; net::CompletionCallback read_callback_; std::vector<std::string> written_packets_; diff --git a/remoting/protocol/jingle_session_unittest.cc b/remoting/protocol/jingle_session_unittest.cc index 44bfc4f..72fbb8b 100644 --- a/remoting/protocol/jingle_session_unittest.cc +++ b/remoting/protocol/jingle_session_unittest.cc @@ -463,10 +463,6 @@ class TCPChannelTester : public ChannelTesterBase { int message_size, int message_count) : ChannelTesterBase(host_session, client_session), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_cb_(this, &TCPChannelTester::OnWritten)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_cb_(this, &TCPChannelTester::OnRead)), write_errors_(0), read_errors_(0), message_size_(message_size), @@ -540,7 +536,9 @@ class TCPChannelTester : public ChannelTesterBase { break; int bytes_to_write = std::min(output_buffer_->BytesRemaining(), message_size_); - result = sockets_[0]->Write(output_buffer_, bytes_to_write, &write_cb_); + result = sockets_[0]->Write(output_buffer_, bytes_to_write, + base::Bind(&TCPChannelTester::OnWritten, + base::Unretained(this))); HandleWriteResult(result); }; } @@ -564,7 +562,9 @@ class TCPChannelTester : public ChannelTesterBase { int result = 1; while (result > 0) { input_buffer_->SetCapacity(input_buffer_->offset() + message_size_); - result = sockets_[1]->Read(input_buffer_, message_size_, &read_cb_); + result = sockets_[1]->Read(input_buffer_, message_size_, + base::Bind(&TCPChannelTester::OnRead, + base::Unretained(this))); HandleReadResult(result); }; } @@ -593,8 +593,6 @@ class TCPChannelTester : public ChannelTesterBase { scoped_refptr<net::DrainableIOBuffer> output_buffer_; scoped_refptr<net::GrowableIOBuffer> input_buffer_; - net::OldCompletionCallbackImpl<TCPChannelTester> write_cb_; - net::OldCompletionCallbackImpl<TCPChannelTester> read_cb_; int write_errors_; int read_errors_; int message_size_; @@ -635,10 +633,6 @@ class UDPChannelTester : public ChannelTesterBase { UDPChannelTester(Session* host_session, Session* client_session) : ChannelTesterBase(host_session, client_session), - ALLOW_THIS_IN_INITIALIZER_LIST( - write_cb_(this, &UDPChannelTester::OnWritten)), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_cb_(this, &UDPChannelTester::OnRead)), write_errors_(0), read_errors_(0), packets_sent_(0), @@ -705,7 +699,9 @@ class UDPChannelTester : public ChannelTesterBase { // Put index of this packet in the beginning of the packet body. memcpy(packet->data(), &packets_sent_, sizeof(packets_sent_)); - int result = sockets_[0]->Write(packet, kMessageSize, &write_cb_); + int result = sockets_[0]->Write(packet, kMessageSize, + base::Bind(&UDPChannelTester::OnWritten, + base::Unretained(this))); HandleWriteResult(result); } @@ -733,7 +729,9 @@ class UDPChannelTester : public ChannelTesterBase { int kReadSize = kMessageSize * 2; read_buffer_ = new net::IOBuffer(kReadSize); - result = sockets_[1]->Read(read_buffer_, kReadSize, &read_cb_); + result = sockets_[1]->Read(read_buffer_, kReadSize, + base::Bind(&UDPChannelTester::OnRead, + base::Unretained(this))); HandleReadResult(result); }; } @@ -775,8 +773,6 @@ class UDPChannelTester : public ChannelTesterBase { scoped_refptr<net::IOBuffer> sent_packets_[kMessages]; scoped_refptr<net::IOBuffer> read_buffer_; - net::OldCompletionCallbackImpl<UDPChannelTester> write_cb_; - net::OldCompletionCallbackImpl<UDPChannelTester> read_cb_; int write_errors_; int read_errors_; int packets_sent_; diff --git a/remoting/protocol/jingle_stream_connector.cc b/remoting/protocol/jingle_stream_connector.cc index 4eab85e..d7ce7fa 100644 --- a/remoting/protocol/jingle_stream_connector.cc +++ b/remoting/protocol/jingle_stream_connector.cc @@ -41,9 +41,7 @@ JingleStreamConnector::JingleStreamConnector( : session_(session), name_(name), callback_(callback), - raw_channel_(NULL), - ALLOW_THIS_IN_INITIALIZER_LIST(tcp_connect_callback_( - this, &JingleStreamConnector::OnTCPConnect)) { + raw_channel_(NULL) { } JingleStreamConnector::~JingleStreamConnector() { @@ -83,11 +81,13 @@ bool JingleStreamConnector::EstablishTCPConnection(net::Socket* socket) { adapter->SetSendBufferSize(kTcpSendBufferSize); tcp_socket_.reset(adapter); - int result = tcp_socket_->Connect(&tcp_connect_callback_); + int result = tcp_socket_->Connect( + base::Bind(&JingleStreamConnector::OnTCPConnect, + base::Unretained(this))); if (result == net::ERR_IO_PENDING) { return true; } else if (result == net::OK) { - tcp_connect_callback_.Run(result); + OnTCPConnect(result); return true; } diff --git a/remoting/protocol/jingle_stream_connector.h b/remoting/protocol/jingle_stream_connector.h index f37a691..1dd0240b 100644 --- a/remoting/protocol/jingle_stream_connector.h +++ b/remoting/protocol/jingle_stream_connector.h @@ -63,9 +63,6 @@ class JingleStreamConnector : public JingleChannelConnector { scoped_ptr<ChannelAuthenticator> authenticator_; - // Callback called by the TCP and SSL layers. - net::OldCompletionCallbackImpl<JingleStreamConnector> tcp_connect_callback_; - DISALLOW_COPY_AND_ASSIGN(JingleStreamConnector); }; diff --git a/remoting/protocol/message_reader.cc b/remoting/protocol/message_reader.cc index 82a3c80..d52448d 100644 --- a/remoting/protocol/message_reader.cc +++ b/remoting/protocol/message_reader.cc @@ -22,9 +22,7 @@ MessageReader::MessageReader() : socket_(NULL), read_pending_(false), pending_messages_(0), - closed_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &MessageReader::OnRead)) { + closed_(false) { } MessageReader::~MessageReader() { @@ -45,7 +43,8 @@ void MessageReader::DoRead() { while (!closed_ && !read_pending_ && pending_messages_ == 0) { read_buffer_ = new net::IOBuffer(kReadBufferSize); int result = socket_->Read( - read_buffer_, kReadBufferSize, &read_callback_); + read_buffer_, kReadBufferSize, base::Bind(&MessageReader::OnRead, + base::Unretained(this))); HandleReadResult(result); } } diff --git a/remoting/protocol/message_reader.h b/remoting/protocol/message_reader.h index fb89f91..3c33b5a 100644 --- a/remoting/protocol/message_reader.h +++ b/remoting/protocol/message_reader.h @@ -73,7 +73,6 @@ class MessageReader : public base::RefCountedThreadSafe<MessageReader> { bool closed_; scoped_refptr<net::IOBuffer> read_buffer_; - net::OldCompletionCallbackImpl<MessageReader> read_callback_; MessageDecoder message_decoder_; diff --git a/remoting/protocol/pepper_stream_channel.cc b/remoting/protocol/pepper_stream_channel.cc index e51d265..48d3a33 100644 --- a/remoting/protocol/pepper_stream_channel.cc +++ b/remoting/protocol/pepper_stream_channel.cc @@ -45,9 +45,7 @@ PepperStreamChannel::PepperStreamChannel( name_(name), callback_(callback), channel_(NULL), - connected_(false), - ALLOW_THIS_IN_INITIALIZER_LIST(p2p_connect_callback_( - this, &PepperStreamChannel::OnP2PConnect)) { + connected_(false) { } PepperStreamChannel::~PepperStreamChannel() { @@ -122,7 +120,8 @@ void PepperStreamChannel::Connect(pp::Instance* pp_instance, channel_ = new PepperTransportSocketAdapter(transport, name_, this); owned_channel_.reset(channel_); - int result = channel_->Connect(&p2p_connect_callback_); + int result = channel_->Connect(base::Bind(&PepperStreamChannel::OnP2PConnect, + base::Unretained(this))); if (result != net::ERR_IO_PENDING) OnP2PConnect(result); } diff --git a/remoting/protocol/pepper_stream_channel.h b/remoting/protocol/pepper_stream_channel.h index 5c17e4f..ead43e9 100644 --- a/remoting/protocol/pepper_stream_channel.h +++ b/remoting/protocol/pepper_stream_channel.h @@ -67,9 +67,6 @@ class PepperStreamChannel : public PepperChannel, // Indicates that we've finished connecting. bool connected_; - // Callback called by the TCP layer. - net::OldCompletionCallbackImpl<PepperStreamChannel> p2p_connect_callback_; - DISALLOW_COPY_AND_ASSIGN(PepperStreamChannel); }; diff --git a/remoting/protocol/pepper_transport_socket_adapter.cc b/remoting/protocol/pepper_transport_socket_adapter.cc index 0385b9b..ca1c470 100644 --- a/remoting/protocol/pepper_transport_socket_adapter.cc +++ b/remoting/protocol/pepper_transport_socket_adapter.cc @@ -48,9 +48,7 @@ PepperTransportSocketAdapter::PepperTransportSocketAdapter( observer_(observer), transport_(transport), connected_(false), - get_address_pending_(false), - old_read_callback_(NULL), - write_callback_(NULL) { + get_address_pending_(false) { callback_factory_.Initialize(this); } @@ -65,32 +63,11 @@ void PepperTransportSocketAdapter::AddRemoteCandidate( transport_->ReceiveRemoteAddress(candidate); } -int PepperTransportSocketAdapter::Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - DCHECK(!old_read_callback_ && read_callback_.is_null()); - DCHECK(!read_buffer_); - - if (!transport_.get()) - return net::ERR_SOCKET_NOT_CONNECTED; - - int result = PPErrorToNetError(transport_->Recv( - buf->data(), buf_len, - callback_factory_.NewOptionalCallback( - &PepperTransportSocketAdapter::OnRead))); - - if (result == net::ERR_IO_PENDING) { - old_read_callback_ = callback; - read_buffer_ = buf; - } - - return result; -} int PepperTransportSocketAdapter::Read( net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) { DCHECK(CalledOnValidThread()); - DCHECK(!old_read_callback_ && read_callback_.is_null()); + DCHECK(read_callback_.is_null()); DCHECK(!read_buffer_); if (!transport_.get()) @@ -109,10 +86,11 @@ int PepperTransportSocketAdapter::Read( return result; } -int PepperTransportSocketAdapter::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { +int PepperTransportSocketAdapter::Write( + net::IOBuffer* buf, int buf_len, + const net::CompletionCallback& callback) { DCHECK(CalledOnValidThread()); - DCHECK(!write_callback_); + DCHECK(write_callback_.is_null()); DCHECK(!write_buffer_); if (!transport_.get()) @@ -146,29 +124,6 @@ bool PepperTransportSocketAdapter::SetSendBufferSize(int32 size) { } int PepperTransportSocketAdapter::Connect( - net::OldCompletionCallback* callback) { - DCHECK(CalledOnValidThread()); - - if (!transport_.get()) - return net::ERR_UNEXPECTED; - - old_connect_callback_ = callback; - - // This will return false when GetNextAddress() returns an - // error. This helps to detect when the P2P Transport API is not - // supported. - int result = ProcessCandidates(); - if (result != net::OK) - return result; - - result = transport_->Connect( - callback_factory_.NewRequiredCallback( - &PepperTransportSocketAdapter::OnConnect)); - DCHECK_EQ(result, PP_OK_COMPLETIONPENDING); - - return net::ERR_IO_PENDING; -} -int PepperTransportSocketAdapter::Connect( const net::CompletionCallback& callback) { DCHECK(CalledOnValidThread()); @@ -300,49 +255,36 @@ void PepperTransportSocketAdapter::OnNextAddress(int32_t result) { void PepperTransportSocketAdapter::OnConnect(int result) { DCHECK(CalledOnValidThread()); - DCHECK(old_connect_callback_ || !connect_callback_.is_null()); + DCHECK(!connect_callback_.is_null()); if (result == PP_OK) connected_ = true; - if (old_connect_callback_) { - net::OldCompletionCallback* callback = old_connect_callback_; - old_connect_callback_ = NULL; - callback->Run(PPErrorToNetError(result)); - } else { - net::CompletionCallback callback = connect_callback_; - connect_callback_.Reset(); - callback.Run(PPErrorToNetError(result)); - } + net::CompletionCallback callback = connect_callback_; + connect_callback_.Reset(); + callback.Run(PPErrorToNetError(result)); } void PepperTransportSocketAdapter::OnRead(int32_t result) { DCHECK(CalledOnValidThread()); - DCHECK(old_read_callback_ || read_callback_.is_null()); + DCHECK(!read_callback_.is_null()); DCHECK(read_buffer_); - if (old_read_callback_) { - net::OldCompletionCallback* callback = old_read_callback_; - old_read_callback_ = NULL; - read_buffer_ = NULL; - callback->Run(PPErrorToNetError(result)); - } else { - net::CompletionCallback callback = read_callback_; - read_callback_.Reset(); - read_buffer_ = NULL; - callback.Run(PPErrorToNetError(result)); - } + net::CompletionCallback callback = read_callback_; + read_callback_.Reset(); + read_buffer_ = NULL; + callback.Run(PPErrorToNetError(result)); } void PepperTransportSocketAdapter::OnWrite(int32_t result) { DCHECK(CalledOnValidThread()); - DCHECK(write_callback_); + DCHECK(!write_callback_.is_null()); DCHECK(write_buffer_); - net::OldCompletionCallback* callback = write_callback_; - write_callback_ = NULL; + net::CompletionCallback callback = write_callback_; + write_callback_.Reset(); write_buffer_ = NULL; - callback->Run(PPErrorToNetError(result)); + callback.Run(PPErrorToNetError(result)); } } // namespace protocol diff --git a/remoting/protocol/pepper_transport_socket_adapter.h b/remoting/protocol/pepper_transport_socket_adapter.h index 40b75ce..bd84bb8 100644 --- a/remoting/protocol/pepper_transport_socket_adapter.h +++ b/remoting/protocol/pepper_transport_socket_adapter.h @@ -50,16 +50,13 @@ class PepperTransportSocketAdapter : public base::NonThreadSafe, // net::Socket implementation. virtual int Read(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; - virtual int Read(net::IOBuffer* buf, int buf_len, const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; // net::StreamSocket implementation. - virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; virtual void Disconnect() OVERRIDE; virtual bool IsConnected() const OVERRIDE; @@ -88,17 +85,15 @@ class PepperTransportSocketAdapter : public base::NonThreadSafe, scoped_ptr<pp::Transport_Dev> transport_; - net::OldCompletionCallback* old_connect_callback_; net::CompletionCallback connect_callback_; bool connected_; bool get_address_pending_; - net::OldCompletionCallback* old_read_callback_; net::CompletionCallback read_callback_; scoped_refptr<net::IOBuffer> read_buffer_; - net::OldCompletionCallback* write_callback_; + net::CompletionCallback write_callback_; scoped_refptr<net::IOBuffer> write_buffer_; net::BoundNetLog net_log_; diff --git a/remoting/protocol/socket_reader_base.cc b/remoting/protocol/socket_reader_base.cc index 743120d..bb315db 100644 --- a/remoting/protocol/socket_reader_base.cc +++ b/remoting/protocol/socket_reader_base.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -17,9 +17,7 @@ int kReadBufferSize = 4096; SocketReaderBase::SocketReaderBase() : socket_(NULL), - closed_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - read_callback_(this, &SocketReaderBase::OnRead)) { + closed_(false) { } SocketReaderBase::~SocketReaderBase() { } @@ -34,7 +32,8 @@ void SocketReaderBase::DoRead() { while (true) { read_buffer_ = new net::IOBuffer(kReadBufferSize); int result = socket_->Read( - read_buffer_, kReadBufferSize, &read_callback_); + read_buffer_, kReadBufferSize, base::Bind(&SocketReaderBase::OnRead, + base::Unretained(this))); HandleReadResult(result); if (result < 0) break; diff --git a/remoting/protocol/socket_reader_base.h b/remoting/protocol/socket_reader_base.h index a315217..3877f13 100644 --- a/remoting/protocol/socket_reader_base.h +++ b/remoting/protocol/socket_reader_base.h @@ -32,7 +32,6 @@ class SocketReaderBase { net::Socket* socket_; bool closed_; scoped_refptr<net::IOBuffer> read_buffer_; - net::OldCompletionCallbackImpl<SocketReaderBase> read_callback_; }; } // namespace remoting diff --git a/remoting/protocol/v1_client_channel_authenticator.cc b/remoting/protocol/v1_client_channel_authenticator.cc index 312403b..1f995fc 100644 --- a/remoting/protocol/v1_client_channel_authenticator.cc +++ b/remoting/protocol/v1_client_channel_authenticator.cc @@ -21,11 +21,7 @@ V1ClientChannelAuthenticator::V1ClientChannelAuthenticator( const std::string& shared_secret) : host_cert_(host_cert), shared_secret_(shared_secret), - socket_(NULL), - ALLOW_THIS_IN_INITIALIZER_LIST(connect_callback_( - this, &V1ClientChannelAuthenticator::OnConnected)), - ALLOW_THIS_IN_INITIALIZER_LIST(auth_write_callback_( - this, &V1ClientChannelAuthenticator::OnAuthBytesWritten)) { + socket_(NULL) { } V1ClientChannelAuthenticator::~V1ClientChannelAuthenticator() { @@ -59,7 +55,9 @@ void V1ClientChannelAuthenticator::SecureAndAuthenticate( net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( socket, host_and_port, ssl_config, NULL, context)); - int result = socket_->Connect(&connect_callback_); + int result = socket_->Connect( + base::Bind(&V1ClientChannelAuthenticator::OnConnected, + base::Unretained(this))); if (result == net::ERR_IO_PENDING) return; OnConnected(result); @@ -99,9 +97,10 @@ void V1ClientChannelAuthenticator::OnConnected(int result) { void V1ClientChannelAuthenticator::WriteAuthenticationBytes() { while (true) { - int result = socket_->Write(auth_write_buf_, - auth_write_buf_->BytesRemaining(), - &auth_write_callback_); + int result = socket_->Write( + auth_write_buf_, auth_write_buf_->BytesRemaining(), + base::Bind(&V1ClientChannelAuthenticator::OnAuthBytesWritten, + base::Unretained(this))); if (result == net::ERR_IO_PENDING) break; if (!HandleAuthBytesWritten(result)) diff --git a/remoting/protocol/v1_client_channel_authenticator.h b/remoting/protocol/v1_client_channel_authenticator.h index d8b2659..fcf4e89 100644 --- a/remoting/protocol/v1_client_channel_authenticator.h +++ b/remoting/protocol/v1_client_channel_authenticator.h @@ -49,11 +49,6 @@ class V1ClientChannelAuthenticator : public ChannelAuthenticator, scoped_ptr<net::CertVerifier> cert_verifier_; scoped_refptr<net::DrainableIOBuffer> auth_write_buf_; - net::OldCompletionCallbackImpl<V1ClientChannelAuthenticator> - connect_callback_; - net::OldCompletionCallbackImpl<V1ClientChannelAuthenticator> - auth_write_callback_; - DISALLOW_COPY_AND_ASSIGN(V1ClientChannelAuthenticator); }; diff --git a/remoting/protocol/v1_host_channel_authenticator.cc b/remoting/protocol/v1_host_channel_authenticator.cc index c124d47..c27340b 100644 --- a/remoting/protocol/v1_host_channel_authenticator.cc +++ b/remoting/protocol/v1_host_channel_authenticator.cc @@ -25,9 +25,7 @@ V1HostChannelAuthenticator::V1HostChannelAuthenticator( shared_secret_(shared_secret), socket_(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(connect_callback_( - this, &V1HostChannelAuthenticator::OnConnected)), - ALLOW_THIS_IN_INITIALIZER_LIST(auth_read_callback_( - this, &V1HostChannelAuthenticator::OnAuthBytesRead)) { + this, &V1HostChannelAuthenticator::OnConnected)) { } V1HostChannelAuthenticator::~V1HostChannelAuthenticator() { @@ -74,9 +72,11 @@ void V1HostChannelAuthenticator::OnConnected(int result) { void V1HostChannelAuthenticator::DoAuthRead(){ while (true) { - int result = socket_->Read(auth_read_buf_, - auth_read_buf_->RemainingCapacity(), - &auth_read_callback_); + int result = socket_->Read( + auth_read_buf_, + auth_read_buf_->RemainingCapacity(), + base::Bind(&V1HostChannelAuthenticator::OnAuthBytesRead, + base::Unretained(this))); if (result == net::ERR_IO_PENDING) break; if (!HandleAuthBytesRead(result)) diff --git a/remoting/protocol/v1_host_channel_authenticator.h b/remoting/protocol/v1_host_channel_authenticator.h index 081eff7..7820fae 100644 --- a/remoting/protocol/v1_host_channel_authenticator.h +++ b/remoting/protocol/v1_host_channel_authenticator.h @@ -55,8 +55,6 @@ class V1HostChannelAuthenticator : public ChannelAuthenticator, net::OldCompletionCallbackImpl<V1HostChannelAuthenticator> connect_callback_; - net::OldCompletionCallbackImpl<V1HostChannelAuthenticator> - auth_read_callback_; DISALLOW_COPY_AND_ASSIGN(V1HostChannelAuthenticator); }; diff --git a/webkit/plugins/ppapi/ppb_transport_impl.cc b/webkit/plugins/ppapi/ppb_transport_impl.cc index 44593d4..da656c8 100644 --- a/webkit/plugins/ppapi/ppb_transport_impl.cc +++ b/webkit/plugins/ppapi/ppb_transport_impl.cc @@ -68,11 +68,7 @@ WebKit::WebFrame* GetFrameForResource(const ::ppapi::Resource* resource) { PPB_Transport_Impl::PPB_Transport_Impl(PP_Instance instance) : Resource(instance), started_(false), - writable_(false), - ALLOW_THIS_IN_INITIALIZER_LIST( - channel_write_callback_(this, &PPB_Transport_Impl::OnWritten)), - ALLOW_THIS_IN_INITIALIZER_LIST( - channel_read_callback_(this, &PPB_Transport_Impl::OnRead)) { + writable_(false) { } PPB_Transport_Impl::~PPB_Transport_Impl() { @@ -323,7 +319,9 @@ int32_t PPB_Transport_Impl::Recv(void* data, uint32_t len, scoped_refptr<net::IOBuffer> buffer = new net::WrappedIOBuffer(static_cast<const char*>(data)); - int result = MapNetError(channel->Read(buffer, len, &channel_read_callback_)); + int result = MapNetError( + channel->Read(buffer, len, base::Bind(&PPB_Transport_Impl::OnRead, + base::Unretained(this)))); if (result == PP_OK_COMPLETIONPENDING) { recv_callback_ = new TrackedCompletionCallback( plugin_module->GetCallbackTracker(), pp_resource(), callback); @@ -352,8 +350,9 @@ int32_t PPB_Transport_Impl::Send(const void* data, uint32_t len, scoped_refptr<net::IOBuffer> buffer = new net::WrappedIOBuffer(static_cast<const char*>(data)); - int result = MapNetError(channel->Write(buffer, len, - &channel_write_callback_)); + int result = MapNetError( + channel->Write(buffer, len, base::Bind(&PPB_Transport_Impl::OnWritten, + base::Unretained(this)))); if (result == PP_OK_COMPLETIONPENDING) { send_callback_ = new TrackedCompletionCallback( plugin_module->GetCallbackTracker(), pp_resource(), callback); diff --git a/webkit/plugins/ppapi/ppb_transport_impl.h b/webkit/plugins/ppapi/ppb_transport_impl.h index f768ad9..1e5ecfa 100644 --- a/webkit/plugins/ppapi/ppb_transport_impl.h +++ b/webkit/plugins/ppapi/ppb_transport_impl.h @@ -73,9 +73,6 @@ class PPB_Transport_Impl : public ::ppapi::Resource, scoped_refptr<TrackedCompletionCallback> recv_callback_; scoped_refptr<TrackedCompletionCallback> send_callback_; - net::OldCompletionCallbackImpl<PPB_Transport_Impl> channel_write_callback_; - net::OldCompletionCallbackImpl<PPB_Transport_Impl> channel_read_callback_; - DISALLOW_COPY_AND_ASSIGN(PPB_Transport_Impl); }; |