summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjkarlin <jkarlin@chromium.org>2014-10-24 07:29:34 -0700
committerCommit bot <commit-bot@chromium.org>2014-10-24 14:29:58 +0000
commit05431b14d85d159302effdd81850f4376cc9ae15 (patch)
treeeb3f8449c6c088df618a8821c4bf4eef3f0491b6
parent3eb4c81eaed932e31892a8e09028e2ee067fa024 (diff)
downloadchromium_src-05431b14d85d159302effdd81850f4376cc9ae15.zip
chromium_src-05431b14d85d159302effdd81850f4376cc9ae15.tar.gz
chromium_src-05431b14d85d159302effdd81850f4376cc9ae15.tar.bz2
[ServiceWorkerCache] Return the real size of the cache in GetOriginUsage.
The size of the cache on disk is used if the backend is persistent, else the size of the entries in memory is returned. Downstream of: https://codereview.chromium.org/674873002/ BUG=420159 Review URL: https://codereview.chromium.org/672943002 Cr-Commit-Position: refs/heads/master@{#301097}
-rw-r--r--content/browser/service_worker/service_worker_cache.cc36
-rw-r--r--content/browser/service_worker/service_worker_cache.h7
-rw-r--r--content/browser/service_worker/service_worker_cache_storage.cc14
-rw-r--r--content/browser/service_worker/service_worker_cache_storage.h4
-rw-r--r--content/browser/service_worker/service_worker_cache_storage_manager.cc16
-rw-r--r--content/browser/service_worker/service_worker_cache_storage_manager.h2
-rw-r--r--content/browser/service_worker/service_worker_cache_storage_manager_unittest.cc33
-rw-r--r--content/browser/service_worker/service_worker_cache_unittest.cc28
8 files changed, 134 insertions, 6 deletions
diff --git a/content/browser/service_worker/service_worker_cache.cc b/content/browser/service_worker/service_worker_cache.cc
index bde3918..6146de7 100644
--- a/content/browser/service_worker/service_worker_cache.cc
+++ b/content/browser/service_worker/service_worker_cache.cc
@@ -42,6 +42,10 @@ const int kMaxCacheBytes = 512 * 1024 * 1024;
// Buffer size for cache and blob reading/writing.
const int kBufferSize = 1024 * 512;
+void NotReachedCompletionCallback(int rv) {
+ NOTREACHED();
+}
+
blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType(
ServiceWorkerRequestResponseHeaders_ResponseType response_type) {
switch (response_type) {
@@ -966,6 +970,34 @@ void ServiceWorkerCache::Close() {
backend_.reset();
}
+int64 ServiceWorkerCache::MemoryBackedSize() const {
+ if (!backend_ || !memory_only_)
+ return 0;
+
+ scoped_ptr<disk_cache::Backend::Iterator> backend_iter =
+ backend_->CreateIterator();
+ disk_cache::Entry* entry = nullptr;
+
+ int64 sum = 0;
+
+ std::vector<disk_cache::Entry*> entries;
+ int rv = net::OK;
+ while ((rv = backend_iter->OpenNextEntry(
+ &entry, base::Bind(NotReachedCompletionCallback))) == net::OK) {
+ entries.push_back(entry); // Open the entries without mutating them.
+ }
+ DCHECK(rv !=
+ net::ERR_IO_PENDING); // Expect all memory ops to be synchronous.
+
+ for (disk_cache::Entry* entry : entries) {
+ sum += entry->GetDataSize(INDEX_HEADERS) +
+ entry->GetDataSize(INDEX_RESPONSE_BODY);
+ entry->Close();
+ }
+
+ return sum;
+}
+
ServiceWorkerCache::ServiceWorkerCache(
const GURL& origin,
const base::FilePath& path,
@@ -978,6 +1010,7 @@ ServiceWorkerCache::ServiceWorkerCache(
quota_manager_proxy_(quota_manager_proxy),
blob_storage_context_(blob_context),
initialized_(false),
+ memory_only_(path.empty()),
weak_ptr_factory_(this) {
}
@@ -1112,8 +1145,7 @@ void ServiceWorkerCache::CreateBackend(const ErrorCallback& callback) {
DCHECK(!backend_);
// Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction.
- net::CacheType cache_type =
- path_.empty() ? net::MEMORY_CACHE : net::APP_CACHE;
+ net::CacheType cache_type = memory_only_ ? net::MEMORY_CACHE : net::APP_CACHE;
scoped_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr());
diff --git a/content/browser/service_worker/service_worker_cache.h b/content/browser/service_worker/service_worker_cache.h
index 6596609..ae9b7f3 100644
--- a/content/browser/service_worker/service_worker_cache.h
+++ b/content/browser/service_worker/service_worker_cache.h
@@ -90,6 +90,10 @@ class CONTENT_EXPORT ServiceWorkerCache
// Prevent further operations on this object and delete the backend.
void Close();
+ // The size of the cache contents in memory. Returns 0 if the cache backend is
+ // not a memory cache backend.
+ int64 MemoryBackedSize() const;
+
void set_backend(scoped_ptr<disk_cache::Backend> backend) {
backend_ = backend.Pass();
}
@@ -146,6 +150,9 @@ class CONTENT_EXPORT ServiceWorkerCache
bool initialized_;
std::vector<base::Closure> init_callbacks_;
+ // Whether or not to store data in disk or memory.
+ bool memory_only_;
+
base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache);
diff --git a/content/browser/service_worker/service_worker_cache_storage.cc b/content/browser/service_worker/service_worker_cache_storage.cc
index 307f658..b16d54c 100644
--- a/content/browser/service_worker/service_worker_cache_storage.cc
+++ b/content/browser/service_worker/service_worker_cache_storage.cc
@@ -500,6 +500,20 @@ void ServiceWorkerCacheStorage::CloseAllCaches() {
}
}
+int64 ServiceWorkerCacheStorage::MemoryBackedSize() const {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ if (!initialized_ || !memory_only_)
+ return 0;
+
+ int64 sum = 0;
+ for (auto& key_value : cache_map_) {
+ if (key_value.second)
+ sum += key_value.second->MemoryBackedSize();
+ }
+ return sum;
+}
+
// Init is run lazily so that it is called on the proper MessageLoop.
void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
diff --git a/content/browser/service_worker/service_worker_cache_storage.h b/content/browser/service_worker/service_worker_cache_storage.h
index 17757ffc6f..25ab831 100644
--- a/content/browser/service_worker/service_worker_cache_storage.h
+++ b/content/browser/service_worker/service_worker_cache_storage.h
@@ -85,6 +85,10 @@ class CONTENT_EXPORT ServiceWorkerCacheStorage {
void CloseAllCaches();
+ // The size of all of the origin's contents in memory. Returns 0 if the cache
+ // backend is not a memory backend.
+ int64 MemoryBackedSize() const;
+
private:
class MemoryLoader;
class SimpleCacheLoader;
diff --git a/content/browser/service_worker/service_worker_cache_storage_manager.cc b/content/browser/service_worker/service_worker_cache_storage_manager.cc
index 941f5ad..ed3d3467 100644
--- a/content/browser/service_worker/service_worker_cache_storage_manager.cc
+++ b/content/browser/service_worker/service_worker_cache_storage_manager.cc
@@ -185,8 +185,20 @@ void ServiceWorkerCacheStorageManager::GetOriginUsage(
const storage::QuotaClient::GetUsageCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
- // TODO(jkarlin): Get the actual disk usage for the origin.
- callback.Run(0);
+ if (IsMemoryBacked()) {
+ int64 sum = 0;
+ for (const auto& key_value : cache_storage_map_)
+ sum += key_value.second->MemoryBackedSize();
+ callback.Run(sum);
+ return;
+ }
+
+ PostTaskAndReplyWithResult(
+ cache_task_runner_.get(),
+ FROM_HERE,
+ base::Bind(base::ComputeDirectorySize,
+ ConstructOriginPath(root_path_, origin_url)),
+ base::Bind(callback));
}
void ServiceWorkerCacheStorageManager::GetOrigins(
diff --git a/content/browser/service_worker/service_worker_cache_storage_manager.h b/content/browser/service_worker/service_worker_cache_storage_manager.h
index 0d568ac..b549fbb 100644
--- a/content/browser/service_worker/service_worker_cache_storage_manager.h
+++ b/content/browser/service_worker/service_worker_cache_storage_manager.h
@@ -31,6 +31,7 @@ class QuotaManagerProxy;
namespace content {
class ServiceWorkerCacheQuotaClient;
+class ServiceWorkerCacheStorageManagerTest;
// Keeps track of a ServiceWorkerCacheStorage per origin. There is one
// ServiceWorkerCacheStorageManager per ServiceWorkerContextCore.
@@ -79,6 +80,7 @@ class CONTENT_EXPORT ServiceWorkerCacheStorageManager {
private:
friend class ServiceWorkerCacheQuotaClient;
+ friend class ServiceWorkerCacheStorageManagerTest;
typedef std::map<GURL, ServiceWorkerCacheStorage*>
ServiceWorkerCacheStorageMap;
diff --git a/content/browser/service_worker/service_worker_cache_storage_manager_unittest.cc b/content/browser/service_worker/service_worker_cache_storage_manager_unittest.cc
index 5cb341ef..ee78a59 100644
--- a/content/browser/service_worker/service_worker_cache_storage_manager_unittest.cc
+++ b/content/browser/service_worker/service_worker_cache_storage_manager_unittest.cc
@@ -210,6 +210,10 @@ class ServiceWorkerCacheStorageManagerTest : public testing::Test {
return !error;
}
+ ServiceWorkerCacheStorage* CacheStorageForOrigin(const GURL& origin) {
+ return cache_manager_->FindOrCreateServiceWorkerCacheManager(origin);
+ }
+
protected:
TestBrowserContext browser_context_;
TestBrowserThreadBundle browser_thread_bundle_;
@@ -438,6 +442,32 @@ TEST_P(ServiceWorkerCacheStorageManagerTestP, DeleteBeforeRelease) {
EXPECT_TRUE(callback_cache_->AsWeakPtr());
}
+TEST_F(ServiceWorkerCacheStorageManagerMemoryOnlyTest, MemoryBackedSize) {
+ ServiceWorkerCacheStorage* cache_storage = CacheStorageForOrigin(origin1_);
+ EXPECT_EQ(0, cache_storage->MemoryBackedSize());
+
+ EXPECT_TRUE(Open(origin1_, "foo"));
+ scoped_refptr<ServiceWorkerCache> foo_cache = callback_cache_;
+ EXPECT_TRUE(Open(origin1_, "bar"));
+ scoped_refptr<ServiceWorkerCache> bar_cache = callback_cache_;
+ EXPECT_EQ(0, cache_storage->MemoryBackedSize());
+
+ EXPECT_TRUE(CachePut(foo_cache, GURL("http://example.com/foo")));
+ EXPECT_LT(0, cache_storage->MemoryBackedSize());
+ int64 foo_size = cache_storage->MemoryBackedSize();
+
+ EXPECT_TRUE(CachePut(bar_cache, GURL("http://example.com/foo")));
+ EXPECT_EQ(foo_size * 2, cache_storage->MemoryBackedSize());
+}
+
+TEST_F(ServiceWorkerCacheStorageManagerTest, MemoryBackedSizePersistent) {
+ ServiceWorkerCacheStorage* cache_storage = CacheStorageForOrigin(origin1_);
+ EXPECT_EQ(0, cache_storage->MemoryBackedSize());
+ EXPECT_TRUE(Open(origin1_, "foo"));
+ EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
+ EXPECT_EQ(0, cache_storage->MemoryBackedSize());
+}
+
class ServiceWorkerCacheQuotaClientTest
: public ServiceWorkerCacheStorageManagerTest {
protected:
@@ -539,8 +569,7 @@ TEST_P(ServiceWorkerCacheQuotaClientTestP, QuotaGetOriginUsage) {
EXPECT_EQ(0, QuotaGetOriginUsage(origin1_));
EXPECT_TRUE(Open(origin1_, "foo"));
EXPECT_TRUE(CachePut(callback_cache_, GURL("http://example.com/foo")));
- // TODO(jkarlin): Once usage is working properly update this value.
- EXPECT_EQ(0, QuotaGetOriginUsage(origin1_));
+ EXPECT_LT(0, QuotaGetOriginUsage(origin1_));
}
TEST_P(ServiceWorkerCacheQuotaClientTestP, QuotaGetOriginsForType) {
diff --git a/content/browser/service_worker/service_worker_cache_unittest.cc b/content/browser/service_worker/service_worker_cache_unittest.cc
index 2ed040e..0920ed9 100644
--- a/content/browser/service_worker/service_worker_cache_unittest.cc
+++ b/content/browser/service_worker/service_worker_cache_unittest.cc
@@ -300,6 +300,12 @@ class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest,
bool MemoryOnly() override { return !GetParam(); }
};
+class ServiceWorkerCacheMemoryOnlyTest
+ : public ServiceWorkerCacheTest,
+ public testing::WithParamInterface<bool> {
+ bool MemoryOnly() override { return true; }
+};
+
TEST_P(ServiceWorkerCacheTestP, PutNoBody) {
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
EXPECT_TRUE(callback_response_);
@@ -565,6 +571,28 @@ TEST_P(ServiceWorkerCacheTestP, QuotaManagerModified) {
EXPECT_EQ(0, sum_delta);
}
+TEST_F(ServiceWorkerCacheMemoryOnlyTest, MemoryBackedSize) {
+ EXPECT_EQ(0, cache_->MemoryBackedSize());
+ EXPECT_TRUE(Put(no_body_request_, no_body_response_));
+ EXPECT_LT(0, cache_->MemoryBackedSize());
+ int64 no_body_size = cache_->MemoryBackedSize();
+
+ EXPECT_TRUE(Delete(no_body_request_));
+ EXPECT_EQ(0, cache_->MemoryBackedSize());
+
+ EXPECT_TRUE(Put(body_request_, body_response_));
+ EXPECT_LT(no_body_size, cache_->MemoryBackedSize());
+
+ EXPECT_TRUE(Delete(body_request_));
+ EXPECT_EQ(0, cache_->MemoryBackedSize());
+}
+
+TEST_F(ServiceWorkerCacheTest, MemoryBackedSizePersistent) {
+ EXPECT_EQ(0, cache_->MemoryBackedSize());
+ EXPECT_TRUE(Put(no_body_request_, no_body_response_));
+ EXPECT_EQ(0, cache_->MemoryBackedSize());
+}
+
INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest,
ServiceWorkerCacheTestP,
::testing::Values(false, true));