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