| 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_registration.h" | 5 #include "content/browser/service_worker/service_worker_registration.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "content/browser/service_worker/embedded_worker_status.h" | 9 #include "content/browser/service_worker/embedded_worker_status.h" |
| 10 #include "content/browser/service_worker/service_worker_context_core.h" | 10 #include "content/browser/service_worker/service_worker_context_core.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 pattern(), registration_id_, | 100 pattern(), registration_id_, |
| 101 is_deleted_ ? ServiceWorkerRegistrationInfo::IS_DELETED | 101 is_deleted_ ? ServiceWorkerRegistrationInfo::IS_DELETED |
| 102 : ServiceWorkerRegistrationInfo::IS_NOT_DELETED, | 102 : ServiceWorkerRegistrationInfo::IS_NOT_DELETED, |
| 103 GetVersionInfo(active_version_.get()), | 103 GetVersionInfo(active_version_.get()), |
| 104 GetVersionInfo(waiting_version_.get()), | 104 GetVersionInfo(waiting_version_.get()), |
| 105 GetVersionInfo(installing_version_.get()), resources_total_size_bytes_); | 105 GetVersionInfo(installing_version_.get()), resources_total_size_bytes_); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void ServiceWorkerRegistration::SetActiveVersion( | 108 void ServiceWorkerRegistration::SetActiveVersion( |
| 109 const scoped_refptr<ServiceWorkerVersion>& version) { | 109 const scoped_refptr<ServiceWorkerVersion>& version) { |
| 110 should_activate_when_ready_ = false; | |
| 111 if (active_version_ == version) | 110 if (active_version_ == version) |
| 112 return; | 111 return; |
| 113 | 112 |
| 113 should_activate_when_ready_ = false; |
| 114 |
| 114 ChangedVersionAttributesMask mask; | 115 ChangedVersionAttributesMask mask; |
| 115 if (version) | 116 if (version) |
| 116 UnsetVersionInternal(version.get(), &mask); | 117 UnsetVersionInternal(version.get(), &mask); |
| 117 if (active_version_) | 118 if (active_version_) |
| 118 active_version_->RemoveListener(this); | 119 active_version_->RemoveListener(this); |
| 119 active_version_ = version; | 120 active_version_ = version; |
| 120 if (active_version_) | 121 if (active_version_) |
| 121 active_version_->AddListener(this); | 122 active_version_->AddListener(this); |
| 122 mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION); | 123 mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION); |
| 123 | 124 |
| 124 NotifyVersionAttributesChanged(mask); | 125 NotifyVersionAttributesChanged(mask); |
| 125 } | 126 } |
| 126 | 127 |
| 127 void ServiceWorkerRegistration::SetWaitingVersion( | 128 void ServiceWorkerRegistration::SetWaitingVersion( |
| 128 const scoped_refptr<ServiceWorkerVersion>& version) { | 129 const scoped_refptr<ServiceWorkerVersion>& version) { |
| 129 should_activate_when_ready_ = false; | |
| 130 if (waiting_version_ == version) | 130 if (waiting_version_ == version) |
| 131 return; | 131 return; |
| 132 | 132 |
| 133 should_activate_when_ready_ = false; |
| 134 |
| 133 ChangedVersionAttributesMask mask; | 135 ChangedVersionAttributesMask mask; |
| 134 if (version) | 136 if (version) |
| 135 UnsetVersionInternal(version.get(), &mask); | 137 UnsetVersionInternal(version.get(), &mask); |
| 136 waiting_version_ = version; | 138 waiting_version_ = version; |
| 137 mask.add(ChangedVersionAttributesMask::WAITING_VERSION); | 139 mask.add(ChangedVersionAttributesMask::WAITING_VERSION); |
| 138 | 140 |
| 139 NotifyVersionAttributesChanged(mask); | 141 NotifyVersionAttributesChanged(mask); |
| 140 } | 142 } |
| 141 | 143 |
| 142 void ServiceWorkerRegistration::SetInstallingVersion( | 144 void ServiceWorkerRegistration::SetInstallingVersion( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 164 | 166 |
| 165 void ServiceWorkerRegistration::UnsetVersionInternal( | 167 void ServiceWorkerRegistration::UnsetVersionInternal( |
| 166 ServiceWorkerVersion* version, | 168 ServiceWorkerVersion* version, |
| 167 ChangedVersionAttributesMask* mask) { | 169 ChangedVersionAttributesMask* mask) { |
| 168 DCHECK(version); | 170 DCHECK(version); |
| 169 if (installing_version_.get() == version) { | 171 if (installing_version_.get() == version) { |
| 170 installing_version_ = NULL; | 172 installing_version_ = NULL; |
| 171 mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION); | 173 mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION); |
| 172 } else if (waiting_version_.get() == version) { | 174 } else if (waiting_version_.get() == version) { |
| 173 waiting_version_ = NULL; | 175 waiting_version_ = NULL; |
| 176 should_activate_when_ready_ = false; |
| 174 mask->add(ChangedVersionAttributesMask::WAITING_VERSION); | 177 mask->add(ChangedVersionAttributesMask::WAITING_VERSION); |
| 175 } else if (active_version_.get() == version) { | 178 } else if (active_version_.get() == version) { |
| 176 active_version_->RemoveListener(this); | 179 active_version_->RemoveListener(this); |
| 177 active_version_ = NULL; | 180 active_version_ = NULL; |
| 178 mask->add(ChangedVersionAttributesMask::ACTIVE_VERSION); | 181 mask->add(ChangedVersionAttributesMask::ACTIVE_VERSION); |
| 179 } | 182 } |
| 180 } | 183 } |
| 181 | 184 |
| 182 void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() { | 185 void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() { |
| 183 DCHECK(waiting_version()); | 186 DCHECK(waiting_version()); |
| 184 should_activate_when_ready_ = true; | 187 should_activate_when_ready_ = true; |
| 185 | 188 if (IsReadyToActivate()) |
| 186 if (!active_version() || !active_version()->HasControllee() || | 189 ActivateWaitingVersion(false /* delay */); |
| 187 waiting_version()->skip_waiting()) { | |
| 188 ActivateWaitingVersion(false); | |
| 189 } | |
| 190 } | 190 } |
| 191 | 191 |
| 192 void ServiceWorkerRegistration::ClaimClients() { | 192 void ServiceWorkerRegistration::ClaimClients() { |
| 193 DCHECK(context_); | 193 DCHECK(context_); |
| 194 DCHECK(active_version()); | 194 DCHECK(active_version()); |
| 195 | 195 |
| 196 for (std::unique_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = | 196 for (std::unique_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = |
| 197 context_->GetProviderHostIterator(); | 197 context_->GetProviderHostIterator(); |
| 198 !it->IsAtEnd(); it->Advance()) { | 198 !it->IsAtEnd(); it->Advance()) { |
| 199 ServiceWorkerProviderHost* host = it->GetProviderHost(); | 199 ServiceWorkerProviderHost* host = it->GetProviderHost(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 callback, | 246 callback, |
| 247 most_recent_version)); | 247 most_recent_version)); |
| 248 } | 248 } |
| 249 | 249 |
| 250 void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) { | 250 void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) { |
| 251 if (!context_) | 251 if (!context_) |
| 252 return; | 252 return; |
| 253 DCHECK_EQ(active_version(), version); | 253 DCHECK_EQ(active_version(), version); |
| 254 if (is_uninstalling_) | 254 if (is_uninstalling_) |
| 255 Clear(); | 255 Clear(); |
| 256 else if (should_activate_when_ready_) | 256 else if (IsReadyToActivate()) |
| 257 ActivateWaitingVersion(true); | 257 ActivateWaitingVersion(true /* delay */); |
| 258 } |
| 259 |
| 260 void ServiceWorkerRegistration::OnNoWork(ServiceWorkerVersion* version) { |
| 261 if (!context_) |
| 262 return; |
| 263 DCHECK_EQ(active_version(), version); |
| 264 if (IsReadyToActivate()) |
| 265 ActivateWaitingVersion(false /* delay */); |
| 266 } |
| 267 |
| 268 bool ServiceWorkerRegistration::IsReadyToActivate() const { |
| 269 if (!should_activate_when_ready_) |
| 270 return false; |
| 271 |
| 272 DCHECK(waiting_version()); |
| 273 const ServiceWorkerVersion* active = active_version(); |
| 274 if (!active) |
| 275 return true; |
| 276 if (!active->HasWork() && |
| 277 (!active->HasControllee() || waiting_version()->skip_waiting())) { |
| 278 return true; |
| 279 } |
| 280 return false; |
| 258 } | 281 } |
| 259 | 282 |
| 260 void ServiceWorkerRegistration::ActivateWaitingVersion(bool delay) { | 283 void ServiceWorkerRegistration::ActivateWaitingVersion(bool delay) { |
| 261 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 284 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 262 DCHECK(context_); | 285 DCHECK(context_); |
| 263 DCHECK(waiting_version()); | 286 DCHECK(IsReadyToActivate()); |
| 264 DCHECK(should_activate_when_ready_); | |
| 265 should_activate_when_ready_ = false; | 287 should_activate_when_ready_ = false; |
| 266 scoped_refptr<ServiceWorkerVersion> activating_version = waiting_version(); | 288 scoped_refptr<ServiceWorkerVersion> activating_version = waiting_version(); |
| 267 scoped_refptr<ServiceWorkerVersion> exiting_version = active_version(); | 289 scoped_refptr<ServiceWorkerVersion> exiting_version = active_version(); |
| 268 | 290 |
| 269 if (activating_version->is_redundant()) | 291 if (activating_version->is_redundant()) |
| 270 return; // Activation is no longer relevant. | 292 return; // Activation is no longer relevant. |
| 271 | 293 |
| 272 // "5. If exitingWorker is not null, | 294 // "5. If exitingWorker is not null, |
| 273 if (exiting_version.get()) { | 295 if (exiting_version.get()) { |
| 274 // TODO(michaeln): should wait for events to be complete | 296 // TODO(falken): Update the quoted spec comments once |
| 297 // https://github.com/slightlyoff/ServiceWorker/issues/916 is codified in |
| 298 // the spec. |
| 275 // "1. Wait for exitingWorker to finish handling any in-progress requests." | 299 // "1. Wait for exitingWorker to finish handling any in-progress requests." |
| 300 // This is already handled by IsReadyToActivate(). |
| 276 // "2. Terminate exitingWorker." | 301 // "2. Terminate exitingWorker." |
| 277 exiting_version->StopWorker( | 302 exiting_version->StopWorker( |
| 278 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 303 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 279 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and | 304 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and |
| 280 // "redundant" as the arguments." | 305 // "redundant" as the arguments." |
| 281 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT); | 306 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT); |
| 282 } | 307 } |
| 283 | 308 |
| 284 // "6. Set serviceWorkerRegistration.activeWorker to activatingWorker." | 309 // "6. Set serviceWorkerRegistration.activeWorker to activatingWorker." |
| 285 // "7. Set serviceWorkerRegistration.waitingWorker to null." | 310 // "7. Set serviceWorkerRegistration.waitingWorker to null." |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 if (!context_) { | 499 if (!context_) { |
| 475 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 500 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 476 return; | 501 return; |
| 477 } | 502 } |
| 478 context_->storage()->NotifyDoneInstallingRegistration( | 503 context_->storage()->NotifyDoneInstallingRegistration( |
| 479 this, version.get(), status); | 504 this, version.get(), status); |
| 480 callback.Run(status); | 505 callback.Run(status); |
| 481 } | 506 } |
| 482 | 507 |
| 483 } // namespace content | 508 } // namespace content |
| OLD | NEW |