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 <vector> | |
| 8 | |
| 9 #include "content/browser/service_worker/service_worker_job_coordinator.h" | |
| 7 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
| 8 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 9 #include "url/gurl.h" | 12 #include "url/gurl.h" |
| 10 | 13 |
| 11 namespace content { | 14 namespace content { |
| 12 | 15 |
| 13 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( | 16 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( |
| 14 ServiceWorkerStorage* storage, | 17 ServiceWorkerStorage* storage, |
| 15 const RegistrationCompleteCallback& callback) | 18 ServiceWorkerJobCoordinator* coordinator, |
| 16 : storage_(storage), callback_(callback), weak_factory_(this) {} | 19 const GURL& pattern, |
| 20 const GURL& script_url, | |
| 21 RegistrationType type) | |
| 22 : storage_(storage), | |
| 23 coordinator_(coordinator), | |
| 24 pattern_(pattern), | |
| 25 script_url_(script_url), | |
| 26 weak_factory_(this), | |
| 27 type_(type) {} | |
| 17 | 28 |
| 18 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() {} | 29 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() {} |
| 19 | 30 |
| 20 void ServiceWorkerRegisterJob::StartRegister(const GURL& pattern, | 31 void ServiceWorkerRegisterJob::AddCallback( |
| 21 const GURL& script_url) { | 32 const RegistrationCallback& callback) { |
| 33 callbacks_.push_back(callback); | |
| 34 } | |
| 35 | |
| 36 void ServiceWorkerRegisterJob::Start() { | |
| 37 if (type_ == REGISTER) | |
| 38 StartRegister(); | |
| 39 else | |
| 40 StartUnregister(); | |
| 41 } | |
| 42 | |
| 43 bool ServiceWorkerRegisterJob::Equals(ServiceWorkerRegisterJob* job) { | |
| 44 return job->type() == type_ && job->script_url() == script_url_; | |
|
kinuko
2014/01/09 10:54:08
When both types are UNREGISTER we still seem to ca
alecflett
2014/01/09 21:23:14
good point. We don't actually hit this in practice
| |
| 45 } | |
| 46 | |
| 47 void ServiceWorkerRegisterJob::StartRegister() { | |
| 22 // Set up a chain of callbacks, in reverse order. Each of these | 48 // Set up a chain of callbacks, in reverse order. Each of these |
| 23 // callbacks may be called asynchronously by the previous callback. | 49 // callbacks may be called asynchronously by the previous callback. |
| 24 RegistrationCallback finish_registration(base::Bind( | 50 RegistrationCallback finish_registration(base::Bind( |
| 25 &ServiceWorkerRegisterJob::RegisterComplete, weak_factory_.GetWeakPtr())); | 51 &ServiceWorkerRegisterJob::RegisterComplete, weak_factory_.GetWeakPtr())); |
| 26 | 52 |
| 27 UnregistrationCallback register_new( | 53 UnregistrationCallback register_new( |
| 28 base::Bind(&ServiceWorkerRegisterJob::RegisterPatternAndContinue, | 54 base::Bind(&ServiceWorkerRegisterJob::RegisterPatternAndContinue, |
| 29 weak_factory_.GetWeakPtr(), | 55 weak_factory_.GetWeakPtr(), |
| 30 pattern, | |
| 31 script_url, | |
| 32 finish_registration)); | 56 finish_registration)); |
| 33 | 57 |
| 34 ServiceWorkerStorage::FindRegistrationCallback unregister_old( | 58 ServiceWorkerStorage::FindRegistrationCallback unregister_old( |
| 35 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, | 59 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, |
| 36 weak_factory_.GetWeakPtr(), | 60 weak_factory_.GetWeakPtr(), |
| 37 pattern, | |
| 38 script_url, | |
| 39 register_new)); | 61 register_new)); |
| 40 | 62 |
| 41 storage_->FindRegistrationForPattern(pattern, unregister_old); | 63 storage_->FindRegistrationForPattern(pattern_, unregister_old); |
| 42 } | 64 } |
| 43 | 65 |
| 44 void ServiceWorkerRegisterJob::StartUnregister(const GURL& pattern) { | 66 void ServiceWorkerRegisterJob::StartUnregister() { |
| 45 // Set up a chain of callbacks, in reverse order. Each of these | 67 // Set up a chain of callbacks, in reverse order. Each of these |
| 46 // callbacks may be called asynchronously by the previous callback. | 68 // callbacks may be called asynchronously by the previous callback. |
| 47 UnregistrationCallback finish_unregistration( | 69 UnregistrationCallback finish_unregistration( |
| 48 base::Bind(&ServiceWorkerRegisterJob::UnregisterComplete, | 70 base::Bind(&ServiceWorkerRegisterJob::UnregisterComplete, |
| 49 weak_factory_.GetWeakPtr())); | 71 weak_factory_.GetWeakPtr())); |
| 50 | 72 |
| 51 ServiceWorkerStorage::FindRegistrationCallback unregister( | 73 ServiceWorkerStorage::FindRegistrationCallback unregister( |
| 52 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, | 74 base::Bind(&ServiceWorkerRegisterJob::UnregisterPatternAndContinue, |
| 53 weak_factory_.GetWeakPtr(), | 75 weak_factory_.GetWeakPtr(), |
| 54 pattern, | |
| 55 GURL(), | |
| 56 finish_unregistration)); | 76 finish_unregistration)); |
| 57 | 77 |
| 58 storage_->FindRegistrationForPattern(pattern, unregister); | 78 storage_->FindRegistrationForPattern(pattern_, unregister); |
| 59 } | 79 } |
| 60 | 80 |
| 61 void ServiceWorkerRegisterJob::RegisterPatternAndContinue( | 81 void ServiceWorkerRegisterJob::RegisterPatternAndContinue( |
| 62 const GURL& pattern, | |
| 63 const GURL& script_url, | |
| 64 const RegistrationCallback& callback, | 82 const RegistrationCallback& callback, |
| 65 ServiceWorkerRegistrationStatus previous_status) { | 83 ServiceWorkerRegistrationStatus previous_status) { |
| 66 if (previous_status != REGISTRATION_OK) { | 84 if (previous_status != REGISTRATION_OK) { |
| 67 BrowserThread::PostTask( | 85 BrowserThread::PostTask( |
| 68 BrowserThread::IO, | 86 BrowserThread::IO, |
| 69 FROM_HERE, | 87 FROM_HERE, |
| 70 base::Bind(callback, | 88 base::Bind(callback, |
| 71 previous_status, | 89 previous_status, |
| 72 scoped_refptr<ServiceWorkerRegistration>())); | 90 scoped_refptr<ServiceWorkerRegistration>())); |
| 73 return; | 91 return; |
| 74 } | 92 } |
| 75 | 93 |
| 76 // TODO: Eventually RegisterInternal will be replaced by an asynchronous | 94 // TODO: Eventually RegisterInternal will be replaced by an asynchronous |
| 77 // operation. Pass its resulting status through 'callback'. | 95 // operation. Pass its resulting status through 'callback'. |
| 78 scoped_refptr<ServiceWorkerRegistration> registration = | 96 scoped_refptr<ServiceWorkerRegistration> registration = |
| 79 storage_->RegisterInternal(pattern, script_url); | 97 storage_->RegisterInternal(pattern_, script_url_); |
| 80 BrowserThread::PostTask(BrowserThread::IO, | 98 BrowserThread::PostTask(BrowserThread::IO, |
| 81 FROM_HERE, | 99 FROM_HERE, |
| 82 base::Bind(callback, REGISTRATION_OK, registration)); | 100 base::Bind(callback, REGISTRATION_OK, registration)); |
| 83 } | 101 } |
| 84 | 102 |
| 85 void ServiceWorkerRegisterJob::UnregisterPatternAndContinue( | 103 void ServiceWorkerRegisterJob::UnregisterPatternAndContinue( |
| 86 const GURL& pattern, | |
| 87 const GURL& new_script_url, | |
| 88 const UnregistrationCallback& callback, | 104 const UnregistrationCallback& callback, |
| 89 bool found, | 105 bool found, |
| 90 ServiceWorkerRegistrationStatus previous_status, | 106 ServiceWorkerRegistrationStatus previous_status, |
| 91 const scoped_refptr<ServiceWorkerRegistration>& previous_registration) { | 107 const scoped_refptr<ServiceWorkerRegistration>& previous_registration) { |
| 92 | 108 |
| 93 // The previous registration may not exist, which is ok. | 109 // The previous registration may not exist, which is ok. |
| 94 if (previous_status == REGISTRATION_OK && found && | 110 if (previous_status == REGISTRATION_OK && found && |
| 95 (new_script_url.is_empty() || | 111 (script_url_.is_empty() || |
| 96 previous_registration->script_url() != new_script_url)) { | 112 previous_registration->script_url() != script_url_)) { |
| 97 // TODO: Eventually UnregisterInternal will be replaced by an | 113 // TODO: Eventually UnregisterInternal will be replaced by an |
| 98 // asynchronous operation. Pass its resulting status though | 114 // asynchronous operation. Pass its resulting status though |
| 99 // 'callback'. | 115 // 'callback'. |
| 100 storage_->UnregisterInternal(pattern); | 116 storage_->UnregisterInternal(pattern_); |
| 117 DCHECK(previous_registration->is_shutdown()); | |
| 101 } | 118 } |
| 102 BrowserThread::PostTask( | 119 BrowserThread::PostTask( |
| 103 BrowserThread::IO, FROM_HERE, base::Bind(callback, previous_status)); | 120 BrowserThread::IO, FROM_HERE, base::Bind(callback, previous_status)); |
| 104 } | 121 } |
| 105 | 122 |
| 123 void ServiceWorkerRegisterJob::RunCallbacks( | |
| 124 ServiceWorkerRegistrationStatus status, | |
| 125 const scoped_refptr<ServiceWorkerRegistration>& registration) { | |
| 126 for (std::vector<RegistrationCallback>::iterator it = | |
| 127 callbacks_.begin(); | |
| 128 it != callbacks_.end(); | |
| 129 ++it) { | |
| 130 it->Run(status, registration); | |
| 131 } | |
| 132 } | |
| 106 void ServiceWorkerRegisterJob::RegisterComplete( | 133 void ServiceWorkerRegisterJob::RegisterComplete( |
| 107 ServiceWorkerRegistrationStatus status, | 134 ServiceWorkerRegistrationStatus status, |
| 108 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 135 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 109 callback_.Run(this, status, registration); | 136 RunCallbacks(status, registration); |
| 137 coordinator_->FinishJob(this); | |
| 110 } | 138 } |
| 111 | 139 |
| 112 void ServiceWorkerRegisterJob::UnregisterComplete( | 140 void ServiceWorkerRegisterJob::UnregisterComplete( |
| 113 ServiceWorkerRegistrationStatus status) { | 141 ServiceWorkerRegistrationStatus status) { |
| 114 callback_.Run(this, status, NULL); | 142 RunCallbacks(status, NULL); |
| 143 coordinator_->FinishJob(this); | |
| 115 } | 144 } |
| 116 | 145 |
| 117 } // namespace content | 146 } // namespace content |
| OLD | NEW |