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_register_job.h" | 5 #include "content/browser/service_worker/service_worker_register_job.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/browser/service_worker/service_worker_context_core.h" | 10 #include "content/browser/service_worker/service_worker_context_core.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 is_promise_resolved_(false), | 37 is_promise_resolved_(false), |
| 38 promise_resolved_status_(SERVICE_WORKER_OK), | 38 promise_resolved_status_(SERVICE_WORKER_OK), |
| 39 weak_factory_(this) {} | 39 weak_factory_(this) {} |
| 40 | 40 |
| 41 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( | 41 ServiceWorkerRegisterJob::ServiceWorkerRegisterJob( |
| 42 base::WeakPtr<ServiceWorkerContextCore> context, | 42 base::WeakPtr<ServiceWorkerContextCore> context, |
| 43 ServiceWorkerRegistration* registration) | 43 ServiceWorkerRegistration* registration) |
| 44 : context_(context), | 44 : context_(context), |
| 45 job_type_(UPDATE_JOB), | 45 job_type_(UPDATE_JOB), |
| 46 pattern_(registration->pattern()), | 46 pattern_(registration->pattern()), |
| 47 script_url_(registration->script_url()), | |
|
michaeln
2014/08/25 22:00:58
Can this data member can remain const and be relia
falken
2014/08/26 01:02:27
Great catch. But we must handle this scenario:
1.
| |
| 48 phase_(INITIAL), | 47 phase_(INITIAL), |
| 49 is_promise_resolved_(false), | 48 is_promise_resolved_(false), |
| 50 promise_resolved_status_(SERVICE_WORKER_OK), | 49 promise_resolved_status_(SERVICE_WORKER_OK), |
| 51 weak_factory_(this) { | 50 weak_factory_(this) { |
| 51 DCHECK(registration); | |
| 52 internal_.registration = registration; | 52 internal_.registration = registration; |
| 53 } | 53 } |
| 54 | 54 |
| 55 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() { | 55 ServiceWorkerRegisterJob::~ServiceWorkerRegisterJob() { |
| 56 DCHECK(!context_ || | 56 DCHECK(!context_ || |
| 57 phase_ == INITIAL || phase_ == COMPLETE || phase_ == ABORT) | 57 phase_ == INITIAL || phase_ == COMPLETE || phase_ == ABORT) |
| 58 << "Jobs should only be interrupted during shutdown."; | 58 << "Jobs should only be interrupted during shutdown."; |
| 59 } | 59 } |
| 60 | 60 |
| 61 void ServiceWorkerRegisterJob::AddCallback(const RegistrationCallback& callback, | 61 void ServiceWorkerRegisterJob::AddCallback(const RegistrationCallback& callback, |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 | 183 |
| 184 if (!existing_registration) { | 184 if (!existing_registration) { |
| 185 RegisterAndContinue(SERVICE_WORKER_OK); | 185 RegisterAndContinue(SERVICE_WORKER_OK); |
| 186 return; | 186 return; |
| 187 } | 187 } |
| 188 | 188 |
| 189 // "Set registration.[[Uninstalling]] to false." | 189 // "Set registration.[[Uninstalling]] to false." |
| 190 existing_registration->AbortPendingClear(); | 190 existing_registration->AbortPendingClear(); |
| 191 | 191 |
| 192 // "If scriptURL is equal to registration.[[ScriptURL]], then:" | 192 // "If scriptURL is equal to registration.[[ScriptURL]], then:" |
| 193 if (existing_registration->script_url() == script_url_) { | 193 if (existing_registration->GetNewestVersion()->script_url() == script_url_) { |
| 194 // Spec says to resolve with registration.[[GetNewestWorker]]. We come close | 194 // Spec says to resolve with registration.[[GetNewestWorker]]. We come close |
| 195 // by resolving with the active version. | 195 // by resolving with the active version. |
| 196 set_registration(existing_registration); | 196 set_registration(existing_registration); |
| 197 | 197 |
| 198 if (!existing_registration->active_version()) { | 198 if (!existing_registration->active_version()) { |
| 199 UpdateAndContinue(); | 199 UpdateAndContinue(); |
| 200 return; | 200 return; |
| 201 } | 201 } |
| 202 | 202 |
| 203 ResolvePromise( | 203 ResolvePromise( |
| 204 status, existing_registration, existing_registration->active_version()); | 204 status, existing_registration, existing_registration->active_version()); |
| 205 Complete(SERVICE_WORKER_OK); | 205 Complete(SERVICE_WORKER_OK); |
| 206 return; | 206 return; |
| 207 } | 207 } |
| 208 | 208 |
| 209 // "Set registration.[[ScriptURL]] to scriptURL." We accomplish this by | 209 // "Set registration.[[ScriptURL]] to scriptURL." We accomplish this by |
| 210 // deleting the existing registration and registering a new one. | 210 // deleting the existing registration and registering a new one. |
| 211 // TODO(michaeln): Deactivate the live existing_registration object and | 211 // TODO(michaeln): Deactivate the live existing_registration object and |
| 212 // eventually call storage->DeleteVersionResources() when it no longer has any | 212 // eventually call storage->DeleteVersionResources() when it no longer has any |
| 213 // controllees. | 213 // controllees. |
| 214 context_->storage()->DeleteRegistration( | 214 context_->storage()->DeleteRegistration( |
| 215 existing_registration->id(), | 215 existing_registration->id(), |
| 216 existing_registration->script_url().GetOrigin(), | 216 existing_registration->pattern().GetOrigin(), |
| 217 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, | 217 base::Bind(&ServiceWorkerRegisterJob::RegisterAndContinue, |
| 218 weak_factory_.GetWeakPtr())); | 218 weak_factory_.GetWeakPtr())); |
| 219 } | 219 } |
| 220 | 220 |
| 221 void ServiceWorkerRegisterJob::ContinueWithUpdate( | 221 void ServiceWorkerRegisterJob::ContinueWithUpdate( |
| 222 ServiceWorkerStatusCode status, | 222 ServiceWorkerStatusCode status, |
| 223 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { | 223 const scoped_refptr<ServiceWorkerRegistration>& existing_registration) { |
| 224 DCHECK_EQ(UPDATE_JOB, job_type_); | 224 DCHECK_EQ(UPDATE_JOB, job_type_); |
| 225 if (status != SERVICE_WORKER_OK) { | 225 if (status != SERVICE_WORKER_OK) { |
| 226 Complete(status); | 226 Complete(status); |
| 227 return; | 227 return; |
| 228 } | 228 } |
| 229 | 229 |
| 230 if (existing_registration != registration()) { | 230 if (existing_registration != registration()) { |
| 231 Complete(SERVICE_WORKER_ERROR_NOT_FOUND); | 231 Complete(SERVICE_WORKER_ERROR_NOT_FOUND); |
| 232 return; | 232 return; |
| 233 } | 233 } |
| 234 | 234 |
| 235 // A previous job may have unregistered this registration. | |
| 236 if (registration()->is_uninstalling()) { | |
| 237 Complete(SERVICE_WORKER_ERROR_NOT_FOUND); | |
| 238 return; | |
| 239 } | |
| 240 | |
| 241 script_url_ = registration()->GetNewestVersion()->script_url(); | |
| 242 | |
| 235 // TODO(michaeln): If the last update check was less than 24 hours | 243 // TODO(michaeln): If the last update check was less than 24 hours |
| 236 // ago, depending on the freshness of the cached worker script we | 244 // ago, depending on the freshness of the cached worker script we |
| 237 // may be able to complete the update job right here. | 245 // may be able to complete the update job right here. |
| 238 | 246 |
| 239 UpdateAndContinue(); | 247 UpdateAndContinue(); |
| 240 } | 248 } |
| 241 | 249 |
| 242 // Creates a new ServiceWorkerRegistration. | 250 // Creates a new ServiceWorkerRegistration. |
| 243 void ServiceWorkerRegisterJob::RegisterAndContinue( | 251 void ServiceWorkerRegisterJob::RegisterAndContinue( |
| 244 ServiceWorkerStatusCode status) { | 252 ServiceWorkerStatusCode status) { |
| 245 SetPhase(REGISTER); | 253 SetPhase(REGISTER); |
| 246 if (status != SERVICE_WORKER_OK) { | 254 if (status != SERVICE_WORKER_OK) { |
| 247 // Abort this registration job. | 255 // Abort this registration job. |
| 248 Complete(status); | 256 Complete(status); |
| 249 return; | 257 return; |
| 250 } | 258 } |
| 251 | 259 |
| 252 set_registration(new ServiceWorkerRegistration( | 260 set_registration(new ServiceWorkerRegistration( |
| 253 pattern_, script_url_, context_->storage()->NewRegistrationId(), | 261 pattern_, context_->storage()->NewRegistrationId(), context_)); |
| 254 context_)); | |
| 255 AssociateProviderHostsToRegistration(registration()); | 262 AssociateProviderHostsToRegistration(registration()); |
| 256 UpdateAndContinue(); | 263 UpdateAndContinue(); |
| 257 } | 264 } |
| 258 | 265 |
| 259 // This function corresponds to the spec's [[Update]] algorithm. | 266 // This function corresponds to the spec's [[Update]] algorithm. |
| 260 void ServiceWorkerRegisterJob::UpdateAndContinue() { | 267 void ServiceWorkerRegisterJob::UpdateAndContinue() { |
| 261 SetPhase(UPDATE); | 268 SetPhase(UPDATE); |
| 262 context_->storage()->NotifyInstallingRegistration(registration()); | 269 context_->storage()->NotifyInstallingRegistration(registration()); |
| 263 | 270 |
| 264 // TODO(falken): "If serviceWorkerRegistration.installingWorker is not null.." | 271 // TODO(falken): "If serviceWorkerRegistration.installingWorker is not null.." |
| 265 // then terminate the installing worker. It doesn't make sense to implement | 272 // then terminate the installing worker. It doesn't make sense to implement |
| 266 // yet since we always activate the worker if install completed, so there can | 273 // yet since we always activate the worker if install completed, so there can |
| 267 // be no installing worker at this point. | 274 // be no installing worker at this point. |
| 268 | 275 |
| 269 // "Let serviceWorker be a newly-created ServiceWorker object..." and start | 276 // "Let serviceWorker be a newly-created ServiceWorker object..." and start |
| 270 // the worker. | 277 // the worker. |
| 271 set_new_version(new ServiceWorkerVersion( | 278 set_new_version(new ServiceWorkerVersion(registration(), |
| 272 registration(), context_->storage()->NewVersionId(), context_)); | 279 script_url_, |
| 280 context_->storage()->NewVersionId(), | |
| 281 context_)); | |
| 273 | 282 |
| 274 bool pause_after_download = job_type_ == UPDATE_JOB; | 283 bool pause_after_download = job_type_ == UPDATE_JOB; |
| 275 if (pause_after_download) | 284 if (pause_after_download) |
| 276 new_version()->embedded_worker()->AddListener(this); | 285 new_version()->embedded_worker()->AddListener(this); |
| 277 new_version()->StartWorkerWithCandidateProcesses( | 286 new_version()->StartWorkerWithCandidateProcesses( |
| 278 pending_process_ids_, | 287 pending_process_ids_, |
| 279 pause_after_download, | 288 pause_after_download, |
| 280 base::Bind(&ServiceWorkerRegisterJob::OnStartWorkerFinished, | 289 base::Bind(&ServiceWorkerRegisterJob::OnStartWorkerFinished, |
| 281 weak_factory_.GetWeakPtr())); | 290 weak_factory_.GetWeakPtr())); |
| 282 } | 291 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 379 if (registration()) { | 388 if (registration()) { |
| 380 if (new_version()) { | 389 if (new_version()) { |
| 381 registration()->UnsetVersion(new_version()); | 390 registration()->UnsetVersion(new_version()); |
| 382 new_version()->Doom(); | 391 new_version()->Doom(); |
| 383 } | 392 } |
| 384 if (!registration()->waiting_version() && | 393 if (!registration()->waiting_version() && |
| 385 !registration()->active_version()) { | 394 !registration()->active_version()) { |
| 386 registration()->NotifyRegistrationFailed(); | 395 registration()->NotifyRegistrationFailed(); |
| 387 context_->storage()->DeleteRegistration( | 396 context_->storage()->DeleteRegistration( |
| 388 registration()->id(), | 397 registration()->id(), |
| 389 registration()->script_url().GetOrigin(), | 398 registration()->pattern().GetOrigin(), |
| 390 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 399 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 391 } | 400 } |
| 392 } | 401 } |
| 393 if (!is_promise_resolved_) | 402 if (!is_promise_resolved_) |
| 394 ResolvePromise(status, NULL, NULL); | 403 ResolvePromise(status, NULL, NULL); |
| 395 } | 404 } |
| 396 DCHECK(callbacks_.empty()); | 405 DCHECK(callbacks_.empty()); |
| 397 if (registration()) { | 406 if (registration()) { |
| 398 context_->storage()->NotifyDoneInstallingRegistration( | 407 context_->storage()->NotifyDoneInstallingRegistration( |
| 399 registration(), new_version(), status); | 408 registration(), new_version(), status); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 477 ServiceWorkerProviderHost* host = it->GetProviderHost(); | 486 ServiceWorkerProviderHost* host = it->GetProviderHost(); |
| 478 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(), | 487 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(), |
| 479 host->document_url())) { | 488 host->document_url())) { |
| 480 if (host->CanAssociateRegistration(registration)) | 489 if (host->CanAssociateRegistration(registration)) |
| 481 host->AssociateRegistration(registration); | 490 host->AssociateRegistration(registration); |
| 482 } | 491 } |
| 483 } | 492 } |
| 484 } | 493 } |
| 485 | 494 |
| 486 } // namespace content | 495 } // namespace content |
| OLD | NEW |