summaryrefslogtreecommitdiffstats
path: root/net/http
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-19 20:59:27 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-19 20:59:27 +0000
commit2a65aceb8e3ed63fecf62191ef6fb64d257bf484 (patch)
tree5f27b5c30f11f32cb8ca87126ef744d637c14590 /net/http
parenta18c4a5c9a056a2f49eccf360d141c215c85d053 (diff)
downloadchromium_src-2a65aceb8e3ed63fecf62191ef6fb64d257bf484.zip
chromium_src-2a65aceb8e3ed63fecf62191ef6fb64d257bf484.tar.gz
chromium_src-2a65aceb8e3ed63fecf62191ef6fb64d257bf484.tar.bz2
base::Bind: Convert most of disk_cache.
BUG=none TEST=none R=csilv Review URL: http://codereview.chromium.org/8963030 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@115019 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/http')
-rw-r--r--net/http/disk_cache_based_ssl_host_info.cc36
-rw-r--r--net/http/disk_cache_based_ssl_host_info.h2
-rw-r--r--net/http/http_cache.cc73
-rw-r--r--net/http/http_cache.h9
-rw-r--r--net/http/http_cache_transaction.cc52
-rw-r--r--net/http/http_cache_transaction.h8
-rw-r--r--net/http/http_cache_unittest.cc60
-rw-r--r--net/http/mock_http_cache.cc115
-rw-r--r--net/http/mock_http_cache.h46
-rw-r--r--net/http/partial_data.cc25
-rw-r--r--net/http/partial_data.h6
11 files changed, 251 insertions, 181 deletions
diff --git a/net/http/disk_cache_based_ssl_host_info.cc b/net/http/disk_cache_based_ssl_host_info.cc
index 003618e..46a557f 100644
--- a/net/http/disk_cache_based_ssl_host_info.cc
+++ b/net/http/disk_cache_based_ssl_host_info.cc
@@ -4,8 +4,10 @@
#include "net/http/disk_cache_based_ssl_host_info.h"
+#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
+#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/http/http_cache.h"
@@ -39,8 +41,8 @@ DiskCacheBasedSSLHostInfo::DiskCacheBasedSSLHostInfo(
CertVerifier* cert_verifier,
HttpCache* http_cache)
: SSLHostInfo(hostname, ssl_config, cert_verifier),
- weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
- callback_(new CallbackImpl(weak_ptr_factory_.GetWeakPtr(),
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
+ callback_(new CallbackImpl(weak_factory_.GetWeakPtr(),
&DiskCacheBasedSSLHostInfo::OnIOComplete)),
state_(GET_BACKEND),
ready_(false),
@@ -205,12 +207,16 @@ int DiskCacheBasedSSLHostInfo::DoCreateOrOpenComplete(int rv) {
int DiskCacheBasedSSLHostInfo::DoGetBackend() {
state_ = GET_BACKEND_COMPLETE;
- return http_cache_->GetBackend(callback_->backend_pointer(), callback_);
+ return http_cache_->GetBackend(
+ callback_->backend_pointer(),
+ base::Bind(&net::OldCompletionCallbackAdapter, callback_));
}
int DiskCacheBasedSSLHostInfo::DoOpen() {
state_ = OPEN_COMPLETE;
- return backend_->OpenEntry(key(), callback_->entry_pointer(), callback_);
+ return backend_->OpenEntry(
+ key(), callback_->entry_pointer(),
+ base::Bind(&net::OldCompletionCallbackAdapter, callback_));
}
int DiskCacheBasedSSLHostInfo::DoRead() {
@@ -222,8 +228,9 @@ int DiskCacheBasedSSLHostInfo::DoRead() {
read_buffer_ = new IOBuffer(size);
state_ = READ_COMPLETE;
- return entry_->ReadData(0 /* index */, 0 /* offset */, read_buffer_,
- size, callback_);
+ return entry_->ReadData(
+ 0 /* index */, 0 /* offset */, read_buffer_, size,
+ base::Bind(&net::OldCompletionCallbackAdapter, callback_));
}
int DiskCacheBasedSSLHostInfo::DoWrite() {
@@ -231,17 +238,24 @@ int DiskCacheBasedSSLHostInfo::DoWrite() {
memcpy(write_buffer_->data(), new_data_.data(), new_data_.size());
state_ = WRITE_COMPLETE;
- return entry_->WriteData(0 /* index */, 0 /* offset */, write_buffer_,
- new_data_.size(), callback_, true /* truncate */);
+ return entry_->WriteData(
+ 0 /* index */, 0 /* offset */, write_buffer_, new_data_.size(),
+ base::Bind(&net::OldCompletionCallbackAdapter, callback_),
+ true /* truncate */);
}
int DiskCacheBasedSSLHostInfo::DoCreateOrOpen() {
DCHECK(entry_ == NULL);
state_ = CREATE_OR_OPEN_COMPLETE;
- if (found_entry_)
- return backend_->OpenEntry(key(), callback_->entry_pointer(), callback_);
+ if (found_entry_) {
+ return backend_->OpenEntry(
+ key(), callback_->entry_pointer(),
+ base::Bind(&net::OldCompletionCallbackAdapter, callback_));
+ }
- return backend_->CreateEntry(key(), callback_->entry_pointer(), callback_);
+ return backend_->CreateEntry(
+ key(), callback_->entry_pointer(),
+ base::Bind(&net::OldCompletionCallbackAdapter, callback_));
}
int DiskCacheBasedSSLHostInfo::DoWaitForDataReadyDone() {
diff --git a/net/http/disk_cache_based_ssl_host_info.h b/net/http/disk_cache_based_ssl_host_info.h
index 2339ae9..5fd2be80 100644
--- a/net/http/disk_cache_based_ssl_host_info.h
+++ b/net/http/disk_cache_based_ssl_host_info.h
@@ -105,7 +105,7 @@ class NET_EXPORT_PRIVATE DiskCacheBasedSSLHostInfo
// IsCallbackPending returns true if we have a pending callback.
bool IsCallbackPending() const;
- base::WeakPtrFactory<DiskCacheBasedSSLHostInfo> weak_ptr_factory_;
+ base::WeakPtrFactory<DiskCacheBasedSSLHostInfo> weak_factory_;
CallbackImpl* callback_;
State state_;
bool ready_;
diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc
index 8ee0ac6..1dcb368 100644
--- a/net/http/http_cache.cc
+++ b/net/http/http_cache.cc
@@ -13,6 +13,7 @@
#endif
#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/format_macros.h"
#include "base/location.h"
@@ -89,9 +90,9 @@ HttpCache::BackendFactory* HttpCache::DefaultBackend::InMemory(int max_bytes) {
return new DefaultBackend(MEMORY_CACHE, FilePath(), max_bytes, NULL);
}
-int HttpCache::DefaultBackend::CreateBackend(NetLog* net_log,
- disk_cache::Backend** backend,
- OldCompletionCallback* callback) {
+int HttpCache::DefaultBackend::CreateBackend(
+ NetLog* net_log, disk_cache::Backend** backend,
+ const CompletionCallback& callback) {
DCHECK_GE(max_bytes_, 0);
return disk_cache::CreateCacheBackend(type_, path_, max_bytes_, true,
thread_, net_log, backend, callback);
@@ -143,11 +144,16 @@ enum WorkItemOperation {
class HttpCache::WorkItem {
public:
WorkItem(WorkItemOperation operation, Transaction* trans, ActiveEntry** entry)
- : operation_(operation), trans_(trans), entry_(entry), callback_(NULL),
+ : operation_(operation),
+ trans_(trans),
+ entry_(entry),
backend_(NULL) {}
WorkItem(WorkItemOperation operation, Transaction* trans,
- OldCompletionCallback* cb, disk_cache::Backend** backend)
- : operation_(operation), trans_(trans), entry_(NULL), callback_(cb),
+ const net::CompletionCallback& cb, disk_cache::Backend** backend)
+ : operation_(operation),
+ trans_(trans),
+ entry_(NULL),
+ callback_(cb),
backend_(backend) {}
~WorkItem() {}
@@ -165,8 +171,8 @@ class HttpCache::WorkItem {
bool DoCallback(int result, disk_cache::Backend* backend) {
if (backend_)
*backend_ = backend;
- if (callback_) {
- callback_->Run(result);
+ if (!callback_.is_null()) {
+ callback_.Run(result);
return true;
}
return false;
@@ -175,15 +181,15 @@ class HttpCache::WorkItem {
WorkItemOperation operation() { return operation_; }
void ClearTransaction() { trans_ = NULL; }
void ClearEntry() { entry_ = NULL; }
- void ClearCallback() { callback_ = NULL; }
+ void ClearCallback() { callback_.Reset(); }
bool Matches(Transaction* trans) const { return trans == trans_; }
- bool IsValid() const { return trans_ || entry_ || callback_; }
+ bool IsValid() const { return trans_ || entry_ || !callback_.is_null(); }
private:
WorkItemOperation operation_;
Transaction* trans_;
ActiveEntry** entry_;
- OldCompletionCallback* callback_; // User callback.
+ net::CompletionCallback callback_; // User callback.
disk_cache::Backend** backend_;
};
@@ -279,7 +285,9 @@ void HttpCache::MetadataWriter::VerifyResponse(int result) {
if (response_info->response_time != expected_response_time_)
return SelfDestroy();
- result = transaction_->WriteMetadata(buf_, buf_len_, &callback_);
+ result = transaction_->WriteMetadata(
+ buf_, buf_len_,
+ base::Bind(&MetadataWriter::OnIOComplete, base::Unretained(this)));
if (result != ERR_IO_PENDING)
SelfDestroy();
}
@@ -422,8 +430,8 @@ HttpCache::~HttpCache() {
}
int HttpCache::GetBackend(disk_cache::Backend** backend,
- OldCompletionCallback* callback) {
- DCHECK(callback != NULL);
+ const net::CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
if (disk_cache_.get()) {
*backend = disk_cache_.get();
@@ -452,8 +460,10 @@ void HttpCache::WriteMetadata(const GURL& url,
return;
// Do lazy initialization of disk cache if needed.
- if (!disk_cache_.get())
- CreateBackend(NULL, NULL); // We don't care about the result.
+ if (!disk_cache_.get()) {
+ // We don't care about the result.
+ CreateBackend(NULL, net::CompletionCallback());
+ }
HttpCache::Transaction* trans = new HttpCache::Transaction(this);
MetadataWriter* writer = new MetadataWriter(trans);
@@ -492,8 +502,10 @@ void HttpCache::OnExternalCacheHit(const GURL& url,
int HttpCache::CreateTransaction(scoped_ptr<HttpTransaction>* trans) {
// Do lazy initialization of disk cache if needed.
- if (!disk_cache_.get())
- CreateBackend(NULL, NULL); // We don't care about the result.
+ if (!disk_cache_.get()) {
+ // We don't care about the result.
+ CreateBackend(NULL, net::CompletionCallback());
+ }
trans->reset(new HttpCache::Transaction(this));
return OK;
@@ -512,7 +524,7 @@ HttpNetworkSession* HttpCache::GetSession() {
//-----------------------------------------------------------------------------
int HttpCache::CreateBackend(disk_cache::Backend** backend,
- OldCompletionCallback* callback) {
+ const net::CompletionCallback& callback) {
if (!backend_factory_.get())
return ERR_FAILED;
@@ -525,7 +537,7 @@ int HttpCache::CreateBackend(disk_cache::Backend** backend,
// entry, so we use an empty key for it.
PendingOp* pending_op = GetPendingOp("");
if (pending_op->writer) {
- if (callback)
+ if (!callback.is_null())
pending_op->pending_queue.push_back(item.release());
return ERR_IO_PENDING;
}
@@ -536,8 +548,9 @@ int HttpCache::CreateBackend(disk_cache::Backend** backend,
BackendCallback* my_callback = new BackendCallback(this, pending_op);
pending_op->callback = my_callback;
- int rv = backend_factory_->CreateBackend(net_log_, &pending_op->backend,
- my_callback);
+ int rv = backend_factory_->CreateBackend(
+ net_log_, &pending_op->backend,
+ base::Bind(&net::OldCompletionCallbackAdapter, my_callback));
if (rv != ERR_IO_PENDING) {
pending_op->writer->ClearCallback();
my_callback->Run(rv);
@@ -553,7 +566,8 @@ int HttpCache::GetBackendForTransaction(Transaction* trans) {
if (!building_backend_)
return ERR_FAILED;
- WorkItem* item = new WorkItem(WI_CREATE_BACKEND, trans, NULL, NULL);
+ WorkItem* item = new WorkItem(
+ WI_CREATE_BACKEND, trans, net::CompletionCallback(), NULL);
PendingOp* pending_op = GetPendingOp("");
DCHECK(pending_op->writer);
pending_op->pending_queue.push_back(item);
@@ -762,7 +776,9 @@ int HttpCache::OpenEntry(const std::string& key, ActiveEntry** entry,
BackendCallback* my_callback = new BackendCallback(this, pending_op);
pending_op->callback = my_callback;
- int rv = disk_cache_->OpenEntry(key, &(pending_op->disk_entry), my_callback);
+ int rv = disk_cache_->OpenEntry(
+ key, &(pending_op->disk_entry),
+ base::Bind(&net::OldCompletionCallbackAdapter, my_callback));
if (rv != ERR_IO_PENDING) {
item->ClearTransaction();
my_callback->Run(rv);
@@ -788,8 +804,9 @@ int HttpCache::CreateEntry(const std::string& key, ActiveEntry** entry,
BackendCallback* my_callback = new BackendCallback(this, pending_op);
pending_op->callback = my_callback;
- int rv = disk_cache_->CreateEntry(key, &(pending_op->disk_entry),
- my_callback);
+ int rv = disk_cache_->CreateEntry(
+ key, &(pending_op->disk_entry),
+ base::Bind(&net::OldCompletionCallbackAdapter, my_callback));
if (rv != ERR_IO_PENDING) {
item->ClearTransaction();
my_callback->Run(rv);
@@ -1018,7 +1035,7 @@ void HttpCache::OnProcessPendingQueue(ActiveEntry* entry) {
entry->will_process_pending_queue = false;
DCHECK(!entry->writer);
- // If no one is interested in this entry, then we can de-activate it.
+ // If no one is interested in this entry, then we can deactivate it.
if (entry->pending_queue.empty()) {
if (entry->readers.empty())
DestroyEntry(entry);
@@ -1104,7 +1121,7 @@ void HttpCache::OnIOComplete(int result, PendingOp* pending_op) {
if (item->operation() == WI_CREATE_ENTRY) {
if (result == OK) {
- // A second Create request, but the first request succeded.
+ // A second Create request, but the first request succeeded.
item->NotifyTransaction(ERR_CACHE_CREATE_FAILURE, NULL);
} else {
if (op != WI_CREATE_ENTRY) {
diff --git a/net/http/http_cache.h b/net/http/http_cache.h
index 00d5b98..9d1b9aa 100644
--- a/net/http/http_cache.h
+++ b/net/http/http_cache.h
@@ -89,7 +89,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory,
// |callback| because the object can be deleted from within the callback.
virtual int CreateBackend(NetLog* net_log,
disk_cache::Backend** backend,
- OldCompletionCallback* callback) = 0;
+ const CompletionCallback& callback) = 0;
};
// A default backend factory for the common use cases.
@@ -108,7 +108,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory,
// BackendFactory implementation.
virtual int CreateBackend(NetLog* net_log,
disk_cache::Backend** backend,
- OldCompletionCallback* callback) OVERRIDE;
+ const CompletionCallback& callback) OVERRIDE;
private:
CacheType type_;
@@ -156,7 +156,8 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory,
// a network error code, and it could be ERR_IO_PENDING, in which case the
// |callback| will be notified when the operation completes. The pointer that
// receives the |backend| must remain valid until the operation completes.
- int GetBackend(disk_cache::Backend** backend, OldCompletionCallback* callback);
+ int GetBackend(disk_cache::Backend** backend,
+ const net::CompletionCallback& callback);
// Returns the current backend (can be NULL).
disk_cache::Backend* GetCurrentBackend() const;
@@ -242,7 +243,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory,
// Creates the |backend| object and notifies the |callback| when the operation
// completes. Returns an error code.
int CreateBackend(disk_cache::Backend** backend,
- OldCompletionCallback* callback);
+ const net::CompletionCallback& callback);
// Makes sure that the backend creation is complete before allowing the
// provided transaction to use the object. Returns an error code. |trans|
diff --git a/net/http/http_cache_transaction.cc b/net/http/http_cache_transaction.cc
index a17e890..13ce79c 100644
--- a/net/http/http_cache_transaction.cc
+++ b/net/http/http_cache_transaction.cc
@@ -12,6 +12,7 @@
#include <string>
+#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/field_trial.h"
@@ -19,6 +20,7 @@
#include "base/string_util.h"
#include "base/time.h"
#include "net/base/cert_status_flags.h"
+#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
@@ -167,10 +169,10 @@ HttpCache::Transaction::~Transaction() {
}
int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
- OldCompletionCallback* callback) {
+ const CompletionCallback& callback) {
DCHECK(buf);
DCHECK_GT(buf_len, 0);
- DCHECK(callback);
+ DCHECK(!callback.is_null());
if (!cache_ || !entry_)
return ERR_UNEXPECTED;
@@ -1111,7 +1113,9 @@ int HttpCache::Transaction::DoTruncateCachedData() {
net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA, NULL);
// Truncate the stream.
- return WriteToEntry(kResponseContentIndex, 0, NULL, 0, cache_callback_);
+ return WriteToEntry(
+ kResponseContentIndex, 0, NULL, 0,
+ base::Bind(&net::OldCompletionCallbackAdapter, cache_callback_));
}
int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
@@ -1134,7 +1138,9 @@ int HttpCache::Transaction::DoTruncateCachedMetadata() {
if (net_log_.IsLoggingAllEvents())
net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO, NULL);
- return WriteToEntry(kMetadataIndex, 0, NULL, 0, cache_callback_);
+ return WriteToEntry(
+ kMetadataIndex, 0, NULL, 0,
+ base::Bind(&net::OldCompletionCallbackAdapter, cache_callback_));
}
int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
@@ -1186,8 +1192,9 @@ int HttpCache::Transaction::DoCacheReadResponse() {
net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO, NULL);
cache_callback_->AddRef(); // Balanced in DoCacheReadResponseComplete.
- return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_,
- io_buf_len_, cache_callback_);
+ return entry_->disk_entry->ReadData(
+ kResponseInfoIndex, 0, read_buf_, io_buf_len_,
+ base::Bind(&net::OldCompletionCallbackAdapter, cache_callback_));
}
int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
@@ -1278,9 +1285,9 @@ int HttpCache::Transaction::DoCacheReadMetadata() {
net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO, NULL);
cache_callback_->AddRef(); // Balanced in DoCacheReadMetadataComplete.
- return entry_->disk_entry->ReadData(kMetadataIndex, 0, response_.metadata,
- response_.metadata->size(),
- cache_callback_);
+ return entry_->disk_entry->ReadData(
+ kMetadataIndex, 0, response_.metadata, response_.metadata->size(),
+ base::Bind(&net::OldCompletionCallbackAdapter, cache_callback_));
}
int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
@@ -1299,7 +1306,8 @@ int HttpCache::Transaction::DoCacheQueryData() {
// Balanced in DoCacheQueryDataComplete.
cache_callback_->AddRef();
- return entry_->disk_entry->ReadyForSparseIO(cache_callback_);
+ return entry_->disk_entry->ReadyForSparseIO(
+ base::Bind(&net::OldCompletionCallbackAdapter, cache_callback_));
}
int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
@@ -1320,12 +1328,14 @@ int HttpCache::Transaction::DoCacheReadData() {
if (net_log_.IsLoggingAllEvents())
net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA, NULL);
if (partial_.get()) {
- return partial_->CacheRead(entry_->disk_entry, read_buf_, io_buf_len_,
- cache_callback_);
+ return partial_->CacheRead(
+ entry_->disk_entry, read_buf_, io_buf_len_,
+ base::Bind(&net::OldCompletionCallbackAdapter, cache_callback_));
}
- return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
- read_buf_, io_buf_len_, cache_callback_);
+ return entry_->disk_entry->ReadData(
+ kResponseContentIndex, read_offset_, read_buf_, io_buf_len_,
+ base::Bind(&net::OldCompletionCallbackAdapter, cache_callback_));
}
int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
@@ -1357,7 +1367,9 @@ int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA, NULL);
cache_callback_->AddRef(); // Balanced in DoCacheWriteDataComplete.
- return AppendResponseDataToEntry(read_buf_, num_bytes, cache_callback_);
+ return AppendResponseDataToEntry(
+ read_buf_, num_bytes,
+ base::Bind(&net::OldCompletionCallbackAdapter, cache_callback_));
}
int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
@@ -1932,7 +1944,7 @@ int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
int HttpCache::Transaction::WriteToEntry(int index, int offset,
IOBuffer* data, int data_len,
- OldCompletionCallback* callback) {
+ const CompletionCallback& callback) {
if (!entry_)
return data_len;
@@ -1983,12 +1995,14 @@ int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
// destructor of this object so cache_callback_ may be currently in use.
write_headers_callback_->AddRef();
io_buf_len_ = data->pickle()->size();
- return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data, io_buf_len_,
- write_headers_callback_, true);
+ return entry_->disk_entry->WriteData(
+ kResponseInfoIndex, 0, data, io_buf_len_,
+ base::Bind(&net::OldCompletionCallbackAdapter, write_headers_callback_),
+ true);
}
int HttpCache::Transaction::AppendResponseDataToEntry(
- IOBuffer* data, int data_len, OldCompletionCallback* callback) {
+ IOBuffer* data, int data_len, const CompletionCallback& callback) {
if (!entry_ || !data_len)
return data_len;
diff --git a/net/http/http_cache_transaction.h b/net/http/http_cache_transaction.h
index b6c11c7..f60a445 100644
--- a/net/http/http_cache_transaction.h
+++ b/net/http/http_cache_transaction.h
@@ -68,7 +68,7 @@ class HttpCache::Transaction : public HttpTransaction {
// Returns the number of bytes actually written, or a net error code. If the
// operation cannot complete immediately, returns ERR_IO_PENDING, grabs a
// reference to the buffer (until completion), and notifies the caller using
- // the provided |callback| when the operatiopn finishes.
+ // the provided |callback| when the operation finishes.
//
// The first time this method is called for a given transaction, previous
// meta-data will be overwritten with the provided data, and subsequent
@@ -80,7 +80,7 @@ class HttpCache::Transaction : public HttpTransaction {
// method.
int WriteMetadata(IOBuffer* buf,
int buf_len,
- OldCompletionCallback* callback);
+ const CompletionCallback& callback);
// This transaction is being deleted and we are not done writing to the cache.
// We need to indicate that the response data was truncated. Returns true on
@@ -291,7 +291,7 @@ class HttpCache::Transaction : public HttpTransaction {
// cache entry is destroyed. Future calls to this function will just do
// nothing without side-effect. Returns a network error code.
int WriteToEntry(int index, int offset, IOBuffer* data, int data_len,
- OldCompletionCallback* callback);
+ const CompletionCallback& callback);
// Called to write response_ to the cache entry. |truncated| indicates if the
// entry should be marked as incomplete.
@@ -300,7 +300,7 @@ class HttpCache::Transaction : public HttpTransaction {
// Called to append response data to the cache entry. Returns a network error
// code.
int AppendResponseDataToEntry(IOBuffer* data, int data_len,
- OldCompletionCallback* callback);
+ const CompletionCallback& callback);
// Called when we are done writing to the cache entry.
void DoneWritingToEntry(bool success);
diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc
index 0c0ff6f..2ffa866 100644
--- a/net/http/http_cache_unittest.cc
+++ b/net/http/http_cache_unittest.cc
@@ -5,6 +5,7 @@
#include "net/http/http_cache.h"
#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/memory/scoped_vector.h"
#include "base/message_loop.h"
#include "base/string_util.h"
@@ -32,18 +33,27 @@ using base::Time;
namespace {
-class DeleteCacheOldCompletionCallback : public TestOldCompletionCallback {
+class DeleteCacheCompletionCallback : public TestCompletionCallbackBase {
public:
- explicit DeleteCacheOldCompletionCallback(MockHttpCache* cache)
- : cache_(cache) {}
+ explicit DeleteCacheCompletionCallback(MockHttpCache* cache)
+ : cache_(cache),
+ ALLOW_THIS_IN_INITIALIZER_LIST(callback_(
+ base::Bind(&DeleteCacheCompletionCallback::OnComplete,
+ base::Unretained(this)))) {
+ }
+
+ const net::CompletionCallback& callback() const { return callback_; }
- virtual void RunWithParams(const Tuple1<int>& params) {
+ private:
+ void OnComplete(int result) {
delete cache_;
- TestOldCompletionCallback::RunWithParams(params);
+ SetResult(result);
}
- private:
MockHttpCache* cache_;
+ net::CompletionCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback);
};
//-----------------------------------------------------------------------------
@@ -344,8 +354,8 @@ void CreateTruncatedEntry(std::string raw_headers, MockHttpCache* cache) {
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100));
int len = static_cast<int>(base::strlcpy(buf->data(),
"rg: 00-09 rg: 10-19 ", 100));
- TestOldCompletionCallback cb;
- int rv = entry->WriteData(1, 0, buf, len, &cb, true);
+ net::TestCompletionCallback cb;
+ int rv = entry->WriteData(1, 0, buf, len, cb.callback(), true);
EXPECT_EQ(len, cb.GetResult(rv));
entry->Close();
}
@@ -395,9 +405,9 @@ TEST(HttpCache, GetBackend) {
MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0));
disk_cache::Backend* backend;
- TestOldCompletionCallback cb;
+ net::TestCompletionCallback cb;
// This will lazily initialize the backend.
- int rv = cache.http_cache()->GetBackend(&backend, &cb);
+ int rv = cache.http_cache()->GetBackend(&backend, cb.callback());
EXPECT_EQ(net::OK, cb.GetResult(rv));
}
@@ -1449,14 +1459,14 @@ TEST(HttpCache, DeleteCacheWaitingForBackend) {
// We cannot call FinishCreation because the factory itself will go away with
// the cache, so grab the callback and attempt to use it.
- net::OldCompletionCallback* callback = factory->callback();
+ net::CompletionCallback callback = factory->callback();
disk_cache::Backend** backend = factory->backend();
cache.reset();
MessageLoop::current()->RunAllPending();
*backend = NULL;
- callback->Run(net::ERR_ABORTED);
+ callback.Run(net::ERR_ABORTED);
}
// Tests that we can delete the cache while creating the backend, from within
@@ -1465,9 +1475,9 @@ TEST(HttpCache, DeleteCacheWaitingForBackend2) {
MockBlockingBackendFactory* factory = new MockBlockingBackendFactory();
MockHttpCache* cache = new MockHttpCache(factory);
- DeleteCacheOldCompletionCallback cb(cache);
+ DeleteCacheCompletionCallback cb(cache);
disk_cache::Backend* backend;
- int rv = cache->http_cache()->GetBackend(&backend, &cb);
+ int rv = cache->http_cache()->GetBackend(&backend, cb.callback());
EXPECT_EQ(net::ERR_IO_PENDING, rv);
// Now let's queue a regular transaction
@@ -1480,8 +1490,8 @@ TEST(HttpCache, DeleteCacheWaitingForBackend2) {
c->trans->Start(&request, &c->callback, net::BoundNetLog());
// And another direct backend request.
- TestOldCompletionCallback cb2;
- rv = cache->http_cache()->GetBackend(&backend, &cb2);
+ net::TestCompletionCallback cb2;
+ rv = cache->http_cache()->GetBackend(&backend, cb2.callback());
EXPECT_EQ(net::ERR_IO_PENDING, rv);
// Just to make sure that everything is still pending.
@@ -2814,8 +2824,8 @@ TEST(HttpCache, GET_Previous206_NotSparse) {
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500));
int len = static_cast<int>(base::strlcpy(buf->data(),
kRangeGET_TransactionOK.data, 500));
- TestOldCompletionCallback cb;
- int rv = entry->WriteData(1, 0, buf, len, &cb, true);
+ net::TestCompletionCallback cb;
+ int rv = entry->WriteData(1, 0, buf, len, cb.callback(), true);
EXPECT_EQ(len, cb.GetResult(rv));
entry->Close();
@@ -2858,8 +2868,8 @@ TEST(HttpCache, RangeGET_Previous206_NotSparse_2) {
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500));
int len = static_cast<int>(base::strlcpy(buf->data(),
kRangeGET_TransactionOK.data, 500));
- TestOldCompletionCallback cb;
- int rv = entry->WriteData(1, 0, buf, len, &cb, true);
+ net::TestCompletionCallback cb;
+ int rv = entry->WriteData(1, 0, buf, len, cb.callback(), true);
EXPECT_EQ(len, cb.GetResult(rv));
entry->Close();
@@ -2900,8 +2910,8 @@ TEST(HttpCache, GET_Previous206_NotValidation) {
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500));
int len = static_cast<int>(base::strlcpy(buf->data(),
kRangeGET_TransactionOK.data, 500));
- TestOldCompletionCallback cb;
- int rv = entry->WriteData(1, 0, buf, len, &cb, true);
+ net::TestCompletionCallback cb;
+ int rv = entry->WriteData(1, 0, buf, len, cb.callback(), true);
EXPECT_EQ(len, cb.GetResult(rv));
entry->Close();
@@ -3298,8 +3308,8 @@ TEST(HttpCache, RangeGET_InvalidResponse3) {
ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en));
int64 cached_start = 0;
- TestOldCompletionCallback cb;
- int rv = en->GetAvailableRange(40, 20, &cached_start, &cb);
+ net::TestCompletionCallback cb;
+ int rv = en->GetAvailableRange(40, 20, &cached_start, cb.callback());
EXPECT_EQ(10, cb.GetResult(rv));
EXPECT_EQ(50, cached_start);
en->Close();
@@ -4616,7 +4626,7 @@ TEST(HttpCache, StopCachingTruncatedEntry) {
RemoveMockTransaction(&kRangeGET_TransactionOK);
}
-// Tests that we detect truncated rersources from the net when there is
+// Tests that we detect truncated resources from the net when there is
// a Content-Length header.
TEST(HttpCache, TruncatedByContentLength) {
MockHttpCache cache;
diff --git a/net/http/mock_http_cache.cc b/net/http/mock_http_cache.cc
index 5145e66..ed8976f 100644
--- a/net/http/mock_http_cache.cc
+++ b/net/http/mock_http_cache.cc
@@ -46,7 +46,7 @@ void CallbackForwader(const net::CompletionCallback& callback, int result) {
struct MockDiskEntry::CallbackInfo {
scoped_refptr<MockDiskEntry> entry;
- net::OldCompletionCallback* callback;
+ net::CompletionCallback callback;
int result;
};
@@ -88,10 +88,11 @@ int32 MockDiskEntry::GetDataSize(int index) const {
return static_cast<int32>(data_[index].size());
}
-int MockDiskEntry::ReadData(int index, int offset, net::IOBuffer* buf,
- int buf_len, net::OldCompletionCallback* callback) {
+int MockDiskEntry::ReadData(
+ int index, int offset, net::IOBuffer* buf, int buf_len,
+ const net::CompletionCallback& callback) {
DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
- DCHECK(callback);
+ DCHECK(!callback.is_null());
if (fail_requests_)
return net::ERR_CACHE_READ_FAILURE;
@@ -111,11 +112,11 @@ int MockDiskEntry::ReadData(int index, int offset, net::IOBuffer* buf,
return net::ERR_IO_PENDING;
}
-int MockDiskEntry::WriteData(int index, int offset, net::IOBuffer* buf,
- int buf_len, net::OldCompletionCallback* callback,
- bool truncate) {
+int MockDiskEntry::WriteData(
+ int index, int offset, net::IOBuffer* buf, int buf_len,
+ const net::CompletionCallback& callback, bool truncate) {
DCHECK(index >= 0 && index < kNumCacheEntryDataIndices);
- DCHECK(callback);
+ DCHECK(!callback.is_null());
DCHECK(truncate);
if (fail_requests_) {
@@ -138,8 +139,8 @@ int MockDiskEntry::WriteData(int index, int offset, net::IOBuffer* buf,
}
int MockDiskEntry::ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len,
- net::OldCompletionCallback* callback) {
- DCHECK(callback);
+ const net::CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
if (!sparse_ || busy_)
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
if (offset < 0)
@@ -168,8 +169,8 @@ int MockDiskEntry::ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len,
int MockDiskEntry::WriteSparseData(int64 offset, net::IOBuffer* buf,
int buf_len,
- net::OldCompletionCallback* callback) {
- DCHECK(callback);
+ const net::CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
if (busy_)
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
if (!sparse_) {
@@ -200,8 +201,8 @@ int MockDiskEntry::WriteSparseData(int64 offset, net::IOBuffer* buf,
}
int MockDiskEntry::GetAvailableRange(int64 offset, int len, int64* start,
- net::OldCompletionCallback* callback) {
- DCHECK(callback);
+ const net::CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
if (!sparse_ || busy_)
return net::ERR_CACHE_OPERATION_NOT_SUPPORTED;
if (offset < 0)
@@ -245,16 +246,16 @@ void MockDiskEntry::CancelSparseIO() {
cancel_ = true;
}
-int MockDiskEntry::ReadyForSparseIO(net::OldCompletionCallback* callback) {
+int MockDiskEntry::ReadyForSparseIO(const net::CompletionCallback& callback) {
if (!cancel_)
return net::OK;
cancel_ = false;
- DCHECK(callback);
+ DCHECK(!callback.is_null());
if (MockHttpCache::GetTestMode(test_mode_) & TEST_MODE_SYNC_CACHE_READ)
return net::OK;
- // The pending operation is already in the message loop (and hopefuly
+ // The pending operation is already in the message loop (and hopefully
// already in the second pass). Just notify the caller that it finished.
CallbackLater(callback, 0);
return net::ERR_IO_PENDING;
@@ -269,7 +270,7 @@ void MockDiskEntry::IgnoreCallbacks(bool value) {
return;
ignore_callbacks_ = value;
if (!value)
- StoreAndDeliverCallbacks(false, NULL, NULL, 0);
+ StoreAndDeliverCallbacks(false, NULL, net::CompletionCallback(), 0);
}
MockDiskEntry::~MockDiskEntry() {
@@ -278,7 +279,7 @@ MockDiskEntry::~MockDiskEntry() {
// Unlike the callbacks for MockHttpTransaction, we want this one to run even
// if the consumer called Close on the MockDiskEntry. We achieve that by
// leveraging the fact that this class is reference counted.
-void MockDiskEntry::CallbackLater(net::OldCompletionCallback* callback,
+void MockDiskEntry::CallbackLater(const net::CompletionCallback& callback,
int result) {
if (ignore_callbacks_)
return StoreAndDeliverCallbacks(true, this, callback, result);
@@ -286,7 +287,8 @@ void MockDiskEntry::CallbackLater(net::OldCompletionCallback* callback,
&MockDiskEntry::RunCallback, this, callback, result));
}
-void MockDiskEntry::RunCallback(net::OldCompletionCallback* callback, int result) {
+void MockDiskEntry::RunCallback(
+ const net::CompletionCallback& callback, int result) {
if (busy_) {
// This is kind of hacky, but controlling the behavior of just this entry
// from a test is sort of complicated. What we really want to do is
@@ -295,22 +297,22 @@ void MockDiskEntry::RunCallback(net::OldCompletionCallback* callback, int result
// this operation (already posted to the message loop)... and without
// just delaying for n mS (which may cause trouble with slow bots). So
// we re-post this operation (all async sparse IO operations will take two
- // trips trhough the message loop instead of one).
+ // trips through the message loop instead of one).
if (!delayed_) {
delayed_ = true;
return CallbackLater(callback, result);
}
}
busy_ = false;
- callback->Run(result);
+ callback.Run(result);
}
// When |store| is true, stores the callback to be delivered later; otherwise
// delivers any callback previously stored.
// Static.
-void MockDiskEntry::StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry,
- net::OldCompletionCallback* callback,
- int result) {
+void MockDiskEntry::StoreAndDeliverCallbacks(
+ bool store, MockDiskEntry* entry, const net::CompletionCallback& callback,
+ int result) {
static std::vector<CallbackInfo> callback_list;
if (store) {
CallbackInfo c = {entry, callback, result};
@@ -344,8 +346,8 @@ int32 MockDiskCache::GetEntryCount() const {
}
int MockDiskCache::OpenEntry(const std::string& key, disk_cache::Entry** entry,
- net::OldCompletionCallback* callback) {
- DCHECK(callback);
+ const net::CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
if (fail_requests_)
return net::ERR_CACHE_OPEN_FAILURE;
@@ -370,15 +372,14 @@ int MockDiskCache::OpenEntry(const std::string& key, disk_cache::Entry** entry,
if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
return net::OK;
- CallbackLater(
- base::Bind(&net::OldCompletionCallbackAdapter, callback), net::OK);
+ CallbackLater(callback, net::OK);
return net::ERR_IO_PENDING;
}
int MockDiskCache::CreateEntry(const std::string& key,
disk_cache::Entry** entry,
- net::OldCompletionCallback* callback) {
- DCHECK(callback);
+ const net::CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
if (fail_requests_)
return net::ERR_CACHE_CREATE_FAILURE;
@@ -410,8 +411,7 @@ int MockDiskCache::CreateEntry(const std::string& key,
if (GetTestModeForEntry(key) & TEST_MODE_SYNC_CACHE_START)
return net::OK;
- CallbackLater(
- base::Bind(&net::OldCompletionCallbackAdapter, callback), net::OK);
+ CallbackLater(callback, net::OK);
return net::ERR_IO_PENDING;
}
@@ -442,7 +442,7 @@ int MockDiskCache::DoomEntriesBetween(const base::Time initial_time,
}
int MockDiskCache::DoomEntriesSince(const base::Time initial_time,
- net::OldCompletionCallback* callback) {
+ const net::CompletionCallback& callback) {
return net::ERR_NOT_IMPLEMENTED;
}
@@ -478,7 +478,7 @@ void MockDiskCache::CallbackLater(const net::CompletionCallback& callback,
int MockBackendFactory::CreateBackend(net::NetLog* net_log,
disk_cache::Backend** backend,
- net::OldCompletionCallback* callback) {
+ const net::CompletionCallback& callback) {
*backend = new MockDiskCache();
return net::OK;
}
@@ -494,9 +494,9 @@ MockHttpCache::MockHttpCache(net::HttpCache::BackendFactory* disk_cache_factory)
}
MockDiskCache* MockHttpCache::disk_cache() {
- TestOldCompletionCallback cb;
+ net::TestCompletionCallback cb;
disk_cache::Backend* backend;
- int rv = http_cache_.GetBackend(&backend, &cb);
+ int rv = http_cache_.GetBackend(&backend, cb.callback());
rv = cb.GetResult(rv);
return (rv == net::OK) ? static_cast<MockDiskCache*>(backend) : NULL;
}
@@ -506,9 +506,9 @@ bool MockHttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry,
bool* response_truncated) {
int size = disk_entry->GetDataSize(0);
- TestOldCompletionCallback cb;
+ net::TestCompletionCallback cb;
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(size));
- int rv = disk_entry->ReadData(0, 0, buffer, size, &cb);
+ int rv = disk_entry->ReadData(0, 0, buffer, size, cb.callback());
rv = cb.GetResult(rv);
EXPECT_EQ(size, rv);
@@ -524,28 +524,28 @@ bool MockHttpCache::WriteResponseInfo(
response_info->Persist(
&pickle, skip_transient_headers, response_truncated);
- TestOldCompletionCallback cb;
+ net::TestCompletionCallback cb;
scoped_refptr<net::WrappedIOBuffer> data(new net::WrappedIOBuffer(
reinterpret_cast<const char*>(pickle.data())));
int len = static_cast<int>(pickle.size());
- int rv = disk_entry->WriteData(0, 0, data, len, &cb, true);
+ int rv = disk_entry->WriteData(0, 0, data, len, cb.callback(), true);
rv = cb.GetResult(rv);
return (rv == len);
}
bool MockHttpCache::OpenBackendEntry(const std::string& key,
disk_cache::Entry** entry) {
- TestOldCompletionCallback cb;
- int rv = disk_cache()->OpenEntry(key, entry, &cb);
+ net::TestCompletionCallback cb;
+ int rv = disk_cache()->OpenEntry(key, entry, cb.callback());
return (cb.GetResult(rv) == net::OK);
}
bool MockHttpCache::CreateBackendEntry(const std::string& key,
disk_cache::Entry** entry,
net::NetLog* net_log) {
- TestOldCompletionCallback cb;
- int rv = disk_cache()->CreateEntry(key, entry, &cb);
+ net::TestCompletionCallback cb;
+ int rv = disk_cache()->CreateEntry(key, entry, cb.callback());
return (cb.GetResult(rv) == net::OK);
}
@@ -566,15 +566,15 @@ void MockHttpCache::SetTestMode(int test_mode) {
int MockDiskCacheNoCB::CreateEntry(const std::string& key,
disk_cache::Entry** entry,
- net::OldCompletionCallback* callback) {
+ const net::CompletionCallback& callback) {
return net::ERR_IO_PENDING;
}
//-----------------------------------------------------------------------------
-int MockBackendNoCbFactory::CreateBackend(net::NetLog* net_log,
- disk_cache::Backend** backend,
- net::OldCompletionCallback* callback) {
+int MockBackendNoCbFactory::CreateBackend(
+ net::NetLog* net_log, disk_cache::Backend** backend,
+ const net::CompletionCallback& callback) {
*backend = new MockDiskCacheNoCB();
return net::OK;
}
@@ -582,12 +582,17 @@ int MockBackendNoCbFactory::CreateBackend(net::NetLog* net_log,
//-----------------------------------------------------------------------------
MockBlockingBackendFactory::MockBlockingBackendFactory()
- : backend_(NULL), callback_(NULL), block_(true), fail_(false) {
+ : backend_(NULL),
+ block_(true),
+ fail_(false) {
+}
+
+MockBlockingBackendFactory::~MockBlockingBackendFactory() {
}
int MockBlockingBackendFactory::CreateBackend(
net::NetLog* net_log, disk_cache::Backend** backend,
- net::OldCompletionCallback* callback) {
+ const net::CompletionCallback& callback) {
if (!block_) {
if (!fail_)
*backend = new MockDiskCache();
@@ -601,11 +606,11 @@ int MockBlockingBackendFactory::CreateBackend(
void MockBlockingBackendFactory::FinishCreation() {
block_ = false;
- if (callback_) {
+ if (!callback_.is_null()) {
if (!fail_)
*backend_ = new MockDiskCache();
- net::OldCompletionCallback* cb = callback_;
- callback_ = NULL;
- cb->Run(Result()); // This object can be deleted here.
+ net::CompletionCallback cb = callback_;
+ callback_.Reset();
+ cb.Run(Result()); // This object can be deleted here.
}
}
diff --git a/net/http/mock_http_cache.h b/net/http/mock_http_cache.h
index d934e49..8cddc05 100644
--- a/net/http/mock_http_cache.h
+++ b/net/http/mock_http_cache.h
@@ -34,20 +34,22 @@ class MockDiskEntry : public disk_cache::Entry,
virtual base::Time GetLastModified() const OVERRIDE;
virtual int32 GetDataSize(int index) const OVERRIDE;
virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len,
- net::OldCompletionCallback* callback) OVERRIDE;
+ const net::CompletionCallback& callback) OVERRIDE;
virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len,
- net::OldCompletionCallback* callback,
+ const net::CompletionCallback& callback,
bool truncate) OVERRIDE;
virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len,
- net::OldCompletionCallback* callback) OVERRIDE;
- virtual int WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len,
- net::OldCompletionCallback* callback) OVERRIDE;
- virtual int GetAvailableRange(int64 offset, int len, int64* start,
- net::OldCompletionCallback* callback) OVERRIDE;
+ const net::CompletionCallback& callback) OVERRIDE;
+ virtual int WriteSparseData(
+ int64 offset, net::IOBuffer* buf, int buf_len,
+ const net::CompletionCallback& callback) OVERRIDE;
+ virtual int GetAvailableRange(
+ int64 offset, int len, int64* start,
+ const net::CompletionCallback& callback) OVERRIDE;
virtual bool CouldBeSparse() const OVERRIDE;
virtual void CancelSparseIO() OVERRIDE;
virtual int ReadyForSparseIO(
- net::OldCompletionCallback* completion_callback) OVERRIDE;
+ const net::CompletionCallback& completion_callback) OVERRIDE;
// Fail most subsequent requests.
void set_fail_requests() { fail_requests_ = true; }
@@ -66,14 +68,14 @@ class MockDiskEntry : public disk_cache::Entry,
// Unlike the callbacks for MockHttpTransaction, we want this one to run even
// if the consumer called Close on the MockDiskEntry. We achieve that by
// leveraging the fact that this class is reference counted.
- void CallbackLater(net::OldCompletionCallback* callback, int result);
+ void CallbackLater(const net::CompletionCallback& callback, int result);
- void RunCallback(net::OldCompletionCallback* callback, int result);
+ void RunCallback(const net::CompletionCallback& callback, int result);
// When |store| is true, stores the callback to be delivered later; otherwise
// delivers any callback previously stored.
static void StoreAndDeliverCallbacks(bool store, MockDiskEntry* entry,
- net::OldCompletionCallback* callback,
+ const net::CompletionCallback& callback,
int result);
static const int kNumCacheEntryDataIndices = 3;
@@ -97,9 +99,9 @@ class MockDiskCache : public disk_cache::Backend {
virtual int32 GetEntryCount() const OVERRIDE;
virtual int OpenEntry(const std::string& key, disk_cache::Entry** entry,
- net::OldCompletionCallback* callback) OVERRIDE;
+ const net::CompletionCallback& callback) OVERRIDE;
virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry,
- net::OldCompletionCallback* callback) OVERRIDE;
+ const net::CompletionCallback& callback) OVERRIDE;
virtual int DoomEntry(const std::string& key,
const net::CompletionCallback& callback) OVERRIDE;
virtual int DoomAllEntries(const net::CompletionCallback& callback) OVERRIDE;
@@ -107,8 +109,9 @@ class MockDiskCache : public disk_cache::Backend {
const base::Time initial_time,
const base::Time end_time,
const net::CompletionCallback& callback) OVERRIDE;
- virtual int DoomEntriesSince(const base::Time initial_time,
- net::OldCompletionCallback* callback) OVERRIDE;
+ virtual int DoomEntriesSince(
+ const base::Time initial_time,
+ const net::CompletionCallback& callback) OVERRIDE;
virtual int OpenNextEntry(void** iter, disk_cache::Entry** next_entry,
const net::CompletionCallback& callback) OVERRIDE;
virtual void EndEnumeration(void** iter) OVERRIDE;
@@ -150,7 +153,7 @@ class MockBackendFactory : public net::HttpCache::BackendFactory {
public:
virtual int CreateBackend(net::NetLog* net_log,
disk_cache::Backend** backend,
- net::OldCompletionCallback* callback) OVERRIDE;
+ const net::CompletionCallback& callback) OVERRIDE;
};
class MockHttpCache {
@@ -197,24 +200,25 @@ class MockHttpCache {
// This version of the disk cache doesn't invoke CreateEntry callbacks.
class MockDiskCacheNoCB : public MockDiskCache {
virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry,
- net::OldCompletionCallback* callback) OVERRIDE;
+ const net::CompletionCallback& callback) OVERRIDE;
};
class MockBackendNoCbFactory : public net::HttpCache::BackendFactory {
public:
virtual int CreateBackend(net::NetLog* net_log,
disk_cache::Backend** backend,
- net::OldCompletionCallback* callback) OVERRIDE;
+ const net::CompletionCallback& callback) OVERRIDE;
};
// This backend factory allows us to control the backend instantiation.
class MockBlockingBackendFactory : public net::HttpCache::BackendFactory {
public:
MockBlockingBackendFactory();
+ virtual ~MockBlockingBackendFactory();
virtual int CreateBackend(net::NetLog* net_log,
disk_cache::Backend** backend,
- net::OldCompletionCallback* callback) OVERRIDE;
+ const net::CompletionCallback& callback) OVERRIDE;
// Completes the backend creation. Any blocked call will be notified via the
// provided callback.
@@ -223,13 +227,13 @@ class MockBlockingBackendFactory : public net::HttpCache::BackendFactory {
disk_cache::Backend** backend() { return backend_; }
void set_fail(bool fail) { fail_ = fail; }
- net::OldCompletionCallback* callback() { return callback_; }
+ const net::CompletionCallback& callback() { return callback_; }
private:
int Result() { return fail_ ? net::ERR_FAILED : net::OK; }
disk_cache::Backend** backend_;
- net::OldCompletionCallback* callback_;
+ net::CompletionCallback callback_;
bool block_;
bool fail_;
};
diff --git a/net/http/partial_data.cc b/net/http/partial_data.cc
index 290c0e7..4bdedac 100644
--- a/net/http/partial_data.cc
+++ b/net/http/partial_data.cc
@@ -4,6 +4,8 @@
#include "net/http/partial_data.h"
+#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/format_macros.h"
#include "base/logging.h"
#include "base/string_number_conversions.h"
@@ -66,13 +68,12 @@ class PartialData::Core {
PartialData* owner_;
int64 start_;
- net::OldCompletionCallbackImpl<Core> callback_;
+
DISALLOW_COPY_AND_ASSIGN(Core);
};
PartialData::Core::Core(PartialData* owner)
- : owner_(owner),
- ALLOW_THIS_IN_INITIALIZER_LIST(callback_(this, &Core::OnIOComplete)) {
+ : owner_(owner) {
DCHECK(!owner_->core_);
owner_->core_ = this;
}
@@ -89,7 +90,9 @@ void PartialData::Core::Cancel() {
int PartialData::Core::GetAvailableRange(disk_cache::Entry* entry, int64 offset,
int len, int64* start) {
- int rv = entry->GetAvailableRange(offset, len, &start_, &callback_);
+ int rv = entry->GetAvailableRange(
+ offset, len, &start_, base::Bind(&PartialData::Core::OnIOComplete,
+ base::Unretained(this)));
if (rv != net::ERR_IO_PENDING) {
// The callback will not be invoked. Lets cleanup.
*start = start_;
@@ -416,8 +419,9 @@ void PartialData::FixContentLength(HttpResponseHeaders* headers) {
resource_size_));
}
-int PartialData::CacheRead(disk_cache::Entry* entry, IOBuffer* data,
- int data_len, OldCompletionCallback* callback) {
+int PartialData::CacheRead(
+ disk_cache::Entry* entry, IOBuffer* data, int data_len,
+ const net::CompletionCallback& callback) {
int read_len = std::min(data_len, cached_min_len_);
if (!read_len)
return 0;
@@ -436,12 +440,13 @@ int PartialData::CacheRead(disk_cache::Entry* entry, IOBuffer* data,
return rv;
}
-int PartialData::CacheWrite(disk_cache::Entry* entry, IOBuffer* data,
- int data_len, OldCompletionCallback* callback) {
+int PartialData::CacheWrite(
+ disk_cache::Entry* entry, IOBuffer* data, int data_len,
+ const net::CompletionCallback& callback) {
DVLOG(3) << "To write: " << data_len;
if (sparse_entry_) {
- return entry->WriteSparseData(current_range_start_, data, data_len,
- callback);
+ return entry->WriteSparseData(
+ current_range_start_, data, data_len, callback);
} else {
if (current_range_start_ > kint32max)
return ERR_INVALID_ARGUMENT;
diff --git a/net/http/partial_data.h b/net/http/partial_data.h
index bf14cf0..b996b98 100644
--- a/net/http/partial_data.h
+++ b/net/http/partial_data.h
@@ -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.
@@ -98,12 +98,12 @@ class PartialData {
// operation completes, OnCacheReadCompleted() must be called with the result
// of the operation.
int CacheRead(disk_cache::Entry* entry, IOBuffer* data, int data_len,
- OldCompletionCallback* callback);
+ const net::CompletionCallback& callback);
// Writes |data_len| bytes to cache. This is basically a wrapper around the
// API of the cache that provides the right arguments for the current range.
int CacheWrite(disk_cache::Entry* entry, IOBuffer* data, int data_len,
- OldCompletionCallback* callback);
+ const net::CompletionCallback& callback);
// This method should be called when CacheRead() finishes the read, to update
// the internal state about the current range.