Skip to content

Commit

Permalink
Clear disk cache when the cache is not initialized
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hclam@chromium.org committed Nov 7, 2009
1 parent 09fe949 commit cfc076e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
10 changes: 4 additions & 6 deletions chrome/browser/browsing_data_remover.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
38 changes: 20 additions & 18 deletions net/http/http_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
}
Expand All @@ -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) {
}
Expand All @@ -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) {
}
Expand All @@ -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) {
}
Expand All @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions net/http/http_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -205,7 +209,6 @@ class HttpCache : public HttpTransactionFactory,

ScopedRunnableMethodFactory<HttpCache> task_factory_;

bool in_memory_cache_;
bool enable_range_support_;
int cache_size_;

Expand Down
12 changes: 11 additions & 1 deletion net/http/http_cache_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion net/url_request/view_cache_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit cfc076e

Please sign in to comment.