diff options
author | jkarlin <jkarlin@chromium.org> | 2014-11-06 15:05:04 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-11-06 23:05:28 +0000 |
commit | 7fbcce797ed32cf1d653ca5e4bf1dfad4cd7fc46 (patch) | |
tree | 3ad8ec28a8fe9281649ceb0dfe6d1ee91fee7181 /content/browser/service_worker/service_worker_cache.cc | |
parent | 5503de1eeae0437c1f8160d69b04d6241bc5e974 (diff) | |
download | chromium_src-7fbcce797ed32cf1d653ca5e4bf1dfad4cd7fc46.zip chromium_src-7fbcce797ed32cf1d653ca5e4bf1dfad4cd7fc46.tar.gz chromium_src-7fbcce797ed32cf1d653ca5e4bf1dfad4cd7fc46.tar.bz2 |
[ServiceWorkerCache] Make the state of the cache backend explicit
The ServiceWorkerCache's backend transitions from uninitialized, to
open, to closed. Make that explicit in code by adding a state
variable instead of checking the state of the initialized_ and
backend_ member variables.
BUG=430884
Review URL: https://codereview.chromium.org/701403002
Cr-Commit-Position: refs/heads/master@{#303115}
Diffstat (limited to 'content/browser/service_worker/service_worker_cache.cc')
-rw-r--r-- | content/browser/service_worker/service_worker_cache.cc | 92 |
1 files changed, 54 insertions, 38 deletions
diff --git a/content/browser/service_worker/service_worker_cache.cc b/content/browser/service_worker/service_worker_cache.cc index c19793a..fe47ab8 100644 --- a/content/browser/service_worker/service_worker_cache.cc +++ b/content/browser/service_worker/service_worker_cache.cc @@ -715,8 +715,8 @@ void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request, base::Closure continuation = base::Bind(&ServiceWorkerCache::PutImpl, base::Passed(put_context.Pass())); - if (!initialized_) { - Init(continuation); + if (backend_state_ == BackendUninitialized) { + InitBackend(continuation); return; } @@ -730,15 +730,20 @@ void ServiceWorkerCache::Match(scoped_ptr<ServiceWorkerFetchRequest> request, base::Bind(&ServiceWorkerCache::PendingResponseCallback, weak_ptr_factory_.GetWeakPtr(), callback); - if (!initialized_) { - Init(base::Bind(&ServiceWorkerCache::Match, weak_ptr_factory_.GetWeakPtr(), - base::Passed(request.Pass()), pending_callback)); - return; - } - if (!backend_) { - pending_callback.Run(ErrorTypeStorage, scoped_ptr<ServiceWorkerResponse>(), - scoped_ptr<storage::BlobDataHandle>()); - return; + switch (backend_state_) { + case BackendUninitialized: + InitBackend(base::Bind(&ServiceWorkerCache::Match, + weak_ptr_factory_.GetWeakPtr(), + base::Passed(request.Pass()), pending_callback)); + return; + case BackendClosed: + pending_callback.Run(ErrorTypeStorage, + scoped_ptr<ServiceWorkerResponse>(), + scoped_ptr<storage::BlobDataHandle>()); + return; + case BackendOpen: + DCHECK(backend_); + break; } scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); @@ -764,14 +769,18 @@ void ServiceWorkerCache::Delete(scoped_ptr<ServiceWorkerFetchRequest> request, base::Bind(&ServiceWorkerCache::PendingErrorCallback, weak_ptr_factory_.GetWeakPtr(), callback); - if (!initialized_) { - Init(base::Bind(&ServiceWorkerCache::Delete, weak_ptr_factory_.GetWeakPtr(), - base::Passed(request.Pass()), pending_callback)); - return; - } - if (!backend_) { - pending_callback.Run(ErrorTypeStorage); - return; + switch (backend_state_) { + case BackendUninitialized: + InitBackend(base::Bind(&ServiceWorkerCache::Delete, + weak_ptr_factory_.GetWeakPtr(), + base::Passed(request.Pass()), pending_callback)); + return; + case BackendClosed: + pending_callback.Run(ErrorTypeStorage); + return; + case BackendOpen: + DCHECK(backend_); + break; } scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); @@ -795,14 +804,18 @@ void ServiceWorkerCache::Keys(const RequestsCallback& callback) { RequestsCallback pending_callback = base::Bind(&ServiceWorkerCache::PendingRequestsCallback, weak_ptr_factory_.GetWeakPtr(), callback); - if (!initialized_) { - Init(base::Bind(&ServiceWorkerCache::Keys, weak_ptr_factory_.GetWeakPtr(), - pending_callback)); - return; - } - if (!backend_) { - pending_callback.Run(ErrorTypeStorage, scoped_ptr<Requests>()); - return; + + switch (backend_state_) { + case BackendUninitialized: + InitBackend(base::Bind(&ServiceWorkerCache::Keys, + weak_ptr_factory_.GetWeakPtr(), pending_callback)); + return; + case BackendClosed: + pending_callback.Run(ErrorTypeStorage, scoped_ptr<Requests>()); + return; + case BackendOpen: + DCHECK(backend_); + break; } // 1. Iterate through all of the entries, open them, and add them to a vector. @@ -832,24 +845,24 @@ void ServiceWorkerCache::Keys(const RequestsCallback& callback) { } void ServiceWorkerCache::Close(const base::Closure& callback) { - DCHECK(!initialized_ || backend_) + DCHECK(backend_state_ != BackendClosed) << "Don't call ServiceWorkerCache::Close() twice."; + backend_state_ = BackendClosed; + if (pending_ops_ > 0) { DCHECK(ops_complete_callback_.is_null()); - initialized_ = true; // So that future operations halt. ops_complete_callback_ = base::Bind( &ServiceWorkerCache::Close, weak_ptr_factory_.GetWeakPtr(), callback); return; } - initialized_ = true; backend_.reset(); callback.Run(); } int64 ServiceWorkerCache::MemoryBackedSize() const { - if (!backend_ || !memory_only_) + if (backend_state_ != BackendOpen || !memory_only_) return 0; scoped_ptr<disk_cache::Backend::Iterator> backend_iter = @@ -887,7 +900,7 @@ ServiceWorkerCache::ServiceWorkerCache( request_context_(request_context), quota_manager_proxy_(quota_manager_proxy), blob_storage_context_(blob_context), - initialized_(false), + backend_state_(BackendUninitialized), memory_only_(path.empty()), pending_ops_(0), weak_ptr_factory_(this) { @@ -895,7 +908,8 @@ ServiceWorkerCache::ServiceWorkerCache( // static void ServiceWorkerCache::PutImpl(scoped_ptr<PutContext> put_context) { - if (!put_context->cache || !put_context->cache->backend_) { + if (!put_context->cache || + put_context->cache->backend_state_ != BackendOpen) { put_context->callback.Run(ErrorTypeStorage, scoped_ptr<ServiceWorkerResponse>(), scoped_ptr<storage::BlobDataHandle>()); @@ -913,7 +927,8 @@ void ServiceWorkerCache::PutImpl(scoped_ptr<PutContext> put_context) { // static void ServiceWorkerCache::PutDidDelete(scoped_ptr<PutContext> put_context, ErrorType delete_error) { - if (!put_context->cache || !put_context->cache->backend_) { + if (!put_context->cache || + put_context->cache->backend_state_ != BackendOpen) { put_context->callback.Run(ErrorTypeStorage, scoped_ptr<ServiceWorkerResponse>(), scoped_ptr<storage::BlobDataHandle>()); @@ -1101,7 +1116,7 @@ void ServiceWorkerCache::KeysDidOpenNextEntry( return; } - if (!cache->backend_) { + if (cache->backend_state_ != BackendOpen) { keys_context->original_callback.Run(ErrorTypeNotFound, scoped_ptr<Requests>()); return; @@ -1201,8 +1216,8 @@ void ServiceWorkerCache::CreateBackend(const ErrorCallback& callback) { create_cache_callback.Run(rv); } -void ServiceWorkerCache::Init(const base::Closure& callback) { - DCHECK(!initialized_); +void ServiceWorkerCache::InitBackend(const base::Closure& callback) { + DCHECK(backend_state_ == BackendUninitialized); init_callbacks_.push_back(callback); // If this isn't the first call to Init then return as the initialization @@ -1215,7 +1230,8 @@ void ServiceWorkerCache::Init(const base::Closure& callback) { } void ServiceWorkerCache::InitDone(ErrorType error) { - initialized_ = true; + backend_state_ = + (error == ErrorTypeOK && backend_) ? BackendOpen : BackendClosed; for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); it != init_callbacks_.end(); ++it) { |