Commit 43027f01 authored by jkarlin's avatar jkarlin Committed by Commit bot

Make ServiceWorkerCacheStorage::CacheLoader::LoadCache synchronous

The CacheLoader has two functions for creating a cache:
1. LoadCache -- mkdir the cache directory and load the cache into memory
2. CreateCache  -- rm -rf the directory and then call LoadCache

This CL:

1. Makes LoadCache synchronous by moving the mkdir to CreateCache (in case of loading an existing cache the mkdir was unnecessary)
2. Deletes a bunch of async code
3. Renames LoadCache to CreateServiceWorkerCache

Related CLs:
1. https://crrev.com/542703002: Change ownership of the parameters to ServiceWorkerCache:: Put and Match.
2. https://crrev.com/545533002: Move ServiceWorkerCache backend creation to a lazy init function.
* 3. https://crrev.com/548533002: Make ServiceWorkerCacheStorage::CacheLoader::LoadCache synchronous
4. https://crrev.com/549493002: Expose ServiceWorkerCache objects to ServiceWorkerCacheStorageManager clients.

BUG=392621

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

Cr-Commit-Position: refs/heads/master@{#294377}
parent adf4bb5d
...@@ -57,10 +57,10 @@ class ServiceWorkerCacheStorage::CacheLoader { ...@@ -57,10 +57,10 @@ class ServiceWorkerCacheStorage::CacheLoader {
virtual ~CacheLoader() {} virtual ~CacheLoader() {}
// Loads the given cache_name, the cache is NULL if it fails. If the cache // Creates a ServiceWorkerCache with the given name. It does not attempt to
// doesn't exist a new one is created. // load the backend, that happens lazily when the cache is used.
virtual void LoadCache(const std::string& cache_name, virtual scoped_ptr<ServiceWorkerCache> CreateServiceWorkerCache(
const CacheCallback& callback) = 0; const std::string& cache_name) = 0;
// Deletes any pre-existing cache of the same name and then loads it. // Deletes any pre-existing cache of the same name and then loads it.
virtual void CreateCache(const std::string& cache_name, virtual void CreateCache(const std::string& cache_name,
...@@ -80,8 +80,6 @@ class ServiceWorkerCacheStorage::CacheLoader { ...@@ -80,8 +80,6 @@ class ServiceWorkerCacheStorage::CacheLoader {
const StringsCallback& callback) = 0; const StringsCallback& callback) = 0;
protected: protected:
virtual void LoadCacheImpl(const std::string&) {}
scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
net::URLRequestContext* request_context_; net::URLRequestContext* request_context_;
base::WeakPtr<storage::BlobStorageContext> blob_context_; base::WeakPtr<storage::BlobStorageContext> blob_context_;
...@@ -94,16 +92,16 @@ class ServiceWorkerCacheStorage::MemoryLoader ...@@ -94,16 +92,16 @@ class ServiceWorkerCacheStorage::MemoryLoader
net::URLRequestContext* request_context, net::URLRequestContext* request_context,
base::WeakPtr<storage::BlobStorageContext> blob_context) base::WeakPtr<storage::BlobStorageContext> blob_context)
: CacheLoader(cache_task_runner, request_context, blob_context) {} : CacheLoader(cache_task_runner, request_context, blob_context) {}
virtual void LoadCache(const std::string& cache_name,
const CacheCallback& callback) OVERRIDE { virtual scoped_ptr<ServiceWorkerCache> CreateServiceWorkerCache(
NOTREACHED(); const std::string& cache_name) OVERRIDE {
return ServiceWorkerCache::CreateMemoryCache(request_context_,
blob_context_);
} }
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 = callback.Run(CreateServiceWorkerCache(cache_name).Pass());
ServiceWorkerCache::CreateMemoryCache(request_context_, blob_context_);
callback.Run(cache.Pass());
} }
virtual void CleanUpDeletedCache(const std::string& cache_name, virtual void CleanUpDeletedCache(const std::string& cache_name,
...@@ -136,56 +134,14 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader ...@@ -136,56 +134,14 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader
origin_path_(origin_path), origin_path_(origin_path),
weak_ptr_factory_(this) {} weak_ptr_factory_(this) {}
virtual void LoadCache(const std::string& cache_name, virtual scoped_ptr<ServiceWorkerCache> CreateServiceWorkerCache(
const CacheCallback& callback) OVERRIDE { const std::string& cache_name) OVERRIDE {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
// 1. Create the cache's directory if necessary. (LoadCreateDirectoryInPool) return ServiceWorkerCache::CreatePersistentCache(
// 2. Create the cache object. (LoadDidCreateDirectory)
cache_task_runner_->PostTask(
FROM_HERE,
base::Bind(&SimpleCacheLoader::LoadCreateDirectoryInPool,
CreatePersistentCachePath(origin_path_, cache_name), CreatePersistentCachePath(origin_path_, cache_name),
cache_name, request_context_,
callback, blob_context_);
weak_ptr_factory_.GetWeakPtr(),
base::MessageLoopProxy::current()));
}
static void LoadCreateDirectoryInPool(
const base::FilePath& path,
const std::string& cache_name,
const CacheCallback& callback,
base::WeakPtr<SimpleCacheLoader> loader,
const scoped_refptr<base::MessageLoopProxy>& original_loop) {
bool rv = base::CreateDirectory(path);
original_loop->PostTask(
FROM_HERE,
base::Bind(&SimpleCacheLoader::LoadDidCreateDirectory,
cache_name,
callback,
loader,
rv));
}
static void LoadDidCreateDirectory(const std::string& cache_name,
const CacheCallback& callback,
base::WeakPtr<SimpleCacheLoader> loader,
bool dir_rv) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!dir_rv || !loader) {
callback.Run(scoped_ptr<ServiceWorkerCache>());
return;
}
scoped_ptr<ServiceWorkerCache> cache =
ServiceWorkerCache::CreatePersistentCache(
CreatePersistentCachePath(loader->origin_path_, cache_name),
loader->request_context_,
loader->blob_context_);
callback.Run(cache.Pass());
} }
virtual void CreateCache(const std::string& cache_name, virtual void CreateCache(const std::string& cache_name,
...@@ -199,29 +155,32 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader ...@@ -199,29 +155,32 @@ class ServiceWorkerCacheStorage::SimpleCacheLoader
base::FilePath cache_path = base::FilePath cache_path =
CreatePersistentCachePath(origin_path_, cache_name); CreatePersistentCachePath(origin_path_, cache_name);
cache_task_runner_->PostTask( PostTaskAndReplyWithResult(
cache_task_runner_.get(),
FROM_HERE, FROM_HERE,
base::Bind(&SimpleCacheLoader::CreateCacheDeleteFilesInPool, base::Bind(&SimpleCacheLoader::CreateCachePrepDirInPool, cache_path),
cache_path, base::Bind(&SimpleCacheLoader::CreateCachePreppedDir,
cache_name, cache_name,
callback, callback,
weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr()));
base::MessageLoopProxy::current()));
} }
static void CreateCacheDeleteFilesInPool( static bool CreateCachePrepDirInPool(const base::FilePath& cache_path) {
const base::FilePath& cache_path, if (base::PathExists(cache_path))
const std::string& cache_name, base::DeleteFile(cache_path, /* recursive */ true);
return base::CreateDirectory(cache_path);
}
static void CreateCachePreppedDir(const std::string& cache_name,
const CacheCallback& callback, const CacheCallback& callback,
base::WeakPtr<SimpleCacheLoader> loader, base::WeakPtr<SimpleCacheLoader> loader,
const scoped_refptr<base::MessageLoopProxy>& original_loop) { bool success) {
base::FilePath path(cache_path); if (!success || !loader) {
if (base::PathExists(path)) callback.Run(scoped_ptr<ServiceWorkerCache>());
base::DeleteFile(path, /* recursive */ true); return;
}
// Jump straight into LoadCache on the same thread. callback.Run(loader->CreateServiceWorkerCache(cache_name));
LoadCreateDirectoryInPool(
cache_path, cache_name, callback, loader, original_loop);
} }
virtual void CleanUpDeletedCache(const std::string& cache_name, virtual void CleanUpDeletedCache(const std::string& cache_name,
...@@ -541,52 +500,14 @@ void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( ...@@ -541,52 +500,14 @@ void ServiceWorkerCacheStorage::LazyInitDidLoadIndex(
scoped_ptr<std::vector<std::string> > indexed_cache_names) { scoped_ptr<std::vector<std::string> > indexed_cache_names) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (indexed_cache_names->empty()) { for (std::vector<std::string>::iterator it = indexed_cache_names->begin();
LazyInitDone(); it != indexed_cache_names->end();
return; ++it) {
} scoped_ptr<ServiceWorkerCache> cache =
cache_loader_->CreateServiceWorkerCache(*it);
std::vector<std::string>::const_iterator iter = indexed_cache_names->begin(); AddCacheToMaps(*it, cache.Pass());
std::vector<std::string>::const_iterator iter_next = iter + 1;
cache_loader_->LoadCache(
*iter,
base::Bind(&ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName,
weak_factory_.GetWeakPtr(),
callback,
base::Passed(indexed_cache_names.Pass()),
iter_next,
*iter));
}
void ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName(
const base::Closure& callback,
scoped_ptr<std::vector<std::string> > indexed_cache_names,
const std::vector<std::string>::const_iterator& iter,
const std::string& cache_name,
scoped_ptr<ServiceWorkerCache> cache) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (cache)
AddCacheToMaps(cache_name, cache.Pass());
if (iter == indexed_cache_names->end()) {
LazyInitDone();
return;
} }
std::vector<std::string>::const_iterator iter_next = iter + 1;
cache_loader_->LoadCache(
*iter,
base::Bind(&ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName,
weak_factory_.GetWeakPtr(),
callback,
base::Passed(indexed_cache_names.Pass()),
iter_next,
*iter));
}
void ServiceWorkerCacheStorage::LazyInitDone() {
initialized_ = true; initialized_ = true;
for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); for (std::vector<base::Closure>::iterator it = init_callbacks_.begin();
it != init_callbacks_.end(); it != init_callbacks_.end();
......
...@@ -105,13 +105,6 @@ class CONTENT_EXPORT ServiceWorkerCacheStorage { ...@@ -105,13 +105,6 @@ class CONTENT_EXPORT ServiceWorkerCacheStorage {
void LazyInitDidLoadIndex( void LazyInitDidLoadIndex(
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);
void LazyInitIterateAndLoadCacheName(
const base::Closure& callback,
scoped_ptr<std::vector<std::string> > indexed_cache_names,
const std::vector<std::string>::const_iterator& iter,
const std::string& cache_name,
scoped_ptr<ServiceWorkerCache> cache);
void LazyInitDone();
CacheContext* AddCacheToMaps(const std::string& cache_name, CacheContext* AddCacheToMaps(const std::string& cache_name,
scoped_ptr<ServiceWorkerCache> cache); scoped_ptr<ServiceWorkerCache> cache);
......
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