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,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/files/memory_mapped_file.h" #include "base/files/memory_mapped_file.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/sha1.h" #include "base/sha1.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "content/browser/service_worker/service_worker_cache.h" #include "content/browser/service_worker/service_worker_cache.h"
...@@ -21,6 +22,23 @@ ...@@ -21,6 +22,23 @@
namespace content { namespace content {
// static
const int ServiceWorkerCacheStorage::kInvalidCacheID = -1;
// The meta information related to each ServiceWorkerCache that the
// ServiceWorkerCacheManager needs to keep track of.
// TODO(jkarlin): Add reference counting so that the deletion of javascript
// objects can delete the ServiceWorkerCache.
struct ServiceWorkerCacheStorage::CacheContext {
CacheContext(const std::string& name,
CacheID id,
scoped_ptr<ServiceWorkerCache> cache)
: name(name), id(id), cache(cache.Pass()) {}
std::string name;
CacheID id;
scoped_ptr<ServiceWorkerCache> cache;
};
// Handles the loading and clean up of ServiceWorkerCache objects. // Handles the loading and clean up of ServiceWorkerCache objects.
class ServiceWorkerCacheStorage::CacheLoader class ServiceWorkerCacheStorage::CacheLoader
: public base::RefCountedThreadSafe< : public base::RefCountedThreadSafe<
...@@ -53,7 +71,8 @@ class ServiceWorkerCacheStorage::CacheLoader ...@@ -53,7 +71,8 @@ class ServiceWorkerCacheStorage::CacheLoader
const BoolCallback& callback) = 0; const BoolCallback& callback) = 0;
// Writes the cache names (and sizes) to disk if applicable. // Writes the cache names (and sizes) to disk if applicable.
virtual void WriteIndex(CacheMap* caches, const BoolCallback& callback) = 0; virtual void WriteIndex(const CacheMap& caches,
const BoolCallback& callback) = 0;
// Loads the cache names from disk if applicable. // Loads the cache names from disk if applicable.
virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names,
...@@ -86,8 +105,7 @@ class ServiceWorkerCacheStorage::MemoryLoader ...@@ -86,8 +105,7 @@ class ServiceWorkerCacheStorage::MemoryLoader
virtual void CreateCache(const std::string& cache_name, virtual void CreateCache(const std::string& cache_name,
const CacheCallback& callback) OVERRIDE { const CacheCallback& callback) OVERRIDE {
scoped_ptr<ServiceWorkerCache> cache = scoped_ptr<ServiceWorkerCache> cache =
ServiceWorkerCache::CreateMemoryCache( ServiceWorkerCache::CreateMemoryCache(request_context_, blob_context_);
cache_name, request_context_, blob_context_);
callback.Run(cache.Pass()); callback.Run(cache.Pass());
} }
...@@ -96,7 +114,7 @@ class ServiceWorkerCacheStorage::MemoryLoader ...@@ -96,7 +114,7 @@ class ServiceWorkerCacheStorage::MemoryLoader
callback.Run(true); callback.Run(true);
} }
virtual void WriteIndex(CacheMap* caches, virtual void WriteIndex(const CacheMap& caches,
const BoolCallback& callback) OVERRIDE { const BoolCallback& callback) OVERRIDE {
callback.Run(false); callback.Run(false);
} }
...@@ -167,7 +185,6 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader ...@@ -167,7 +185,6 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader
scoped_ptr<ServiceWorkerCache> cache = scoped_ptr<ServiceWorkerCache> cache =
ServiceWorkerCache::CreatePersistentCache( ServiceWorkerCache::CreatePersistentCache(
CreatePersistentCachePath(origin_path_, cache_name), CreatePersistentCachePath(origin_path_, cache_name),
cache_name,
request_context_, request_context_,
blob_context_); blob_context_);
callback.Run(cache.Pass()); callback.Run(cache.Pass());
...@@ -243,7 +260,7 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader ...@@ -243,7 +260,7 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader
original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); original_loop->PostTask(FROM_HERE, base::Bind(callback, rv));
} }
virtual void WriteIndex(CacheMap* caches, virtual void WriteIndex(const CacheMap& caches,
const BoolCallback& callback) OVERRIDE { const BoolCallback& callback) OVERRIDE {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
...@@ -252,11 +269,11 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader ...@@ -252,11 +269,11 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader
ServiceWorkerCacheStorageIndex index; ServiceWorkerCacheStorageIndex index;
for (CacheMap::const_iterator iter(caches); !iter.IsAtEnd(); for (CacheMap::const_iterator it = caches.begin(); it != caches.end();
iter.Advance()) { ++it) {
const ServiceWorkerCache* cache = iter.GetCurrentValue(); const CacheContext* cache = it->second;
ServiceWorkerCacheStorageIndex::Cache* index_cache = index.add_cache(); ServiceWorkerCacheStorageIndex::Cache* index_cache = index.add_cache();
index_cache->set_name(cache->name()); index_cache->set_name(cache->name);
index_cache->set_size(0); // TODO(jkarlin): Make this real. index_cache->set_size(0); // TODO(jkarlin): Make this real.
} }
...@@ -274,7 +291,6 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader ...@@ -274,7 +291,6 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader
tmp_path, tmp_path,
index_path, index_path,
serialized, serialized,
caches,
callback, callback,
base::MessageLoopProxy::current())); base::MessageLoopProxy::current()));
} }
...@@ -283,7 +299,6 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader ...@@ -283,7 +299,6 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader
const base::FilePath& tmp_path, const base::FilePath& tmp_path,
const base::FilePath& index_path, const base::FilePath& index_path,
const std::string& data, const std::string& data,
CacheMap* caches,
const BoolCallback& callback, const BoolCallback& callback,
const scoped_refptr<base::MessageLoopProxy>& original_loop) { const scoped_refptr<base::MessageLoopProxy>& original_loop) {
DCHECK(cache_task_runner_->RunsTasksOnCurrentThread()); DCHECK(cache_task_runner_->RunsTasksOnCurrentThread());
...@@ -379,6 +394,7 @@ ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( ...@@ -379,6 +394,7 @@ ServiceWorkerCacheStorage::ServiceWorkerCacheStorage(
net::URLRequestContext* request_context, net::URLRequestContext* request_context,
base::WeakPtr<storage::BlobStorageContext> blob_context) base::WeakPtr<storage::BlobStorageContext> blob_context)
: initialized_(false), : initialized_(false),
next_cache_id_(0),
origin_path_(path), origin_path_(path),
cache_task_runner_(cache_task_runner), cache_task_runner_(cache_task_runner),
weak_factory_(this) { weak_factory_(this) {
...@@ -391,6 +407,7 @@ ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( ...@@ -391,6 +407,7 @@ ServiceWorkerCacheStorage::ServiceWorkerCacheStorage(
} }
ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() {
STLDeleteContainerPairSecondPointers(cache_map_.begin(), cache_map_.end());
} }
void ServiceWorkerCacheStorage::CreateCache( void ServiceWorkerCacheStorage::CreateCache(
...@@ -405,12 +422,12 @@ void ServiceWorkerCacheStorage::CreateCache( ...@@ -405,12 +422,12 @@ void ServiceWorkerCacheStorage::CreateCache(
} }
if (cache_name.empty()) { if (cache_name.empty()) {
callback.Run(0, CACHE_STORAGE_ERROR_EMPTY_KEY); callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_EMPTY_KEY);
return; return;
} }
if (GetLoadedCache(cache_name)) { if (GetLoadedCache(cache_name)) {
callback.Run(0, CACHE_STORAGE_ERROR_EXISTS); callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_EXISTS);
return; return;
} }
...@@ -436,22 +453,25 @@ void ServiceWorkerCacheStorage::GetCache( ...@@ -436,22 +453,25 @@ void ServiceWorkerCacheStorage::GetCache(
} }
if (cache_name.empty()) { if (cache_name.empty()) {
callback.Run(0, CACHE_STORAGE_ERROR_EMPTY_KEY); callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_EMPTY_KEY);
return; return;
} }
ServiceWorkerCache* cache = GetLoadedCache(cache_name); CacheContext* cache_context = GetLoadedCache(cache_name);
if (!cache) { if (!cache_context) {
callback.Run(0, CACHE_STORAGE_ERROR_NOT_FOUND); callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_NOT_FOUND);
return; return;
} }
ServiceWorkerCache* cache = cache_context->cache.get();
if (cache->HasCreatedBackend()) if (cache->HasCreatedBackend())
return callback.Run(cache->id(), CACHE_STORAGE_ERROR_NO_ERROR); return callback.Run(cache_context->id, CACHE_STORAGE_ERROR_NO_ERROR);
cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend,
weak_factory_.GetWeakPtr(), weak_factory_.GetWeakPtr(),
cache->AsWeakPtr(), cache->AsWeakPtr(),
cache_context->id,
callback)); callback));
} }
...@@ -495,18 +515,19 @@ void ServiceWorkerCacheStorage::DeleteCache( ...@@ -495,18 +515,19 @@ void ServiceWorkerCacheStorage::DeleteCache(
return; return;
} }
ServiceWorkerCache* cache = GetLoadedCache(cache_name); scoped_ptr<CacheContext> cache_context(GetLoadedCache(cache_name));
if (!cache) { if (!cache_context) {
callback.Run(false, CACHE_STORAGE_ERROR_NOT_FOUND); callback.Run(false, CACHE_STORAGE_ERROR_NOT_FOUND);
return; return;
} }
name_map_.erase(cache_name); name_map_.erase(cache_name);
cache_map_.Remove(cache->id()); // deletes cache cache_map_.erase(cache_context->id);
cache_context.reset();
// Update the Index // Update the Index
cache_loader_->WriteIndex( cache_loader_->WriteIndex(
&cache_map_, cache_map_,
base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex, base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex,
weak_factory_.GetWeakPtr(), weak_factory_.GetWeakPtr(),
cache_name, cache_name,
...@@ -535,6 +556,7 @@ void ServiceWorkerCacheStorage::EnumerateCaches( ...@@ -535,6 +556,7 @@ void ServiceWorkerCacheStorage::EnumerateCaches(
void ServiceWorkerCacheStorage::DidCreateBackend( void ServiceWorkerCacheStorage::DidCreateBackend(
base::WeakPtr<ServiceWorkerCache> cache, base::WeakPtr<ServiceWorkerCache> cache,
CacheID cache_id,
const CacheAndErrorCallback& callback, const CacheAndErrorCallback& callback,
ServiceWorkerCache::ErrorType error) { ServiceWorkerCache::ErrorType error) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
...@@ -542,11 +564,11 @@ void ServiceWorkerCacheStorage::DidCreateBackend( ...@@ -542,11 +564,11 @@ void ServiceWorkerCacheStorage::DidCreateBackend(
if (error != ServiceWorkerCache::ErrorTypeOK || !cache) { if (error != ServiceWorkerCache::ErrorTypeOK || !cache) {
// TODO(jkarlin): This should delete the directory and try again in case // TODO(jkarlin): This should delete the directory and try again in case
// the cache is simply corrupt. // the cache is simply corrupt.
callback.Run(0, CACHE_STORAGE_ERROR_STORAGE); callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_STORAGE);
return; return;
} }
callback.Run(cache->id(), CACHE_STORAGE_ERROR_NO_ERROR); callback.Run(cache_id, CACHE_STORAGE_ERROR_NO_ERROR);
} }
// Init is run lazily so that it is called on the proper MessageLoop. // Init is run lazily so that it is called on the proper MessageLoop.
...@@ -595,18 +617,20 @@ void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( ...@@ -595,18 +617,20 @@ void ServiceWorkerCacheStorage::LazyInitDidLoadIndex(
weak_factory_.GetWeakPtr(), weak_factory_.GetWeakPtr(),
callback, callback,
base::Passed(indexed_cache_names.Pass()), base::Passed(indexed_cache_names.Pass()),
iter_next)); iter_next,
*iter));
} }
void ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName( void ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName(
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) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (cache) if (cache)
AddCacheToMaps(cache.Pass()); AddCacheToMaps(cache_name, cache.Pass());
if (iter == indexed_cache_names->end()) { if (iter == indexed_cache_names->end()) {
LazyInitDone(); LazyInitDone();
...@@ -620,7 +644,8 @@ void ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName( ...@@ -620,7 +644,8 @@ void ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName(
weak_factory_.GetWeakPtr(), weak_factory_.GetWeakPtr(),
callback, callback,
base::Passed(indexed_cache_names.Pass()), base::Passed(indexed_cache_names.Pass()),
iter_next)); iter_next,
*iter));
} }
void ServiceWorkerCacheStorage::LazyInitDone() { void ServiceWorkerCacheStorage::LazyInitDone() {
...@@ -633,14 +658,17 @@ void ServiceWorkerCacheStorage::LazyInitDone() { ...@@ -633,14 +658,17 @@ void ServiceWorkerCacheStorage::LazyInitDone() {
init_callbacks_.clear(); init_callbacks_.clear();
} }
void ServiceWorkerCacheStorage::AddCacheToMaps( ServiceWorkerCacheStorage::CacheContext*
ServiceWorkerCacheStorage::AddCacheToMaps(
const std::string& cache_name,
scoped_ptr<ServiceWorkerCache> cache) { scoped_ptr<ServiceWorkerCache> cache) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
ServiceWorkerCache* cache_ptr = cache.release(); CacheID id = next_cache_id_++;
CacheID id = cache_map_.Add(cache_ptr); CacheContext* cache_context = new CacheContext(cache_name, id, cache.Pass());
name_map_.insert(std::make_pair(cache_ptr->name(), id)); cache_map_.insert(std::make_pair(id, cache_context)); // Takes ownership
cache_ptr->set_id(id); name_map_.insert(std::make_pair(cache_name, id));
return cache_context;
} }
void ServiceWorkerCacheStorage::CreateCacheDidCreateCache( void ServiceWorkerCacheStorage::CreateCacheDidCreateCache(
...@@ -650,36 +678,36 @@ void ServiceWorkerCacheStorage::CreateCacheDidCreateCache( ...@@ -650,36 +678,36 @@ void ServiceWorkerCacheStorage::CreateCacheDidCreateCache(
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!cache) { if (!cache) {
callback.Run(0, CACHE_STORAGE_ERROR_STORAGE); callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_CLOSING);
return; return;
} }
base::WeakPtr<ServiceWorkerCache> cache_ptr = cache->AsWeakPtr(); CacheContext* cache_context = AddCacheToMaps(cache_name, cache.Pass());
AddCacheToMaps(cache.Pass());
cache_loader_->WriteIndex( cache_loader_->WriteIndex(
&cache_map_, cache_map_,
base::Bind( base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidWriteIndex,
&ServiceWorkerCacheStorage::CreateCacheDidWriteIndex, weak_factory_.GetWeakPtr(),
weak_factory_.GetWeakPtr(), callback,
callback, cache_context->cache->AsWeakPtr(),
cache_ptr)); // cache is owned by this->CacheMap and won't be deleted cache_context->id));
} }
void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex(
const CacheAndErrorCallback& callback, const CacheAndErrorCallback& callback,
base::WeakPtr<ServiceWorkerCache> cache, base::WeakPtr<ServiceWorkerCache> cache,
CacheID id,
bool success) { bool success) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!cache) { if (!cache) {
callback.Run(false, CACHE_STORAGE_ERROR_STORAGE); callback.Run(false, CACHE_STORAGE_ERROR_CLOSING);
return; return;
} }
cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend,
weak_factory_.GetWeakPtr(), weak_factory_.GetWeakPtr(),
cache, cache,
id,
callback)); callback));
} }
...@@ -704,18 +732,18 @@ void ServiceWorkerCacheStorage::DeleteCacheDidCleanUp( ...@@ -704,18 +732,18 @@ void ServiceWorkerCacheStorage::DeleteCacheDidCleanUp(
callback.Run(true, CACHE_STORAGE_ERROR_NO_ERROR); callback.Run(true, CACHE_STORAGE_ERROR_NO_ERROR);
} }
ServiceWorkerCache* ServiceWorkerCacheStorage::GetLoadedCache( ServiceWorkerCacheStorage::CacheContext*
const std::string& cache_name) const { ServiceWorkerCacheStorage::GetLoadedCache(const std::string& cache_name) const {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(initialized_); DCHECK(initialized_);
NameMap::const_iterator it = name_map_.find(cache_name); NameMap::const_iterator name_iter = name_map_.find(cache_name);
if (it == name_map_.end()) if (name_iter == name_map_.end())
return NULL; return NULL;
ServiceWorkerCache* cache = cache_map_.Lookup(it->second); CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second);
DCHECK(cache); DCHECK(map_iter != cache_map_.end());
return cache; return map_iter->second;
} }
} // namespace content } // namespace content
...@@ -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