| 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, |
| 438 ServiceWorkerMetrics::RecordInstallEventStatus(status); | 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_ERROR_FAILED; |
| 446 switch (result) { |
| 447 case blink::WebServiceWorkerEventResultCompleted: |
| 448 status = SERVICE_WORKER_OK; |
| 449 break; |
| 450 case blink::WebServiceWorkerEventResultRejected: |
| 451 status = SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; |
| 452 break; |
| 453 default: |
| 454 NOTREACHED(); |
| 455 } |
| 439 | 456 |
| 440 if (status != SERVICE_WORKER_OK) { | 457 if (status != SERVICE_WORKER_OK) { |
| 441 // "8. If installFailed is true, then:..." | 458 OnInstallFailed(status); |
| 442 Complete(status, std::string("ServiceWorker failed to install: ") + | |
| 443 ServiceWorkerStatusToString(status)); | |
| 444 return; | 459 return; |
| 445 } | 460 } |
| 446 | 461 |
| 462 ServiceWorkerMetrics::RecordInstallEventStatus(status); |
| 463 |
| 447 SetPhase(STORE); | 464 SetPhase(STORE); |
| 448 DCHECK(!registration()->last_update_check().is_null()); | 465 DCHECK(!registration()->last_update_check().is_null()); |
| 466 new_version()->set_has_fetch_handler(has_fetch_handler); |
| 449 context_->storage()->StoreRegistration( | 467 context_->storage()->StoreRegistration( |
| 450 registration(), | 468 registration(), |
| 451 new_version(), | 469 new_version(), |
| 452 base::Bind(&ServiceWorkerRegisterJob::OnStoreRegistrationComplete, | 470 base::Bind(&ServiceWorkerRegisterJob::OnStoreRegistrationComplete, |
| 453 weak_factory_.GetWeakPtr())); | 471 weak_factory_.GetWeakPtr())); |
| 454 } | 472 } |
| 455 | 473 |
| 474 void ServiceWorkerRegisterJob::OnInstallFailed(ServiceWorkerStatusCode status) { |
| 475 ServiceWorkerMetrics::RecordInstallEventStatus(status); |
| 476 |
| 477 if (status != SERVICE_WORKER_OK) { |
| 478 Complete(status, std::string("ServiceWorker failed to install: ") + |
| 479 ServiceWorkerStatusToString(status)); |
| 480 } else { |
| 481 NOTREACHED() << "OnInstallFailed should not handle SERVICE_WORKER_OK"; |
| 482 } |
| 483 } |
| 484 |
| 456 void ServiceWorkerRegisterJob::OnStoreRegistrationComplete( | 485 void ServiceWorkerRegisterJob::OnStoreRegistrationComplete( |
| 457 ServiceWorkerStatusCode status) { | 486 ServiceWorkerStatusCode status) { |
| 458 if (status != SERVICE_WORKER_OK) { | 487 if (status != SERVICE_WORKER_OK) { |
| 459 Complete(status); | 488 Complete(status); |
| 460 return; | 489 return; |
| 461 } | 490 } |
| 462 | 491 |
| 463 // "9. If registration.waitingWorker is not null, then:..." | 492 // "9. If registration.waitingWorker is not null, then:..." |
| 464 if (registration()->waiting_version()) { | 493 if (registration()->waiting_version()) { |
| 465 // "1. Run the [[UpdateState]] algorithm passing registration.waitingWorker | 494 // "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() || | 631 new_version()->force_bypass_cache_for_scripts() || |
| 603 registration()->last_update_check().is_null()) { | 632 registration()->last_update_check().is_null()) { |
| 604 registration()->set_last_update_check(base::Time::Now()); | 633 registration()->set_last_update_check(base::Time::Now()); |
| 605 | 634 |
| 606 if (registration()->has_installed_version()) | 635 if (registration()->has_installed_version()) |
| 607 context_->storage()->UpdateLastUpdateCheckTime(registration()); | 636 context_->storage()->UpdateLastUpdateCheckTime(registration()); |
| 608 } | 637 } |
| 609 } | 638 } |
| 610 | 639 |
| 611 } // namespace content | 640 } // namespace content |
| OLD | NEW |