summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--net/http/http_cache.cc66
-rw-r--r--net/http/http_cache.h21
-rw-r--r--net/http/http_cache_unittest.cc13
3 files changed, 84 insertions, 16 deletions
diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc
index 162cc26..585f9b5 100644
--- a/net/http/http_cache.cc
+++ b/net/http/http_cache.cc
@@ -37,8 +37,18 @@ namespace net {
int HttpCache::DefaultBackend::CreateBackend(disk_cache::Backend** backend,
CompletionCallback* callback) {
DCHECK_GE(max_bytes_, 0);
- return disk_cache::CreateCacheBackend(type_, path_, max_bytes_, true,
- thread_, backend, callback);
+ if (callback)
+ return disk_cache::CreateCacheBackend(type_, path_, max_bytes_, true,
+ thread_, backend, callback);
+
+ // This is just old code needed to support synchronous cache creation.
+ // TODO(rvargas): Remove this once all callers provide a callback.
+ if (type_ == MEMORY_CACHE) {
+ *backend = disk_cache::CreateInMemoryCacheBackend(max_bytes_);
+ } else {
+ *backend = disk_cache::CreateCacheBackend(path_, true, max_bytes_, type_);
+ }
+ return (*backend ? OK : ERR_FAILED);
}
//-----------------------------------------------------------------------------
@@ -291,6 +301,19 @@ HttpCache::~HttpCache() {
}
}
+disk_cache::Backend* HttpCache::GetBackend() {
+ if (disk_cache_.get())
+ return disk_cache_.get();
+
+ if (backend_factory_.get()) {
+ disk_cache::Backend* backend;
+ if (OK == backend_factory_->CreateBackend(&backend, NULL))
+ disk_cache_.reset(backend);
+ backend_factory_.reset(); // Reclaim memory.
+ }
+ return disk_cache_.get();
+}
+
int HttpCache::GetBackend(disk_cache::Backend** backend,
CompletionCallback* callback) {
DCHECK(callback != NULL);
@@ -331,6 +354,40 @@ void HttpCache::Suspend(bool suspend) {
}
// static
+bool HttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry,
+ HttpResponseInfo* response_info,
+ bool* response_truncated) {
+ int size = disk_entry->GetDataSize(kResponseInfoIndex);
+
+ scoped_refptr<IOBuffer> buffer = new IOBuffer(size);
+ int rv = disk_entry->ReadData(kResponseInfoIndex, 0, buffer, size, NULL);
+ if (rv != size) {
+ DLOG(ERROR) << "ReadData failed: " << rv;
+ return false;
+ }
+
+ return ParseResponseInfo(buffer->data(), size, response_info,
+ response_truncated);
+}
+
+// static
+bool HttpCache::WriteResponseInfo(disk_cache::Entry* disk_entry,
+ const HttpResponseInfo* response_info,
+ bool skip_transient_headers,
+ bool response_truncated) {
+ Pickle pickle;
+ response_info->Persist(
+ &pickle, skip_transient_headers, response_truncated);
+
+ scoped_refptr<WrappedIOBuffer> data = new WrappedIOBuffer(
+ reinterpret_cast<const char*>(pickle.data()));
+ int len = static_cast<int>(pickle.size());
+
+ return disk_entry->WriteData(kResponseInfoIndex, 0, data, len, NULL,
+ true) == len;
+}
+
+// static
bool HttpCache::ParseResponseInfo(const char* data, int len,
HttpResponseInfo* response_info,
bool* response_truncated) {
@@ -344,10 +401,7 @@ void HttpCache::WriteMetadata(const GURL& url,
if (!buf_len)
return;
- // Do lazy initialization of disk cache if needed.
- if (!disk_cache_.get())
- CreateBackend(NULL, NULL); // We don't care about the result.
-
+ GetBackend();
HttpCache::Transaction* trans =
new HttpCache::Transaction(this, enable_range_support_);
MetadataWriter* writer = new MetadataWriter(trans);
diff --git a/net/http/http_cache.h b/net/http/http_cache.h
index 04dc144..79c2134 100644
--- a/net/http/http_cache.h
+++ b/net/http/http_cache.h
@@ -135,6 +135,12 @@ class HttpCache : public HttpTransactionFactory,
HttpTransactionFactory* network_layer() { return network_layer_.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.
+ // This method is deprecated.
+ disk_cache::Backend* GetBackend();
+
// Retrieves the cache backend for this HttpCache instance. If the backend
// is not initialized yet, this method will initialize it. The return value is
// a network error code, and it could be ERR_IO_PENDING, in which case the
@@ -151,6 +157,21 @@ class HttpCache : public HttpTransactionFactory,
virtual HttpNetworkSession* GetSession();
virtual void Suspend(bool suspend);
+ // Helper function for reading response info from the disk cache. If the
+ // cache doesn't have the whole resource *|request_truncated| is set to true.
+ // NOTE: This method is deprecated.
+ static bool ReadResponseInfo(disk_cache::Entry* disk_entry,
+ HttpResponseInfo* response_info,
+ bool* response_truncated);
+
+ // Helper function for writing response info into the disk cache. If the
+ // cache doesn't have the whole resource |request_truncated| should be true.
+ // NOTE: This method is deprecated.
+ static bool WriteResponseInfo(disk_cache::Entry* disk_entry,
+ const HttpResponseInfo* response_info,
+ bool skip_transient_headers,
+ bool response_truncated);
+
// Given a header data blob, convert it to a response info object.
static bool ParseResponseInfo(const char* data, int len,
HttpResponseInfo* response_info,
diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc
index ed2f377..4337d3b 100644
--- a/net/http/http_cache_unittest.cc
+++ b/net/http/http_cache_unittest.cc
@@ -591,11 +591,7 @@ class MockHttpCache {
return static_cast<MockNetworkLayer*>(http_cache_.network_layer());
}
MockDiskCache* disk_cache() {
- TestCompletionCallback cb;
- disk_cache::Backend* backend;
- int rv = http_cache_.GetBackend(&backend, &cb);
- rv = cb.GetResult(rv);
- return (rv == net::OK) ? static_cast<MockDiskCache*>(backend) : NULL;
+ return static_cast<MockDiskCache*>(http_cache_.GetBackend());
}
// Helper function for reading response info from the disk cache.
@@ -1022,11 +1018,8 @@ TEST(HttpCache, CreateThenDestroy) {
TEST(HttpCache, GetBackend) {
MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0));
- disk_cache::Backend* backend;
- TestCompletionCallback cb;
// This will lazily initialize the backend.
- int rv = cache.http_cache()->GetBackend(&backend, &cb);
- EXPECT_EQ(net::OK, cb.GetResult(rv));
+ EXPECT_TRUE(cache.http_cache()->GetBackend());
}
TEST(HttpCache, SimpleGET) {
@@ -1083,7 +1076,7 @@ TEST(HttpCache, SimpleGETNoDiskCache2) {
RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction);
EXPECT_EQ(1, cache.network_layer()->transaction_count());
- EXPECT_FALSE(cache.http_cache()->GetCurrentBackend());
+ EXPECT_FALSE(cache.http_cache()->GetBackend());
}
TEST(HttpCache, SimpleGETWithDiskFailures) {