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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 403 ResolvePromise(SERVICE_WORKER_OK, std::string(), registration()); | 403 ResolvePromise(SERVICE_WORKER_OK, std::string(), registration()); |
| 404 | 404 |
| 405 // "Fire a simple event named updatefound..." | 405 // "Fire a simple event named updatefound..." |
| 406 registration()->NotifyUpdateFound(); | 406 registration()->NotifyUpdateFound(); |
| 407 | 407 |
| 408 // "Fire an event named install..." | 408 // "Fire an event named install..." |
| 409 new_version()->RunAfterStartWorker( | 409 new_version()->RunAfterStartWorker( |
| 410 ServiceWorkerMetrics::EventType::INSTALL, | 410 ServiceWorkerMetrics::EventType::INSTALL, |
| 411 base::Bind(&ServiceWorkerRegisterJob::DispatchInstallEvent, | 411 base::Bind(&ServiceWorkerRegisterJob::DispatchInstallEvent, |
| 412 weak_factory_.GetWeakPtr()), | 412 weak_factory_.GetWeakPtr()), |
| 413 base::Bind(&ServiceWorkerRegisterJob::OnInstallFinished, | 413 base::Bind(&ServiceWorkerRegisterJob::OnInstallFailed, |
| 414 weak_factory_.GetWeakPtr())); | 414 weak_factory_.GetWeakPtr())); |
| 415 | 415 |
| 416 // A subsequent registration job may terminate our installing worker. It can | 416 // A subsequent registration job may terminate our installing worker. It can |
| 417 // only do so after we've started the worker and dispatched the install | 417 // only do so after we've started the worker and dispatched the install |
| 418 // event, as those are atomic substeps in the [[Install]] algorithm. | 418 // event, as those are atomic substeps in the [[Install]] algorithm. |
| 419 if (doom_installing_worker_) | 419 if (doom_installing_worker_) |
| 420 Complete(SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); | 420 Complete(SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); |
| 421 } | 421 } |
| 422 | 422 |
| 423 void ServiceWorkerRegisterJob::DispatchInstallEvent() { | 423 void ServiceWorkerRegisterJob::DispatchInstallEvent() { |
| 424 DCHECK_EQ(ServiceWorkerVersion::INSTALLING, new_version()->status()) | 424 DCHECK_EQ(ServiceWorkerVersion::INSTALLING, new_version()->status()) |
| 425 << new_version()->status(); | 425 << new_version()->status(); |
| 426 DCHECK_EQ(ServiceWorkerVersion::RUNNING, new_version()->running_status()) | 426 DCHECK_EQ(ServiceWorkerVersion::RUNNING, new_version()->running_status()) |
| 427 << "Worker stopped too soon after it was started."; | 427 << "Worker stopped too soon after it was started."; |
| 428 int request_id = new_version()->StartRequest( | 428 int request_id = new_version()->StartRequest( |
| 429 ServiceWorkerMetrics::EventType::INSTALL, | 429 ServiceWorkerMetrics::EventType::INSTALL, |
| 430 base::Bind(&ServiceWorkerRegisterJob::OnInstallFailed, | |
| 431 weak_factory_.GetWeakPtr())); | |
| 432 new_version()->DispatchEvent<ServiceWorkerHostMsg_InstallEventFinished>( | |
| 433 request_id, ServiceWorkerMsg_InstallEvent(request_id), | |
| 430 base::Bind(&ServiceWorkerRegisterJob::OnInstallFinished, | 434 base::Bind(&ServiceWorkerRegisterJob::OnInstallFinished, |
| 431 weak_factory_.GetWeakPtr())); | 435 weak_factory_.GetWeakPtr())); |
| 432 new_version()->DispatchSimpleEvent<ServiceWorkerHostMsg_InstallEventFinished>( | |
| 433 request_id, ServiceWorkerMsg_InstallEvent(request_id)); | |
| 434 } | 436 } |
| 435 | 437 |
| 436 void ServiceWorkerRegisterJob::OnInstallFinished( | 438 void ServiceWorkerRegisterJob::OnInstallFinished( |
| 437 ServiceWorkerStatusCode status) { | 439 int request_id, |
| 440 blink::WebServiceWorkerEventResult result, | |
| 441 bool has_fetch_handler) { | |
| 442 new_version()->FinishRequest( | |
| 443 request_id, result == blink::WebServiceWorkerEventResultCompleted); | |
| 444 | |
| 445 ServiceWorkerStatusCode status = SERVICE_WORKER_OK; | |
| 446 if (result == blink::WebServiceWorkerEventResultRejected) | |
| 447 status = SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; | |
|
falken
2016/05/24 06:25:48
This construct is a bit scary. If we someday add a
shimazu
2016/05/24 08:30:27
Done.
| |
| 448 | |
| 438 ServiceWorkerMetrics::RecordInstallEventStatus(status); | 449 ServiceWorkerMetrics::RecordInstallEventStatus(status); |
|
falken
2016/05/24 06:25:48
I think you need this in OnInstallFailed() also.
shimazu
2016/05/24 08:30:27
Done.
| |
| 439 | |
| 440 if (status != SERVICE_WORKER_OK) { | 450 if (status != SERVICE_WORKER_OK) { |
| 441 // "8. If installFailed is true, then:..." | |
| 442 Complete(status, std::string("ServiceWorker failed to install: ") + | 451 Complete(status, std::string("ServiceWorker failed to install: ") + |
| 443 ServiceWorkerStatusToString(status)); | 452 ServiceWorkerStatusToString(status)); |
|
falken
2016/05/24 06:25:48
Prefer to call OnInstallFailed over copy/pasting t
shimazu
2016/05/24 08:30:27
Done.
| |
| 444 return; | 453 return; |
| 445 } | 454 } |
| 446 | 455 |
| 447 SetPhase(STORE); | 456 SetPhase(STORE); |
| 448 DCHECK(!registration()->last_update_check().is_null()); | 457 DCHECK(!registration()->last_update_check().is_null()); |
| 458 new_version()->set_has_fetch_handler(has_fetch_handler); | |
| 449 context_->storage()->StoreRegistration( | 459 context_->storage()->StoreRegistration( |
| 450 registration(), | 460 registration(), |
| 451 new_version(), | 461 new_version(), |
| 452 base::Bind(&ServiceWorkerRegisterJob::OnStoreRegistrationComplete, | 462 base::Bind(&ServiceWorkerRegisterJob::OnStoreRegistrationComplete, |
| 453 weak_factory_.GetWeakPtr())); | 463 weak_factory_.GetWeakPtr())); |
| 454 } | 464 } |
| 455 | 465 |
| 466 void ServiceWorkerRegisterJob::OnInstallFailed(ServiceWorkerStatusCode status) { | |
| 467 if (status != SERVICE_WORKER_OK) { | |
| 468 Complete(status, std::string("ServiceWorker failed to install: ") + | |
| 469 ServiceWorkerStatusToString(status)); | |
| 470 } else { | |
| 471 NOTREACHED() << "OnInstallFailed should not handle SERVICE_WORKER_OK"; | |
| 472 } | |
| 473 } | |
| 474 | |
| 456 void ServiceWorkerRegisterJob::OnStoreRegistrationComplete( | 475 void ServiceWorkerRegisterJob::OnStoreRegistrationComplete( |
| 457 ServiceWorkerStatusCode status) { | 476 ServiceWorkerStatusCode status) { |
| 458 if (status != SERVICE_WORKER_OK) { | 477 if (status != SERVICE_WORKER_OK) { |
| 459 Complete(status); | 478 Complete(status); |
| 460 return; | 479 return; |
| 461 } | 480 } |
| 462 | 481 |
| 463 // "9. If registration.waitingWorker is not null, then:..." | 482 // "9. If registration.waitingWorker is not null, then:..." |
| 464 if (registration()->waiting_version()) { | 483 if (registration()->waiting_version()) { |
| 465 // "1. Run the [[UpdateState]] algorithm passing registration.waitingWorker | 484 // "1. Run the [[UpdateState]] algorithm passing registration.waitingWorker |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 602 new_version()->force_bypass_cache_for_scripts() || | 621 new_version()->force_bypass_cache_for_scripts() || |
| 603 registration()->last_update_check().is_null()) { | 622 registration()->last_update_check().is_null()) { |
| 604 registration()->set_last_update_check(base::Time::Now()); | 623 registration()->set_last_update_check(base::Time::Now()); |
| 605 | 624 |
| 606 if (registration()->has_installed_version()) | 625 if (registration()->has_installed_version()) |
| 607 context_->storage()->UpdateLastUpdateCheckTime(registration()); | 626 context_->storage()->UpdateLastUpdateCheckTime(registration()); |
| 608 } | 627 } |
| 609 } | 628 } |
| 610 | 629 |
| 611 } // namespace content | 630 } // namespace content |
| OLD | NEW |