Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Unified Diff: net/socket/client_socket_pool_base.h

Issue 6052006: Proof of concept for tr1-based Task replacement. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/net/base
Patch Set: Examples of replacing CreateFunctor, ScopedRMF, CompletionCallback Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/socket/client_socket_pool_base.h
diff --git a/net/socket/client_socket_pool_base.h b/net/socket/client_socket_pool_base.h
index 8e6eb139aed050a82d973f0c05319c3e12f24075..37ea0732c5579992fef030e7e5262814f7c0673a 100644
--- a/net/socket/client_socket_pool_base.h
+++ b/net/socket/client_socket_pool_base.h
@@ -30,6 +30,7 @@
#include <string>
#include "base/basictypes.h"
+#include "base/closure.h"
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "base/task.h"
@@ -165,11 +166,12 @@ class ClientSocketPoolBaseHelper
};
typedef uint32 Flags;
+ typedef ::std::tr1::function<void(int)> Tr1CompletionCallback;
class Request {
public:
Request(ClientSocketHandle* handle,
- CompletionCallback* callback,
+ Tr1CompletionCallback callback,
RequestPriority priority,
Flags flags,
const BoundNetLog& net_log);
@@ -177,14 +179,14 @@ class ClientSocketPoolBaseHelper
virtual ~Request();
ClientSocketHandle* handle() const { return handle_; }
- CompletionCallback* callback() const { return callback_; }
+ Tr1CompletionCallback callback() const { return callback_; }
RequestPriority priority() const { return priority_; }
Flags flags() const { return flags_; }
const BoundNetLog& net_log() const { return net_log_; }
private:
ClientSocketHandle* const handle_;
- CompletionCallback* const callback_;
+ Tr1CompletionCallback const callback_;
const RequestPriority priority_;
const Flags flags_;
BoundNetLog net_log_;
@@ -346,10 +348,10 @@ class ClientSocketPoolBaseHelper
return pending_requests_.front()->priority();
}
- bool HasBackupJob() const { return !method_factory_.empty(); }
+ bool HasBackupJob() const { return !closure_canceller_.empty(); }
void CleanupBackupJob() {
- method_factory_.RevokeAll();
+ closure_canceller_.RevokeAll();
}
// Set a timer to create a backup socket if it takes too long to create one.
@@ -385,7 +387,7 @@ class ClientSocketPoolBaseHelper
RequestQueue pending_requests_;
int active_socket_count_; // number of active sockets used by clients
// A factory to pin the backup_job tasks.
- ScopedRunnableMethodFactory<Group> method_factory_;
+ base::ClosureCanceller closure_canceller_;
};
typedef std::map<std::string, Group*> GroupMap;
@@ -394,10 +396,10 @@ class ClientSocketPoolBaseHelper
struct CallbackResultPair {
CallbackResultPair() : callback(NULL), result(OK) {}
- CallbackResultPair(CompletionCallback* callback_in, int result_in)
+ CallbackResultPair(Tr1CompletionCallback callback_in, int result_in)
: callback(callback_in), result(result_in) {}
- CompletionCallback* callback;
+ Tr1CompletionCallback callback;
int result;
};
@@ -493,7 +495,7 @@ class ClientSocketPoolBaseHelper
// current message loop. Inserts |callback| into |pending_callback_map_|,
// keyed by |handle|.
void InvokeUserCallbackLater(
- ClientSocketHandle* handle, CompletionCallback* callback, int rv);
+ ClientSocketHandle* handle, Tr1CompletionCallback callback, int rv);
// Invokes the user callback for |handle|. By the time this task has run,
// it's possible that the request has been cancelled, so |handle| may not
@@ -541,7 +543,7 @@ class ClientSocketPoolBaseHelper
// make sure that they are discarded rather than reused.
int pool_generation_number_;
- ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_;
+ base::ClosureCanceller closure_canceller_;
DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBaseHelper);
};
@@ -562,8 +564,25 @@ class ClientSocketPoolBase {
internal::ClientSocketPoolBaseHelper::Flags flags,
const scoped_refptr<SocketParams>& params,
const BoundNetLog& net_log)
+ // Do a hacky type transition from the CompletionCallback to
+ // Tr1CompletionCallback by binding the method. This is complicated
+ // by the fact that the function we're wrapper has overloads which
+ // forces a cast to hint which overload we want to wrap. This should
+ // not be an issue in most of the code since overloads are discouraged
+ // by the style guide.
+ //
+ // Also note that we We don't need to keep a reference to the callback
+ // because the API does not take ownership. One plus of Tr1 callback is
+ // that there is no such ownership ambiguity. But the downside is
+ // refcounting in the callback.
: internal::ClientSocketPoolBaseHelper::Request(
- handle, callback, priority, flags, net_log),
+ handle,
+ ::std::tr1::bind<void(CompletionCallback::*)(const int&)>(
+ &CompletionCallback::Run,
+ callback,
+ std::tr1::placeholders::_1),
+ priority,
+ flags, net_log),
params_(params) {}
const scoped_refptr<SocketParams>& params() const { return params_; }

Powered by Google App Engine
This is Rietveld 408576698