Commit 20976ed7 authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

Add ownership to CacheStorage.

Modify Cache Storage API to include an ownership field. This gives the
ability to create private caches for internal usage, without exposing
the cache entries to developers.

The ownership enum will be part of the key for cache storage map look-ups.
The ownership enum will also be part of the path where the cache is
saved to disk. This way we can have more than one cache for the same
origin, and they are written to different places.

All caches will update the same quota client.

TBR=kenrb@chromium.org,mek@chromium.org

Bug: 838908
Change-Id: I5494d696d9837abbbe2ef428be0f03e4360f2bda
Reviewed-on: https://chromium-review.googlesource.com/1037264
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarJoshua Bell <jsbell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558309}
parent 8326d5f3
...@@ -249,7 +249,7 @@ void CacheStorageDispatcherHost::Has( ...@@ -249,7 +249,7 @@ void CacheStorageDispatcherHost::Has(
if (!ValidState()) if (!ValidState())
return; return;
context_->cache_manager()->HasCache( context_->cache_manager()->HasCache(
origin, base::UTF16ToUTF8(cache_name), origin, CacheStorageOwner::kCacheAPI, base::UTF16ToUTF8(cache_name),
base::BindOnce(&CacheStorageDispatcherHost::OnHasCallback, this, base::BindOnce(&CacheStorageDispatcherHost::OnHasCallback, this,
std::move(callback))); std::move(callback)));
} }
...@@ -267,7 +267,7 @@ void CacheStorageDispatcherHost::Open( ...@@ -267,7 +267,7 @@ void CacheStorageDispatcherHost::Open(
if (!ValidState()) if (!ValidState())
return; return;
context_->cache_manager()->OpenCache( context_->cache_manager()->OpenCache(
origin, base::UTF16ToUTF8(cache_name), origin, CacheStorageOwner::kCacheAPI, base::UTF16ToUTF8(cache_name),
base::BindOnce(&CacheStorageDispatcherHost::OnOpenCallback, this, origin, base::BindOnce(&CacheStorageDispatcherHost::OnOpenCallback, this, origin,
std::move(callback))); std::move(callback)));
} }
...@@ -284,7 +284,9 @@ void CacheStorageDispatcherHost::Delete( ...@@ -284,7 +284,9 @@ void CacheStorageDispatcherHost::Delete(
} }
if (!ValidState()) if (!ValidState())
return; return;
context_->cache_manager()->DeleteCache(origin, base::UTF16ToUTF8(cache_name), context_->cache_manager()->DeleteCache(origin, CacheStorageOwner::kCacheAPI,
base::UTF16ToUTF8(cache_name),
std::move(callback)); std::move(callback));
} }
...@@ -301,8 +303,9 @@ void CacheStorageDispatcherHost::Keys( ...@@ -301,8 +303,9 @@ void CacheStorageDispatcherHost::Keys(
if (!ValidState()) if (!ValidState())
return; return;
context_->cache_manager()->EnumerateCaches( context_->cache_manager()->EnumerateCaches(
origin, base::BindOnce(&CacheStorageDispatcherHost::OnKeysCallback, this, origin, CacheStorageOwner::kCacheAPI,
std::move(callback))); base::BindOnce(&CacheStorageDispatcherHost::OnKeysCallback, this,
std::move(callback)));
} }
void CacheStorageDispatcherHost::Match( void CacheStorageDispatcherHost::Match(
...@@ -324,14 +327,17 @@ void CacheStorageDispatcherHost::Match( ...@@ -324,14 +327,17 @@ void CacheStorageDispatcherHost::Match(
if (match_params.cache_name.is_null()) { if (match_params.cache_name.is_null()) {
context_->cache_manager()->MatchAllCaches( context_->cache_manager()->MatchAllCaches(
origin, std::move(scoped_request), std::move(match_params), origin, CacheStorageOwner::kCacheAPI, std::move(scoped_request),
std::move(match_params),
base::BindOnce(&CacheStorageDispatcherHost::OnMatchCallback, this, base::BindOnce(&CacheStorageDispatcherHost::OnMatchCallback, this,
std::move(callback))); std::move(callback)));
return; return;
} }
context_->cache_manager()->MatchCache( context_->cache_manager()->MatchCache(
origin, base::UTF16ToUTF8(match_params.cache_name.string()), origin, CacheStorageOwner::kCacheAPI,
base::UTF16ToUTF8(match_params.cache_name.string()),
std::move(scoped_request), std::move(match_params), std::move(scoped_request), std::move(match_params),
base::BindOnce(&CacheStorageDispatcherHost::OnMatchCallback, this, base::BindOnce(&CacheStorageDispatcherHost::OnMatchCallback, this,
std::move(callback))); std::move(callback)));
} }
......
...@@ -39,6 +39,20 @@ namespace cache_storage_manager_unittest { ...@@ -39,6 +39,20 @@ namespace cache_storage_manager_unittest {
class CacheStorageManagerTest; class CacheStorageManagerTest;
} }
// WARNING: The enum values are iterated over, so make sure the
// values are contiguous when adding new ones.
enum class CacheStorageOwner {
kMinValue,
// Caches that can be accessed by the JS CacheStorage API (developer facing).
kCacheAPI = kMinValue,
// Private cache to store background fetch downloads.
kBackgroundFetch,
kMaxValue = kBackgroundFetch
};
// Keeps track of a CacheStorage per origin. There is one // Keeps track of a CacheStorage per origin. There is one
// CacheStorageManager per ServiceWorkerContextCore. // CacheStorageManager per ServiceWorkerContextCore.
// TODO(jkarlin): Remove CacheStorage from memory once they're no // TODO(jkarlin): Remove CacheStorage from memory once they're no
...@@ -55,29 +69,36 @@ class CONTENT_EXPORT CacheStorageManager { ...@@ -55,29 +69,36 @@ class CONTENT_EXPORT CacheStorageManager {
// Map a database identifier (computed from an origin) to the path. // Map a database identifier (computed from an origin) to the path.
static base::FilePath ConstructOriginPath(const base::FilePath& root_path, static base::FilePath ConstructOriginPath(const base::FilePath& root_path,
const url::Origin& origin); const url::Origin& origin,
CacheStorageOwner owner);
virtual ~CacheStorageManager(); virtual ~CacheStorageManager();
// Methods to support the CacheStorage spec. These methods call the // Methods to support the CacheStorage spec. These methods call the
// corresponding CacheStorage method on the appropriate thread. // corresponding CacheStorage method on the appropriate thread.
void OpenCache(const url::Origin& origin, void OpenCache(const url::Origin& origin,
CacheStorageOwner owner,
const std::string& cache_name, const std::string& cache_name,
CacheStorage::CacheAndErrorCallback callback); CacheStorage::CacheAndErrorCallback callback);
void HasCache(const url::Origin& origin, void HasCache(const url::Origin& origin,
CacheStorageOwner owner,
const std::string& cache_name, const std::string& cache_name,
CacheStorage::BoolAndErrorCallback callback); CacheStorage::BoolAndErrorCallback callback);
void DeleteCache(const url::Origin& origin, void DeleteCache(const url::Origin& origin,
CacheStorageOwner owner,
const std::string& cache_name, const std::string& cache_name,
CacheStorage::ErrorCallback callback); CacheStorage::ErrorCallback callback);
void EnumerateCaches(const url::Origin& origin, void EnumerateCaches(const url::Origin& origin,
CacheStorageOwner owner,
CacheStorage::IndexCallback callback); CacheStorage::IndexCallback callback);
void MatchCache(const url::Origin& origin, void MatchCache(const url::Origin& origin,
CacheStorageOwner owner,
const std::string& cache_name, const std::string& cache_name,
std::unique_ptr<ServiceWorkerFetchRequest> request, std::unique_ptr<ServiceWorkerFetchRequest> request,
const CacheStorageCacheQueryParams& match_params, const CacheStorageCacheQueryParams& match_params,
CacheStorageCache::ResponseCallback callback); CacheStorageCache::ResponseCallback callback);
void MatchAllCaches(const url::Origin& origin, void MatchAllCaches(const url::Origin& origin,
CacheStorageOwner owner,
std::unique_ptr<ServiceWorkerFetchRequest> request, std::unique_ptr<ServiceWorkerFetchRequest> request,
const CacheStorageCacheQueryParams& match_params, const CacheStorageCacheQueryParams& match_params,
CacheStorageCache::ResponseCallback callback); CacheStorageCache::ResponseCallback callback);
...@@ -106,7 +127,9 @@ class CONTENT_EXPORT CacheStorageManager { ...@@ -106,7 +127,9 @@ class CONTENT_EXPORT CacheStorageManager {
friend class cache_storage_manager_unittest::CacheStorageManagerTest; friend class cache_storage_manager_unittest::CacheStorageManagerTest;
friend class CacheStorageQuotaClient; friend class CacheStorageQuotaClient;
typedef std::map<url::Origin, std::unique_ptr<CacheStorage>> CacheStorageMap; typedef std::map<std::pair<url::Origin, CacheStorageOwner>,
std::unique_ptr<CacheStorage>>
CacheStorageMap;
CacheStorageManager( CacheStorageManager(
const base::FilePath& path, const base::FilePath& path,
...@@ -114,7 +137,8 @@ class CONTENT_EXPORT CacheStorageManager { ...@@ -114,7 +137,8 @@ class CONTENT_EXPORT CacheStorageManager {
scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy); scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy);
// The returned CacheStorage* is owned by this manager. // The returned CacheStorage* is owned by this manager.
CacheStorage* FindOrCreateCacheStorage(const url::Origin& origin); CacheStorage* FindOrCreateCacheStorage(const url::Origin& origin,
CacheStorageOwner owner);
// QuotaClient and Browsing Data Deletion support // QuotaClient and Browsing Data Deletion support
void GetAllOriginsUsage(CacheStorageContext::GetUsageInfoCallback callback); void GetAllOriginsUsage(CacheStorageContext::GetUsageInfoCallback callback);
...@@ -130,10 +154,12 @@ class CONTENT_EXPORT CacheStorageManager { ...@@ -130,10 +154,12 @@ class CONTENT_EXPORT CacheStorageManager {
void DeleteOriginData(const url::Origin& origin, void DeleteOriginData(const url::Origin& origin,
storage::QuotaClient::DeletionCallback callback); storage::QuotaClient::DeletionCallback callback);
void DeleteOriginData(const url::Origin& origin); void DeleteOriginData(const url::Origin& origin);
void NotifyStorageModified(const url::Origin& origin,
std::unique_ptr<CacheStorage> cache_storage,
base::OnceClosure callback,
int64_t origin_size);
void DeleteOriginDidClose(const url::Origin& origin, void DeleteOriginDidClose(const url::Origin& origin,
storage::QuotaClient::DeletionCallback callback, storage::QuotaClient::DeletionCallback callback);
std::unique_ptr<CacheStorage> cache_storage,
int64_t origin_size);
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter() scoped_refptr<net::URLRequestContextGetter> url_request_context_getter()
const { const {
......
...@@ -243,7 +243,8 @@ void RenderMessageFilter::DidGenerateCacheableMetadataInCacheStorage( ...@@ -243,7 +243,8 @@ void RenderMessageFilter::DidGenerateCacheableMetadataInCacheStorage(
memcpy(buf->data(), &data.front(), data.size()); memcpy(buf->data(), &data.front(), data.size());
cache_storage_context_->cache_manager()->OpenCache( cache_storage_context_->cache_manager()->OpenCache(
cache_storage_origin, cache_storage_cache_name, cache_storage_origin, CacheStorageOwner::kCacheAPI,
cache_storage_cache_name,
base::BindOnce(&RenderMessageFilter::OnCacheStorageOpenCallback, base::BindOnce(&RenderMessageFilter::OnCacheStorageOpenCallback,
weak_ptr_factory_.GetWeakPtr(), url, weak_ptr_factory_.GetWeakPtr(), url,
expected_response_time, buf, data.size())); expected_response_time, buf, data.size()));
......
...@@ -2801,7 +2801,7 @@ class CacheStorageSideDataSizeChecker ...@@ -2801,7 +2801,7 @@ class CacheStorageSideDataSizeChecker
void OpenCacheOnIOThread(int* result, const base::Closure& continuation) { void OpenCacheOnIOThread(int* result, const base::Closure& continuation) {
cache_storage_context_->cache_manager()->OpenCache( cache_storage_context_->cache_manager()->OpenCache(
url::Origin::Create(origin_), cache_name_, url::Origin::Create(origin_), CacheStorageOwner::kCacheAPI, cache_name_,
base::BindOnce(&self::OnCacheStorageOpenCallback, this, result, base::BindOnce(&self::OnCacheStorageOpenCallback, this, result,
continuation)); continuation));
} }
......
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