summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-07 02:27:23 +0000
committerhclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-07 02:27:23 +0000
commitcfc076ec466fc48e91c309448c4e5be3467b42c7 (patch)
tree721e6d222b9ce7411d4d925cdd7de7d06d9d65e4
parent09fe9499ec1cb0d327a03e433022cb8603a16933 (diff)
downloadchromium_src-cfc076ec466fc48e91c309448c4e5be3467b42c7.zip
chromium_src-cfc076ec466fc48e91c309448c4e5be3467b42c7.tar.gz
chromium_src-cfc076ec466fc48e91c309448c4e5be3467b42c7.tar.bz2
Clear disk cache when the cache is not initialized
BUG=24765 TEST=unit test, clear browsing data and the cache, media cache will be cleared even if a media object was not loaded. Since the disk cache backend in HttpCache is lazily initialized, clearing the cache before it receives the first transaction would have no effect. So initialize the disk cache explicitly when we clear the cache. Review URL: http://codereview.chromium.org/378015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31361 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/browsing_data_remover.cc10
-rw-r--r--net/http/http_cache.cc38
-rw-r--r--net/http/http_cache.h7
-rw-r--r--net/http/http_cache_unittest.cc12
-rw-r--r--net/url_request/view_cache_helper.cc2
5 files changed, 41 insertions, 28 deletions
diff --git a/chrome/browser/browsing_data_remover.cc b/chrome/browser/browsing_data_remover.cc
index 0a646fa..4477ce7 100644
--- a/chrome/browser/browsing_data_remover.cc
+++ b/chrome/browser/browsing_data_remover.cc
@@ -240,10 +240,9 @@ void BrowsingDataRemover::ClearCacheOnIOThread(
// Get a pointer to the cache.
net::HttpTransactionFactory* factory =
main_context_getter->GetURLRequestContext()->http_transaction_factory();
- disk_cache::Backend* cache = factory->GetCache()->disk_cache();
+ disk_cache::Backend* cache = factory->GetCache()->GetBackend();
- // |cache| can be null since it is lazily initialized, in this case we do
- // nothing.
+ // |cache| can be null if it cannot be initialized.
if (cache) {
if (delete_begin.is_null())
cache->DoomAllEntries();
@@ -254,10 +253,9 @@ void BrowsingDataRemover::ClearCacheOnIOThread(
// Get a pointer to the media cache.
factory = media_context_getter->GetURLRequestContext()->
http_transaction_factory();
- cache = factory->GetCache()->disk_cache();
+ cache = factory->GetCache()->GetBackend();
- // |cache| can be null since it is lazily initialized, in this case we do
- // nothing.
+ // |cache| can be null if it cannot be initialized.
if (cache) {
if (delete_begin.is_null())
cache->DoomAllEntries();
diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc
index b7158f0..8f5db67 100644
--- a/net/http/http_cache.cc
+++ b/net/http/http_cache.cc
@@ -867,7 +867,7 @@ void HttpCache::Transaction::SetRequest(LoadLog* load_log,
bool HttpCache::Transaction::ShouldPassThrough() {
// We may have a null disk_cache if there is an error we cannot recover from,
// like not enough disk space, or sharing violations.
- if (!cache_->disk_cache())
+ if (!cache_->disk_cache_.get())
return true;
// When using the record/playback modes, we always use the cache
@@ -1642,7 +1642,6 @@ HttpCache::HttpCache(HostResolver* host_resolver,
network_layer_(HttpNetworkLayer::CreateFactory(
host_resolver, proxy_service, ssl_config_service)),
ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
- in_memory_cache_(false),
enable_range_support_(true),
cache_size_(cache_size) {
}
@@ -1655,7 +1654,6 @@ HttpCache::HttpCache(HttpNetworkSession* session,
type_(DISK_CACHE),
network_layer_(HttpNetworkLayer::CreateFactory(session)),
ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
- in_memory_cache_(false),
enable_range_support_(true),
cache_size_(cache_size) {
}
@@ -1669,7 +1667,6 @@ HttpCache::HttpCache(HostResolver* host_resolver,
network_layer_(HttpNetworkLayer::CreateFactory(
host_resolver, proxy_service, ssl_config_service)),
ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
- in_memory_cache_(true),
enable_range_support_(true),
cache_size_(cache_size) {
}
@@ -1681,7 +1678,6 @@ HttpCache::HttpCache(HttpTransactionFactory* network_layer,
network_layer_(network_layer),
disk_cache_(disk_cache),
ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
- in_memory_cache_(false),
enable_range_support_(true),
cache_size_(0) {
}
@@ -1705,21 +1701,27 @@ HttpCache::~HttpCache() {
delete *it;
}
+disk_cache::Backend* HttpCache::GetBackend() {
+ if (disk_cache_.get())
+ return disk_cache_.get();
+
+ DCHECK_GE(cache_size_, 0);
+ if (type_ == MEMORY_CACHE) {
+ // We may end up with no folder name and no cache if the initialization
+ // of the disk cache fails. We want to be sure that what we wanted to have
+ // was an in-memory cache.
+ disk_cache_.reset(disk_cache::CreateInMemoryCacheBackend(cache_size_));
+ } else if (!disk_cache_dir_.empty()) {
+ disk_cache_.reset(disk_cache::CreateCacheBackend(disk_cache_dir_, true,
+ cache_size_, type_));
+ disk_cache_dir_ = FilePath(); // Reclaim memory.
+ }
+ return disk_cache_.get();
+}
+
int HttpCache::CreateTransaction(scoped_ptr<HttpTransaction>* trans) {
// Do lazy initialization of disk cache if needed.
- if (!disk_cache_.get()) {
- DCHECK_GE(cache_size_, 0);
- if (in_memory_cache_) {
- // We may end up with no folder name and no cache if the initialization
- // of the disk cache fails. We want to be sure that what we wanted to have
- // was an in-memory cache.
- disk_cache_.reset(disk_cache::CreateInMemoryCacheBackend(cache_size_));
- } else if (!disk_cache_dir_.empty()) {
- disk_cache_.reset(disk_cache::CreateCacheBackend(disk_cache_dir_, true,
- cache_size_, type_));
- disk_cache_dir_ = FilePath(); // Reclaim memory.
- }
- }
+ GetBackend();
trans->reset(new HttpCache::Transaction(this, enable_range_support_));
return OK;
}
diff --git a/net/http/http_cache.h b/net/http/http_cache.h
index 68347f9..7926d5d 100644
--- a/net/http/http_cache.h
+++ b/net/http/http_cache.h
@@ -93,7 +93,11 @@ class HttpCache : public HttpTransactionFactory,
disk_cache::Backend* disk_cache);
HttpTransactionFactory* network_layer() { return network_layer_.get(); }
- disk_cache::Backend* disk_cache() { return disk_cache_.get(); }
+
+ // Returns the cache backend for this HttpCache instance. If the backend
+ // is not initialized yet, this method will initialize it. If the return
+ // value is NULL then the backend cannot be initialized.
+ disk_cache::Backend* GetBackend();
// HttpTransactionFactory implementation:
virtual int CreateTransaction(scoped_ptr<HttpTransaction>* trans);
@@ -205,7 +209,6 @@ class HttpCache : public HttpTransactionFactory,
ScopedRunnableMethodFactory<HttpCache> task_factory_;
- bool in_memory_cache_;
bool enable_range_support_;
int cache_size_;
diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc
index 0378ca0..f7db18c 100644
--- a/net/http/http_cache_unittest.cc
+++ b/net/http/http_cache_unittest.cc
@@ -8,6 +8,7 @@
#include "base/message_loop.h"
#include "base/scoped_vector.h"
#include "base/string_util.h"
+#include "net/base/cache_type.h"
#include "net/base/net_errors.h"
#include "net/base/load_flags.h"
#include "net/base/load_log_unittest.h"
@@ -458,7 +459,7 @@ class MockHttpCache {
return static_cast<MockNetworkLayer*>(http_cache_.network_layer());
}
MockDiskCache* disk_cache() {
- return static_cast<MockDiskCache*>(http_cache_.disk_cache());
+ return static_cast<MockDiskCache*>(http_cache_.GetBackend());
}
private:
@@ -768,6 +769,15 @@ TEST(HttpCache, CreateThenDestroy) {
ASSERT_TRUE(trans.get());
}
+TEST(HttpCache, GetBackend) {
+ // This will initialize a cache object with NULL backend.
+ MockHttpCache cache(NULL);
+
+ // This will lazily initialize the backend.
+ cache.http_cache()->set_type(net::MEMORY_CACHE);
+ EXPECT_TRUE(cache.http_cache()->GetBackend());
+}
+
TEST(HttpCache, SimpleGET) {
MockHttpCache cache;
diff --git a/net/url_request/view_cache_helper.cc b/net/url_request/view_cache_helper.cc
index 1e66d21..80176cc 100644
--- a/net/url_request/view_cache_helper.cc
+++ b/net/url_request/view_cache_helper.cc
@@ -116,7 +116,7 @@ static disk_cache::Backend* GetDiskCache(URLRequestContext* context) {
if (!http_cache)
return NULL;
- return http_cache->disk_cache();
+ return http_cache->GetBackend();
}
static std::string FormatStatistics(disk_cache::Backend* disk_cache) {