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_registration.h" | 5 #include "content/browser/service_worker/service_worker_registration.h" |
| 6 | 6 |
| 7 #include "content/browser/service_worker/service_worker_context_core.h" | 7 #include "content/browser/service_worker/service_worker_context_core.h" |
| 8 #include "content/browser/service_worker/service_worker_info.h" | 8 #include "content/browser/service_worker/service_worker_info.h" |
| 9 #include "content/browser/service_worker/service_worker_register_job.h" | 9 #include "content/browser/service_worker/service_worker_register_job.h" |
| 10 #include "content/browser/service_worker/service_worker_utils.h" | 10 #include "content/browser/service_worker/service_worker_utils.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 is_deleted_(false), | 33 is_deleted_(false), |
| 34 should_activate_when_ready_(false), | 34 should_activate_when_ready_(false), |
| 35 context_(context) { | 35 context_(context) { |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 37 DCHECK(context_); | 37 DCHECK(context_); |
| 38 context_->AddLiveRegistration(this); | 38 context_->AddLiveRegistration(this); |
| 39 } | 39 } |
| 40 | 40 |
| 41 ServiceWorkerRegistration::~ServiceWorkerRegistration() { | 41 ServiceWorkerRegistration::~ServiceWorkerRegistration() { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 43 DCHECK(!listeners_.might_have_observers()); | |
| 43 if (context_) | 44 if (context_) |
| 44 context_->RemoveLiveRegistration(registration_id_); | 45 context_->RemoveLiveRegistration(registration_id_); |
| 45 ResetShouldActivateWhenReady(); | 46 ResetShouldActivateWhenReady(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 void ServiceWorkerRegistration::AddListener(Listener* listener) { | 49 void ServiceWorkerRegistration::AddListener(Listener* listener) { |
| 49 listeners_.AddObserver(listener); | 50 listeners_.AddObserver(listener); |
| 50 } | 51 } |
| 51 | 52 |
| 52 void ServiceWorkerRegistration::RemoveListener(Listener* listener) { | 53 void ServiceWorkerRegistration::RemoveListener(Listener* listener) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 if (should_activate_when_ready_) | 135 if (should_activate_when_ready_) |
| 135 return; | 136 return; |
| 136 if (active_version() && active_version()->HasControllee()) { | 137 if (active_version() && active_version()->HasControllee()) { |
| 137 active_version()->AddListener(this); | 138 active_version()->AddListener(this); |
| 138 should_activate_when_ready_ = true; | 139 should_activate_when_ready_ = true; |
| 139 return; | 140 return; |
| 140 } | 141 } |
| 141 ActivateWaitingVersion(); | 142 ActivateWaitingVersion(); |
| 142 } | 143 } |
| 143 | 144 |
| 145 void ServiceWorkerRegistration::SetIsDeleted() { | |
| 146 is_deleted_ = true; | |
| 147 FOR_EACH_OBSERVER(Listener, listeners_, OnRegistrationDeleted(this)); | |
| 148 } | |
| 149 | |
| 144 void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) { | 150 void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) { |
| 145 DCHECK_EQ(active_version(), version); | 151 DCHECK_EQ(active_version(), version); |
| 146 DCHECK(should_activate_when_ready_); | 152 DCHECK(should_activate_when_ready_); |
| 147 active_version_->RemoveListener(this); | 153 active_version_->RemoveListener(this); |
| 148 should_activate_when_ready_ = false; | 154 should_activate_when_ready_ = false; |
| 149 ActivateWaitingVersion(); | 155 ActivateWaitingVersion(); |
| 150 } | 156 } |
| 151 | 157 |
| 152 void ServiceWorkerRegistration::ActivateWaitingVersion() { | 158 void ServiceWorkerRegistration::ActivateWaitingVersion() { |
| 153 DCHECK(context_); | 159 DCHECK(context_); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 168 // "2. Terminate exitingWorker." | 174 // "2. Terminate exitingWorker." |
| 169 exiting_version->StopWorker( | 175 exiting_version->StopWorker( |
| 170 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 176 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 171 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and | 177 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and |
| 172 // "redundant" as the arguments." | 178 // "redundant" as the arguments." |
| 173 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT); | 179 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT); |
| 174 } | 180 } |
| 175 | 181 |
| 176 // "5. Set serviceWorkerRegistration.activeWorker to activatingWorker." | 182 // "5. Set serviceWorkerRegistration.activeWorker to activatingWorker." |
| 177 // "6. Set serviceWorkerRegistration.waitingWorker to null." | 183 // "6. Set serviceWorkerRegistration.waitingWorker to null." |
| 178 ServiceWorkerRegisterJob::DisassociateVersionFromDocuments( | |
| 179 context_, activating_version); | |
| 180 SetActiveVersion(activating_version); | 184 SetActiveVersion(activating_version); |
| 181 ServiceWorkerRegisterJob::AssociateActiveVersionToDocuments( | |
| 182 context_, activating_version); | |
| 183 | 185 |
| 184 // "7. Run the [[UpdateState]] algorithm passing registration.activeWorker and | 186 // "7. Run the [[UpdateState]] algorithm passing registration.activeWorker and |
| 185 // "activating" as arguments." | 187 // "activating" as arguments." |
| 186 activating_version->SetStatus(ServiceWorkerVersion::ACTIVATING); | 188 activating_version->SetStatus(ServiceWorkerVersion::ACTIVATING); |
| 187 | 189 |
| 188 // TODO(nhiroki): "8. Fire a simple event named controllerchange..." | 190 // TODO(nhiroki): "8. Fire a simple event named controllerchange..." |
| 189 | 191 |
| 190 // "9. Queue a task to fire an event named activate..." | 192 // "9. Queue a task to fire an event named activate..." |
| 191 activating_version->DispatchActivateEvent( | 193 activating_version->DispatchActivateEvent( |
| 192 base::Bind(&ServiceWorkerRegistration::OnActivateEventFinished, | 194 base::Bind(&ServiceWorkerRegistration::OnActivateEventFinished, |
| 193 this, activating_version)); | 195 this, activating_version)); |
| 194 } | 196 } |
| 195 | 197 |
| 196 void ServiceWorkerRegistration::OnActivateEventFinished( | 198 void ServiceWorkerRegistration::OnActivateEventFinished( |
| 197 ServiceWorkerVersion* activating_version, | 199 ServiceWorkerVersion* activating_version, |
| 198 ServiceWorkerStatusCode status) { | 200 ServiceWorkerStatusCode status) { |
| 199 if (!context_ || activating_version != active_version()) | 201 if (!context_ || activating_version != active_version()) |
| 200 return; | 202 return; |
| 201 // TODO(kinuko,falken): For some error cases (e.g. ServiceWorker is | 203 // TODO(kinuko,falken): For some error cases (e.g. ServiceWorker is |
| 202 // unexpectedly terminated) we may want to retry sending the event again. | 204 // unexpectedly terminated) we may want to retry sending the event again. |
| 203 if (status != SERVICE_WORKER_OK) { | 205 if (status != SERVICE_WORKER_OK) { |
| 204 // "11. If activateFailed is true, then:..." | 206 // "11. If activateFailed is true, then:..." |
| 205 ServiceWorkerRegisterJob::DisassociateVersionFromDocuments( | |
| 206 context_, activating_version); | |
| 207 UnsetVersion(activating_version); | 207 UnsetVersion(activating_version); |
| 208 activating_version->Doom(); | 208 activating_version->Doom(); |
| 209 if (!waiting_version()) { | 209 if (!waiting_version()) { |
| 210 // Delete the records from the db. | 210 // Delete the records from the db. |
| 211 context_->storage()->DeleteRegistration( | 211 context_->storage()->DeleteRegistration( |
| 212 id(), script_url().GetOrigin(), | 212 id(), script_url().GetOrigin(), |
| 213 base::Bind(&ServiceWorkerRegistration::OnDeleteFinished, this)); | 213 base::Bind(&ServiceWorkerRegistration::OnDeleteFinished, this)); |
| 214 // But not from memory if there is a version in the pipeline. | 214 // But not from memory if there is a version in the pipeline. |
| 215 if (installing_version()) | 215 if (installing_version()) |
| 216 is_deleted_ = false; | 216 is_deleted_ = false; |
|
falken
2014/08/06 16:09:35
As here, it's possible to flip back out of is_dele
michaeln
2014/08/07 01:34:35
agreed that its not deletion that matters so much
nhiroki
2014/08/07 02:57:27
Acknowledged.
| |
| 217 } | 217 } |
| 218 return; | 218 return; |
| 219 } | 219 } |
| 220 | 220 |
| 221 // "12. Run the [[UpdateState]] algorithm passing registration.activeWorker | 221 // "12. Run the [[UpdateState]] algorithm passing registration.activeWorker |
| 222 // and "activated" as the arguments." | 222 // and "activated" as the arguments." |
| 223 activating_version->SetStatus(ServiceWorkerVersion::ACTIVATED); | 223 activating_version->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 224 if (context_) { | 224 if (context_) { |
| 225 context_->storage()->UpdateToActiveState( | 225 context_->storage()->UpdateToActiveState( |
| 226 this, | 226 this, |
| 227 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 227 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 228 } | 228 } |
| 229 } | 229 } |
| 230 | 230 |
| 231 void ServiceWorkerRegistration::ResetShouldActivateWhenReady() { | 231 void ServiceWorkerRegistration::ResetShouldActivateWhenReady() { |
| 232 if (should_activate_when_ready_) { | 232 if (should_activate_when_ready_) { |
| 233 active_version()->RemoveListener(this); | 233 active_version()->RemoveListener(this); |
| 234 should_activate_when_ready_ = false; | 234 should_activate_when_ready_ = false; |
| 235 } | 235 } |
| 236 } | 236 } |
| 237 | 237 |
| 238 void ServiceWorkerRegistration::OnDeleteFinished( | 238 void ServiceWorkerRegistration::OnDeleteFinished( |
| 239 ServiceWorkerStatusCode status) { | 239 ServiceWorkerStatusCode status) { |
| 240 // Intentionally empty completion callback, used to prevent | 240 // Intentionally empty completion callback, used to prevent |
| 241 // |this| from being deleted until the storage method completes. | 241 // |this| from being deleted until the storage method completes. |
| 242 } | 242 } |
| 243 | 243 |
| 244 } // namespace content | 244 } // namespace content |
| OLD | NEW |