Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/service_worker/service_worker_register_job.h" | 5 #include "content/browser/service_worker/service_worker_register_job.h" |
| 6 | 6 |
| 7 #include "content/browser/service_worker/service_worker_job_coordinator.h" | |
| 7 #include "content/browser/service_worker/service_worker_registration.h" | 8 #include "content/browser/service_worker/service_worker_registration.h" |
| 8 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 9 #include "url/gurl.h" | 10 #include "url/gurl.h" |
| 10 | 11 |
| 11 namespace content { | 12 namespace content { |
| 12 | 13 |
| 13 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( | 14 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( |
| 14 const base::WeakPtr<ServiceWorkerStorage>& storage, | 15 const base::WeakPtr<ServiceWorkerStorage>& storage, |
| 15 const RegistrationCompleteCallback& callback) | 16 ServiceWorkerJobCoordinator* coordinator, |
| 16 : storage_(storage), callback_(callback), weak_factory_(this) {} | 17 const GURL& pattern, |
| 18 const GURL& script_url, | |
| 19 RegistrationType type) | |
| 20 : storage_(storage), | |
| 21 coordinator_(coordinator), | |
| 22 pattern_(pattern), | |
| 23 script_url_(script_url), | |
| 24 weak_factory_(this), | |
| 25 type_(type) {} | |
|
kinuko
2014/01/08 08:38:36
running_ is not initialized (or I hope we can just
alecflett
2014/01/08 23:59:16
Done.
| |
| 17 | 26 |
| 18 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() {} | 27 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() {} |
| 19 | 28 |
| 20 void ServiceWorkerRegisterJob::StartRegister(const GURL& pattern, | 29 void ServiceWorkerRegisterJob::AddCallback( |
| 21 const GURL& script_url) { | 30 const RegistrationCompleteCallback& callback) { |
| 31 callbacks_.push_back(callback); | |
| 32 } | |
| 33 | |
| 34 void ServiceWorkerRegisterJob::StartRegister() { | |
| 35 | |
| 22 // Set up a chain of callbacks, in reverse order. Each of these | 36 // Set up a chain of callbacks, in reverse order. Each of these |
| 23 // callbacks may be called asynchronously by the previous callback. | 37 // callbacks may be called asynchronously by the previous callback. |
| 24 RegistrationCallback finish_registration(base::Bind( | 38 RegistrationCallback finish_registration(base::Bind( |
| 25 &ServiceWorkerRegisterJob::RegisterComplete, weak_factory_.GetWeakPtr())); | 39 &ServiceWorkerRegisterJob::RegisterComplete, weak_factory_.GetWeakPtr())); |
| 26 | 40 |
| 27 UnregistrationCallback register_new( | 41 UnregistrationCallback register_new( |
| 28 base::Bind(&ServiceWorkerRegisterJob::RegisterPatternAndContinue, | 42 base::Bind(&ServiceWorkerRegisterJob::RegisterPatternAndContinue, |
| 29 weak_factory_.GetWeakPtr(), | 43 weak_factory_.GetWeakPtr(), |
| 30 pattern, | |
| 31 script_url, | |
| 32 finish_registration)); | 44 finish_registration)); |
| 33 | 45 |
| 34 ServiceWorkerStorage::FindRegistrationCallback unregister_old( | 46 ServiceWorkerStorage::FindRegistrationCallback unregister_old( |
| 35 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, | 47 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, |
| 36 weak_factory_.GetWeakPtr(), | 48 weak_factory_.GetWeakPtr(), |
| 37 pattern, | |
| 38 script_url, | |
| 39 register_new)); | 49 register_new)); |
| 40 | 50 |
| 41 storage_->FindRegistrationForPattern(pattern, unregister_old); | 51 storage_->FindRegistrationForPattern(pattern_, unregister_old); |
| 42 } | 52 } |
| 43 | 53 |
| 44 void ServiceWorkerRegisterJob::StartUnregister(const GURL& pattern) { | 54 void ServiceWorkerRegisterJob::StartUnregister() { |
| 45 // Set up a chain of callbacks, in reverse order. Each of these | 55 // Set up a chain of callbacks, in reverse order. Each of these |
| 46 // callbacks may be called asynchronously by the previous callback. | 56 // callbacks may be called asynchronously by the previous callback. |
| 47 UnregistrationCallback finish_unregistration( | 57 UnregistrationCallback finish_unregistration( |
| 48 base::Bind(&ServiceWorkerRegisterJob::UnregisterComplete, | 58 base::Bind(&ServiceWorkerRegisterJob::UnregisterComplete, |
| 49 weak_factory_.GetWeakPtr())); | 59 weak_factory_.GetWeakPtr())); |
| 50 | 60 |
| 51 ServiceWorkerStorage::FindRegistrationCallback unregister( | 61 ServiceWorkerStorage::FindRegistrationCallback unregister( |
| 52 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, | 62 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, |
| 53 weak_factory_.GetWeakPtr(), | 63 weak_factory_.GetWeakPtr(), |
| 54 pattern, | |
| 55 GURL(), | |
| 56 finish_unregistration)); | 64 finish_unregistration)); |
| 57 | 65 |
| 58 storage_->FindRegistrationForPattern(pattern, unregister); | 66 storage_->FindRegistrationForPattern(pattern_, unregister); |
| 59 } | 67 } |
| 60 | 68 |
| 61 void ServiceWorkerRegisterJob::RegisterPatternAndContinue( | 69 void ServiceWorkerRegisterJob::RegisterPatternAndContinue( |
| 62 const GURL& pattern, | |
| 63 const GURL& script_url, | |
| 64 const RegistrationCallback& callback, | 70 const RegistrationCallback& callback, |
| 65 ServiceWorkerRegistrationStatus previous_status) { | 71 ServiceWorkerRegistrationStatus previous_status) { |
| 66 if (previous_status != REGISTRATION_OK) { | 72 if (previous_status != REGISTRATION_OK) { |
| 67 BrowserThread::PostTask( | 73 BrowserThread::PostTask( |
| 68 BrowserThread::IO, | 74 BrowserThread::IO, |
| 69 FROM_HERE, | 75 FROM_HERE, |
| 70 base::Bind(callback, | 76 base::Bind(callback, |
| 71 previous_status, | 77 previous_status, |
| 72 scoped_refptr<ServiceWorkerRegistration>())); | 78 scoped_refptr<ServiceWorkerRegistration>())); |
| 73 return; | 79 return; |
| 74 } | 80 } |
| 75 | 81 |
| 76 // TODO: Eventually RegisterInternal will be replaced by an asynchronous | 82 // TODO: Eventually RegisterInternal will be replaced by an asynchronous |
| 77 // operation. Pass its resulting status through 'callback'. | 83 // operation. Pass its resulting status through 'callback'. |
| 78 scoped_refptr<ServiceWorkerRegistration> registration = | 84 scoped_refptr<ServiceWorkerRegistration> registration = |
| 79 storage_->RegisterInternal(pattern, script_url); | 85 storage_->RegisterInternal(pattern_, script_url_); |
| 80 BrowserThread::PostTask(BrowserThread::IO, | 86 BrowserThread::PostTask(BrowserThread::IO, |
| 81 FROM_HERE, | 87 FROM_HERE, |
| 82 base::Bind(callback, REGISTRATION_OK, registration)); | 88 base::Bind(callback, REGISTRATION_OK, registration)); |
| 83 } | 89 } |
| 84 | 90 |
| 85 void ServiceWorkerRegisterJob::UnregisterPatternAndContinue( | 91 void ServiceWorkerRegisterJob::UnregisterPatternAndContinue( |
| 86 const GURL& pattern, | |
| 87 const GURL& new_script_url, | |
| 88 const UnregistrationCallback& callback, | 92 const UnregistrationCallback& callback, |
| 89 bool found, | 93 bool found, |
| 90 ServiceWorkerRegistrationStatus previous_status, | 94 ServiceWorkerRegistrationStatus previous_status, |
| 91 const scoped_refptr<ServiceWorkerRegistration>& previous_registration) { | 95 const scoped_refptr<ServiceWorkerRegistration>& previous_registration) { |
| 92 | 96 |
| 93 // The previous registration may not exist, which is ok. | 97 // The previous registration may not exist, which is ok. |
| 94 if (previous_status == REGISTRATION_OK && found && | 98 if (previous_status == REGISTRATION_OK && found && |
| 95 (new_script_url.is_empty() || | 99 (script_url_.is_empty() || |
| 96 previous_registration->script_url() != new_script_url)) { | 100 previous_registration->script_url() != script_url_)) { |
| 97 // TODO: Eventually UnregisterInternal will be replaced by an | 101 // TODO: Eventually UnregisterInternal will be replaced by an |
| 98 // asynchronous operation. Pass its resulting status though | 102 // asynchronous operation. Pass its resulting status though |
| 99 // 'callback'. | 103 // 'callback'. |
| 100 storage_->UnregisterInternal(pattern); | 104 storage_->UnregisterInternal(pattern_); |
| 105 DCHECK(previous_registration->is_shutdown()); | |
| 101 } | 106 } |
| 102 BrowserThread::PostTask( | 107 BrowserThread::PostTask( |
| 103 BrowserThread::IO, FROM_HERE, base::Bind(callback, previous_status)); | 108 BrowserThread::IO, FROM_HERE, base::Bind(callback, previous_status)); |
| 104 } | 109 } |
| 105 | 110 |
| 111 void ServiceWorkerRegisterJob::RunCallbacks( | |
| 112 ServiceWorkerRegistrationStatus status, | |
| 113 const scoped_refptr<ServiceWorkerRegistration>& registration) { | |
| 114 for (std::vector<RegistrationCompleteCallback>::iterator it = | |
| 115 callbacks_.begin(); | |
| 116 it != callbacks_.end(); | |
| 117 ++it) { | |
| 118 it->Run(this, status, registration); | |
| 119 } | |
| 120 } | |
| 106 void ServiceWorkerRegisterJob::RegisterComplete( | 121 void ServiceWorkerRegisterJob::RegisterComplete( |
| 107 ServiceWorkerRegistrationStatus status, | 122 ServiceWorkerRegistrationStatus status, |
| 108 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 123 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 109 callback_.Run(this, status, registration); | 124 RunCallbacks(status, registration); |
| 125 running_ = false; | |
| 126 coordinator_->FinishJob(this); | |
| 110 } | 127 } |
| 111 | 128 |
| 112 void ServiceWorkerRegisterJob::UnregisterComplete( | 129 void ServiceWorkerRegisterJob::UnregisterComplete( |
| 113 ServiceWorkerRegistrationStatus status) { | 130 ServiceWorkerRegistrationStatus status) { |
| 114 callback_.Run(this, status, NULL); | 131 RunCallbacks(status, NULL); |
| 132 running_ = false; | |
| 133 coordinator_->FinishJob(this); | |
| 115 } | 134 } |
| 116 | 135 |
| 117 } // namespace content | 136 } // namespace content |
| OLD | NEW |