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 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 SetPhase(REGISTER); | 240 SetPhase(REGISTER); |
| 241 if (status != SERVICE_WORKER_OK) { | 241 if (status != SERVICE_WORKER_OK) { |
| 242 // Abort this registration job. | 242 // Abort this registration job. |
| 243 Complete(status); | 243 Complete(status); |
| 244 return; | 244 return; |
| 245 } | 245 } |
| 246 | 246 |
| 247 set_registration(new ServiceWorkerRegistration( | 247 set_registration(new ServiceWorkerRegistration( |
| 248 pattern_, script_url_, context_->storage()->NewRegistrationId(), | 248 pattern_, script_url_, context_->storage()->NewRegistrationId(), |
| 249 context_)); | 249 context_)); |
| 250 AssociateProviderHostToRegistration(registration()); | |
| 250 UpdateAndContinue(); | 251 UpdateAndContinue(); |
| 251 } | 252 } |
| 252 | 253 |
| 253 // This function corresponds to the spec's [[Update]] algorithm. | 254 // This function corresponds to the spec's [[Update]] algorithm. |
| 254 void ServiceWorkerRegisterJob::UpdateAndContinue() { | 255 void ServiceWorkerRegisterJob::UpdateAndContinue() { |
| 255 SetPhase(UPDATE); | 256 SetPhase(UPDATE); |
| 256 context_->storage()->NotifyInstallingRegistration(registration()); | 257 context_->storage()->NotifyInstallingRegistration(registration()); |
| 257 | 258 |
| 258 // TODO(falken): "If serviceWorkerRegistration.installingWorker is not null.." | 259 // TODO(falken): "If serviceWorkerRegistration.installingWorker is not null.." |
| 259 // then terminate the installing worker. It doesn't make sense to implement | 260 // then terminate the installing worker. It doesn't make sense to implement |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 289 ResolvePromise(status, registration(), new_version()); | 290 ResolvePromise(status, registration(), new_version()); |
| 290 InstallAndContinue(); | 291 InstallAndContinue(); |
| 291 } | 292 } |
| 292 | 293 |
| 293 // This function corresponds to the spec's _Install algorithm. | 294 // This function corresponds to the spec's _Install algorithm. |
| 294 void ServiceWorkerRegisterJob::InstallAndContinue() { | 295 void ServiceWorkerRegisterJob::InstallAndContinue() { |
| 295 SetPhase(INSTALL); | 296 SetPhase(INSTALL); |
| 296 | 297 |
| 297 // "3. Set registration.installingWorker to worker." | 298 // "3. Set registration.installingWorker to worker." |
| 298 registration()->SetInstallingVersion(new_version()); | 299 registration()->SetInstallingVersion(new_version()); |
| 299 AssociateInstallingVersionToDocuments(context_, new_version()); | |
| 300 | 300 |
| 301 // "4. Run the [[UpdateState]] algorithm passing registration.installingWorker | 301 // "4. Run the [[UpdateState]] algorithm passing registration.installingWorker |
| 302 // and "installing" as the arguments." | 302 // and "installing" as the arguments." |
| 303 new_version()->SetStatus(ServiceWorkerVersion::INSTALLING); | 303 new_version()->SetStatus(ServiceWorkerVersion::INSTALLING); |
| 304 | 304 |
| 305 // TODO(nhiroki,michaeln): "5. Fire a simple event named updatefound..." | 305 // TODO(nhiroki,michaeln): "5. Fire a simple event named updatefound..." |
| 306 | 306 |
| 307 // "6. Fire an event named install..." | 307 // "6. Fire an event named install..." |
| 308 new_version()->DispatchInstallEvent( | 308 new_version()->DispatchInstallEvent( |
| 309 -1, | 309 -1, |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 339 // "9. If registration.waitingWorker is not null, then:..." | 339 // "9. If registration.waitingWorker is not null, then:..." |
| 340 if (registration()->waiting_version()) { | 340 if (registration()->waiting_version()) { |
| 341 // "1. Run the [[UpdateState]] algorithm passing registration.waitingWorker | 341 // "1. Run the [[UpdateState]] algorithm passing registration.waitingWorker |
| 342 // and "redundant" as the arguments." | 342 // and "redundant" as the arguments." |
| 343 registration()->waiting_version()->SetStatus( | 343 registration()->waiting_version()->SetStatus( |
| 344 ServiceWorkerVersion::REDUNDANT); | 344 ServiceWorkerVersion::REDUNDANT); |
| 345 } | 345 } |
| 346 | 346 |
| 347 // "10. Set registration.waitingWorker to registration.installingWorker." | 347 // "10. Set registration.waitingWorker to registration.installingWorker." |
| 348 // "11. Set registration.installingWorker to null." | 348 // "11. Set registration.installingWorker to null." |
| 349 DisassociateVersionFromDocuments(context_, new_version()); | |
| 350 registration()->SetWaitingVersion(new_version()); | 349 registration()->SetWaitingVersion(new_version()); |
| 351 AssociateWaitingVersionToDocuments(context_, new_version()); | |
| 352 | 350 |
| 353 // "12. Run the [[UpdateState]] algorithm passing registration.waitingWorker | 351 // "12. Run the [[UpdateState]] algorithm passing registration.waitingWorker |
| 354 // and "installed" as the arguments." | 352 // and "installed" as the arguments." |
| 355 new_version()->SetStatus(ServiceWorkerVersion::INSTALLED); | 353 new_version()->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 356 | 354 |
| 357 // TODO(michaeln): "13. If activateImmediate is true, then..." | 355 // TODO(michaeln): "13. If activateImmediate is true, then..." |
| 358 | 356 |
| 359 // "14. Wait until no document is using registration as their | 357 // "14. Wait until no document is using registration as their |
| 360 // Service Worker registration." | 358 // Service Worker registration." |
| 361 registration()->ActivateWaitingVersionWhenReady(); | 359 registration()->ActivateWaitingVersionWhenReady(); |
| 362 | 360 |
| 363 Complete(SERVICE_WORKER_OK); | 361 Complete(SERVICE_WORKER_OK); |
| 364 } | 362 } |
| 365 | 363 |
| 366 void ServiceWorkerRegisterJob::Complete(ServiceWorkerStatusCode status) { | 364 void ServiceWorkerRegisterJob::Complete(ServiceWorkerStatusCode status) { |
| 367 CompleteInternal(status); | 365 CompleteInternal(status); |
| 368 context_->job_coordinator()->FinishJob(pattern_, this); | 366 context_->job_coordinator()->FinishJob(pattern_, this); |
| 369 } | 367 } |
| 370 | 368 |
| 371 void ServiceWorkerRegisterJob::CompleteInternal( | 369 void ServiceWorkerRegisterJob::CompleteInternal( |
| 372 ServiceWorkerStatusCode status) { | 370 ServiceWorkerStatusCode status) { |
| 373 SetPhase(COMPLETE); | 371 SetPhase(COMPLETE); |
| 374 if (status != SERVICE_WORKER_OK) { | 372 if (status != SERVICE_WORKER_OK) { |
| 375 if (registration()) { | 373 if (registration()) { |
| 376 if (new_version()) { | 374 if (new_version()) { |
| 377 DisassociateVersionFromDocuments(context_, new_version()); | |
| 378 registration()->UnsetVersion(new_version()); | 375 registration()->UnsetVersion(new_version()); |
| 379 new_version()->Doom(); | 376 new_version()->Doom(); |
| 380 } | 377 } |
| 381 if (!registration()->active_version()) { | 378 if (!registration()->active_version()) { |
|
michaeln
2014/08/07 01:34:35
and to call UnassociateFailedRegistration() here
nhiroki
2014/08/07 02:57:27
Done.
| |
| 382 context_->storage()->DeleteRegistration( | 379 context_->storage()->DeleteRegistration( |
| 383 registration()->id(), | 380 registration()->id(), |
| 384 registration()->script_url().GetOrigin(), | 381 registration()->script_url().GetOrigin(), |
| 385 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 382 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 386 } | 383 } |
| 387 } | 384 } |
| 388 if (!is_promise_resolved_) | 385 if (!is_promise_resolved_) |
| 389 ResolvePromise(status, NULL, NULL); | 386 ResolvePromise(status, NULL, NULL); |
| 390 } | 387 } |
| 391 DCHECK(callbacks_.empty()); | 388 DCHECK(callbacks_.empty()); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 446 ResolvePromise(SERVICE_WORKER_OK, registration(), current_version); | 443 ResolvePromise(SERVICE_WORKER_OK, registration(), current_version); |
| 447 Complete(SERVICE_WORKER_ERROR_EXISTS); | 444 Complete(SERVICE_WORKER_ERROR_EXISTS); |
| 448 return; | 445 return; |
| 449 } | 446 } |
| 450 | 447 |
| 451 // Proceed with really starting the worker. | 448 // Proceed with really starting the worker. |
| 452 new_version()->embedded_worker()->ResumeAfterDownload(); | 449 new_version()->embedded_worker()->ResumeAfterDownload(); |
| 453 new_version()->embedded_worker()->RemoveListener(this); | 450 new_version()->embedded_worker()->RemoveListener(this); |
| 454 } | 451 } |
| 455 | 452 |
| 456 // static | 453 void ServiceWorkerRegisterJob::AssociateProviderHostToRegistration( |
|
falken
2014/08/06 16:09:35
nit: it's multiple hosts, so s/ProviderHost/Provid
nhiroki
2014/08/07 02:57:27
Done.
| |
| 457 void ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | 454 ServiceWorkerRegistration* registration) { |
| 458 base::WeakPtr<ServiceWorkerContextCore> context, | 455 DCHECK(registration); |
| 459 ServiceWorkerVersion* version) { | |
| 460 DCHECK(context); | |
| 461 DCHECK(version); | |
| 462 | |
| 463 for (scoped_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = | 456 for (scoped_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = |
| 464 context->GetProviderHostIterator(); | 457 context_->GetProviderHostIterator(); |
| 465 !it->IsAtEnd(); it->Advance()) { | 458 !it->IsAtEnd(); it->Advance()) { |
| 466 ServiceWorkerProviderHost* host = it->GetProviderHost(); | 459 ServiceWorkerProviderHost* host = it->GetProviderHost(); |
| 467 if (ServiceWorkerUtils::ScopeMatches(version->scope(), | 460 if (ServiceWorkerUtils::ScopeMatches(registration->pattern(), |
| 468 host->document_url())) { | 461 host->document_url())) { |
| 469 if (!host->CanAssociateVersion(version)) | 462 if (host->CanAssociateRegistration(registration)) |
| 470 continue; | 463 host->SetRegistration(registration); |
| 471 host->SetInstallingVersion(version); | |
| 472 } | 464 } |
| 473 } | 465 } |
| 474 } | 466 } |
| 475 | 467 |
| 476 // static | |
| 477 void ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments( | |
| 478 base::WeakPtr<ServiceWorkerContextCore> context, | |
| 479 ServiceWorkerVersion* version) { | |
| 480 DCHECK(context); | |
| 481 DCHECK(version); | |
| 482 | |
| 483 for (scoped_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = | |
| 484 context->GetProviderHostIterator(); | |
| 485 !it->IsAtEnd(); it->Advance()) { | |
| 486 ServiceWorkerProviderHost* host = it->GetProviderHost(); | |
| 487 if (ServiceWorkerUtils::ScopeMatches(version->scope(), | |
| 488 host->document_url())) { | |
| 489 if (!host->CanAssociateVersion(version)) | |
| 490 continue; | |
| 491 host->SetWaitingVersion(version); | |
| 492 } | |
| 493 } | |
| 494 } | |
| 495 | |
| 496 // static | |
| 497 void ServiceWorkerRegisterJob::AssociateActiveVersionToDocuments( | |
| 498 base::WeakPtr<ServiceWorkerContextCore> context, | |
| 499 ServiceWorkerVersion* version) { | |
| 500 DCHECK(context); | |
| 501 DCHECK(version); | |
| 502 | |
| 503 for (scoped_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = | |
| 504 context->GetProviderHostIterator(); | |
| 505 !it->IsAtEnd(); it->Advance()) { | |
| 506 ServiceWorkerProviderHost* host = it->GetProviderHost(); | |
| 507 if (ServiceWorkerUtils::ScopeMatches(version->scope(), | |
| 508 host->document_url())) { | |
| 509 if (!host->CanAssociateVersion(version)) | |
| 510 continue; | |
| 511 host->SetActiveVersion(version); | |
| 512 } | |
| 513 } | |
| 514 } | |
| 515 | |
| 516 // static | |
| 517 void ServiceWorkerRegisterJob::DisassociateVersionFromDocuments( | |
| 518 base::WeakPtr<ServiceWorkerContextCore> context, | |
| 519 ServiceWorkerVersion* version) { | |
| 520 DCHECK(context); | |
| 521 for (scoped_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = | |
| 522 context->GetProviderHostIterator(); | |
| 523 !it->IsAtEnd(); it->Advance()) { | |
| 524 ServiceWorkerProviderHost* host = it->GetProviderHost(); | |
| 525 host->UnsetVersion(version); | |
| 526 } | |
| 527 } | |
| 528 | |
| 529 } // namespace content | 468 } // namespace content |
| OLD | NEW |