[CacheStorage] Check quota before put operations
BUG=410201
Review URL: https://codereview.chromium.org/1636613002
Cr-Commit-Position: refs/heads/master@{#371785}
diff --git a/content/browser/cache_storage/cache_storage_cache.cc b/content/browser/cache_storage/cache_storage_cache.cc
index 2fc7411..8d4f811 100644
--- a/content/browser/cache_storage/cache_storage_cache.cc
+++ b/content/browser/cache_storage/cache_storage_cache.cc
@@ -15,6 +15,7 @@
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
+#include "base/thread_task_runner_handle.h"
#include "content/browser/cache_storage/cache_storage.pb.h"
#include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h"
#include "content/browser/cache_storage/cache_storage_scheduler.h"
@@ -58,9 +59,9 @@
enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY };
-// The maximum size of an individual cache. Ultimately cache size is controlled
-// per-origin.
-const int kMaxCacheBytes = 512 * 1024 * 1024;
+// The maximum size of each cache. Ultimately, cache size
+// is controlled per-origin by the QuotaManager.
+const int kMaxCacheBytes = std::numeric_limits<int>::max();
void NotReachedCompletionCallback(int rv) {
NOTREACHED();
@@ -276,6 +277,7 @@
scoped_refptr<net::URLRequestContextGetter> request_context_getter;
scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy;
disk_cache::ScopedEntryPtr cache_entry;
+ int64_t available_bytes = 0;
private:
DISALLOW_COPY_AND_ASSIGN(PutContext);
@@ -794,6 +796,31 @@
return;
}
+ quota_manager_proxy_->GetUsageAndQuota(
+ base::ThreadTaskRunnerHandle::Get().get(), origin_,
+ storage::kStorageTypeTemporary,
+ base::Bind(&CacheStorageCache::PutDidGetUsageAndQuota,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(std::move(put_context))));
+}
+
+void CacheStorageCache::PutDidGetUsageAndQuota(
+ scoped_ptr<PutContext> put_context,
+ storage::QuotaStatusCode status_code,
+ int64_t usage,
+ int64_t quota) {
+ if (backend_state_ != BACKEND_OPEN) {
+ put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
+ return;
+ }
+
+ if (status_code != storage::kQuotaStatusOk) {
+ put_context->callback.Run(CACHE_STORAGE_ERROR_QUOTA_EXCEEDED);
+ return;
+ }
+
+ put_context->available_bytes = quota - usage;
+
scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*());
disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
@@ -860,6 +887,12 @@
scoped_refptr<net::StringIOBuffer> buffer(
new net::StringIOBuffer(std::move(serialized)));
+ int64_t bytes_to_write = buffer->size() + put_context->response->blob_size;
+ if (put_context->available_bytes < bytes_to_write) {
+ put_context->callback.Run(CACHE_STORAGE_ERROR_QUOTA_EXCEEDED);
+ return;
+ }
+
// Get a temporary copy of the entry pointer before passing it in base::Bind.
disk_cache::Entry* temp_entry_ptr = put_context->cache_entry.get();
diff --git a/content/browser/cache_storage/cache_storage_cache.h b/content/browser/cache_storage/cache_storage_cache.h
index a9dcd4c..58ded2e 100644
--- a/content/browser/cache_storage/cache_storage_cache.h
+++ b/content/browser/cache_storage/cache_storage_cache.h
@@ -18,6 +18,7 @@
#include "content/common/cache_storage/cache_storage_types.h"
#include "content/common/service_worker/service_worker_types.h"
#include "net/disk_cache/disk_cache.h"
+#include "storage/common/quota/quota_status_code.h"
namespace net {
class URLRequestContextGetter;
@@ -190,6 +191,10 @@
void PutImpl(scoped_ptr<PutContext> put_context);
void PutDidDelete(scoped_ptr<PutContext> put_context,
CacheStorageError delete_error);
+ void PutDidGetUsageAndQuota(scoped_ptr<PutContext> put_context,
+ storage::QuotaStatusCode status_code,
+ int64_t usage,
+ int64_t quota);
void PutDidCreateEntry(scoped_ptr<disk_cache::Entry*> entry_ptr,
scoped_ptr<PutContext> put_context,
int rv);
diff --git a/content/browser/cache_storage/cache_storage_cache_unittest.cc b/content/browser/cache_storage/cache_storage_cache_unittest.cc
index b440758..ae9567b 100644
--- a/content/browser/cache_storage/cache_storage_cache_unittest.cc
+++ b/content/browser/cache_storage/cache_storage_cache_unittest.cc
@@ -23,6 +23,7 @@
#include "content/common/service_worker/service_worker_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/referrer.h"
+#include "content/public/test/mock_special_storage_policy.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/base/test_completion_callback.h"
@@ -41,6 +42,7 @@
namespace {
const char kTestData[] = "Hello World";
+const char kOrigin[] = "http://example.com";
// Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns
// the memory.
@@ -264,8 +266,19 @@
base::RunLoop().RunUntilIdle();
blob_storage_context_ = blob_storage_context->context();
+ if (!MemoryOnly())
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+
+ quota_policy_ = new MockSpecialStoragePolicy;
+ mock_quota_manager_ = new MockQuotaManager(
+ MemoryOnly() /* is incognito */, temp_dir_.path(),
+ base::ThreadTaskRunnerHandle::Get().get(),
+ base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get());
+ mock_quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypeTemporary,
+ 1024 * 1024 * 100);
+
quota_manager_proxy_ = new MockQuotaManagerProxy(
- nullptr, base::ThreadTaskRunnerHandle::Get().get());
+ mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get());
url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
url_request_job_factory_->SetProtocolHandler(
@@ -278,12 +291,8 @@
CreateRequests(blob_storage_context);
- if (!MemoryOnly())
- ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
- base::FilePath path = MemoryOnly() ? base::FilePath() : temp_dir_.path();
-
cache_ = make_scoped_refptr(new TestCacheStorageCache(
- GURL("http://example.com"), path, browser_context_.GetRequestContext(),
+ GURL(kOrigin), temp_dir_.path(), browser_context_.GetRequestContext(),
quota_manager_proxy_, blob_storage_context->context()->AsWeakPtr()));
}
@@ -514,6 +523,8 @@
TestBrowserContext browser_context_;
TestBrowserThreadBundle browser_thread_bundle_;
scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
+ scoped_refptr<MockSpecialStoragePolicy> quota_policy_;
+ scoped_refptr<MockQuotaManager> mock_quota_manager_;
scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
storage::BlobStorageContext* blob_storage_context_;
@@ -936,6 +947,13 @@
EXPECT_EQ(0, sum_delta);
}
+TEST_P(CacheStorageCacheTestP, PutObeysQuotaLimits) {
+ mock_quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypeTemporary,
+ 0);
+ EXPECT_FALSE(Put(no_body_request_, no_body_response_));
+ EXPECT_EQ(CACHE_STORAGE_ERROR_QUOTA_EXCEEDED, callback_error_);
+}
+
TEST_F(CacheStorageCacheMemoryOnlyTest, MemoryBackedSize) {
EXPECT_EQ(0, cache_->MemoryBackedSize());
EXPECT_TRUE(Put(no_body_request_, no_body_response_));
diff --git a/content/browser/cache_storage/cache_storage_dispatcher_host.cc b/content/browser/cache_storage/cache_storage_dispatcher_host.cc
index 073f57d..d0d5207 100644
--- a/content/browser/cache_storage/cache_storage_dispatcher_host.cc
+++ b/content/browser/cache_storage/cache_storage_dispatcher_host.cc
@@ -43,6 +43,8 @@
return blink::WebServiceWorkerCacheErrorNotFound;
case CACHE_STORAGE_ERROR_NOT_FOUND:
return blink::WebServiceWorkerCacheErrorNotFound;
+ case CACHE_STORAGE_ERROR_QUOTA_EXCEEDED:
+ return blink::WebServiceWorkerCacheErrorQuotaExceeded;
}
NOTREACHED();
return blink::WebServiceWorkerCacheErrorNotImplemented;
diff --git a/content/browser/cache_storage/cache_storage_manager_unittest.cc b/content/browser/cache_storage/cache_storage_manager_unittest.cc
index f8d94976..0f549f62 100644
--- a/content/browser/cache_storage/cache_storage_manager_unittest.cc
+++ b/content/browser/cache_storage/cache_storage_manager_unittest.cc
@@ -24,6 +24,7 @@
#include "content/browser/quota/mock_quota_manager_proxy.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/cache_storage_usage_info.h"
+#include "content/public/test/mock_special_storage_policy.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/url_request_context.h"
@@ -75,19 +76,25 @@
url_request_context->set_job_factory(url_request_job_factory_.get());
- quota_manager_proxy_ = new MockQuotaManagerProxy(
- nullptr, base::ThreadTaskRunnerHandle::Get().get());
-
- if (MemoryOnly()) {
- cache_manager_ = CacheStorageManager::Create(
- base::FilePath(), base::ThreadTaskRunnerHandle::Get(),
- quota_manager_proxy_);
- } else {
+ if (!MemoryOnly())
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
- cache_manager_ = CacheStorageManager::Create(
- temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(),
- quota_manager_proxy_);
- }
+
+ quota_policy_ = new MockSpecialStoragePolicy;
+ mock_quota_manager_ = new MockQuotaManager(
+ MemoryOnly() /* is incognito */, temp_dir_.path(),
+ base::ThreadTaskRunnerHandle::Get().get(),
+ base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get());
+ mock_quota_manager_->SetQuota(
+ GURL(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100);
+ mock_quota_manager_->SetQuota(
+ GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100);
+
+ quota_manager_proxy_ = new MockQuotaManagerProxy(
+ mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get());
+
+ cache_manager_ = CacheStorageManager::Create(
+ temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(),
+ quota_manager_proxy_);
cache_manager_->SetBlobParametersForCache(
browser_context_.GetRequestContext(),
@@ -307,6 +314,8 @@
scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
storage::BlobStorageContext* blob_storage_context_;
+ scoped_refptr<MockSpecialStoragePolicy> quota_policy_;
+ scoped_refptr<MockQuotaManager> mock_quota_manager_;
scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
scoped_ptr<CacheStorageManager> cache_manager_;
diff --git a/content/browser/quota/mock_quota_manager_proxy.cc b/content/browser/quota/mock_quota_manager_proxy.cc
index a8b955d8..697577b 100644
--- a/content/browser/quota/mock_quota_manager_proxy.cc
+++ b/content/browser/quota/mock_quota_manager_proxy.cc
@@ -36,6 +36,16 @@
}
}
+void MockQuotaManagerProxy::GetUsageAndQuota(
+ base::SequencedTaskRunner* original_task_runner,
+ const GURL& origin,
+ StorageType type,
+ const QuotaManager::GetUsageAndQuotaCallback& callback) {
+ if (mock_manager()) {
+ mock_manager()->GetUsageAndQuota(origin, type, callback);
+ }
+}
+
void MockQuotaManagerProxy::NotifyStorageAccessed(
QuotaClient::ID client_id, const GURL& origin, StorageType type) {
++storage_accessed_count_;
diff --git a/content/browser/quota/mock_quota_manager_proxy.h b/content/browser/quota/mock_quota_manager_proxy.h
index 118761d..e550a9d9 100644
--- a/content/browser/quota/mock_quota_manager_proxy.h
+++ b/content/browser/quota/mock_quota_manager_proxy.h
@@ -41,7 +41,7 @@
base::SequencedTaskRunner* original_task_runner,
const GURL& origin,
StorageType type,
- const QuotaManager::GetUsageAndQuotaCallback& callback) override {}
+ const QuotaManager::GetUsageAndQuotaCallback& callback) override;
// Validates the |client_id| and updates the internal access count
// which can be accessed via notify_storage_accessed_count().
diff --git a/content/common/cache_storage/cache_storage_types.h b/content/common/cache_storage/cache_storage_types.h
index bd29296..947c9df 100644
--- a/content/common/cache_storage/cache_storage_types.h
+++ b/content/common/cache_storage/cache_storage_types.h
@@ -53,7 +53,8 @@
CACHE_STORAGE_ERROR_EXISTS,
CACHE_STORAGE_ERROR_STORAGE,
CACHE_STORAGE_ERROR_NOT_FOUND,
- CACHE_STORAGE_ERROR_LAST = CACHE_STORAGE_ERROR_NOT_FOUND
+ CACHE_STORAGE_ERROR_QUOTA_EXCEEDED,
+ CACHE_STORAGE_ERROR_LAST = CACHE_STORAGE_ERROR_QUOTA_EXCEEDED
};
} // namespace content
diff --git a/third_party/WebKit/Source/modules/cachestorage/CacheStorageError.cpp b/third_party/WebKit/Source/modules/cachestorage/CacheStorageError.cpp
index b91682b..114788b 100644
--- a/third_party/WebKit/Source/modules/cachestorage/CacheStorageError.cpp
+++ b/third_party/WebKit/Source/modules/cachestorage/CacheStorageError.cpp
@@ -20,6 +20,8 @@
return DOMException::create(NotFoundError, "Entry was not found.");
case WebServiceWorkerCacheErrorExists:
return DOMException::create(InvalidAccessError, "Entry already exists.");
+ case WebServiceWorkerCacheErrorQuotaExceeded:
+ return DOMException::create(QuotaExceededError, "Quota exceeded.");
default:
ASSERT_NOT_REACHED();
return DOMException::create(NotSupportedError, "Unknown error.");
diff --git a/third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerCacheError.h b/third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerCacheError.h
index a7fc0fe..ba9474f 100644
--- a/third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerCacheError.h
+++ b/third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerCacheError.h
@@ -11,6 +11,7 @@
WebServiceWorkerCacheErrorNotImplemented,
WebServiceWorkerCacheErrorNotFound,
WebServiceWorkerCacheErrorExists,
+ WebServiceWorkerCacheErrorQuotaExceeded,
WebServiceWorkerCacheErrorLast = WebServiceWorkerCacheErrorExists
};
diff --git a/tools/metrics/histograms/histograms.xml b/tools/metrics/histograms/histograms.xml
index dcb59f5..d3cc3988 100644
--- a/tools/metrics/histograms/histograms.xml
+++ b/tools/metrics/histograms/histograms.xml
@@ -77249,6 +77249,7 @@
<int value="1" label="Exists Error"/>
<int value="2" label="Storage Error"/>
<int value="3" label="Not Found Error"/>
+ <int value="4" label="Quota Exceeded Error"/>
</enum>
<enum name="ServiceWorkerCacheResponseType" type="int">