diff options
Diffstat (limited to 'net')
-rw-r--r-- | net/http/http_cache.cc | 38 | ||||
-rw-r--r-- | net/http/http_cache.h | 7 | ||||
-rw-r--r-- | net/http/http_cache_unittest.cc | 12 | ||||
-rw-r--r-- | net/url_request/view_cache_helper.cc | 2 |
4 files changed, 37 insertions, 22 deletions
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) { |