diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-06 23:33:24 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-06 23:33:24 +0000 |
commit | dbf036fcb743cfdcd5be421364c8b89b10ee3f55 (patch) | |
tree | 034c3028c8b523d2bb6c2703416b26a81bc31663 /jingle/notifier | |
parent | ad24b1827fe58c4a22c0cddb5791a95f2ab1b21b (diff) | |
download | chromium_src-dbf036fcb743cfdcd5be421364c8b89b10ee3f55.zip chromium_src-dbf036fcb743cfdcd5be421364c8b89b10ee3f55.tar.gz chromium_src-dbf036fcb743cfdcd5be421364c8b89b10ee3f55.tar.bz2 |
base::Bind: Convert StreamSocket::Connect.
BUG=none
TEST=none
R=csilv
Review URL: http://codereview.chromium.org/8801004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113283 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'jingle/notifier')
5 files changed, 84 insertions, 18 deletions
diff --git a/jingle/notifier/base/fake_ssl_client_socket.cc b/jingle/notifier/base/fake_ssl_client_socket.cc index 2ab7ef5..e247038 100644 --- a/jingle/notifier/base/fake_ssl_client_socket.cc +++ b/jingle/notifier/base/fake_ssl_client_socket.cc @@ -89,7 +89,7 @@ FakeSSLClientSocket::FakeSSLClientSocket( transport_socket_(transport_socket), next_handshake_state_(STATE_NONE), handshake_completed_(false), - user_connect_callback_(NULL), + old_user_connect_callback_(NULL), write_buf_(NewDrainableIOBufferWithSize(arraysize(kSslClientHello))), read_buf_(NewDrainableIOBufferWithSize(arraysize(kSslServerHello))) { CHECK(transport_socket_.get()); @@ -126,17 +126,34 @@ int FakeSSLClientSocket::Connect(net::OldCompletionCallback* callback) { DCHECK(callback); DCHECK_EQ(next_handshake_state_, STATE_NONE); DCHECK(!handshake_completed_); - DCHECK(!user_connect_callback_); + 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) { - user_connect_callback_ = callback; + 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. + DCHECK(!callback.is_null()); + DCHECK_EQ(next_handshake_state_, STATE_NONE); + DCHECK(!handshake_completed_); + DCHECK(user_connect_callback_.is_null()); + 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) + user_connect_callback_ = callback; + + return status; +} int FakeSSLClientSocket::DoHandshakeLoop() { DCHECK_NE(next_handshake_state_, STATE_NONE); @@ -167,9 +184,16 @@ int FakeSSLClientSocket::DoHandshakeLoop() { void FakeSSLClientSocket::RunUserConnectCallback(int status) { DCHECK_LE(status, net::OK); next_handshake_state_ = STATE_NONE; - net::OldCompletionCallback* user_connect_callback = user_connect_callback_; - user_connect_callback_ = NULL; - user_connect_callback->Run(status); + 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); + } } void FakeSSLClientSocket::DoHandshakeLoopWithUserConnectCallback() { @@ -191,7 +215,7 @@ int FakeSSLClientSocket::DoConnect() { void FakeSSLClientSocket::OnConnectDone(int status) { DCHECK_NE(status, net::ERR_IO_PENDING); DCHECK_LE(status, net::OK); - DCHECK(user_connect_callback_); + DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); if (status != net::OK) { RunUserConnectCallback(status); return; @@ -219,7 +243,7 @@ int FakeSSLClientSocket::DoSendClientHello() { void FakeSSLClientSocket::OnSendClientHelloDone(int status) { DCHECK_NE(status, net::ERR_IO_PENDING); - DCHECK(user_connect_callback_); + DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); if (status < net::OK) { RunUserConnectCallback(status); return; @@ -252,7 +276,7 @@ int FakeSSLClientSocket::DoVerifyServerHello() { void FakeSSLClientSocket::OnVerifyServerHelloDone(int status) { DCHECK_NE(status, net::ERR_IO_PENDING); - DCHECK(user_connect_callback_); + DCHECK(old_user_connect_callback_ || !user_connect_callback_.is_null()); if (status < net::OK) { RunUserConnectCallback(status); return; @@ -295,7 +319,8 @@ void FakeSSLClientSocket::Disconnect() { transport_socket_->Disconnect(); next_handshake_state_ = STATE_NONE; handshake_completed_ = false; - user_connect_callback_ = NULL; + 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 9a5af54..9061abe 100644 --- a/jingle/notifier/base/fake_ssl_client_socket.h +++ b/jingle/notifier/base/fake_ssl_client_socket.h @@ -53,6 +53,7 @@ class FakeSSLClientSocket : public net::StreamSocket { 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; virtual bool IsConnectedAndIdle() const OVERRIDE; @@ -107,7 +108,8 @@ class FakeSSLClientSocket : public net::StreamSocket { bool handshake_completed_; // The callback passed to Connect(). - net::OldCompletionCallback* user_connect_callback_; + net::OldCompletionCallback* old_user_connect_callback_; + net::CompletionCallback user_connect_callback_; scoped_refptr<net::DrainableIOBuffer> write_buf_; scoped_refptr<net::DrainableIOBuffer> read_buf_; diff --git a/jingle/notifier/base/fake_ssl_client_socket_unittest.cc b/jingle/notifier/base/fake_ssl_client_socket_unittest.cc index 86f5fb1..6c73af5 100644 --- a/jingle/notifier/base/fake_ssl_client_socket_unittest.cc +++ b/jingle/notifier/base/fake_ssl_client_socket_unittest.cc @@ -52,6 +52,7 @@ class MockClientSocket : public net::StreamSocket { 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()); MOCK_CONST_METHOD0(IsConnectedAndIdle, bool()); diff --git a/jingle/notifier/base/proxy_resolving_client_socket.cc b/jingle/notifier/base/proxy_resolving_client_socket.cc index 4c31ba3..b3a95f9 100644 --- a/jingle/notifier/base/proxy_resolving_client_socket.cc +++ b/jingle/notifier/base/proxy_resolving_client_socket.cc @@ -37,7 +37,7 @@ ProxyResolvingClientSocket::ProxyResolvingClientSocket( request_context_getter->GetURLRequestContext()->net_log(), net::NetLog::SOURCE_SOCKET)), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), - user_connect_callback_(NULL) { + old_user_connect_callback_(NULL) { DCHECK(request_context_getter); net::URLRequestContext* request_context = request_context_getter->GetURLRequestContext(); @@ -97,7 +97,35 @@ bool ProxyResolvingClientSocket::SetSendBufferSize(int32 size) { } int ProxyResolvingClientSocket::Connect(net::OldCompletionCallback* callback) { - DCHECK(!user_connect_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()); tried_direct_connect_fallback_ = false; @@ -126,9 +154,16 @@ int ProxyResolvingClientSocket::Connect(net::OldCompletionCallback* callback) { void ProxyResolvingClientSocket::RunUserConnectCallback(int status) { DCHECK_LE(status, net::OK); - net::OldCompletionCallback* user_connect_callback = user_connect_callback_; - user_connect_callback_ = NULL; - user_connect_callback->Run(status); + 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); + } } // Always runs asynchronously. @@ -287,7 +322,8 @@ void ProxyResolvingClientSocket::Disconnect() { CloseTransportSocket(); if (pac_request_) network_session_->proxy_service()->CancelPacRequest(pac_request_); - user_connect_callback_ = NULL; + old_user_connect_callback_ = NULL; + user_connect_callback_.Reset(); } bool ProxyResolvingClientSocket::IsConnected() const { diff --git a/jingle/notifier/base/proxy_resolving_client_socket.h b/jingle/notifier/base/proxy_resolving_client_socket.h index 0c40a22..0f27bc1 100644 --- a/jingle/notifier/base/proxy_resolving_client_socket.h +++ b/jingle/notifier/base/proxy_resolving_client_socket.h @@ -53,6 +53,7 @@ class ProxyResolvingClientSocket : public net::StreamSocket { 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; virtual bool IsConnectedAndIdle() const OVERRIDE; @@ -95,7 +96,8 @@ class ProxyResolvingClientSocket : public net::StreamSocket { base::WeakPtrFactory<ProxyResolvingClientSocket> weak_factory_; // The callback passed to Connect(). - net::OldCompletionCallback* user_connect_callback_; + net::OldCompletionCallback* old_user_connect_callback_; + net::CompletionCallback user_connect_callback_; }; } // namespace notifier |