Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(819)

Unified Diff: content/browser/service_worker/service_worker_cache.cc

Issue 545533002: Move ServiceWorkerCache backend creation to a lazy init function. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@no_pointers_keys
Patch Set: Nits Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/service_worker_cache.cc
diff --git a/content/browser/service_worker/service_worker_cache.cc b/content/browser/service_worker/service_worker_cache.cc
index 04dd45467ddc4cb85aa71d9d4ac59388411d89d7..a730ed1b2bdbc9a9ca94689d879b71f41e3abad7 100644
--- a/content/browser/service_worker/service_worker_cache.cc
+++ b/content/browser/service_worker/service_worker_cache.cc
@@ -650,46 +650,21 @@ base::WeakPtr<ServiceWorkerCache> ServiceWorkerCache::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
-void ServiceWorkerCache::CreateBackend(const ErrorCallback& callback) {
- DCHECK(!backend_);
-
- // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction.
- net::CacheType cache_type =
- path_.empty() ? net::MEMORY_CACHE : net::APP_CACHE;
-
- scoped_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr());
-
- // Temporary pointer so that backend_ptr can be Pass()'d in Bind below.
- ScopedBackendPtr* backend = backend_ptr.get();
-
- net::CompletionCallback create_cache_callback =
- base::Bind(CreateBackendDidCreate,
- callback,
- base::Passed(backend_ptr.Pass()),
- weak_ptr_factory_.GetWeakPtr());
-
- // TODO(jkarlin): Use the cache MessageLoopProxy that ServiceWorkerCacheCore
- // has for disk caches.
- // TODO(jkarlin): Switch to SimpleCache after it supports APP_CACHE and after
- // debugging why the QuickStressBody unittest fails with it.
- int rv = disk_cache::CreateCacheBackend(
- cache_type,
- net::CACHE_BACKEND_SIMPLE,
- path_,
- kMaxCacheBytes,
- false, /* force */
- BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE).get(),
- NULL,
- backend,
- create_cache_callback);
- if (rv != net::ERR_IO_PENDING)
- create_cache_callback.Run(rv);
-}
-
void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request,
scoped_ptr<ServiceWorkerResponse> response,
const ErrorCallback& callback) {
- DCHECK(backend_);
+ if (!initialized_) {
+ Init(base::Bind(&ServiceWorkerCache::Put,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(request.Pass()),
+ base::Passed(response.Pass()),
+ callback));
+ return;
+ }
+ if (!backend_) {
+ callback.Run(ErrorTypeStorage);
+ return;
+ }
scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*);
@@ -730,7 +705,19 @@ void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request,
void ServiceWorkerCache::Match(scoped_ptr<ServiceWorkerFetchRequest> request,
const ResponseCallback& callback) {
- DCHECK(backend_);
+ if (!initialized_) {
+ Init(base::Bind(&ServiceWorkerCache::Match,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(request.Pass()),
+ callback));
+ return;
+ }
+ if (!backend_) {
+ callback.Run(ErrorTypeStorage,
+ scoped_ptr<ServiceWorkerResponse>(),
+ scoped_ptr<storage::BlobDataHandle>());
+ return;
+ }
scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*);
@@ -753,7 +740,17 @@ void ServiceWorkerCache::Match(scoped_ptr<ServiceWorkerFetchRequest> request,
void ServiceWorkerCache::Delete(scoped_ptr<ServiceWorkerFetchRequest> request,
const ErrorCallback& callback) {
- DCHECK(backend_);
+ if (!initialized_) {
+ Init(base::Bind(&ServiceWorkerCache::Delete,
+ weak_ptr_factory_.GetWeakPtr(),
+ base::Passed(request.Pass()),
+ callback));
+ return;
+ }
+ if (!backend_) {
+ callback.Run(ErrorTypeStorage);
+ return;
+ }
scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*);
@@ -774,7 +771,15 @@ void ServiceWorkerCache::Delete(scoped_ptr<ServiceWorkerFetchRequest> request,
}
void ServiceWorkerCache::Keys(const RequestsCallback& callback) {
- DCHECK(backend_);
+ if (!initialized_) {
+ Init(base::Bind(
+ &ServiceWorkerCache::Keys, weak_ptr_factory_.GetWeakPtr(), callback));
+ return;
+ }
+ if (!backend_) {
+ callback.Run(ErrorTypeStorage, scoped_ptr<Requests>());
+ return;
+ }
// 1. Iterate through all of the entries, open them, and add them to a vector.
// 2. For each open entry:
@@ -802,10 +807,6 @@ void ServiceWorkerCache::Keys(const RequestsCallback& callback) {
open_entry_callback.Run(rv);
}
-bool ServiceWorkerCache::HasCreatedBackend() const {
- return backend_;
-}
-
ServiceWorkerCache::ServiceWorkerCache(
const base::FilePath& path,
net::URLRequestContext* request_context,
@@ -813,6 +814,7 @@ ServiceWorkerCache::ServiceWorkerCache(
: path_(path),
request_context_(request_context),
blob_storage_context_(blob_context),
+ initialized_(false),
weak_ptr_factory_(this) {
}
@@ -899,4 +901,60 @@ void ServiceWorkerCache::KeysDidReadHeaders(
KeysProcessNextEntry(keys_context.Pass(), iter + 1);
}
+void ServiceWorkerCache::CreateBackend(const ErrorCallback& callback) {
+ DCHECK(!backend_);
+
+ // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction.
+ net::CacheType cache_type =
+ path_.empty() ? net::MEMORY_CACHE : net::APP_CACHE;
+
+ scoped_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr());
+
+ // Temporary pointer so that backend_ptr can be Pass()'d in Bind below.
+ ScopedBackendPtr* backend = backend_ptr.get();
+
+ net::CompletionCallback create_cache_callback =
+ base::Bind(CreateBackendDidCreate,
+ callback,
+ base::Passed(backend_ptr.Pass()),
+ weak_ptr_factory_.GetWeakPtr());
+
+ // TODO(jkarlin): Use the cache MessageLoopProxy that ServiceWorkerCacheCore
+ // has for disk caches.
+ int rv = disk_cache::CreateCacheBackend(
+ cache_type,
+ net::CACHE_BACKEND_SIMPLE,
+ path_,
+ kMaxCacheBytes,
+ false, /* force */
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE).get(),
+ NULL,
+ backend,
+ create_cache_callback);
+ if (rv != net::ERR_IO_PENDING)
+ create_cache_callback.Run(rv);
+}
+
+void ServiceWorkerCache::Init(const base::Closure& callback) {
+ init_callbacks_.push_back(callback);
+
+ // If this isn't the first call to Init then return as the initialization
+ // has already started.
+ if (init_callbacks_.size() > 1u)
+ return;
+
+ CreateBackend(base::Bind(&ServiceWorkerCache::InitDone,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+void ServiceWorkerCache::InitDone(ErrorType error) {
+ initialized_ = true;
+ for (std::vector<base::Closure>::iterator it = init_callbacks_.begin();
+ it != init_callbacks_.end();
+ ++it) {
+ it->Run();
+ }
+ init_callbacks_.clear();
+}
+
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698