| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 void ServiceWorkerRegistration::NotifyRegistrationFailed() { | 59 void ServiceWorkerRegistration::NotifyRegistrationFailed() { |
| 60 FOR_EACH_OBSERVER(Listener, listeners_, OnRegistrationFailed(this)); | 60 FOR_EACH_OBSERVER(Listener, listeners_, OnRegistrationFailed(this)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 ServiceWorkerRegistrationInfo ServiceWorkerRegistration::GetInfo() { | 63 ServiceWorkerRegistrationInfo ServiceWorkerRegistration::GetInfo() { |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 65 return ServiceWorkerRegistrationInfo( | 65 return ServiceWorkerRegistrationInfo( |
| 66 script_url(), | 66 script_url(), |
| 67 pattern(), | 67 pattern(), |
| 68 registration_id_, | 68 registration_id_, |
| 69 GetVersionInfo(active_version_), | 69 GetVersionInfo(active_version_.get()), |
| 70 GetVersionInfo(waiting_version_), | 70 GetVersionInfo(waiting_version_.get()), |
| 71 GetVersionInfo(installing_version_)); | 71 GetVersionInfo(installing_version_.get())); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void ServiceWorkerRegistration::SetActiveVersion( | 74 void ServiceWorkerRegistration::SetActiveVersion( |
| 75 ServiceWorkerVersion* version) { | 75 ServiceWorkerVersion* version) { |
| 76 should_activate_when_ready_ = false; | 76 should_activate_when_ready_ = false; |
| 77 SetVersionInternal(version, &active_version_, | 77 SetVersionInternal(version, &active_version_, |
| 78 ChangedVersionAttributesMask::ACTIVE_VERSION); | 78 ChangedVersionAttributesMask::ACTIVE_VERSION); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void ServiceWorkerRegistration::SetWaitingVersion( | 81 void ServiceWorkerRegistration::SetWaitingVersion( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 107 ServiceWorkerVersion* version, | 107 ServiceWorkerVersion* version, |
| 108 scoped_refptr<ServiceWorkerVersion>* data_member, | 108 scoped_refptr<ServiceWorkerVersion>* data_member, |
| 109 int change_flag) { | 109 int change_flag) { |
| 110 if (version == data_member->get()) | 110 if (version == data_member->get()) |
| 111 return; | 111 return; |
| 112 scoped_refptr<ServiceWorkerVersion> protect(version); | 112 scoped_refptr<ServiceWorkerVersion> protect(version); |
| 113 ChangedVersionAttributesMask mask; | 113 ChangedVersionAttributesMask mask; |
| 114 if (version) | 114 if (version) |
| 115 UnsetVersionInternal(version, &mask); | 115 UnsetVersionInternal(version, &mask); |
| 116 *data_member = version; | 116 *data_member = version; |
| 117 if (active_version_ && active_version_ == version) | 117 if (active_version_.get() && active_version_.get() == version) |
| 118 active_version_->AddListener(this); | 118 active_version_->AddListener(this); |
| 119 mask.add(change_flag); | 119 mask.add(change_flag); |
| 120 ServiceWorkerRegistrationInfo info = GetInfo(); | 120 ServiceWorkerRegistrationInfo info = GetInfo(); |
| 121 FOR_EACH_OBSERVER(Listener, listeners_, | 121 FOR_EACH_OBSERVER(Listener, listeners_, |
| 122 OnVersionAttributesChanged(this, mask, info)); | 122 OnVersionAttributesChanged(this, mask, info)); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void ServiceWorkerRegistration::UnsetVersionInternal( | 125 void ServiceWorkerRegistration::UnsetVersionInternal( |
| 126 ServiceWorkerVersion* version, | 126 ServiceWorkerVersion* version, |
| 127 ChangedVersionAttributesMask* mask) { | 127 ChangedVersionAttributesMask* mask) { |
| 128 DCHECK(version); | 128 DCHECK(version); |
| 129 if (installing_version_ == version) { | 129 if (installing_version_.get() == version) { |
| 130 installing_version_ = NULL; | 130 installing_version_ = NULL; |
| 131 mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION); | 131 mask->add(ChangedVersionAttributesMask::INSTALLING_VERSION); |
| 132 } else if (waiting_version_ == version) { | 132 } else if (waiting_version_.get() == version) { |
| 133 waiting_version_ = NULL; | 133 waiting_version_ = NULL; |
| 134 mask->add(ChangedVersionAttributesMask::WAITING_VERSION); | 134 mask->add(ChangedVersionAttributesMask::WAITING_VERSION); |
| 135 } else if (active_version_ == version) { | 135 } else if (active_version_.get() == version) { |
| 136 active_version_->RemoveListener(this); | 136 active_version_->RemoveListener(this); |
| 137 active_version_ = NULL; | 137 active_version_ = NULL; |
| 138 mask->add(ChangedVersionAttributesMask::ACTIVE_VERSION); | 138 mask->add(ChangedVersionAttributesMask::ACTIVE_VERSION); |
| 139 } | 139 } |
| 140 } | 140 } |
| 141 | 141 |
| 142 void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() { | 142 void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() { |
| 143 DCHECK(waiting_version()); | 143 DCHECK(waiting_version()); |
| 144 should_activate_when_ready_ = true; | 144 should_activate_when_ready_ = true; |
| 145 if (!active_version() || !active_version()->HasControllee()) | 145 if (!active_version() || !active_version()->HasControllee()) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 164 | 164 |
| 165 void ServiceWorkerRegistration::AbortPendingClear() { | 165 void ServiceWorkerRegistration::AbortPendingClear() { |
| 166 DCHECK(context_); | 166 DCHECK(context_); |
| 167 if (!is_uninstalling()) | 167 if (!is_uninstalling()) |
| 168 return; | 168 return; |
| 169 is_uninstalling_ = false; | 169 is_uninstalling_ = false; |
| 170 context_->storage()->NotifyDoneUninstallingRegistration(this); | 170 context_->storage()->NotifyDoneUninstallingRegistration(this); |
| 171 | 171 |
| 172 scoped_refptr<ServiceWorkerVersion> most_recent_version = | 172 scoped_refptr<ServiceWorkerVersion> most_recent_version = |
| 173 waiting_version() ? waiting_version() : active_version(); | 173 waiting_version() ? waiting_version() : active_version(); |
| 174 DCHECK(most_recent_version); | 174 DCHECK(most_recent_version.get()); |
| 175 context_->storage()->NotifyInstallingRegistration(this); | 175 context_->storage()->NotifyInstallingRegistration(this); |
| 176 context_->storage()->StoreRegistration( | 176 context_->storage()->StoreRegistration( |
| 177 this, | 177 this, |
| 178 most_recent_version, | 178 most_recent_version.get(), |
| 179 base::Bind(&ServiceWorkerRegistration::OnStoreFinished, | 179 base::Bind(&ServiceWorkerRegistration::OnStoreFinished, |
| 180 this, | 180 this, |
| 181 most_recent_version)); | 181 most_recent_version)); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) { | 184 void ServiceWorkerRegistration::OnNoControllees(ServiceWorkerVersion* version) { |
| 185 DCHECK_EQ(active_version(), version); | 185 DCHECK_EQ(active_version(), version); |
| 186 if (is_uninstalling_) | 186 if (is_uninstalling_) |
| 187 Clear(); | 187 Clear(); |
| 188 else if (should_activate_when_ready_) | 188 else if (should_activate_when_ready_) |
| 189 ActivateWaitingVersion(); | 189 ActivateWaitingVersion(); |
| 190 is_uninstalling_ = false; | 190 is_uninstalling_ = false; |
| 191 should_activate_when_ready_ = false; | 191 should_activate_when_ready_ = false; |
| 192 } | 192 } |
| 193 | 193 |
| 194 void ServiceWorkerRegistration::ActivateWaitingVersion() { | 194 void ServiceWorkerRegistration::ActivateWaitingVersion() { |
| 195 DCHECK(context_); | 195 DCHECK(context_); |
| 196 DCHECK(waiting_version()); | 196 DCHECK(waiting_version()); |
| 197 DCHECK(should_activate_when_ready_); | 197 DCHECK(should_activate_when_ready_); |
| 198 should_activate_when_ready_ = false; | 198 should_activate_when_ready_ = false; |
| 199 scoped_refptr<ServiceWorkerVersion> activating_version = waiting_version(); | 199 scoped_refptr<ServiceWorkerVersion> activating_version = waiting_version(); |
| 200 scoped_refptr<ServiceWorkerVersion> exiting_version = active_version(); | 200 scoped_refptr<ServiceWorkerVersion> exiting_version = active_version(); |
| 201 | 201 |
| 202 if (activating_version->is_doomed() || | 202 if (activating_version->is_doomed() || |
| 203 activating_version->status() == ServiceWorkerVersion::REDUNDANT) { | 203 activating_version->status() == ServiceWorkerVersion::REDUNDANT) { |
| 204 return; // Activation is no longer relevant. | 204 return; // Activation is no longer relevant. |
| 205 } | 205 } |
| 206 | 206 |
| 207 // "4. If exitingWorker is not null, | 207 // "4. If exitingWorker is not null, |
| 208 if (exiting_version) { | 208 if (exiting_version.get()) { |
| 209 DCHECK(!exiting_version->HasControllee()); | 209 DCHECK(!exiting_version->HasControllee()); |
| 210 // TODO(michaeln): should wait for events to be complete | 210 // TODO(michaeln): should wait for events to be complete |
| 211 // "1. Wait for exitingWorker to finish handling any in-progress requests." | 211 // "1. Wait for exitingWorker to finish handling any in-progress requests." |
| 212 // "2. Terminate exitingWorker." | 212 // "2. Terminate exitingWorker." |
| 213 exiting_version->StopWorker( | 213 exiting_version->StopWorker( |
| 214 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 214 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 215 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and | 215 // "3. Run the [[UpdateState]] algorithm passing exitingWorker and |
| 216 // "redundant" as the arguments." | 216 // "redundant" as the arguments." |
| 217 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT); | 217 exiting_version->SetStatus(ServiceWorkerVersion::REDUNDANT); |
| 218 } | 218 } |
| 219 | 219 |
| 220 // "5. Set serviceWorkerRegistration.activeWorker to activatingWorker." | 220 // "5. Set serviceWorkerRegistration.activeWorker to activatingWorker." |
| 221 // "6. Set serviceWorkerRegistration.waitingWorker to null." | 221 // "6. Set serviceWorkerRegistration.waitingWorker to null." |
| 222 SetActiveVersion(activating_version); | 222 SetActiveVersion(activating_version.get()); |
| 223 | 223 |
| 224 // "7. Run the [[UpdateState]] algorithm passing registration.activeWorker and | 224 // "7. Run the [[UpdateState]] algorithm passing registration.activeWorker and |
| 225 // "activating" as arguments." | 225 // "activating" as arguments." |
| 226 activating_version->SetStatus(ServiceWorkerVersion::ACTIVATING); | 226 activating_version->SetStatus(ServiceWorkerVersion::ACTIVATING); |
| 227 | 227 |
| 228 // TODO(nhiroki): "8. Fire a simple event named controllerchange..." | 228 // TODO(nhiroki): "8. Fire a simple event named controllerchange..." |
| 229 | 229 |
| 230 // "9. Queue a task to fire an event named activate..." | 230 // "9. Queue a task to fire an event named activate..." |
| 231 activating_version->DispatchActivateEvent( | 231 activating_version->DispatchActivateEvent( |
| 232 base::Bind(&ServiceWorkerRegistration::OnActivateEventFinished, | 232 base::Bind(&ServiceWorkerRegistration::OnActivateEventFinished, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 void ServiceWorkerRegistration::OnDeleteFinished( | 269 void ServiceWorkerRegistration::OnDeleteFinished( |
| 270 ServiceWorkerStatusCode status) { | 270 ServiceWorkerStatusCode status) { |
| 271 // Intentionally empty completion callback, used to prevent | 271 // Intentionally empty completion callback, used to prevent |
| 272 // |this| from being deleted until the storage method completes. | 272 // |this| from being deleted until the storage method completes. |
| 273 } | 273 } |
| 274 | 274 |
| 275 void ServiceWorkerRegistration::Clear() { | 275 void ServiceWorkerRegistration::Clear() { |
| 276 context_->storage()->NotifyDoneUninstallingRegistration(this); | 276 context_->storage()->NotifyDoneUninstallingRegistration(this); |
| 277 | 277 |
| 278 ChangedVersionAttributesMask mask; | 278 ChangedVersionAttributesMask mask; |
| 279 if (installing_version_) { | 279 if (installing_version_.get()) { |
| 280 installing_version_->Doom(); | 280 installing_version_->Doom(); |
| 281 installing_version_ = NULL; | 281 installing_version_ = NULL; |
| 282 mask.add(ChangedVersionAttributesMask::INSTALLING_VERSION); | 282 mask.add(ChangedVersionAttributesMask::INSTALLING_VERSION); |
| 283 } | 283 } |
| 284 if (waiting_version_) { | 284 if (waiting_version_.get()) { |
| 285 waiting_version_->Doom(); | 285 waiting_version_->Doom(); |
| 286 waiting_version_ = NULL; | 286 waiting_version_ = NULL; |
| 287 mask.add(ChangedVersionAttributesMask::WAITING_VERSION); | 287 mask.add(ChangedVersionAttributesMask::WAITING_VERSION); |
| 288 } | 288 } |
| 289 if (active_version_) { | 289 if (active_version_.get()) { |
| 290 active_version_->Doom(); | 290 active_version_->Doom(); |
| 291 active_version_->RemoveListener(this); | 291 active_version_->RemoveListener(this); |
| 292 active_version_ = NULL; | 292 active_version_ = NULL; |
| 293 mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION); | 293 mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION); |
| 294 } | 294 } |
| 295 if (mask.changed()) { | 295 if (mask.changed()) { |
| 296 ServiceWorkerRegistrationInfo info = GetInfo(); | 296 ServiceWorkerRegistrationInfo info = GetInfo(); |
| 297 FOR_EACH_OBSERVER(Listener, listeners_, | 297 FOR_EACH_OBSERVER(Listener, listeners_, |
| 298 OnVersionAttributesChanged(this, mask, info)); | 298 OnVersionAttributesChanged(this, mask, info)); |
| 299 } | 299 } |
| 300 } | 300 } |
| 301 | 301 |
| 302 void ServiceWorkerRegistration::OnStoreFinished( | 302 void ServiceWorkerRegistration::OnStoreFinished( |
| 303 scoped_refptr<ServiceWorkerVersion> version, | 303 scoped_refptr<ServiceWorkerVersion> version, |
| 304 ServiceWorkerStatusCode status) { | 304 ServiceWorkerStatusCode status) { |
| 305 if (!context_) | 305 if (!context_) |
| 306 return; | 306 return; |
| 307 context_->storage()->NotifyDoneInstallingRegistration( | 307 context_->storage()->NotifyDoneInstallingRegistration( |
| 308 this, version.get(), status); | 308 this, version.get(), status); |
| 309 } | 309 } |
| 310 | 310 |
| 311 } // namespace content | 311 } // namespace content |
| OLD | NEW |