summaryrefslogtreecommitdiffstats
path: root/net/disk_cache
diff options
context:
space:
mode:
authorqsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-01 11:57:53 +0000
committerqsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-01 11:57:53 +0000
commit8c3f5a3423c155d0a90048fb04c7da0df0f81540 (patch)
tree83e7c29a104bcb6a60ee589a0f8fbecdc20fd8e0 /net/disk_cache
parent335f57f55c9b7862499152638c2b3085cd4ca19d (diff)
downloadchromium_src-8c3f5a3423c155d0a90048fb04c7da0df0f81540.zip
chromium_src-8c3f5a3423c155d0a90048fb04c7da0df0f81540.tar.gz
chromium_src-8c3f5a3423c155d0a90048fb04c7da0df0f81540.tar.bz2
Change the API of disk_cache::CreateCacheBackend to use scoped_ptr
Using scoped_ptr instead of pointer allows the API to show the transfert of ownership and ensure that the caller will holds it. R=rvargas@chromium.org TBR=bradchen@chromium.org,apatrick@chromium.org,darin@chromium.org Review URL: https://chromiumcodereview.appspot.com/20737002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@215008 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache')
-rw-r--r--net/disk_cache/backend_unittest.cc124
-rw-r--r--net/disk_cache/cache_creator.cc26
-rw-r--r--net/disk_cache/disk_cache.h6
-rw-r--r--net/disk_cache/disk_cache_perftest.cc11
-rw-r--r--net/disk_cache/disk_cache_test_base.cc25
-rw-r--r--net/disk_cache/disk_cache_test_base.h2
-rw-r--r--net/disk_cache/entry_unittest.cc16
-rw-r--r--net/disk_cache/mem_backend_impl.cc10
-rw-r--r--net/disk_cache/mem_backend_impl.h2
-rw-r--r--net/disk_cache/tracing_cache_backend.cc4
-rw-r--r--net/disk_cache/tracing_cache_backend.h2
11 files changed, 97 insertions, 131 deletions
diff --git a/net/disk_cache/backend_unittest.cc b/net/disk_cache/backend_unittest.cc
index 8f0de83..bc48a2e 100644
--- a/net/disk_cache/backend_unittest.cc
+++ b/net/disk_cache/backend_unittest.cc
@@ -41,26 +41,24 @@ namespace {
const char kExistingEntryKey[] = "existing entry key";
-disk_cache::BackendImpl* CreateExistingEntryCache(
+scoped_ptr<disk_cache::BackendImpl> CreateExistingEntryCache(
const base::Thread& cache_thread,
base::FilePath& cache_path) {
net::TestCompletionCallback cb;
- disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
- cache_path, cache_thread.message_loop_proxy(), NULL);
+ scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl(
+ cache_path, cache_thread.message_loop_proxy(), NULL));
int rv = cache->Init(cb.callback());
if (cb.GetResult(rv) != net::OK)
- return NULL;
+ return scoped_ptr<disk_cache::BackendImpl>();
disk_cache::Entry* entry = NULL;
rv = cache->CreateEntry(kExistingEntryKey, &entry, cb.callback());
- if (cb.GetResult(rv) != net::OK) {
- delete cache;
- return NULL;
- }
+ if (cb.GetResult(rv) != net::OK)
+ return scoped_ptr<disk_cache::BackendImpl>();
entry->Close();
- return cache;
+ return cache.Pass();
}
} // namespace
@@ -268,11 +266,10 @@ TEST_F(DiskCacheTest, CreateBackend) {
base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
// Test the private factory method(s).
- disk_cache::Backend* cache = NULL;
+ scoped_ptr<disk_cache::Backend> cache;
cache = disk_cache::MemBackendImpl::CreateBackend(0, NULL);
- ASSERT_TRUE(cache);
- delete cache;
- cache = NULL;
+ ASSERT_TRUE(cache.get());
+ cache.reset();
// Now test the public API.
int rv =
@@ -286,9 +283,8 @@ TEST_F(DiskCacheTest, CreateBackend) {
&cache,
cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
- ASSERT_TRUE(cache);
- delete cache;
- cache = NULL;
+ ASSERT_TRUE(cache.get());
+ cache.reset();
rv = disk_cache::CreateCacheBackend(net::MEMORY_CACHE,
net::CACHE_BACKEND_DEFAULT,
@@ -296,8 +292,8 @@ TEST_F(DiskCacheTest, CreateBackend) {
false, NULL, NULL, &cache,
cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
- ASSERT_TRUE(cache);
- delete cache;
+ ASSERT_TRUE(cache.get());
+ cache.reset();
}
base::MessageLoop::current()->RunUntilIdle();
@@ -314,13 +310,13 @@ TEST_F(DiskCacheBackendTest, CreateBackend_MissingFile) {
net::TestCompletionCallback cb;
bool prev = base::ThreadRestrictions::SetIOAllowed(false);
- disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
- cache_path_, cache_thread.message_loop_proxy().get(), NULL);
+ scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl(
+ cache_path_, cache_thread.message_loop_proxy().get(), NULL));
int rv = cache->Init(cb.callback());
ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv));
base::ThreadRestrictions::SetIOAllowed(prev);
- delete cache;
+ cache.reset();
DisableIntegrityCheck();
}
@@ -390,9 +386,7 @@ void DiskCacheBackendTest::BackendShutdownWithPendingFileIO(bool fast) {
entry->Release();
// The cache destructor will see one pending operation here.
- delete cache_;
- // Prevent the TearDown() to delete the backend again.
- cache_ = NULL;
+ cache_.reset();
if (rv == net::ERR_IO_PENDING) {
if (fast)
@@ -456,9 +450,7 @@ void DiskCacheBackendTest::BackendShutdownWithPendingIO(bool fast) {
entry->Close();
// The cache destructor will see one pending operation here.
- delete cache_;
- // Prevent the TearDown() to delete the backend again.
- cache_ = NULL;
+ cache_.reset();
}
base::MessageLoop::current()->RunUntilIdle();
@@ -496,9 +488,7 @@ void DiskCacheBackendTest::BackendShutdownWithPendingCreate(bool fast) {
int rv = cache_->CreateEntry("some key", &entry, cb.callback());
ASSERT_EQ(net::ERR_IO_PENDING, rv);
- delete cache_;
- // Prevent the TearDown() to delete the backend again.
- cache_ = NULL;
+ cache_.reset();
EXPECT_FALSE(cb.have_result());
}
@@ -529,7 +519,7 @@ TEST_F(DiskCacheTest, TruncatedIndex) {
base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
net::TestCompletionCallback cb;
- disk_cache::Backend* backend = NULL;
+ scoped_ptr<disk_cache::Backend> backend;
int rv =
disk_cache::CreateCacheBackend(net::DISK_CACHE,
net::CACHE_BACKEND_BLOCKFILE,
@@ -542,8 +532,7 @@ TEST_F(DiskCacheTest, TruncatedIndex) {
cb.callback());
ASSERT_NE(net::OK, cb.GetResult(rv));
- ASSERT_TRUE(backend == NULL);
- delete backend;
+ ASSERT_FALSE(backend);
}
void DiskCacheBackendTest::BackendSetSize() {
@@ -1615,8 +1604,7 @@ void DiskCacheBackendTest::BackendTransaction(const std::string& name,
ASSERT_EQ(num_entries - 1, actual);
}
- delete cache_;
- cache_ = NULL;
+ cache_.reset();
cache_impl_ = NULL;
ASSERT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask));
@@ -1735,12 +1723,10 @@ TEST_F(DiskCacheTest, WrongVersion) {
base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
net::TestCompletionCallback cb;
- disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
- cache_path_, cache_thread.message_loop_proxy().get(), NULL);
+ scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl(
+ cache_path_, cache_thread.message_loop_proxy().get(), NULL));
int rv = cache->Init(cb.callback());
ASSERT_EQ(net::ERR_FAILED, cb.GetResult(rv));
-
- delete cache;
}
class BadEntropyProvider : public base::FieldTrial::EntropyProvider {
@@ -1760,11 +1746,10 @@ TEST_F(DiskCacheTest, SimpleCacheControlJoin) {
ASSERT_TRUE(cache_thread.StartWithOptions(
base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
- disk_cache::BackendImpl* cache = CreateExistingEntryCache(cache_thread,
- cache_path_);
- ASSERT_TRUE(cache);
- delete cache;
- cache = NULL;
+ scoped_ptr<disk_cache::BackendImpl> cache =
+ CreateExistingEntryCache(cache_thread, cache_path_);
+ ASSERT_TRUE(cache.get());
+ cache.reset();
// Instantiate the SimpleCacheTrial, forcing this run into the
// ExperimentControl group.
@@ -1772,7 +1757,7 @@ TEST_F(DiskCacheTest, SimpleCacheControlJoin) {
base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial",
"ExperimentControl");
net::TestCompletionCallback cb;
- disk_cache::Backend* base_cache = NULL;
+ scoped_ptr<disk_cache::Backend> base_cache;
int rv =
disk_cache::CreateCacheBackend(net::DISK_CACHE,
net::CACHE_BACKEND_BLOCKFILE,
@@ -1784,9 +1769,7 @@ TEST_F(DiskCacheTest, SimpleCacheControlJoin) {
&base_cache,
cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
- cache = static_cast<disk_cache::BackendImpl*>(base_cache);
EXPECT_EQ(0, base_cache->GetEntryCount());
- delete cache;
}
// Tests that the disk cache can restart in the control group preserving
@@ -1802,19 +1785,16 @@ TEST_F(DiskCacheTest, SimpleCacheControlRestart) {
ASSERT_TRUE(cache_thread.StartWithOptions(
base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
- disk_cache::BackendImpl* cache = CreateExistingEntryCache(cache_thread,
- cache_path_);
- ASSERT_TRUE(cache);
- delete cache;
- cache = NULL;
+ scoped_ptr<disk_cache::BackendImpl> cache =
+ CreateExistingEntryCache(cache_thread, cache_path_);
+ ASSERT_TRUE(cache.get());
net::TestCompletionCallback cb;
const int kRestartCount = 5;
for (int i=0; i < kRestartCount; ++i) {
- cache = new disk_cache::BackendImpl(cache_path_,
- cache_thread.message_loop_proxy(),
- NULL);
+ cache.reset(new disk_cache::BackendImpl(
+ cache_path_, cache_thread.message_loop_proxy(), NULL));
int rv = cache->Init(cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
EXPECT_EQ(1, cache->GetEntryCount());
@@ -1824,8 +1804,6 @@ TEST_F(DiskCacheTest, SimpleCacheControlRestart) {
EXPECT_EQ(net::OK, cb.GetResult(rv));
EXPECT_TRUE(entry);
entry->Close();
- delete cache;
- cache = NULL;
}
}
@@ -1843,10 +1821,9 @@ TEST_F(DiskCacheTest, SimpleCacheControlLeave) {
base::FieldTrialList::CreateFieldTrial("SimpleCacheTrial",
"ExperimentControl");
- disk_cache::BackendImpl* cache = CreateExistingEntryCache(cache_thread,
- cache_path_);
- ASSERT_TRUE(cache);
- delete cache;
+ scoped_ptr<disk_cache::BackendImpl> cache =
+ CreateExistingEntryCache(cache_thread, cache_path_);
+ ASSERT_TRUE(cache.get());
}
// Instantiate the SimpleCacheTrial, forcing this run into the
@@ -1857,8 +1834,8 @@ TEST_F(DiskCacheTest, SimpleCacheControlLeave) {
const int kRestartCount = 5;
for (int i = 0; i < kRestartCount; ++i) {
- disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(
- cache_path_, cache_thread.message_loop_proxy(), NULL);
+ scoped_ptr<disk_cache::BackendImpl> cache(new disk_cache::BackendImpl(
+ cache_path_, cache_thread.message_loop_proxy(), NULL));
int rv = cache->Init(cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
EXPECT_EQ(1, cache->GetEntryCount());
@@ -1868,8 +1845,6 @@ TEST_F(DiskCacheTest, SimpleCacheControlLeave) {
EXPECT_EQ(net::OK, cb.GetResult(rv));
EXPECT_TRUE(entry);
entry->Close();
- delete cache;
- cache = NULL;
}
}
@@ -1897,8 +1872,7 @@ TEST_F(DiskCacheBackendTest, DeleteOld) {
path.clear(); // Make sure path was captured by the previous call.
ASSERT_EQ(net::OK, cb.GetResult(rv));
base::ThreadRestrictions::SetIOAllowed(prev);
- delete cache_;
- cache_ = NULL;
+ cache_.reset();
EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
}
@@ -2674,7 +2648,7 @@ TEST_F(DiskCacheBackendTest, Backend_UsageStats) {
disk_cache::StatsItems::value_type hits("Create hit", "0x1");
EXPECT_EQ(1, std::count(stats.begin(), stats.end(), hits));
- delete cache_;
+ cache_.reset();
// Now open the cache and verify that the stats are still there.
DisableFirstCleanup();
@@ -2800,7 +2774,7 @@ TEST_F(DiskCacheTest, MultipleInstances) {
net::TestCompletionCallback cb;
const int kNumberOfCaches = 2;
- disk_cache::Backend* cache[kNumberOfCaches];
+ scoped_ptr<disk_cache::Backend> cache[kNumberOfCaches];
int rv =
disk_cache::CreateCacheBackend(net::DISK_CACHE,
@@ -2824,7 +2798,7 @@ TEST_F(DiskCacheTest, MultipleInstances) {
cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
- ASSERT_TRUE(cache[0] != NULL && cache[1] != NULL);
+ ASSERT_TRUE(cache[0].get() != NULL && cache[1].get() != NULL);
std::string key("the first key");
disk_cache::Entry* entry;
@@ -2833,8 +2807,6 @@ TEST_F(DiskCacheTest, MultipleInstances) {
ASSERT_EQ(net::OK, cb.GetResult(rv));
entry->Close();
}
- delete cache[0];
- delete cache[1];
}
// Test the six regions of the curve that determines the max cache size.
@@ -3045,7 +3017,7 @@ TEST_F(DiskCacheBackendTest, ShaderCacheUpdateRankForExternalCacheHit) {
void DiskCacheBackendTest::TracingBackendBasics() {
InitCache();
- cache_ = new disk_cache::TracingCacheBackend(cache_);
+ cache_.reset(new disk_cache::TracingCacheBackend(cache_.Pass()));
cache_impl_ = NULL;
EXPECT_EQ(net::DISK_CACHE, cache_->GetCacheType());
if (!simple_cache_mode_) {
@@ -3240,8 +3212,7 @@ TEST_F(DiskCacheBackendTest, SimpleCacheOverBlockfileCache) {
ASSERT_EQ(net::OK, CreateEntry("key", &entry));
ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false));
entry->Close();
- delete cache_;
- cache_ = NULL;
+ cache_.reset();
// Check that the |SimpleBackendImpl| does not favor this structure.
base::Thread cache_thread("CacheThread");
@@ -3273,8 +3244,7 @@ TEST_F(DiskCacheBackendTest, BlockfileCacheOverSimpleCache) {
ASSERT_EQ(net::OK, CreateEntry("key", &entry));
ASSERT_EQ(0, WriteData(entry, 0, 0, buffer.get(), 0, false));
entry->Close();
- delete cache_;
- cache_ = NULL;
+ cache_.reset();
// Check that the |BackendImpl| does not favor this structure.
base::Thread cache_thread("CacheThread");
diff --git a/net/disk_cache/cache_creator.cc b/net/disk_cache/cache_creator.cc
index 8384f82..07a26c9 100644
--- a/net/disk_cache/cache_creator.cc
+++ b/net/disk_cache/cache_creator.cc
@@ -9,6 +9,7 @@
#include "net/base/net_errors.h"
#include "net/disk_cache/backend_impl.h"
#include "net/disk_cache/cache_util.h"
+#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/mem_backend_impl.h"
#include "net/disk_cache/simple/simple_backend_impl.h"
@@ -26,7 +27,7 @@ class CacheCreator {
CacheCreator(const base::FilePath& path, bool force, int max_bytes,
net::CacheType type, net::BackendType backend_type, uint32 flags,
base::MessageLoopProxy* thread, net::NetLog* net_log,
- disk_cache::Backend** backend,
+ scoped_ptr<disk_cache::Backend>* backend,
const net::CompletionCallback& callback);
// Creates the backend.
@@ -47,9 +48,9 @@ class CacheCreator {
net::BackendType backend_type_;
uint32 flags_;
scoped_refptr<base::MessageLoopProxy> thread_;
- disk_cache::Backend** backend_;
+ scoped_ptr<disk_cache::Backend>* backend_;
net::CompletionCallback callback_;
- disk_cache::Backend* created_cache_;
+ scoped_ptr<disk_cache::Backend> created_cache_;
net::NetLog* net_log_;
DISALLOW_COPY_AND_ASSIGN(CacheCreator);
@@ -59,7 +60,7 @@ CacheCreator::CacheCreator(
const base::FilePath& path, bool force, int max_bytes,
net::CacheType type, net::BackendType backend_type, uint32 flags,
base::MessageLoopProxy* thread, net::NetLog* net_log,
- disk_cache::Backend** backend,
+ scoped_ptr<disk_cache::Backend>* backend,
const net::CompletionCallback& callback)
: path_(path),
force_(force),
@@ -71,7 +72,6 @@ CacheCreator::CacheCreator(
thread_(thread),
backend_(backend),
callback_(callback),
- created_cache_(NULL),
net_log_(net_log) {
}
@@ -86,13 +86,13 @@ int CacheCreator::Run() {
disk_cache::SimpleBackendImpl* simple_cache =
new disk_cache::SimpleBackendImpl(path_, max_bytes_, type_,
thread_.get(), net_log_);
- created_cache_ = simple_cache;
+ created_cache_.reset(simple_cache);
return simple_cache->Init(
base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
}
disk_cache::BackendImpl* new_cache =
new disk_cache::BackendImpl(path_, thread_.get(), net_log_);
- created_cache_ = new_cache;
+ created_cache_.reset(new_cache);
new_cache->SetMaxSize(max_bytes_);
new_cache->SetType(type_);
new_cache->SetFlags(flags_);
@@ -106,14 +106,13 @@ void CacheCreator::DoCallback(int result) {
DCHECK_NE(net::ERR_IO_PENDING, result);
if (result == net::OK) {
#ifndef USE_TRACING_CACHE_BACKEND
- *backend_ = created_cache_;
+ *backend_ = created_cache_.Pass();
#else
- *backend_ = new disk_cache::TracingCacheBackend(created_cache_);
+ *backend_.reset(
+ new disk_cache::TracingCacheBackend(created_cache_.Pass()));
#endif
} else {
LOG(ERROR) << "Unable to create cache";
- *backend_ = NULL;
- delete created_cache_;
}
callback_.Run(result);
delete this;
@@ -128,8 +127,7 @@ void CacheCreator::OnIOComplete(int result) {
// This is a failure and we are supposed to try again, so delete the object,
// delete all the files, and try again.
retry_ = true;
- delete created_cache_;
- created_cache_ = NULL;
+ created_cache_.reset();
if (!disk_cache::DelayedCacheCleanup(path_))
return DoCallback(result);
@@ -148,7 +146,7 @@ int CreateCacheBackend(net::CacheType type,
const base::FilePath& path,
int max_bytes,
bool force, base::MessageLoopProxy* thread,
- net::NetLog* net_log, Backend** backend,
+ net::NetLog* net_log, scoped_ptr<Backend>* backend,
const net::CompletionCallback& callback) {
DCHECK(!callback.is_null());
if (type == net::MEMORY_CACHE) {
diff --git a/net/disk_cache/disk_cache.h b/net/disk_cache/disk_cache.h
index d4cb804..3ae8bf4 100644
--- a/net/disk_cache/disk_cache.h
+++ b/net/disk_cache/disk_cache.h
@@ -50,9 +50,11 @@ class Backend;
NET_EXPORT int CreateCacheBackend(net::CacheType type,
net::BackendType backend_type,
const base::FilePath& path,
- int max_bytes, bool force,
+ int max_bytes,
+ bool force,
base::MessageLoopProxy* thread,
- net::NetLog* net_log, Backend** backend,
+ net::NetLog* net_log,
+ scoped_ptr<Backend>* backend,
const net::CompletionCallback& callback);
// The root interface for a disk cache instance.
diff --git a/net/disk_cache/disk_cache_perftest.cc b/net/disk_cache/disk_cache_perftest.cc
index a2474da..f7a1b59 100644
--- a/net/disk_cache/disk_cache_perftest.cc
+++ b/net/disk_cache/disk_cache_perftest.cc
@@ -165,7 +165,7 @@ TEST_F(DiskCacheTest, CacheBackendPerformance) {
ASSERT_TRUE(CleanupCacheDir());
net::TestCompletionCallback cb;
- disk_cache::Backend* cache;
+ scoped_ptr<disk_cache::Backend> cache;
int rv = disk_cache::CreateCacheBackend(
net::DISK_CACHE, net::CACHE_BACKEND_BLOCKFILE, cache_path_, 0, false,
cache_thread.message_loop_proxy().get(), NULL, &cache, cb.callback());
@@ -178,10 +178,10 @@ TEST_F(DiskCacheTest, CacheBackendPerformance) {
TestEntries entries;
int num_entries = 1000;
- EXPECT_TRUE(TimeWrite(num_entries, cache, &entries));
+ EXPECT_TRUE(TimeWrite(num_entries, cache.get(), &entries));
base::MessageLoop::current()->RunUntilIdle();
- delete cache;
+ cache.reset();
ASSERT_TRUE(file_util::EvictFileFromSystemCache(
cache_path_.AppendASCII("index")));
@@ -199,12 +199,11 @@ TEST_F(DiskCacheTest, CacheBackendPerformance) {
cache_thread.message_loop_proxy().get(), NULL, &cache, cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
- EXPECT_TRUE(TimeRead(num_entries, cache, entries, true));
+ EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, true));
- EXPECT_TRUE(TimeRead(num_entries, cache, entries, false));
+ EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, false));
base::MessageLoop::current()->RunUntilIdle();
- delete cache;
}
// Creating and deleting "entries" on a block-file is something quite frequent
diff --git a/net/disk_cache/disk_cache_test_base.cc b/net/disk_cache/disk_cache_test_base.cc
index 219a213..dc7bb6c 100644
--- a/net/disk_cache/disk_cache_test_base.cc
+++ b/net/disk_cache/disk_cache_test_base.cc
@@ -50,8 +50,7 @@ void DiskCacheTest::TearDown() {
}
DiskCacheTestWithCache::DiskCacheTestWithCache()
- : cache_(NULL),
- cache_impl_(NULL),
+ : cache_impl_(NULL),
simple_cache_impl_(NULL),
mem_cache_(NULL),
mask_(0),
@@ -89,7 +88,7 @@ void DiskCacheTestWithCache::SimulateCrash() {
ASSERT_EQ(net::OK, cb.GetResult(rv));
cache_impl_->ClearRefCountForTest();
- delete cache_impl_;
+ cache_.reset();
EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
CreateBackend(disk_cache::kNoRandom, &cache_thread_);
@@ -230,7 +229,7 @@ void DiskCacheTestWithCache::AddDelay() {
void DiskCacheTestWithCache::TearDown() {
base::RunLoop().RunUntilIdle();
- delete cache_;
+ cache_.reset();
if (cache_thread_.IsRunning())
cache_thread_.Stop();
@@ -243,8 +242,8 @@ void DiskCacheTestWithCache::TearDown() {
void DiskCacheTestWithCache::InitMemoryCache() {
mem_cache_ = new disk_cache::MemBackendImpl(NULL);
- cache_ = mem_cache_;
- ASSERT_TRUE(NULL != cache_);
+ cache_.reset(mem_cache_);
+ ASSERT_TRUE(cache_);
if (size_)
EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
@@ -274,15 +273,16 @@ void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) {
if (simple_cache_mode_) {
net::TestCompletionCallback cb;
- disk_cache::SimpleBackendImpl* simple_backend =
+ scoped_ptr<disk_cache::SimpleBackendImpl> simple_backend(
new disk_cache::SimpleBackendImpl(
- cache_path_, size_, type_, make_scoped_refptr(runner).get(), NULL);
+ cache_path_, size_, type_, make_scoped_refptr(runner).get(), NULL));
int rv = simple_backend->Init(cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
- cache_ = simple_cache_impl_ = simple_backend;
+ simple_cache_impl_ = simple_backend.get();
+ cache_ = simple_backend.PassAs<disk_cache::Backend>();
if (simple_cache_wait_for_index_) {
net::TestCompletionCallback wait_for_index_cb;
- rv = simple_backend->index()->ExecuteWhenReady(
+ rv = simple_cache_impl_->index()->ExecuteWhenReady(
wait_for_index_cb.callback());
ASSERT_EQ(net::OK, wait_for_index_cb.GetResult(rv));
}
@@ -293,8 +293,8 @@ void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) {
cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL);
else
cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL);
- cache_ = cache_impl_;
- ASSERT_TRUE(NULL != cache_);
+ cache_.reset(cache_impl_);
+ ASSERT_TRUE(cache_);
if (size_)
EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
if (new_eviction_)
@@ -304,5 +304,4 @@ void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) {
net::TestCompletionCallback cb;
int rv = cache_impl_->Init(cb.callback());
ASSERT_EQ(net::OK, cb.GetResult(rv));
- cache_ = cache_impl_;
}
diff --git a/net/disk_cache/disk_cache_test_base.h b/net/disk_cache/disk_cache_test_base.h
index 0378573..b724906 100644
--- a/net/disk_cache/disk_cache_test_base.h
+++ b/net/disk_cache/disk_cache_test_base.h
@@ -146,7 +146,7 @@ class DiskCacheTestWithCache : public DiskCacheTest {
// cache_ will always have a valid object, regardless of how the cache was
// initialized. The implementation pointers can be NULL.
- disk_cache::Backend* cache_;
+ scoped_ptr<disk_cache::Backend> cache_;
disk_cache::BackendImpl* cache_impl_;
disk_cache::SimpleBackendImpl* simple_cache_impl_;
disk_cache::MemBackendImpl* mem_cache_;
diff --git a/net/disk_cache/entry_unittest.cc b/net/disk_cache/entry_unittest.cc
index f54cec0..857e07f 100644
--- a/net/disk_cache/entry_unittest.cc
+++ b/net/disk_cache/entry_unittest.cc
@@ -2025,17 +2025,17 @@ TEST_F(DiskCacheEntryTest, MemoryOnlyDoomSparseEntry) {
// way to simulate a race is to execute what we want on the callback.
class SparseTestCompletionCallback: public net::TestCompletionCallback {
public:
- explicit SparseTestCompletionCallback(disk_cache::Backend* cache)
- : cache_(cache) {
+ explicit SparseTestCompletionCallback(scoped_ptr<disk_cache::Backend> cache)
+ : cache_(cache.Pass()) {
}
private:
virtual void SetResult(int result) OVERRIDE {
- delete cache_;
+ cache_.reset();
TestCompletionCallback::SetResult(result);
}
- disk_cache::Backend* cache_;
+ scoped_ptr<disk_cache::Backend> cache_;
DISALLOW_COPY_AND_ASSIGN(SparseTestCompletionCallback);
};
@@ -2063,13 +2063,11 @@ TEST_F(DiskCacheEntryTest, DoomSparseEntry2) {
EXPECT_EQ(9, cache_->GetEntryCount());
entry->Close();
- SparseTestCompletionCallback cb(cache_);
- int rv = cache_->DoomEntry(key, cb.callback());
+ disk_cache::Backend* cache = cache_.get();
+ SparseTestCompletionCallback cb(cache_.Pass());
+ int rv = cache->DoomEntry(key, cb.callback());
EXPECT_EQ(net::ERR_IO_PENDING, rv);
EXPECT_EQ(net::OK, cb.WaitForResult());
-
- // TearDown will attempt to delete the cache_.
- cache_ = NULL;
}
void DiskCacheEntryTest::PartialSparseEntry() {
diff --git a/net/disk_cache/mem_backend_impl.cc b/net/disk_cache/mem_backend_impl.cc
index fe5ea25..a6f1bf1 100644
--- a/net/disk_cache/mem_backend_impl.cc
+++ b/net/disk_cache/mem_backend_impl.cc
@@ -41,15 +41,15 @@ MemBackendImpl::~MemBackendImpl() {
}
// Static.
-Backend* MemBackendImpl::CreateBackend(int max_bytes, net::NetLog* net_log) {
- MemBackendImpl* cache = new MemBackendImpl(net_log);
+scoped_ptr<Backend> MemBackendImpl::CreateBackend(int max_bytes,
+ net::NetLog* net_log) {
+ scoped_ptr<MemBackendImpl> cache(new MemBackendImpl(net_log));
cache->SetMaxSize(max_bytes);
if (cache->Init())
- return cache;
+ return cache.PassAs<Backend>();
- delete cache;
LOG(ERROR) << "Unable to create cache";
- return NULL;
+ return scoped_ptr<Backend>();
}
bool MemBackendImpl::Init() {
diff --git a/net/disk_cache/mem_backend_impl.h b/net/disk_cache/mem_backend_impl.h
index bf15f6e..8da39cc 100644
--- a/net/disk_cache/mem_backend_impl.h
+++ b/net/disk_cache/mem_backend_impl.h
@@ -32,7 +32,7 @@ class NET_EXPORT_PRIVATE MemBackendImpl : public Backend {
// size the cache can grow to. If zero is passed in as max_bytes, the cache
// will determine the value to use based on the available memory. The returned
// pointer can be NULL if a fatal error is found.
- static Backend* CreateBackend(int max_bytes, net::NetLog* net_log);
+ static scoped_ptr<Backend> CreateBackend(int max_bytes, net::NetLog* net_log);
// Performs general initialization for this current instance of the cache.
bool Init();
diff --git a/net/disk_cache/tracing_cache_backend.cc b/net/disk_cache/tracing_cache_backend.cc
index b10c974..4966133 100644
--- a/net/disk_cache/tracing_cache_backend.cc
+++ b/net/disk_cache/tracing_cache_backend.cc
@@ -176,8 +176,8 @@ EntryProxy::~EntryProxy() {
}
}
-TracingCacheBackend::TracingCacheBackend(Backend* backend)
- : backend_(backend) {
+TracingCacheBackend::TracingCacheBackend(scoped_ptr<Backend> backend)
+ : backend_(backend.Pass()) {
}
TracingCacheBackend::~TracingCacheBackend() {
diff --git a/net/disk_cache/tracing_cache_backend.h b/net/disk_cache/tracing_cache_backend.h
index 60fb9c2..304a733 100644
--- a/net/disk_cache/tracing_cache_backend.h
+++ b/net/disk_cache/tracing_cache_backend.h
@@ -19,7 +19,7 @@ class EntryProxy;
class NET_EXPORT TracingCacheBackend : public Backend,
public base::SupportsWeakPtr<TracingCacheBackend> {
public:
- explicit TracingCacheBackend(Backend* backend);
+ explicit TracingCacheBackend(scoped_ptr<Backend> backend);
virtual net::CacheType GetCacheType() const OVERRIDE;
virtual int32 GetEntryCount() const OVERRIDE;