Commit 12017303 authored by jkarlin's avatar jkarlin Committed by Commit bot

[CacheStorage] Check quota before put operations

BUG=410201

Review URL: https://codereview.chromium.org/1636613002

Cr-Commit-Position: refs/heads/master@{#371785}
parent 83e906aa
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "base/strings/string_util.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.pb.h"
#include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h" #include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h"
#include "content/browser/cache_storage/cache_storage_scheduler.h" #include "content/browser/cache_storage/cache_storage_scheduler.h"
...@@ -58,9 +59,9 @@ typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback; ...@@ -58,9 +59,9 @@ typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback;
enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY };
// The maximum size of an individual cache. Ultimately cache size is controlled // The maximum size of each cache. Ultimately, cache size
// per-origin. // is controlled per-origin by the QuotaManager.
const int kMaxCacheBytes = 512 * 1024 * 1024; const int kMaxCacheBytes = std::numeric_limits<int>::max();
void NotReachedCompletionCallback(int rv) { void NotReachedCompletionCallback(int rv) {
NOTREACHED(); NOTREACHED();
...@@ -276,6 +277,7 @@ struct CacheStorageCache::PutContext { ...@@ -276,6 +277,7 @@ struct CacheStorageCache::PutContext {
scoped_refptr<net::URLRequestContextGetter> request_context_getter; scoped_refptr<net::URLRequestContextGetter> request_context_getter;
scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy;
disk_cache::ScopedEntryPtr cache_entry; disk_cache::ScopedEntryPtr cache_entry;
int64_t available_bytes = 0;
private: private:
DISALLOW_COPY_AND_ASSIGN(PutContext); DISALLOW_COPY_AND_ASSIGN(PutContext);
...@@ -794,6 +796,31 @@ void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, ...@@ -794,6 +796,31 @@ void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context,
return; 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*()); scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*());
disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
...@@ -860,6 +887,12 @@ void CacheStorageCache::PutDidCreateEntry( ...@@ -860,6 +887,12 @@ void CacheStorageCache::PutDidCreateEntry(
scoped_refptr<net::StringIOBuffer> buffer( scoped_refptr<net::StringIOBuffer> buffer(
new net::StringIOBuffer(std::move(serialized))); 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. // 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(); disk_cache::Entry* temp_entry_ptr = put_context->cache_entry.get();
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "content/common/cache_storage/cache_storage_types.h" #include "content/common/cache_storage/cache_storage_types.h"
#include "content/common/service_worker/service_worker_types.h" #include "content/common/service_worker/service_worker_types.h"
#include "net/disk_cache/disk_cache.h" #include "net/disk_cache/disk_cache.h"
#include "storage/common/quota/quota_status_code.h"
namespace net { namespace net {
class URLRequestContextGetter; class URLRequestContextGetter;
...@@ -190,6 +191,10 @@ class CONTENT_EXPORT CacheStorageCache ...@@ -190,6 +191,10 @@ class CONTENT_EXPORT CacheStorageCache
void PutImpl(scoped_ptr<PutContext> put_context); void PutImpl(scoped_ptr<PutContext> put_context);
void PutDidDelete(scoped_ptr<PutContext> put_context, void PutDidDelete(scoped_ptr<PutContext> put_context,
CacheStorageError delete_error); 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, void PutDidCreateEntry(scoped_ptr<disk_cache::Entry*> entry_ptr,
scoped_ptr<PutContext> put_context, scoped_ptr<PutContext> put_context,
int rv); int rv);
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "content/common/service_worker/service_worker_types.h" #include "content/common/service_worker/service_worker_types.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/common/referrer.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_context.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
#include "net/base/test_completion_callback.h" #include "net/base/test_completion_callback.h"
...@@ -41,6 +42,7 @@ namespace content { ...@@ -41,6 +42,7 @@ namespace content {
namespace { namespace {
const char kTestData[] = "Hello World"; const char kTestData[] = "Hello World";
const char kOrigin[] = "http://example.com";
// Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns
// the memory. // the memory.
...@@ -264,8 +266,19 @@ class CacheStorageCacheTest : public testing::Test { ...@@ -264,8 +266,19 @@ class CacheStorageCacheTest : public testing::Test {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
blob_storage_context_ = blob_storage_context->context(); 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( 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_.reset(new net::URLRequestJobFactoryImpl);
url_request_job_factory_->SetProtocolHandler( url_request_job_factory_->SetProtocolHandler(
...@@ -278,12 +291,8 @@ class CacheStorageCacheTest : public testing::Test { ...@@ -278,12 +291,8 @@ class CacheStorageCacheTest : public testing::Test {
CreateRequests(blob_storage_context); 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( 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())); quota_manager_proxy_, blob_storage_context->context()->AsWeakPtr()));
} }
...@@ -514,6 +523,8 @@ class CacheStorageCacheTest : public testing::Test { ...@@ -514,6 +523,8 @@ class CacheStorageCacheTest : public testing::Test {
TestBrowserContext browser_context_; TestBrowserContext browser_context_;
TestBrowserThreadBundle browser_thread_bundle_; TestBrowserThreadBundle browser_thread_bundle_;
scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; 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_; scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
storage::BlobStorageContext* blob_storage_context_; storage::BlobStorageContext* blob_storage_context_;
...@@ -936,6 +947,13 @@ TEST_P(CacheStorageCacheTestP, QuotaManagerModified) { ...@@ -936,6 +947,13 @@ TEST_P(CacheStorageCacheTestP, QuotaManagerModified) {
EXPECT_EQ(0, sum_delta); 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) { TEST_F(CacheStorageCacheMemoryOnlyTest, MemoryBackedSize) {
EXPECT_EQ(0, cache_->MemoryBackedSize()); EXPECT_EQ(0, cache_->MemoryBackedSize());
EXPECT_TRUE(Put(no_body_request_, no_body_response_)); EXPECT_TRUE(Put(no_body_request_, no_body_response_));
......
...@@ -43,6 +43,8 @@ blink::WebServiceWorkerCacheError ToWebServiceWorkerCacheError( ...@@ -43,6 +43,8 @@ blink::WebServiceWorkerCacheError ToWebServiceWorkerCacheError(
return blink::WebServiceWorkerCacheErrorNotFound; return blink::WebServiceWorkerCacheErrorNotFound;
case CACHE_STORAGE_ERROR_NOT_FOUND: case CACHE_STORAGE_ERROR_NOT_FOUND:
return blink::WebServiceWorkerCacheErrorNotFound; return blink::WebServiceWorkerCacheErrorNotFound;
case CACHE_STORAGE_ERROR_QUOTA_EXCEEDED:
return blink::WebServiceWorkerCacheErrorQuotaExceeded;
} }
NOTREACHED(); NOTREACHED();
return blink::WebServiceWorkerCacheErrorNotImplemented; return blink::WebServiceWorkerCacheErrorNotImplemented;
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "content/browser/quota/mock_quota_manager_proxy.h" #include "content/browser/quota/mock_quota_manager_proxy.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/cache_storage_usage_info.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_context.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
...@@ -75,19 +76,25 @@ class CacheStorageManagerTest : public testing::Test { ...@@ -75,19 +76,25 @@ class CacheStorageManagerTest : public testing::Test {
url_request_context->set_job_factory(url_request_job_factory_.get()); url_request_context->set_job_factory(url_request_job_factory_.get());
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(origin1_), storage::kStorageTypeTemporary, 1024 * 1024 * 100);
mock_quota_manager_->SetQuota(
GURL(origin2_), storage::kStorageTypeTemporary, 1024 * 1024 * 100);
quota_manager_proxy_ = new MockQuotaManagerProxy( quota_manager_proxy_ = new MockQuotaManagerProxy(
nullptr, base::ThreadTaskRunnerHandle::Get().get()); mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get());
if (MemoryOnly()) { cache_manager_ = CacheStorageManager::Create(
cache_manager_ = CacheStorageManager::Create( temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(),
base::FilePath(), base::ThreadTaskRunnerHandle::Get(), quota_manager_proxy_);
quota_manager_proxy_);
} else {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
cache_manager_ = CacheStorageManager::Create(
temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(),
quota_manager_proxy_);
}
cache_manager_->SetBlobParametersForCache( cache_manager_->SetBlobParametersForCache(
browser_context_.GetRequestContext(), browser_context_.GetRequestContext(),
...@@ -307,6 +314,8 @@ class CacheStorageManagerTest : public testing::Test { ...@@ -307,6 +314,8 @@ class CacheStorageManagerTest : public testing::Test {
scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
storage::BlobStorageContext* blob_storage_context_; storage::BlobStorageContext* blob_storage_context_;
scoped_refptr<MockSpecialStoragePolicy> quota_policy_;
scoped_refptr<MockQuotaManager> mock_quota_manager_;
scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_;
scoped_ptr<CacheStorageManager> cache_manager_; scoped_ptr<CacheStorageManager> cache_manager_;
......
...@@ -36,6 +36,16 @@ void MockQuotaManagerProxy::SimulateQuotaManagerDestroyed() { ...@@ -36,6 +36,16 @@ void MockQuotaManagerProxy::SimulateQuotaManagerDestroyed() {
} }
} }
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( void MockQuotaManagerProxy::NotifyStorageAccessed(
QuotaClient::ID client_id, const GURL& origin, StorageType type) { QuotaClient::ID client_id, const GURL& origin, StorageType type) {
++storage_accessed_count_; ++storage_accessed_count_;
......
...@@ -41,7 +41,7 @@ class MockQuotaManagerProxy : public QuotaManagerProxy { ...@@ -41,7 +41,7 @@ class MockQuotaManagerProxy : public QuotaManagerProxy {
base::SequencedTaskRunner* original_task_runner, base::SequencedTaskRunner* original_task_runner,
const GURL& origin, const GURL& origin,
StorageType type, StorageType type,
const QuotaManager::GetUsageAndQuotaCallback& callback) override {} const QuotaManager::GetUsageAndQuotaCallback& callback) override;
// Validates the |client_id| and updates the internal access count // Validates the |client_id| and updates the internal access count
// which can be accessed via notify_storage_accessed_count(). // which can be accessed via notify_storage_accessed_count().
......
...@@ -53,7 +53,8 @@ enum CacheStorageError { ...@@ -53,7 +53,8 @@ enum CacheStorageError {
CACHE_STORAGE_ERROR_EXISTS, CACHE_STORAGE_ERROR_EXISTS,
CACHE_STORAGE_ERROR_STORAGE, CACHE_STORAGE_ERROR_STORAGE,
CACHE_STORAGE_ERROR_NOT_FOUND, 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 } // namespace content
......
...@@ -20,6 +20,8 @@ DOMException* CacheStorageError::createException(WebServiceWorkerCacheError webE ...@@ -20,6 +20,8 @@ DOMException* CacheStorageError::createException(WebServiceWorkerCacheError webE
return DOMException::create(NotFoundError, "Entry was not found."); return DOMException::create(NotFoundError, "Entry was not found.");
case WebServiceWorkerCacheErrorExists: case WebServiceWorkerCacheErrorExists:
return DOMException::create(InvalidAccessError, "Entry already exists."); return DOMException::create(InvalidAccessError, "Entry already exists.");
case WebServiceWorkerCacheErrorQuotaExceeded:
return DOMException::create(QuotaExceededError, "Quota exceeded.");
default: default:
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
return DOMException::create(NotSupportedError, "Unknown error."); return DOMException::create(NotSupportedError, "Unknown error.");
......
...@@ -11,6 +11,7 @@ enum WebServiceWorkerCacheError { ...@@ -11,6 +11,7 @@ enum WebServiceWorkerCacheError {
WebServiceWorkerCacheErrorNotImplemented, WebServiceWorkerCacheErrorNotImplemented,
WebServiceWorkerCacheErrorNotFound, WebServiceWorkerCacheErrorNotFound,
WebServiceWorkerCacheErrorExists, WebServiceWorkerCacheErrorExists,
WebServiceWorkerCacheErrorQuotaExceeded,
WebServiceWorkerCacheErrorLast = WebServiceWorkerCacheErrorExists WebServiceWorkerCacheErrorLast = WebServiceWorkerCacheErrorExists
}; };
......
...@@ -77249,6 +77249,7 @@ To add a new entry, add it with any value and run test to compute valid value. ...@@ -77249,6 +77249,7 @@ To add a new entry, add it with any value and run test to compute valid value.
<int value="1" label="Exists Error"/> <int value="1" label="Exists Error"/>
<int value="2" label="Storage Error"/> <int value="2" label="Storage Error"/>
<int value="3" label="Not Found Error"/> <int value="3" label="Not Found Error"/>
<int value="4" label="Quota Exceeded Error"/>
</enum> </enum>
<enum name="ServiceWorkerCacheResponseType" type="int"> <enum name="ServiceWorkerCacheResponseType" type="int">
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment