Skip to content

Commit

Permalink
base::Bind: Finish converting net/socket.
Browse files Browse the repository at this point in the history
BUG=none
TEST=none
R=groby

Review URL: http://codereview.chromium.org/9008004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@115405 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jhawkins@chromium.org committed Dec 21, 2011
1 parent b08ff47 commit 6ea7b15
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 84 deletions.
2 changes: 1 addition & 1 deletion net/socket/client_socket_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class NET_EXPORT ClientSocketPool {

// Called to cancel a RequestSocket call that returned ERR_IO_PENDING. The
// same handle parameter must be passed to this method as was passed to the
// RequestSocket call being cancelled. The associated OldCompletionCallback is
// RequestSocket call being cancelled. The associated CompletionCallback is
// not run. However, for performance, we will let one ConnectJob complete
// and go idle.
virtual void CancelRequest(const std::string& group_name,
Expand Down
55 changes: 27 additions & 28 deletions net/socket/client_socket_pool_base_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/stringprintf.h"
#include "base/string_number_conversions.h"
Expand Down Expand Up @@ -202,7 +203,7 @@ class TestConnectJob : public ConnectJob {
BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)),
job_type_(job_type),
client_socket_factory_(client_socket_factory),
method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
load_state_(LOAD_STATE_IDLE),
store_additional_error_state_(false) {}

Expand Down Expand Up @@ -253,22 +254,22 @@ class TestConnectJob : public ConnectJob {
// time functions, so this change would be rather invasive.
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
method_factory_.NewRunnableMethod(
&TestConnectJob::DoConnect,
true /* successful */,
true /* async */,
false /* recoverable */),
base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
weak_factory_.GetWeakPtr(),
true /* successful */,
true /* async */,
false /* recoverable */),
kPendingConnectDelay);
return ERR_IO_PENDING;
case kMockPendingFailingJob:
set_load_state(LOAD_STATE_CONNECTING);
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
method_factory_.NewRunnableMethod(
&TestConnectJob::DoConnect,
false /* error */,
true /* async */,
false /* recoverable */),
base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
weak_factory_.GetWeakPtr(),
false /* error */,
true /* async */,
false /* recoverable */),
2);
return ERR_IO_PENDING;
case kMockWaitingJob:
Expand All @@ -277,9 +278,8 @@ class TestConnectJob : public ConnectJob {
return ERR_IO_PENDING;
case kMockAdvancingLoadStateJob:
MessageLoop::current()->PostTask(
FROM_HERE,
method_factory_.NewRunnableMethod(
&TestConnectJob::AdvanceLoadState, load_state_));
FROM_HERE, base::Bind(&TestConnectJob::AdvanceLoadState,
weak_factory_.GetWeakPtr(), load_state_));
return ERR_IO_PENDING;
case kMockRecoverableJob:
return DoConnect(false /* error */, false /* sync */,
Expand All @@ -288,11 +288,11 @@ class TestConnectJob : public ConnectJob {
set_load_state(LOAD_STATE_CONNECTING);
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
method_factory_.NewRunnableMethod(
&TestConnectJob::DoConnect,
false /* error */,
true /* async */,
true /* recoverable */),
base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
weak_factory_.GetWeakPtr(),
false /* error */,
true /* async */,
true /* recoverable */),
2);
return ERR_IO_PENDING;
case kMockAdditionalErrorStateJob:
Expand All @@ -304,11 +304,11 @@ class TestConnectJob : public ConnectJob {
store_additional_error_state_ = true;
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
method_factory_.NewRunnableMethod(
&TestConnectJob::DoConnect,
false /* error */,
true /* async */,
false /* recoverable */),
base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
weak_factory_.GetWeakPtr(),
false /* error */,
true /* async */,
false /* recoverable */),
2);
return ERR_IO_PENDING;
default:
Expand Down Expand Up @@ -347,16 +347,15 @@ class TestConnectJob : public ConnectJob {
state = static_cast<LoadState>(tmp);
set_load_state(state);
MessageLoop::current()->PostTask(
FROM_HERE,
method_factory_.NewRunnableMethod(&TestConnectJob::AdvanceLoadState,
state));
FROM_HERE, base::Bind(&TestConnectJob::AdvanceLoadState,
weak_factory_.GetWeakPtr(), state));
}
}

bool waiting_success_;
const JobType job_type_;
MockClientSocketFactory* const client_socket_factory_;
ScopedRunnableMethodFactory<TestConnectJob> method_factory_;
base::WeakPtrFactory<TestConnectJob> weak_factory_;
LoadState load_state_;
bool store_additional_error_state_;

Expand Down
2 changes: 1 addition & 1 deletion net/socket/ssl_server_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SSLServerSocket : public SSLSocket {
// if the process completes asynchronously. If Disconnect is called before
// completion then the callback will be silently, as for other StreamSocket
// calls.
virtual int Handshake(OldCompletionCallback* callback) = 0;
virtual int Handshake(const CompletionCallback& callback) = 0;
};

// Creates an SSL server socket over an already-connected transport socket.
Expand Down
13 changes: 6 additions & 7 deletions net/socket/ssl_server_socket_nss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ SSLServerSocketNSS::SSLServerSocketNSS(
const SSLConfig& ssl_config)
: transport_send_busy_(false),
transport_recv_busy_(false),
user_handshake_callback_(NULL),
nss_fd_(NULL),
nss_bufs_(NULL),
transport_socket_(transport_socket),
Expand All @@ -86,7 +85,7 @@ SSLServerSocketNSS::~SSLServerSocketNSS() {
}
}

int SSLServerSocketNSS::Handshake(OldCompletionCallback* callback) {
int SSLServerSocketNSS::Handshake(const CompletionCallback& callback) {
net_log_.BeginEvent(NetLog::TYPE_SSL_SERVER_HANDSHAKE, NULL);

int rv = Init();
Expand Down Expand Up @@ -145,7 +144,7 @@ int SSLServerSocketNSS::Connect(const CompletionCallback& callback) {
int SSLServerSocketNSS::Read(IOBuffer* buf, int buf_len,
const CompletionCallback& callback) {
DCHECK(user_read_callback_.is_null());
DCHECK(!user_handshake_callback_);
DCHECK(user_handshake_callback_.is_null());
DCHECK(!user_read_buf_);
DCHECK(nss_bufs_);

Expand Down Expand Up @@ -466,7 +465,7 @@ void SSLServerSocketNSS::OnHandshakeIOComplete(int result) {
if (rv != ERR_IO_PENDING) {
net_log_.EndEventWithNetErrorCode(net::NetLog::TYPE_SSL_SERVER_HANDSHAKE,
rv);
if (user_handshake_callback_)
if (!user_handshake_callback_.is_null())
DoHandshakeCallback(rv);
}
}
Expand Down Expand Up @@ -705,9 +704,9 @@ int SSLServerSocketNSS::DoHandshake() {
void SSLServerSocketNSS::DoHandshakeCallback(int rv) {
DCHECK_NE(rv, ERR_IO_PENDING);

OldCompletionCallback* c = user_handshake_callback_;
user_handshake_callback_ = NULL;
c->Run(rv > OK ? OK : rv);
CompletionCallback c = user_handshake_callback_;
user_handshake_callback_.Reset();
c.Run(rv > OK ? OK : rv);
}

void SSLServerSocketNSS::DoReadCallback(int rv) {
Expand Down
4 changes: 2 additions & 2 deletions net/socket/ssl_server_socket_nss.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SSLServerSocketNSS : public SSLServerSocket {
virtual ~SSLServerSocketNSS();

// SSLServerSocket interface.
virtual int Handshake(OldCompletionCallback* callback) OVERRIDE;
virtual int Handshake(const CompletionCallback& callback) OVERRIDE;
virtual int ExportKeyingMaterial(const base::StringPiece& label,
const base::StringPiece& context,
unsigned char *out,
Expand Down Expand Up @@ -105,7 +105,7 @@ class SSLServerSocketNSS : public SSLServerSocket {

BoundNetLog net_log_;

OldCompletionCallback* user_handshake_callback_;
CompletionCallback user_handshake_callback_;
CompletionCallback user_read_callback_;
CompletionCallback user_write_callback_;

Expand Down
13 changes: 7 additions & 6 deletions net/socket/ssl_server_socket_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "net/base/address_list.h"
#include "net/base/cert_status_flags.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/ip_endpoint.h"
Expand Down Expand Up @@ -326,9 +327,9 @@ TEST_F(SSLServerSocketTest, Handshake) {
Initialize();

TestCompletionCallback connect_callback;
TestOldCompletionCallback handshake_callback;
TestCompletionCallback handshake_callback;

int server_ret = server_socket_->Handshake(&handshake_callback);
int server_ret = server_socket_->Handshake(handshake_callback.callback());
EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);

int client_ret = client_socket_->Connect(connect_callback.callback());
Expand All @@ -351,13 +352,13 @@ TEST_F(SSLServerSocketTest, DataTransfer) {
Initialize();

TestCompletionCallback connect_callback;
TestOldCompletionCallback handshake_callback;
TestCompletionCallback handshake_callback;

// Establish connection.
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);
int server_ret = server_socket_->Handshake(handshake_callback.callback());
ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);

client_ret = connect_callback.GetResult(client_ret);
Expand Down Expand Up @@ -435,12 +436,12 @@ TEST_F(SSLServerSocketTest, ExportKeyingMaterial) {
Initialize();

TestCompletionCallback connect_callback;
TestOldCompletionCallback handshake_callback;
TestCompletionCallback handshake_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);
int server_ret = server_socket_->Handshake(handshake_callback.callback());
ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);

if (client_ret == net::ERR_IO_PENDING) {
Expand Down
31 changes: 14 additions & 17 deletions net/socket/web_socket_server_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
#endif

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/md5.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/task.h"
#include "googleurl/src/gurl.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
Expand Down Expand Up @@ -120,7 +122,7 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket {
handshake_buf_, kHandshakeLimitBytes)),
is_transport_read_pending_(false),
is_transport_write_pending_(false),
method_factory_(this) {
weak_factory_(this) {
DCHECK(transport_socket);
DCHECK(delegate);
}
Expand Down Expand Up @@ -315,17 +317,12 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket {
}

// WebSocketServerSocket implementation.
virtual int Accept(net::OldCompletionCallback* callback) {
virtual int Accept(const net::CompletionCallback& callback) OVERRIDE {
if (phase_ != PHASE_NYMPH)
return net::ERR_UNEXPECTED;
phase_ = PHASE_HANDSHAKE;
net::CompletionCallback cb;
if (callback) {
cb = base::Bind(&net::OldCompletionCallbackAdapter, callback);
}
pending_reqs_.push_front(PendingReq(
PendingReq::TYPE_READ_METADATA, fill_handshake_buf_.get(),
cb));
PendingReq::TYPE_READ_METADATA, fill_handshake_buf_.get(), callback));
ConsiderTransportRead();
return net::ERR_IO_PENDING;
}
Expand Down Expand Up @@ -360,9 +357,9 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket {
// PostTask rather than direct call in order to:
// (1) guarantee calling callback after returning from Read();
// (2) avoid potential stack overflow;
MessageLoop::current()->PostTask(FROM_HERE,
method_factory_.NewRunnableMethod(
&WebSocketServerSocketImpl::OnRead, rv));
MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&WebSocketServerSocketImpl::OnRead,
weak_factory_.GetWeakPtr(), rv));
}
}

Expand All @@ -388,9 +385,9 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket {
// PostTask rather than direct call in order to:
// (1) guarantee calling callback after returning from Read();
// (2) avoid potential stack overflow;
MessageLoop::current()->PostTask(FROM_HERE,
method_factory_.NewRunnableMethod(
&WebSocketServerSocketImpl::OnWrite, rv));
MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&WebSocketServerSocketImpl::OnWrite,
weak_factory_.GetWeakPtr(), rv));
}
}

Expand Down Expand Up @@ -877,14 +874,14 @@ class WebSocketServerSocketImpl : public net::WebSocketServerSocket {
scoped_refptr<net::DrainableIOBuffer> fill_handshake_buf_;
scoped_refptr<net::DrainableIOBuffer> process_handshake_buf_;

// Pending io requests we need to complete.
// Pending IO requests we need to complete.
std::deque<PendingReq> pending_reqs_;

// Whether transport requests are pending.
bool is_transport_read_pending_;
bool is_transport_write_pending_;

ScopedRunnableMethodFactory<WebSocketServerSocketImpl> method_factory_;
base::WeakPtrFactory<WebSocketServerSocketImpl> weak_factory_;

DISALLOW_COPY_AND_ASSIGN(WebSocketServerSocketImpl);
};
Expand Down
2 changes: 1 addition & 1 deletion net/socket/web_socket_server_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class WebSocketServerSocket : public Socket {
// Returns either ERR_IO_PENDING, in which case the given callback will be
// called in the future with the real result, or it completes synchronously,
// returning the result immediately.
virtual int Accept(OldCompletionCallback* callback) = 0;
virtual int Accept(const CompletionCallback& callback) = 0;
};

// Creates websocket server socket atop of already connected socket. This
Expand Down
Loading

0 comments on commit 6ea7b15

Please sign in to comment.