Chromium Code Reviews| Index: content/browser/service_worker/service_worker_registration.cc |
| diff --git a/content/browser/service_worker/service_worker_registration.cc b/content/browser/service_worker/service_worker_registration.cc |
| index cfd1e5e6e0d9554e6d069da150e3b52d4c66c3bd..a31feb46daea8b51f105d4cfa0f3bd9a01ad4846 100644 |
| --- a/content/browser/service_worker/service_worker_registration.cc |
| +++ b/content/browser/service_worker/service_worker_registration.cc |
| @@ -107,10 +107,11 @@ ServiceWorkerRegistrationInfo ServiceWorkerRegistration::GetInfo() { |
| void ServiceWorkerRegistration::SetActiveVersion( |
| const scoped_refptr<ServiceWorkerVersion>& version) { |
| - should_activate_when_ready_ = false; |
| if (active_version_ == version) |
|
nhiroki
2016/07/13 04:29:30
(This comment is not related to this CL. You don't
falken
2016/07/14 13:52:07
Acknowledged. (OTOH, it may be reasonable to write
|
| return; |
| + should_activate_when_ready_ = false; |
| + |
| ChangedVersionAttributesMask mask; |
| if (version) |
| UnsetVersionInternal(version.get(), &mask); |
| @@ -126,10 +127,11 @@ void ServiceWorkerRegistration::SetActiveVersion( |
| void ServiceWorkerRegistration::SetWaitingVersion( |
| const scoped_refptr<ServiceWorkerVersion>& version) { |
| - should_activate_when_ready_ = false; |
| if (waiting_version_ == version) |
|
nhiroki
2016/07/13 04:29:30
ditto.
|
| return; |
| + should_activate_when_ready_ = false; |
| + |
| ChangedVersionAttributesMask mask; |
| if (version) |
| UnsetVersionInternal(version.get(), &mask); |
| @@ -171,6 +173,7 @@ void ServiceWorkerRegistration::UnsetVersionInternal( |
| mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION); |
| } else if (waiting_version_.get() == version) { |
| waiting_version_ = NULL; |
| + should_activate_when_ready_ = false; |
| mask->add(ChangedVersionAttributesMask::WAITING_VERSION); |
| } else if (active_version_.get() == version) { |
| active_version_->RemoveListener(this); |
| @@ -182,11 +185,8 @@ void ServiceWorkerRegistration::UnsetVersionInternal( |
| void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() { |
| DCHECK(waiting_version()); |
| should_activate_when_ready_ = true; |
| - |
| - if (!active_version() || !active_version()->HasControllee() || |
| - waiting_version()->skip_waiting()) { |
| + if (IsReadyToActivate()) |
| ActivateWaitingVersion(false); |
|
kinuko
2016/07/13 02:28:34
nit: add '/* delay */' comment
falken
2016/07/14 13:52:07
Done.
|
| - } |
| } |
| void ServiceWorkerRegistration::ClaimClients() { |
| @@ -253,15 +253,38 @@ void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) { |
| DCHECK_EQ(active_version(), version); |
| if (is_uninstalling_) |
| Clear(); |
| - else if (should_activate_when_ready_) |
| - ActivateWaitingVersion(true); |
| + else if (IsReadyToActivate()) |
| + ActivateWaitingVersion(true /* delay */); |
| +} |
| + |
| +void ServiceWorkerRegistration::OnNoInflightRequests( |
| + ServiceWorkerVersion* version) { |
| + if (!context_) |
| + return; |
| + DCHECK_EQ(active_version(), version); |
| + if (IsReadyToActivate()) |
| + ActivateWaitingVersion(false /* delay */); |
| +} |
| + |
| +bool ServiceWorkerRegistration::IsReadyToActivate() const { |
| + if (!should_activate_when_ready_) |
| + return false; |
| + |
| + DCHECK(waiting_version()); |
| + const ServiceWorkerVersion* active = active_version(); |
| + if (!active) |
| + return true; |
| + if (!active->HasInflightRequests() && |
| + (!active->HasControllee() || waiting_version()->skip_waiting())) { |
| + return true; |
| + } |
| + return false; |
| } |
| void ServiceWorkerRegistration::ActivateWaitingVersion(bool delay) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| DCHECK(context_); |
| - DCHECK(waiting_version()); |
| - DCHECK(should_activate_when_ready_); |
| + DCHECK(IsReadyToActivate()); |
| should_activate_when_ready_ = false; |
| scoped_refptr<ServiceWorkerVersion> activating_version = waiting_version(); |
| scoped_refptr<ServiceWorkerVersion> exiting_version = active_version(); |
| @@ -271,8 +294,11 @@ void ServiceWorkerRegistration::ActivateWaitingVersion(bool delay) { |
| // "5. If exitingWorker is not null, |
| if (exiting_version.get()) { |
| - // TODO(michaeln): should wait for events to be complete |
| + // TODO(falken): Update the quoted spec comments once |
| + // https://github.com/slightlyoff/ServiceWorker/issues/916 is codified in |
| + // the spec. |
| // "1. Wait for exitingWorker to finish handling any in-progress requests." |
| + // This is already handled by IsReadyToActivate(). |
| // "2. Terminate exitingWorker." |
| exiting_version->StopWorker( |
| base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |