Chromium Code Reviews| Index: net/http/http_cache.cc |
| diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc |
| index 58fa5cb14e232fcb67b3329008c24b2c1d305bcb..082b1ecc9117055ccc4c7d421c7b4342fc8b2c07 100644 |
| --- a/net/http/http_cache.cc |
| +++ b/net/http/http_cache.cc |
| @@ -23,6 +23,7 @@ |
| #include "base/string_number_conversions.h" |
| #include "base/string_util.h" |
| #include "base/stringprintf.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" |
| @@ -143,11 +144,25 @@ 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), |
| + old_callback_(NULL), |
| backend_(NULL) {} |
| WorkItem(WorkItemOperation operation, Transaction* trans, |
| OldCompletionCallback* cb, disk_cache::Backend** backend) |
| - : operation_(operation), trans_(trans), entry_(NULL), callback_(cb), |
| + : operation_(operation), |
| + trans_(trans), |
| + entry_(NULL), |
| + old_callback_(cb), |
| + backend_(backend) {} |
|
csilv
2011/12/06 00:56:08
I don't see where this constructor is used, can yo
James Hawkins
2011/12/06 17:25:12
Done.
|
| + WorkItem(WorkItemOperation operation, Transaction* trans, |
| + const CompletionCallback& cb, disk_cache::Backend** backend) |
| + : operation_(operation), |
| + trans_(trans), |
| + entry_(NULL), |
| + callback_(cb), |
| + old_callback_(NULL), |
| backend_(backend) {} |
| ~WorkItem() {} |
| @@ -165,25 +180,36 @@ class HttpCache::WorkItem { |
| bool DoCallback(int result, disk_cache::Backend* backend) { |
| if (backend_) |
| *backend_ = backend; |
| - if (callback_) { |
| - callback_->Run(result); |
| + if (old_callback_) { |
| + old_callback_->Run(result); |
| + return true; |
| + } else if (!callback_.is_null()) { |
| + callback_.Run(result); |
| return true; |
| } |
| return false; |
| } |
| + void ClearCallback() { |
| + old_callback_ = NULL; |
| + callback_.Reset(); |
| + } |
| + |
| + bool IsValid() const { |
| + return trans_ || entry_ || old_callback_ || !callback_.is_null(); |
| + } |
| + |
| WorkItemOperation operation() { return operation_; } |
| void ClearTransaction() { trans_ = NULL; } |
| void ClearEntry() { entry_ = NULL; } |
| - void ClearCallback() { callback_ = NULL; } |
| bool Matches(Transaction* trans) const { return trans == trans_; } |
| - bool IsValid() const { return trans_ || entry_ || callback_; } |
| private: |
| WorkItemOperation operation_; |
| Transaction* trans_; |
| ActiveEntry** entry_; |
| - OldCompletionCallback* callback_; // User callback. |
| + CompletionCallback callback_; |
| + OldCompletionCallback* old_callback_; |
| disk_cache::Backend** backend_; |
| }; |
| @@ -422,8 +448,8 @@ HttpCache::~HttpCache() { |
| } |
| int HttpCache::GetBackend(disk_cache::Backend** backend, |
| - OldCompletionCallback* callback) { |
| - DCHECK(callback != NULL); |
| + const CompletionCallback& callback) { |
| + DCHECK(!callback.is_null()); |
| if (disk_cache_.get()) { |
| *backend = disk_cache_.get(); |
| @@ -452,8 +478,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, CompletionCallback()); |
| + } |
| HttpCache::Transaction* trans = new HttpCache::Transaction(this); |
| MetadataWriter* writer = new MetadataWriter(trans); |
| @@ -492,8 +520,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, CompletionCallback()); |
| + } |
| trans->reset(new HttpCache::Transaction(this)); |
| return OK; |
| @@ -512,7 +542,7 @@ HttpNetworkSession* HttpCache::GetSession() { |
| //----------------------------------------------------------------------------- |
| int HttpCache::CreateBackend(disk_cache::Backend** backend, |
| - OldCompletionCallback* callback) { |
| + const CompletionCallback& callback) { |
| if (!backend_factory_.get()) |
| return ERR_FAILED; |
| @@ -525,7 +555,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; |
| } |
| @@ -553,7 +583,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, |
| + CompletionCallback(), NULL); |
| PendingOp* pending_op = GetPendingOp(""); |
| DCHECK(pending_op->writer); |
| pending_op->pending_queue.push_back(item); |