summaryrefslogtreecommitdiffstats
path: root/net/disk_cache
diff options
context:
space:
mode:
Diffstat (limited to 'net/disk_cache')
-rw-r--r--net/disk_cache/backend_impl.cc84
-rw-r--r--net/disk_cache/backend_impl.h4
-rw-r--r--net/disk_cache/disk_cache.h2
-rw-r--r--net/disk_cache/disk_cache_test_base.cc8
-rw-r--r--net/disk_cache/entry_unittest.cc35
-rw-r--r--net/disk_cache/in_flight_backend_io.cc4
-rw-r--r--net/disk_cache/in_flight_backend_io.h7
-rw-r--r--net/disk_cache/mem_backend_impl.cc2
-rw-r--r--net/disk_cache/mem_backend_impl.h2
-rw-r--r--net/disk_cache/stress_cache.cc22
10 files changed, 88 insertions, 82 deletions
diff --git a/net/disk_cache/backend_impl.cc b/net/disk_cache/backend_impl.cc
index 2cc2f3a..847ecac 100644
--- a/net/disk_cache/backend_impl.cc
+++ b/net/disk_cache/backend_impl.cc
@@ -5,6 +5,7 @@
#include "net/disk_cache/backend_impl.h"
#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/message_loop.h"
@@ -183,7 +184,7 @@ bool InitExperiment(disk_cache::IndexHeader* header, uint32 mask) {
}
if (!header->create_time || !header->lru.filled)
- return true; // Wait untill we fill up the cache.
+ return true; // Wait until we fill up the cache.
int index_load = header->num_entries * 100 / (mask + 1);
if (index_load > 25) {
@@ -218,23 +219,29 @@ class CacheCreator {
base::MessageLoopProxy* thread, net::NetLog* net_log,
disk_cache::Backend** backend,
net::OldCompletionCallback* callback)
- : path_(path), force_(force), retry_(false), max_bytes_(max_bytes),
- type_(type), flags_(flags), thread_(thread), backend_(backend),
- callback_(callback), cache_(NULL), net_log_(net_log),
- ALLOW_THIS_IN_INITIALIZER_LIST(
- my_callback_(this, &CacheCreator::OnIOComplete)) {
+ : path_(path),
+ force_(force),
+ retry_(false),
+ max_bytes_(max_bytes),
+ type_(type),
+ flags_(flags),
+ thread_(thread),
+ backend_(backend),
+ callback_(callback),
+ cache_(NULL),
+ net_log_(net_log) {
}
~CacheCreator() {}
// Creates the backend.
int Run();
- // Callback implementation.
- void OnIOComplete(int result);
-
private:
void DoCallback(int result);
+ // Callback implementation.
+ void OnIOComplete(int result);
+
const FilePath& path_;
bool force_;
bool retry_;
@@ -246,7 +253,6 @@ class CacheCreator {
net::OldCompletionCallback* callback_;
disk_cache::BackendImpl* cache_;
net::NetLog* net_log_;
- net::OldCompletionCallbackImpl<CacheCreator> my_callback_;
DISALLOW_COPY_AND_ASSIGN(CacheCreator);
};
@@ -256,11 +262,25 @@ int CacheCreator::Run() {
cache_->SetMaxSize(max_bytes_);
cache_->SetType(type_);
cache_->SetFlags(flags_);
- int rv = cache_->Init(&my_callback_);
+ int rv = cache_->Init(
+ base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
DCHECK_EQ(net::ERR_IO_PENDING, rv);
return rv;
}
+void CacheCreator::DoCallback(int result) {
+ DCHECK_NE(net::ERR_IO_PENDING, result);
+ if (result == net::OK) {
+ *backend_ = cache_;
+ } else {
+ LOG(ERROR) << "Unable to create cache";
+ *backend_ = NULL;
+ delete cache_;
+ }
+ callback_->Run(result);
+ delete this;
+}
+
void CacheCreator::OnIOComplete(int result) {
if (result == net::OK || !force_ || retry_)
return DoCallback(result);
@@ -279,35 +299,9 @@ void CacheCreator::OnIOComplete(int result) {
DCHECK_EQ(net::ERR_IO_PENDING, rv);
}
-void CacheCreator::DoCallback(int result) {
- DCHECK_NE(net::ERR_IO_PENDING, result);
- if (result == net::OK) {
- *backend_ = cache_;
- } else {
- LOG(ERROR) << "Unable to create cache";
- *backend_ = NULL;
- delete cache_;
- }
- callback_->Run(result);
- delete this;
-}
-
-// ------------------------------------------------------------------------
-
-// A task to perform final cleanup on the background thread.
-class FinalCleanup : public Task {
- public:
- explicit FinalCleanup(disk_cache::BackendImpl* backend) : backend_(backend) {}
- ~FinalCleanup() {}
-
- virtual void Run();
- private:
- disk_cache::BackendImpl* backend_;
- DISALLOW_EVIL_CONSTRUCTORS(FinalCleanup);
-};
-
-void FinalCleanup::Run() {
- backend_->CleanupCache();
+// A callback to perform final cleanup on the background thread.
+void FinalCleanupCallback(disk_cache::BackendImpl* backend) {
+ backend->CleanupCache();
}
} // namespace
@@ -420,8 +414,8 @@ BackendImpl::~BackendImpl() {
// Unit tests may use the same thread for everything.
CleanupCache();
} else {
- background_queue_.background_thread()->PostTask(FROM_HERE,
- new FinalCleanup(this));
+ background_queue_.background_thread()->PostTask(
+ FROM_HERE, base::Bind(&FinalCleanupCallback, base::Unretained(this)));
done_.Wait();
}
}
@@ -449,7 +443,7 @@ int BackendImpl::CreateBackend(const FilePath& full_path, bool force,
return creator->Run();
}
-int BackendImpl::Init(OldCompletionCallback* callback) {
+int BackendImpl::Init(const net::CompletionCallback& callback) {
background_queue_.Init(callback);
return net::ERR_IO_PENDING;
}
@@ -1389,8 +1383,8 @@ int BackendImpl::CreateEntry(const std::string& key, Entry** entry,
}
int BackendImpl::DoomEntry(const std::string& key,
- OldCompletionCallback* callback) {
- DCHECK(callback);
+ const net::CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
background_queue_.DoomEntry(key, callback);
return net::ERR_IO_PENDING;
}
diff --git a/net/disk_cache/backend_impl.h b/net/disk_cache/backend_impl.h
index 6c76c2c..6ef5906 100644
--- a/net/disk_cache/backend_impl.h
+++ b/net/disk_cache/backend_impl.h
@@ -59,7 +59,7 @@ class NET_EXPORT_PRIVATE BackendImpl : public Backend {
OldCompletionCallback* callback);
// Performs general initialization for this current instance of the cache.
- int Init(OldCompletionCallback* callback);
+ int Init(const net::CompletionCallback& callback);
// Performs the actual initialization and final cleanup on destruction.
int SyncInit();
@@ -263,7 +263,7 @@ class NET_EXPORT_PRIVATE BackendImpl : public Backend {
virtual int CreateEntry(const std::string& key, Entry** entry,
OldCompletionCallback* callback) OVERRIDE;
virtual int DoomEntry(const std::string& key,
- OldCompletionCallback* callback) OVERRIDE;
+ const net::CompletionCallback& callback) OVERRIDE;
virtual int DoomAllEntries(const net::CompletionCallback& callback) OVERRIDE;
virtual int DoomEntriesBetween(
const base::Time initial_time,
diff --git a/net/disk_cache/disk_cache.h b/net/disk_cache/disk_cache.h
index a678eec..59231f6 100644
--- a/net/disk_cache/disk_cache.h
+++ b/net/disk_cache/disk_cache.h
@@ -92,7 +92,7 @@ class NET_EXPORT Backend {
// is a net error code. If this method returns ERR_IO_PENDING, the |callback|
// will be invoked after the entry is doomed.
virtual int DoomEntry(const std::string& key,
- OldCompletionCallback* callback) = 0;
+ const net::CompletionCallback& callback) = 0;
// Marks all entries for deletion. The return value is a net error code. If
// this method returns ERR_IO_PENDING, the |callback| will be invoked when the
diff --git a/net/disk_cache/disk_cache_test_base.cc b/net/disk_cache/disk_cache_test_base.cc
index fef178e..3182166 100644
--- a/net/disk_cache/disk_cache_test_base.cc
+++ b/net/disk_cache/disk_cache_test_base.cc
@@ -119,8 +119,8 @@ int DiskCacheTestWithCache::CreateEntry(const std::string& key,
}
int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
- TestOldCompletionCallback cb;
- int rv = cache_->DoomEntry(key, &cb);
+ net::TestCompletionCallback cb;
+ int rv = cache_->DoomEntry(key, cb.callback());
return cb.GetResult(rv);
}
@@ -304,7 +304,7 @@ void DiskCacheTestWithCache::InitDiskCacheImpl() {
cache_impl_->SetType(type_);
cache_impl_->SetFlags(disk_cache::kNoRandom);
- TestOldCompletionCallback cb;
- int rv = cache_impl_->Init(&cb);
+ net::TestCompletionCallback cb;
+ int rv = cache_impl_->Init(cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
}
diff --git a/net/disk_cache/entry_unittest.cc b/net/disk_cache/entry_unittest.cc
index c537ef1..ef8ad89 100644
--- a/net/disk_cache/entry_unittest.cc
+++ b/net/disk_cache/entry_unittest.cc
@@ -1724,22 +1724,31 @@ TEST_F(DiskCacheEntryTest, MemoryOnlyDoomSparseEntry) {
DoomSparseEntry();
}
-// A OldCompletionCallback that deletes the cache from within the callback. The way
-// a TestOldCompletionCallback works means that all tasks (even new ones) are
-// executed by the message loop before returning to the caller so the only way
-// to simulate a race is to execute what we want on the callback.
-class SparseTestOldCompletionCallback : public TestOldCompletionCallback {
+// A CompletionCallback wrapper that deletes the cache from within the callback.
+// The way an OldCompletionCallback works means that all tasks (even new ones)
+// are executed by the message loop before returning to the caller so the only
+// way to simulate a race is to execute what we want on the callback.
+class SparseTestCompletionCallback: public TestCompletionCallbackBase {
public:
- explicit SparseTestOldCompletionCallback(disk_cache::Backend* cache)
- : cache_(cache) {}
+ explicit SparseTestCompletionCallback(disk_cache::Backend* cache)
+ : cache_(cache),
+ ALLOW_THIS_IN_INITIALIZER_LIST(callback_(
+ base::Bind(&SparseTestCompletionCallback::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:
+
disk_cache::Backend* cache_;
- DISALLOW_COPY_AND_ASSIGN(SparseTestOldCompletionCallback);
+ net::CompletionCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(SparseTestCompletionCallback);
};
// Tests that we don't crash when the backend is deleted while we are working
@@ -1765,8 +1774,8 @@ TEST_F(DiskCacheEntryTest, DoomSparseEntry2) {
EXPECT_EQ(9, cache_->GetEntryCount());
entry->Close();
- SparseTestOldCompletionCallback cb(cache_);
- int rv = cache_->DoomEntry(key, &cb);
+ SparseTestCompletionCallback cb(cache_);
+ int rv = cache_->DoomEntry(key, cb.callback());
EXPECT_EQ(net::ERR_IO_PENDING, rv);
EXPECT_EQ(net::OK, cb.WaitForResult());
diff --git a/net/disk_cache/in_flight_backend_io.cc b/net/disk_cache/in_flight_backend_io.cc
index 5cc6a65..30a5500 100644
--- a/net/disk_cache/in_flight_backend_io.cc
+++ b/net/disk_cache/in_flight_backend_io.cc
@@ -314,7 +314,7 @@ InFlightBackendIO::InFlightBackendIO(BackendImpl* backend,
InFlightBackendIO::~InFlightBackendIO() {
}
-void InFlightBackendIO::Init(OldCompletionCallback* callback) {
+void InFlightBackendIO::Init(const net::CompletionCallback& callback) {
scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
operation->Init();
PostOperation(operation);
@@ -335,7 +335,7 @@ void InFlightBackendIO::CreateEntry(const std::string& key, Entry** entry,
}
void InFlightBackendIO::DoomEntry(const std::string& key,
- OldCompletionCallback* callback) {
+ const net::CompletionCallback& callback) {
scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
operation->DoomEntry(key);
PostOperation(operation);
diff --git a/net/disk_cache/in_flight_backend_io.h b/net/disk_cache/in_flight_backend_io.h
index 8d4024a..c3a52d5 100644
--- a/net/disk_cache/in_flight_backend_io.h
+++ b/net/disk_cache/in_flight_backend_io.h
@@ -149,13 +149,14 @@ class InFlightBackendIO : public InFlightIO {
base::MessageLoopProxy* background_thread);
virtual ~InFlightBackendIO();
- // The operations we proxy:
- void Init(net::OldCompletionCallback* callback);
+ // Proxied operations.
+ void Init(const net::CompletionCallback& callback);
void OpenEntry(const std::string& key, Entry** entry,
net::OldCompletionCallback* callback);
void CreateEntry(const std::string& key, Entry** entry,
net::OldCompletionCallback* callback);
- void DoomEntry(const std::string& key, net::OldCompletionCallback* callback);
+ void DoomEntry(const std::string& key,
+ const net::CompletionCallback& callback);
void DoomAllEntries(const net::CompletionCallback& callback);
void DoomEntriesBetween(const base::Time initial_time,
const base::Time end_time,
diff --git a/net/disk_cache/mem_backend_impl.cc b/net/disk_cache/mem_backend_impl.cc
index bd07af2..89b5825 100644
--- a/net/disk_cache/mem_backend_impl.cc
+++ b/net/disk_cache/mem_backend_impl.cc
@@ -145,7 +145,7 @@ int MemBackendImpl::CreateEntry(const std::string& key, Entry** entry,
}
int MemBackendImpl::DoomEntry(const std::string& key,
- OldCompletionCallback* callback) {
+ const net::CompletionCallback& callback) {
if (DoomEntry(key))
return net::OK;
diff --git a/net/disk_cache/mem_backend_impl.h b/net/disk_cache/mem_backend_impl.h
index 8793cec..1b799f4 100644
--- a/net/disk_cache/mem_backend_impl.h
+++ b/net/disk_cache/mem_backend_impl.h
@@ -69,7 +69,7 @@ class NET_EXPORT_PRIVATE MemBackendImpl : public Backend {
virtual int CreateEntry(const std::string& key, Entry** entry,
OldCompletionCallback* callback) OVERRIDE;
virtual int DoomEntry(const std::string& key,
- OldCompletionCallback* callback) OVERRIDE;
+ const net::CompletionCallback& callback) OVERRIDE;
virtual int DoomAllEntries(const net::CompletionCallback& callback) OVERRIDE;
virtual int DoomEntriesBetween(
const base::Time initial_time,
diff --git a/net/disk_cache/stress_cache.cc b/net/disk_cache/stress_cache.cc
index a18df2a..52abb16 100644
--- a/net/disk_cache/stress_cache.cc
+++ b/net/disk_cache/stress_cache.cc
@@ -113,8 +113,8 @@ void StressTheCache(int iteration) {
cache->SetMaxSize(cache_size);
cache->SetFlags(disk_cache::kNoLoadProtection);
- TestOldCompletionCallback cb;
- int rv = cache->Init(&cb);
+ net::TestCompletionCallback cb;
+ int rv = cache->Init(cb.callback());
if (cb.GetResult(rv) != net::OK) {
printf("Unable to initialize cache.\n");
@@ -154,22 +154,24 @@ void StressTheCache(int iteration) {
if (entries[slot])
entries[slot]->Close();
- rv = cache->OpenEntry(keys[key], &entries[slot], &cb);
- if (cb.GetResult(rv) != net::OK) {
- rv = cache->CreateEntry(keys[key], &entries[slot], &cb);
- CHECK_EQ(net::OK, cb.GetResult(rv));
+ TestOldCompletionCallback old_cb;
+ rv = cache->OpenEntry(keys[key], &entries[slot], &old_cb);
+ if (old_cb.GetResult(rv) != net::OK) {
+ rv = cache->CreateEntry(keys[key], &entries[slot], &old_cb);
+ CHECK_EQ(net::OK, old_cb.GetResult(rv));
}
base::snprintf(buffer->data(), kSize,
"i: %d iter: %d, size: %d, truncate: %d ", i, iteration,
size, truncate ? 1 : 0);
- rv = entries[slot]->WriteData(0, 0, buffer, size, &cb, truncate);
- CHECK_EQ(size, cb.GetResult(rv));
+ rv = entries[slot]->WriteData(0, 0, buffer, size, &old_cb, truncate);
+ CHECK_EQ(size, old_cb.GetResult(rv));
if (rand() % 100 > 80) {
key = rand() % kNumKeys;
- rv = cache->DoomEntry(keys[key], &cb);
- cb.GetResult(rv);
+ net::TestCompletionCallback cb2;
+ rv = cache->DoomEntry(keys[key], cb2.callback());
+ cb2.GetResult(rv);
}
if (!(i % 100))