Commit 098fdfb4 authored by jkarlin's avatar jkarlin Committed by Commit bot

Creates CacheContext for ServiceWorkerCacheStorage.

Creates CacheContext which the CacheStorage uses to keep track of
Cache metadata rather than sticking it in the ServiceWorkerCache where
it doesn't belong.  The CacheContext will be used in a downstream CL
to keep reference count information for each cache.

This CL also changes the CacheMap from an IDMap to a std::map with our own id counting.
This lets us change to int64 from int32 as well as helps the transition to reference counting the caches.

Some other upcoming CLs affecting CacheStorage:
* Reference count CacheContext so that the deletion of javascript objects can delete the ServiceWorkerCache
* Make the callbacks in ServiceWorkerCacheStorage unnamed functions that don't require weak pointers.
* Change the CacheID to int64.

BUG=392621

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

Cr-Commit-Position: refs/heads/master@{#291906}
parent 27772016
...@@ -565,21 +565,19 @@ void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback, ...@@ -565,21 +565,19 @@ void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback,
// static // static
scoped_ptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache( scoped_ptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache(
const std::string& name,
net::URLRequestContext* request_context, net::URLRequestContext* request_context,
base::WeakPtr<storage::BlobStorageContext> blob_context) { base::WeakPtr<storage::BlobStorageContext> blob_context) {
return make_scoped_ptr(new ServiceWorkerCache( return make_scoped_ptr(
base::FilePath(), name, request_context, blob_context)); new ServiceWorkerCache(base::FilePath(), request_context, blob_context));
} }
// static // static
scoped_ptr<ServiceWorkerCache> ServiceWorkerCache::CreatePersistentCache( scoped_ptr<ServiceWorkerCache> ServiceWorkerCache::CreatePersistentCache(
const base::FilePath& path, const base::FilePath& path,
const std::string& name,
net::URLRequestContext* request_context, net::URLRequestContext* request_context,
base::WeakPtr<storage::BlobStorageContext> blob_context) { base::WeakPtr<storage::BlobStorageContext> blob_context) {
return make_scoped_ptr( return make_scoped_ptr(
new ServiceWorkerCache(path, name, request_context, blob_context)); new ServiceWorkerCache(path, request_context, blob_context));
} }
ServiceWorkerCache::~ServiceWorkerCache() { ServiceWorkerCache::~ServiceWorkerCache() {
...@@ -709,14 +707,11 @@ bool ServiceWorkerCache::HasCreatedBackend() const { ...@@ -709,14 +707,11 @@ bool ServiceWorkerCache::HasCreatedBackend() const {
ServiceWorkerCache::ServiceWorkerCache( ServiceWorkerCache::ServiceWorkerCache(
const base::FilePath& path, const base::FilePath& path,
const std::string& name,
net::URLRequestContext* request_context, net::URLRequestContext* request_context,
base::WeakPtr<storage::BlobStorageContext> blob_context) base::WeakPtr<storage::BlobStorageContext> blob_context)
: path_(path), : path_(path),
name_(name),
request_context_(request_context), request_context_(request_context),
blob_storage_context_(blob_context), blob_storage_context_(blob_context),
id_(0),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
} }
......
...@@ -47,12 +47,10 @@ class CONTENT_EXPORT ServiceWorkerCache { ...@@ -47,12 +47,10 @@ class CONTENT_EXPORT ServiceWorkerCache {
scoped_ptr<storage::BlobDataHandle>)> scoped_ptr<storage::BlobDataHandle>)>
ResponseCallback; ResponseCallback;
static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( static scoped_ptr<ServiceWorkerCache> CreateMemoryCache(
const std::string& name,
net::URLRequestContext* request_context, net::URLRequestContext* request_context,
base::WeakPtr<storage::BlobStorageContext> blob_context); base::WeakPtr<storage::BlobStorageContext> blob_context);
static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( static scoped_ptr<ServiceWorkerCache> CreatePersistentCache(
const base::FilePath& path, const base::FilePath& path,
const std::string& name,
net::URLRequestContext* request_context, net::URLRequestContext* request_context,
base::WeakPtr<storage::BlobStorageContext> blob_context); base::WeakPtr<storage::BlobStorageContext> blob_context);
...@@ -88,25 +86,18 @@ class CONTENT_EXPORT ServiceWorkerCache { ...@@ -88,25 +86,18 @@ class CONTENT_EXPORT ServiceWorkerCache {
void set_backend(scoped_ptr<disk_cache::Backend> backend) { void set_backend(scoped_ptr<disk_cache::Backend> backend) {
backend_ = backend.Pass(); backend_ = backend.Pass();
} }
void set_name(const std::string& name) { name_ = name; }
const std::string& name() const { return name_; }
int32 id() const { return id_; }
void set_id(int32 id) { id_ = id; }
base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); base::WeakPtr<ServiceWorkerCache> AsWeakPtr();
private: private:
ServiceWorkerCache(const base::FilePath& path, ServiceWorkerCache(const base::FilePath& path,
const std::string& name,
net::URLRequestContext* request_context, net::URLRequestContext* request_context,
base::WeakPtr<storage::BlobStorageContext> blob_context); base::WeakPtr<storage::BlobStorageContext> blob_context);
scoped_ptr<disk_cache::Backend> backend_; scoped_ptr<disk_cache::Backend> backend_;
base::FilePath path_; base::FilePath path_;
std::string name_;
net::URLRequestContext* request_context_; net::URLRequestContext* request_context_;
base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
int32 id_;
base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_;
......
...@@ -40,6 +40,10 @@ WebServiceWorkerCacheError ToWebServiceWorkerCacheError( ...@@ -40,6 +40,10 @@ WebServiceWorkerCacheError ToWebServiceWorkerCacheError(
// TODO(jkarlin): Update this to CACHE_STORAGE_ERROR_EMPTY_KEY once that's // TODO(jkarlin): Update this to CACHE_STORAGE_ERROR_EMPTY_KEY once that's
// added. // added.
return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound; return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound;
case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_CLOSING:
// TODO(jkarlin): Update this to CACHE_STORAGE_ERROR_CLOSING once that's
// added.
return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound;
} }
NOTREACHED(); NOTREACHED();
return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotImplemented; return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotImplemented;
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/id_map.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "content/browser/service_worker/service_worker_cache.h" #include "content/browser/service_worker/service_worker_cache.h"
...@@ -33,8 +32,15 @@ namespace content { ...@@ -33,8 +32,15 @@ namespace content {
// ServiceWorkerCacheStorage holds the set of caches for a given origin. It is // ServiceWorkerCacheStorage holds the set of caches for a given origin. It is
// owned by the ServiceWorkerCacheStorageManager. This class expects to be run // owned by the ServiceWorkerCacheStorageManager. This class expects to be run
// on the IO thread. // on the IO thread.
class ServiceWorkerCacheStorage { class CONTENT_EXPORT ServiceWorkerCacheStorage {
public: public:
// TODO(jkarlin): Convert this (and everything that uses it) to int64_t so
// that we don't run out of id space.
typedef int32_t CacheID;
// The CacheID returned on failed cache operations.
const static int kInvalidCacheID;
enum CacheStorageError { enum CacheStorageError {
CACHE_STORAGE_ERROR_NO_ERROR, CACHE_STORAGE_ERROR_NO_ERROR,
CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, CACHE_STORAGE_ERROR_NOT_IMPLEMENTED,
...@@ -42,6 +48,7 @@ class ServiceWorkerCacheStorage { ...@@ -42,6 +48,7 @@ class ServiceWorkerCacheStorage {
CACHE_STORAGE_ERROR_EXISTS, CACHE_STORAGE_ERROR_EXISTS,
CACHE_STORAGE_ERROR_STORAGE, CACHE_STORAGE_ERROR_STORAGE,
CACHE_STORAGE_ERROR_EMPTY_KEY, CACHE_STORAGE_ERROR_EMPTY_KEY,
CACHE_STORAGE_ERROR_CLOSING
}; };
typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback;
...@@ -87,12 +94,12 @@ class ServiceWorkerCacheStorage { ...@@ -87,12 +94,12 @@ class ServiceWorkerCacheStorage {
class MemoryLoader; class MemoryLoader;
class SimpleCacheLoader; class SimpleCacheLoader;
class CacheLoader; class CacheLoader;
struct CacheContext;
typedef IDMap<ServiceWorkerCache, IDMapOwnPointer> CacheMap; typedef std::map<CacheID, CacheContext*> CacheMap;
typedef CacheMap::KeyType CacheID;
typedef std::map<std::string, CacheID> NameMap; typedef std::map<std::string, CacheID> NameMap;
ServiceWorkerCache* GetLoadedCache(const std::string& cache_name) const; CacheContext* GetLoadedCache(const std::string& cache_name) const;
// Initializer and its callback are below. // Initializer and its callback are below.
void LazyInit(const base::Closure& closure); void LazyInit(const base::Closure& closure);
...@@ -103,14 +110,17 @@ class ServiceWorkerCacheStorage { ...@@ -103,14 +110,17 @@ class ServiceWorkerCacheStorage {
const base::Closure& callback, const base::Closure& callback,
scoped_ptr<std::vector<std::string> > indexed_cache_names, scoped_ptr<std::vector<std::string> > indexed_cache_names,
const std::vector<std::string>::const_iterator& iter, const std::vector<std::string>::const_iterator& iter,
const std::string& cache_name,
scoped_ptr<ServiceWorkerCache> cache); scoped_ptr<ServiceWorkerCache> cache);
void LazyInitDone(); void LazyInitDone();
void DidCreateBackend(base::WeakPtr<ServiceWorkerCache> cache, void DidCreateBackend(base::WeakPtr<ServiceWorkerCache> cache,
CacheID cache_id,
const CacheAndErrorCallback& callback, const CacheAndErrorCallback& callback,
ServiceWorkerCache::ErrorType error); ServiceWorkerCache::ErrorType error);
void AddCacheToMaps(scoped_ptr<ServiceWorkerCache> cache); CacheContext* AddCacheToMaps(const std::string& cache_name,
scoped_ptr<ServiceWorkerCache> cache);
// The CreateCache callbacks are below. // The CreateCache callbacks are below.
void CreateCacheDidCreateCache(const std::string& cache_name, void CreateCacheDidCreateCache(const std::string& cache_name,
...@@ -118,6 +128,7 @@ class ServiceWorkerCacheStorage { ...@@ -118,6 +128,7 @@ class ServiceWorkerCacheStorage {
scoped_ptr<ServiceWorkerCache> cache); scoped_ptr<ServiceWorkerCache> cache);
void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback,
base::WeakPtr<ServiceWorkerCache> cache, base::WeakPtr<ServiceWorkerCache> cache,
CacheID id,
bool success); bool success);
// The DeleteCache callbacks are below. // The DeleteCache callbacks are below.
...@@ -133,9 +144,11 @@ class ServiceWorkerCacheStorage { ...@@ -133,9 +144,11 @@ class ServiceWorkerCacheStorage {
// The list of operations waiting on initialization. // The list of operations waiting on initialization.
std::vector<base::Closure> init_callbacks_; std::vector<base::Closure> init_callbacks_;
// The map of ServiceWorkerCache objects to their integer ids that // The map of CacheIDs to their CacheContext objects. Owns the CacheContext
// ServiceWorkers reference. Owns the cache objects. // object. The CacheIDs are used by JavaScript to reference a
// ServiceWorkerCache.
CacheMap cache_map_; CacheMap cache_map_;
CacheID next_cache_id_; // The next CacheID to use in cache_map_
// The map of cache names to their integer ids. // The map of cache names to their integer ids.
NameMap name_map_; NameMap name_map_;
......
...@@ -92,9 +92,9 @@ class ServiceWorkerCacheStorageManagerTest : public testing::Test { ...@@ -92,9 +92,9 @@ class ServiceWorkerCacheStorageManagerTest : public testing::Test {
bool error = callback_error_ != bool error = callback_error_ !=
ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR; ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR;
if (error) if (error)
EXPECT_EQ(0, callback_cache_id_); EXPECT_EQ(ServiceWorkerCacheStorage::kInvalidCacheID, callback_cache_id_);
else else
EXPECT_LT(0, callback_cache_id_); EXPECT_LE(0, callback_cache_id_);
return !error; return !error;
} }
...@@ -111,9 +111,9 @@ class ServiceWorkerCacheStorageManagerTest : public testing::Test { ...@@ -111,9 +111,9 @@ class ServiceWorkerCacheStorageManagerTest : public testing::Test {
bool error = callback_error_ != bool error = callback_error_ !=
ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR; ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR;
if (error) if (error)
EXPECT_EQ(0, callback_cache_id_); EXPECT_EQ(ServiceWorkerCacheStorage::kInvalidCacheID, callback_cache_id_);
else else
EXPECT_LT(0, callback_cache_id_); EXPECT_LE(0, callback_cache_id_);
return !error; return !error;
} }
...@@ -181,7 +181,7 @@ class ServiceWorkerCacheStorageManagerTest : public testing::Test { ...@@ -181,7 +181,7 @@ class ServiceWorkerCacheStorageManagerTest : public testing::Test {
scoped_ptr<ServiceWorkerCacheStorageManager> cache_manager_; scoped_ptr<ServiceWorkerCacheStorageManager> cache_manager_;
int callback_bool_; int callback_bool_;
int callback_cache_id_; ServiceWorkerCacheStorage::CacheID callback_cache_id_;
ServiceWorkerCacheStorage::CacheStorageError callback_error_; ServiceWorkerCacheStorage::CacheStorageError callback_error_;
std::vector<std::string> callback_strings_; std::vector<std::string> callback_strings_;
...@@ -223,6 +223,14 @@ TEST_P(ServiceWorkerCacheStorageManagerTestP, CreateTwoCaches) { ...@@ -223,6 +223,14 @@ TEST_P(ServiceWorkerCacheStorageManagerTestP, CreateTwoCaches) {
EXPECT_TRUE(CreateCache(origin1_, "bar")); EXPECT_TRUE(CreateCache(origin1_, "bar"));
} }
TEST_P(ServiceWorkerCacheStorageManagerTestP, IDsDiffer) {
EXPECT_TRUE(CreateCache(origin1_, "foo"));
ServiceWorkerCacheStorage::CacheID id1 = callback_cache_id_;
EXPECT_TRUE(CreateCache(origin1_, "bar"));
ServiceWorkerCacheStorage::CacheID id2 = callback_cache_id_;
EXPECT_NE(id1, id2);
}
TEST_P(ServiceWorkerCacheStorageManagerTestP, Create2CachesSameNameDiffSWs) { TEST_P(ServiceWorkerCacheStorageManagerTestP, Create2CachesSameNameDiffSWs) {
EXPECT_TRUE(CreateCache(origin1_, "foo")); EXPECT_TRUE(CreateCache(origin1_, "foo"));
EXPECT_TRUE(CreateCache(origin2_, "foo")); EXPECT_TRUE(CreateCache(origin2_, "foo"));
......
...@@ -66,14 +66,12 @@ class ServiceWorkerCacheTest : public testing::Test { ...@@ -66,14 +66,12 @@ class ServiceWorkerCacheTest : public testing::Test {
if (MemoryOnly()) { if (MemoryOnly()) {
cache_ = ServiceWorkerCache::CreateMemoryCache( cache_ = ServiceWorkerCache::CreateMemoryCache(
"test",
url_request_context, url_request_context,
blob_storage_context->context()->AsWeakPtr()); blob_storage_context->context()->AsWeakPtr());
} else { } else {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
cache_ = ServiceWorkerCache::CreatePersistentCache( cache_ = ServiceWorkerCache::CreatePersistentCache(
temp_dir_.path(), temp_dir_.path(),
"test",
url_request_context, url_request_context,
blob_storage_context->context()->AsWeakPtr()); blob_storage_context->context()->AsWeakPtr());
} }
......
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