Chromium Code Reviews| Index: content/browser/service_worker/service_worker_register_job.cc |
| diff --git a/content/browser/service_worker/service_worker_register_job.cc b/content/browser/service_worker/service_worker_register_job.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f679721f216a2a0530183a29d80cc5c0f3855ae7 |
| --- /dev/null |
| +++ b/content/browser/service_worker/service_worker_register_job.cc |
| @@ -0,0 +1,120 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/service_worker/service_worker_register_job.h" |
| + |
| +#include "content/browser/service_worker/service_worker_registration.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "url/gurl.h" |
| + |
| +namespace content { |
| + |
| +ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( |
| + const base::WeakPtr<ServiceWorkerStorage>& storage, |
| + const RegistrationCompleteCallback& callback) |
| + : storage_(storage), callback_(callback), weak_factory_(this) {} |
| + |
| +ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() {} |
| + |
| +void ServiceWorkerRegisterJob::StartRegister(const GURL& pattern, |
| + const GURL& script_url) { |
| + // Set up a chain of callbacks, in reverse order. Each of these |
| + // callbacks may be called asynchronously by the previous callback. |
|
kinuko
2013/11/27 03:45:23
Are we going to create all these callbacks in reve
alecflett
2013/12/02 16:11:35
Yeah, I started this way (see previous iterations
|
| + ServiceWorkerStorage::RegistrationCallback finish_registration(base::Bind( |
| + &ServiceWorkerRegisterJob::RegisterComplete, weak_factory_.GetWeakPtr())); |
| + |
| + ServiceWorkerStorage::UnregistrationCallback register_new( |
| + base::Bind(&ServiceWorkerRegisterJob::RegisterPatternAndContinue, |
| + weak_factory_.GetWeakPtr(), |
| + pattern, |
| + script_url, |
| + finish_registration)); |
| + |
| + ServiceWorkerStorage::RegistrationCallback unregister_old( |
| + base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, |
| + weak_factory_.GetWeakPtr(), |
| + pattern, |
| + script_url, |
| + register_new)); |
| + |
| + storage_->FindRegistrationForPattern(pattern, unregister_old); |
| +} |
| + |
| +void ServiceWorkerRegisterJob::StartUnregister(const GURL& pattern) { |
| + // Set up a chain of callbacks, in reverse order. Each of these |
| + // callbacks may be called asynchronously by the previous callback. |
| + ServiceWorkerStorage::UnregistrationCallback finish_unregistration( |
| + base::Bind(&ServiceWorkerRegisterJob::UnregisterComplete, |
| + weak_factory_.GetWeakPtr())); |
| + |
| + ServiceWorkerStorage::RegistrationCallback unregister( |
| + base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, |
| + weak_factory_.GetWeakPtr(), |
| + pattern, |
| + GURL(), |
| + finish_unregistration)); |
| + |
| + storage_->FindRegistrationForPattern(pattern, unregister); |
| +} |
| + |
| +void ServiceWorkerRegisterJob::RegisterPatternAndContinue( |
| + const GURL& pattern, |
| + const GURL& script_url, |
| + const ServiceWorkerStorage::RegistrationCallback& callback, |
| + ServiceWorkerStorage::RegistrationStatus previous_status) { |
| + if (previous_status != ServiceWorkerStorage::REGISTRATION_OK && |
| + previous_status != ServiceWorkerStorage::REGISTRATION_NOT_FOUND) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(callback, |
| + previous_status, |
| + scoped_refptr<ServiceWorkerRegistration>(NULL))); |
| + } |
| + |
| + // TODO: Eventually RegisterInternal will be replaced by an asynchronous |
| + // operation. Pass its resulting status though 'callback'. |
| + scoped_refptr<ServiceWorkerRegistration> registration = |
| + storage_->RegisterInternal(pattern, script_url); |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind( |
| + callback, ServiceWorkerStorage::REGISTRATION_OK, registration)); |
| +} |
| + |
| +void ServiceWorkerRegisterJob::UnregisterPatternAndContinue( |
| + const GURL& pattern, |
| + const GURL& new_script_url, |
| + const ServiceWorkerStorage::UnregistrationCallback& callback, |
| + ServiceWorkerStorage::RegistrationStatus previous_status, |
| + const scoped_refptr<ServiceWorkerRegistration>& previous_registration) { |
| + DCHECK(previous_status == ServiceWorkerStorage::REGISTRATION_OK || |
| + previous_status == ServiceWorkerStorage::REGISTRATION_NOT_FOUND); |
| + |
| + // The previous registration may not exist, which is ok. |
| + if (previous_status == ServiceWorkerStorage::REGISTRATION_OK && |
| + (new_script_url.is_empty() || |
| + previous_registration->script_url() != new_script_url)) { |
| + // TODO: Eventually UnregisterInternal will be replaced by an |
| + // asynchronous operation. Pass its resulting status though |
| + // 'callback'. |
| + storage_->UnregisterInternal(pattern); |
| + } |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, base::Bind(callback, previous_status)); |
| +} |
| + |
| +void ServiceWorkerRegisterJob::UnregisterComplete( |
| + ServiceWorkerStorage::RegistrationStatus status) { |
| + callback_.Run(this, status, NULL); |
| +} |
| + |
| +void ServiceWorkerRegisterJob::RegisterComplete( |
| + ServiceWorkerStorage::RegistrationStatus status, |
| + const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| + callback_.Run(this, status, registration); |
| +} |
| + |
| +} // namespace content |