Skip to content

Commit

Permalink
base::Bind: Make use of partial currying to get rid of a helper class.
Browse files Browse the repository at this point in the history
BUG=none
TEST=none
R=ajwong

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114122 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jhawkins@chromium.org committed Dec 13, 2011
1 parent 9fc206d commit 7f4ad94
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
6 changes: 3 additions & 3 deletions base/callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
// There are three main components to the system:
// 1) The Callback classes.
// 2) The Bind() functions.
// 3) The arguments wrappers (eg., Unretained() and ConstRef()).
// 3) The arguments wrappers (e.g., Unretained() and ConstRef()).
//
// The Callback classes represent a generic function pointer. Internally,
// it stores a refcounted piece of state that represents the target function
Expand Down Expand Up @@ -160,7 +160,7 @@
// to refcount a target object if the function being bound is a class method.
//
// To change this behavior, we introduce a set of argument wrappers
// (eg. Unretained(), and ConstRef()). These are simple container templates
// (e.g., Unretained(), and ConstRef()). These are simple container templates
// that are passed by value, and wrap a pointer to argument. See the
// file-level comment in base/bind_helpers.h for more info.
//
Expand Down Expand Up @@ -197,7 +197,7 @@
// Lastly, tr1::function and tr1::bind has a more general and flexible API.
// This includes things like argument reordering by use of
// tr1::bind::placeholder, support for non-const reference parameters, and some
// limited amount of subtyping of the tr1::function object (eg.,
// limited amount of subtyping of the tr1::function object (e.g.,
// tr1::function<int(int)> is convertible to tr1::function<void(int)>).
//
// These are not features that are required in Chromium. Some of them, such as
Expand Down
6 changes: 3 additions & 3 deletions base/callback.h.pump
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ $var MAX_ARITY = 7
// There are three main components to the system:
// 1) The Callback classes.
// 2) The Bind() functions.
// 3) The arguments wrappers (eg., Unretained() and ConstRef()).
// 3) The arguments wrappers (e.g., Unretained() and ConstRef()).
//
// The Callback classes represent a generic function pointer. Internally,
// it stores a refcounted piece of state that represents the target function
Expand Down Expand Up @@ -165,7 +165,7 @@ $var MAX_ARITY = 7
// to refcount a target object if the function being bound is a class method.
//
// To change this behavior, we introduce a set of argument wrappers
// (eg. Unretained(), and ConstRef()). These are simple container templates
// (e.g., Unretained(), and ConstRef()). These are simple container templates
// that are passed by value, and wrap a pointer to argument. See the
// file-level comment in base/bind_helpers.h for more info.
//
Expand Down Expand Up @@ -202,7 +202,7 @@ $var MAX_ARITY = 7
// Lastly, tr1::function and tr1::bind has a more general and flexible API.
// This includes things like argument reordering by use of
// tr1::bind::placeholder, support for non-const reference parameters, and some
// limited amount of subtyping of the tr1::function object (eg.,
// limited amount of subtyping of the tr1::function object (e.g.,
// tr1::function<int(int)> is convertible to tr1::function<void(int)>).
//
// These are not features that are required in Chromium. Some of them, such as
Expand Down
45 changes: 15 additions & 30 deletions chrome/browser/chrome_benchmarking_message_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,12 @@

namespace {

class ClearCacheHelper {
public:
ClearCacheHelper(ChromeBenchmarkingMessageFilter* filter,
IPC::Message* reply_msg)
: filter_(filter),
reply_msg_(reply_msg) {
}

void Run(int result) {
ChromeViewHostMsg_ClearCache::WriteReplyParams(reply_msg_, result);
filter_->Send(reply_msg_);
}

private:
scoped_refptr<ChromeBenchmarkingMessageFilter> filter_;
IPC::Message* reply_msg_;
};
void ClearCacheCallback(ChromeBenchmarkingMessageFilter* filter,
IPC::Message* reply_msg,
int result) {
ChromeViewHostMsg_ClearCache::WriteReplyParams(reply_msg, result);
filter->Send(reply_msg);
}

// Class to assist with clearing out the cache when we want to preserve
// the sslhostinfo entries. It's not very efficient, but its just for debug.
Expand All @@ -50,13 +39,11 @@ class DoomEntriesHelper {
iter_(NULL),
ALLOW_THIS_IN_INITIALIZER_LIST(callback_(
base::Bind(&DoomEntriesHelper::CacheCallback,
base::Unretained(this)))),
clear_cache_helper_(NULL) {
base::Unretained(this)))) {
}

// Takes ownership of |callback|.
void ClearCache(ClearCacheHelper* helper) {
clear_cache_helper_.reset(helper);
void ClearCache(const net::CompletionCallback& callback) {
clear_cache_callback_ = callback;
return CacheCallback(net::OK); // Start clearing the cache.
}

Expand All @@ -66,7 +53,7 @@ class DoomEntriesHelper {
void CacheCallback(int result) {
do {
if (result != net::OK) {
clear_cache_helper_->Run(result);
clear_cache_callback_.Run(result);
delete this;
return;
}
Expand All @@ -90,7 +77,7 @@ class DoomEntriesHelper {
disk_cache::Entry* entry_;
void* iter_;
net::CompletionCallback callback_;
scoped_ptr<ClearCacheHelper> clear_cache_helper_;
net::CompletionCallback clear_cache_callback_;
};

} // namespace
Expand Down Expand Up @@ -138,16 +125,14 @@ void ChromeBenchmarkingMessageFilter::OnClearCache(bool preserve_ssl_host_info,
disk_cache::Backend* backend = request_context_->GetURLRequestContext()->
http_transaction_factory()->GetCache()->GetCurrentBackend();
if (backend) {
scoped_ptr<ClearCacheHelper> clear_cache_helper(
new ClearCacheHelper(this, reply_msg));
net::CompletionCallback callback =
base::Bind(&ClearCacheCallback, make_scoped_refptr(this), reply_msg);
if (preserve_ssl_host_info) {
DoomEntriesHelper* helper = new DoomEntriesHelper(backend);
helper->ClearCache(clear_cache_helper.release()); // Will self clean.
helper->ClearCache(callback); // Will self clean.
return;
} else {
rv = backend->DoomAllEntries(
base::Bind(&ClearCacheHelper::Run,
base::Owned(clear_cache_helper.release())));
rv = backend->DoomAllEntries(callback);
if (rv == net::ERR_IO_PENDING) {
// The callback will send the reply.
return;
Expand Down

0 comments on commit 7f4ad94

Please sign in to comment.