| 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_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 void RunTaskAfterStartWorker( | 96 void RunTaskAfterStartWorker( |
| 97 base::WeakPtr<ServiceWorkerVersion> version, | 97 base::WeakPtr<ServiceWorkerVersion> version, |
| 98 const StatusCallback& error_callback, | 98 const StatusCallback& error_callback, |
| 99 const base::Closure& task, | 99 const base::Closure& task, |
| 100 ServiceWorkerStatusCode status) { | 100 ServiceWorkerStatusCode status) { |
| 101 if (status != SERVICE_WORKER_OK) { | 101 if (status != SERVICE_WORKER_OK) { |
| 102 if (!error_callback.is_null()) | 102 if (!error_callback.is_null()) |
| 103 error_callback.Run(status); | 103 error_callback.Run(status); |
| 104 return; | 104 return; |
| 105 } | 105 } |
| 106 if (version->running_status() != ServiceWorkerVersion::RUNNING) { | 106 if (version->running_status() != EmbeddedWorkerStatus::RUNNING) { |
| 107 // We've tried to start the worker (and it has succeeded), but | 107 // We've tried to start the worker (and it has succeeded), but |
| 108 // it looks it's not running yet. | 108 // it looks it's not running yet. |
| 109 NOTREACHED() << "The worker's not running after successful StartWorker"; | 109 NOTREACHED() << "The worker's not running after successful StartWorker"; |
| 110 if (!error_callback.is_null()) | 110 if (!error_callback.is_null()) |
| 111 error_callback.Run(SERVICE_WORKER_ERROR_START_WORKER_FAILED); | 111 error_callback.Run(SERVICE_WORKER_ERROR_START_WORKER_FAILED); |
| 112 return; | 112 return; |
| 113 } | 113 } |
| 114 task.Run(); | 114 task.Run(); |
| 115 } | 115 } |
| 116 | 116 |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 if (!start_callbacks_.empty()) { | 319 if (!start_callbacks_.empty()) { |
| 320 // RecordStartWorkerResult must be the first element of start_callbacks_. | 320 // RecordStartWorkerResult must be the first element of start_callbacks_. |
| 321 StatusCallback record_start_worker_result = start_callbacks_[0]; | 321 StatusCallback record_start_worker_result = start_callbacks_[0]; |
| 322 start_callbacks_.clear(); | 322 start_callbacks_.clear(); |
| 323 record_start_worker_result.Run(SERVICE_WORKER_ERROR_ABORT); | 323 record_start_worker_result.Run(SERVICE_WORKER_ERROR_ABORT); |
| 324 } | 324 } |
| 325 | 325 |
| 326 if (context_) | 326 if (context_) |
| 327 context_->RemoveLiveVersion(version_id_); | 327 context_->RemoveLiveVersion(version_id_); |
| 328 | 328 |
| 329 if (running_status() == STARTING || running_status() == RUNNING) | 329 if (running_status() == EmbeddedWorkerStatus::STARTING || |
| 330 running_status() == EmbeddedWorkerStatus::RUNNING) { |
| 330 embedded_worker_->Stop(); | 331 embedded_worker_->Stop(); |
| 332 } |
| 331 embedded_worker_->RemoveListener(this); | 333 embedded_worker_->RemoveListener(this); |
| 332 } | 334 } |
| 333 | 335 |
| 334 void ServiceWorkerVersion::SetStatus(Status status) { | 336 void ServiceWorkerVersion::SetStatus(Status status) { |
| 335 if (status_ == status) | 337 if (status_ == status) |
| 336 return; | 338 return; |
| 337 | 339 |
| 338 TRACE_EVENT2("ServiceWorker", "ServiceWorkerVersion::SetStatus", "Script URL", | 340 TRACE_EVENT2("ServiceWorker", "ServiceWorkerVersion::SetStatus", "Script URL", |
| 339 script_url_.spec(), "New Status", VersionStatusToString(status)); | 341 script_url_.spec(), "New Status", VersionStatusToString(status)); |
| 340 | 342 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 weak_factory_.GetWeakPtr(), purpose, status_, | 441 weak_factory_.GetWeakPtr(), purpose, status_, |
| 440 is_browser_startup_complete, callback)); | 442 is_browser_startup_complete, callback)); |
| 441 } | 443 } |
| 442 | 444 |
| 443 void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) { | 445 void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) { |
| 444 TRACE_EVENT_INSTANT2("ServiceWorker", | 446 TRACE_EVENT_INSTANT2("ServiceWorker", |
| 445 "ServiceWorkerVersion::StopWorker (instant)", | 447 "ServiceWorkerVersion::StopWorker (instant)", |
| 446 TRACE_EVENT_SCOPE_THREAD, "Script", script_url_.spec(), | 448 TRACE_EVENT_SCOPE_THREAD, "Script", script_url_.spec(), |
| 447 "Status", VersionStatusToString(status_)); | 449 "Status", VersionStatusToString(status_)); |
| 448 | 450 |
| 449 if (running_status() == STOPPED) { | 451 if (running_status() == EmbeddedWorkerStatus::STOPPED) { |
| 450 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); | 452 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); |
| 451 return; | 453 return; |
| 452 } | 454 } |
| 453 | 455 |
| 454 if (stop_callbacks_.empty()) { | 456 if (stop_callbacks_.empty()) { |
| 455 ServiceWorkerStatusCode status = embedded_worker_->Stop(); | 457 ServiceWorkerStatusCode status = embedded_worker_->Stop(); |
| 456 if (status != SERVICE_WORKER_OK) { | 458 if (status != SERVICE_WORKER_OK) { |
| 457 RunSoon(base::Bind(callback, status)); | 459 RunSoon(base::Bind(callback, status)); |
| 458 return; | 460 return; |
| 459 } | 461 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 event_type, error_callback, | 504 event_type, error_callback, |
| 503 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes), KILL_ON_TIMEOUT); | 505 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes), KILL_ON_TIMEOUT); |
| 504 } | 506 } |
| 505 | 507 |
| 506 int ServiceWorkerVersion::StartRequestWithCustomTimeout( | 508 int ServiceWorkerVersion::StartRequestWithCustomTimeout( |
| 507 ServiceWorkerMetrics::EventType event_type, | 509 ServiceWorkerMetrics::EventType event_type, |
| 508 const StatusCallback& error_callback, | 510 const StatusCallback& error_callback, |
| 509 const base::TimeDelta& timeout, | 511 const base::TimeDelta& timeout, |
| 510 TimeoutBehavior timeout_behavior) { | 512 TimeoutBehavior timeout_behavior) { |
| 511 OnBeginEvent(); | 513 OnBeginEvent(); |
| 512 DCHECK_EQ(RUNNING, running_status()) | 514 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status()) |
| 513 << "Can only start a request with a running worker."; | 515 << "Can only start a request with a running worker."; |
| 514 DCHECK(event_type == ServiceWorkerMetrics::EventType::INSTALL || | 516 DCHECK(event_type == ServiceWorkerMetrics::EventType::INSTALL || |
| 515 event_type == ServiceWorkerMetrics::EventType::ACTIVATE || | 517 event_type == ServiceWorkerMetrics::EventType::ACTIVATE || |
| 516 event_type == ServiceWorkerMetrics::EventType::MESSAGE || | 518 event_type == ServiceWorkerMetrics::EventType::MESSAGE || |
| 517 status() == ACTIVATED) | 519 status() == ACTIVATED) |
| 518 << "Event of type " << static_cast<int>(event_type) | 520 << "Event of type " << static_cast<int>(event_type) |
| 519 << " can only be dispatched to an active worker: " << status(); | 521 << " can only be dispatched to an active worker: " << status(); |
| 520 | 522 |
| 521 PendingRequest<StatusCallback>* request = new PendingRequest<StatusCallback>( | 523 PendingRequest<StatusCallback>* request = new PendingRequest<StatusCallback>( |
| 522 error_callback, base::TimeTicks::Now(), event_type); | 524 error_callback, base::TimeTicks::Now(), event_type); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 549 // order to release worker resources soon. | 551 // order to release worker resources soon. |
| 550 StopWorkerIfIdle(); | 552 StopWorkerIfIdle(); |
| 551 } | 553 } |
| 552 return true; | 554 return true; |
| 553 } | 555 } |
| 554 | 556 |
| 555 void ServiceWorkerVersion::RunAfterStartWorker( | 557 void ServiceWorkerVersion::RunAfterStartWorker( |
| 556 ServiceWorkerMetrics::EventType purpose, | 558 ServiceWorkerMetrics::EventType purpose, |
| 557 const base::Closure& task, | 559 const base::Closure& task, |
| 558 const StatusCallback& error_callback) { | 560 const StatusCallback& error_callback) { |
| 559 if (running_status() == RUNNING) { | 561 if (running_status() == EmbeddedWorkerStatus::RUNNING) { |
| 560 DCHECK(start_callbacks_.empty()); | 562 DCHECK(start_callbacks_.empty()); |
| 561 task.Run(); | 563 task.Run(); |
| 562 return; | 564 return; |
| 563 } | 565 } |
| 564 StartWorker(purpose, | 566 StartWorker(purpose, |
| 565 base::Bind(&RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), | 567 base::Bind(&RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), |
| 566 error_callback, task)); | 568 error_callback, task)); |
| 567 } | 569 } |
| 568 | 570 |
| 569 void ServiceWorkerVersion::AddControllee( | 571 void ServiceWorkerVersion::AddControllee( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 } | 625 } |
| 624 | 626 |
| 625 void ServiceWorkerVersion::SetStartWorkerStatusCode( | 627 void ServiceWorkerVersion::SetStartWorkerStatusCode( |
| 626 ServiceWorkerStatusCode status) { | 628 ServiceWorkerStatusCode status) { |
| 627 start_worker_status_ = status; | 629 start_worker_status_ = status; |
| 628 } | 630 } |
| 629 | 631 |
| 630 void ServiceWorkerVersion::Doom() { | 632 void ServiceWorkerVersion::Doom() { |
| 631 DCHECK(!HasControllee()); | 633 DCHECK(!HasControllee()); |
| 632 SetStatus(REDUNDANT); | 634 SetStatus(REDUNDANT); |
| 633 if (running_status() == STARTING || running_status() == RUNNING) { | 635 if (running_status() == EmbeddedWorkerStatus::STARTING || |
| 636 running_status() == EmbeddedWorkerStatus::RUNNING) { |
| 634 if (embedded_worker()->devtools_attached()) | 637 if (embedded_worker()->devtools_attached()) |
| 635 stop_when_devtools_detached_ = true; | 638 stop_when_devtools_detached_ = true; |
| 636 else | 639 else |
| 637 embedded_worker_->Stop(); | 640 embedded_worker_->Stop(); |
| 638 } | 641 } |
| 639 if (!context_) | 642 if (!context_) |
| 640 return; | 643 return; |
| 641 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | 644 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 642 script_cache_map_.GetResources(&resources); | 645 script_cache_map_.GetResources(&resources); |
| 643 context_->storage()->PurgeResources(resources); | 646 context_->storage()->PurgeResources(resources); |
| 644 } | 647 } |
| 645 | 648 |
| 646 void ServiceWorkerVersion::SetDevToolsAttached(bool attached) { | 649 void ServiceWorkerVersion::SetDevToolsAttached(bool attached) { |
| 647 embedded_worker()->set_devtools_attached(attached); | 650 embedded_worker()->set_devtools_attached(attached); |
| 648 if (stop_when_devtools_detached_ && !attached) { | 651 if (stop_when_devtools_detached_ && !attached) { |
| 649 DCHECK_EQ(REDUNDANT, status()); | 652 DCHECK_EQ(REDUNDANT, status()); |
| 650 if (running_status() == STARTING || running_status() == RUNNING) | 653 if (running_status() == EmbeddedWorkerStatus::STARTING || |
| 654 running_status() == EmbeddedWorkerStatus::RUNNING) { |
| 651 embedded_worker_->Stop(); | 655 embedded_worker_->Stop(); |
| 656 } |
| 652 return; | 657 return; |
| 653 } | 658 } |
| 654 if (attached) { | 659 if (attached) { |
| 655 // TODO(falken): Canceling the timeouts when debugging could cause | 660 // TODO(falken): Canceling the timeouts when debugging could cause |
| 656 // heisenbugs; we should instead run them as normal show an educational | 661 // heisenbugs; we should instead run them as normal show an educational |
| 657 // message in DevTools when they occur. crbug.com/470419 | 662 // message in DevTools when they occur. crbug.com/470419 |
| 658 | 663 |
| 659 // Don't record the startup time metric once DevTools is attached. | 664 // Don't record the startup time metric once DevTools is attached. |
| 660 ClearTick(&start_time_); | 665 ClearTick(&start_time_); |
| 661 skip_recording_startup_time_ = true; | 666 skip_recording_startup_time_ = true; |
| 662 | 667 |
| 663 // Cancel request timeouts. | 668 // Cancel request timeouts. |
| 664 SetAllRequestExpirations(base::TimeTicks()); | 669 SetAllRequestExpirations(base::TimeTicks()); |
| 665 return; | 670 return; |
| 666 } | 671 } |
| 667 if (!start_callbacks_.empty()) { | 672 if (!start_callbacks_.empty()) { |
| 668 // Reactivate the timer for start timeout. | 673 // Reactivate the timer for start timeout. |
| 669 DCHECK(timeout_timer_.IsRunning()); | 674 DCHECK(timeout_timer_.IsRunning()); |
| 670 DCHECK(running_status() == STARTING || running_status() == STOPPING) | 675 DCHECK(running_status() == EmbeddedWorkerStatus::STARTING || |
| 671 << running_status(); | 676 running_status() == EmbeddedWorkerStatus::STOPPING) |
| 677 << static_cast<int>(running_status()); |
| 672 RestartTick(&start_time_); | 678 RestartTick(&start_time_); |
| 673 } | 679 } |
| 674 | 680 |
| 675 // Reactivate request timeouts, setting them all to the same expiration time. | 681 // Reactivate request timeouts, setting them all to the same expiration time. |
| 676 SetAllRequestExpirations( | 682 SetAllRequestExpirations( |
| 677 base::TimeTicks::Now() + | 683 base::TimeTicks::Now() + |
| 678 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes)); | 684 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes)); |
| 679 } | 685 } |
| 680 | 686 |
| 681 void ServiceWorkerVersion::SetMainScriptHttpResponseInfo( | 687 void ServiceWorkerVersion::SetMainScriptHttpResponseInfo( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::Request", | 739 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::Request", |
| 734 request, "Error", "Service Disconnected"); | 740 request, "Error", "Service Disconnected"); |
| 735 request->callback.Run(SERVICE_WORKER_ERROR_FAILED); | 741 request->callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 736 worker_->custom_requests_.Remove(iter.GetCurrentKey()); | 742 worker_->custom_requests_.Remove(iter.GetCurrentKey()); |
| 737 } | 743 } |
| 738 iter.Advance(); | 744 iter.Advance(); |
| 739 } | 745 } |
| 740 } | 746 } |
| 741 | 747 |
| 742 void ServiceWorkerVersion::OnThreadStarted() { | 748 void ServiceWorkerVersion::OnThreadStarted() { |
| 743 DCHECK_EQ(STARTING, running_status()); | 749 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, running_status()); |
| 744 // Activate ping/pong now that JavaScript execution will start. | 750 // Activate ping/pong now that JavaScript execution will start. |
| 745 ping_controller_->Activate(); | 751 ping_controller_->Activate(); |
| 746 } | 752 } |
| 747 | 753 |
| 748 void ServiceWorkerVersion::OnStarting() { | 754 void ServiceWorkerVersion::OnStarting() { |
| 749 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); | 755 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); |
| 750 } | 756 } |
| 751 | 757 |
| 752 void ServiceWorkerVersion::OnStarted() { | 758 void ServiceWorkerVersion::OnStarted() { |
| 753 DCHECK_EQ(RUNNING, running_status()); | 759 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status()); |
| 754 RestartTick(&idle_time_); | 760 RestartTick(&idle_time_); |
| 755 | 761 |
| 756 // Fire all start callbacks. | 762 // Fire all start callbacks. |
| 757 scoped_refptr<ServiceWorkerVersion> protect(this); | 763 scoped_refptr<ServiceWorkerVersion> protect(this); |
| 758 RunCallbacks(this, &start_callbacks_, SERVICE_WORKER_OK); | 764 RunCallbacks(this, &start_callbacks_, SERVICE_WORKER_OK); |
| 759 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); | 765 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); |
| 760 } | 766 } |
| 761 | 767 |
| 762 void ServiceWorkerVersion::OnStopping() { | 768 void ServiceWorkerVersion::OnStopping() { |
| 763 DCHECK(stop_time_.is_null()); | 769 DCHECK(stop_time_.is_null()); |
| 764 RestartTick(&stop_time_); | 770 RestartTick(&stop_time_); |
| 765 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", "ServiceWorkerVersion::StopWorker", | 771 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", "ServiceWorkerVersion::StopWorker", |
| 766 stop_time_.ToInternalValue(), "Script", | 772 stop_time_.ToInternalValue(), "Script", |
| 767 script_url_.spec(), "Version Status", | 773 script_url_.spec(), "Version Status", |
| 768 VersionStatusToString(status_)); | 774 VersionStatusToString(status_)); |
| 769 | 775 |
| 770 // Shorten the interval so stalling in stopped can be fixed quickly. Once the | 776 // Shorten the interval so stalling in stopped can be fixed quickly. Once the |
| 771 // worker stops, the timer is disabled. The interval will be reset to normal | 777 // worker stops, the timer is disabled. The interval will be reset to normal |
| 772 // when the worker starts up again. | 778 // when the worker starts up again. |
| 773 SetTimeoutTimerInterval( | 779 SetTimeoutTimerInterval( |
| 774 base::TimeDelta::FromSeconds(kStopWorkerTimeoutSeconds)); | 780 base::TimeDelta::FromSeconds(kStopWorkerTimeoutSeconds)); |
| 775 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); | 781 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); |
| 776 } | 782 } |
| 777 | 783 |
| 778 void ServiceWorkerVersion::OnStopped( | 784 void ServiceWorkerVersion::OnStopped(EmbeddedWorkerStatus old_status) { |
| 779 EmbeddedWorkerInstance::Status old_status) { | |
| 780 if (IsInstalled(status())) { | 785 if (IsInstalled(status())) { |
| 781 ServiceWorkerMetrics::RecordWorkerStopped( | 786 ServiceWorkerMetrics::RecordWorkerStopped( |
| 782 ServiceWorkerMetrics::StopStatus::NORMAL); | 787 ServiceWorkerMetrics::StopStatus::NORMAL); |
| 783 } | 788 } |
| 784 if (!stop_time_.is_null()) | 789 if (!stop_time_.is_null()) |
| 785 ServiceWorkerMetrics::RecordStopWorkerTime(GetTickDuration(stop_time_)); | 790 ServiceWorkerMetrics::RecordStopWorkerTime(GetTickDuration(stop_time_)); |
| 786 | 791 |
| 787 OnStoppedInternal(old_status); | 792 OnStoppedInternal(old_status); |
| 788 } | 793 } |
| 789 | 794 |
| 790 void ServiceWorkerVersion::OnDetached( | 795 void ServiceWorkerVersion::OnDetached(EmbeddedWorkerStatus old_status) { |
| 791 EmbeddedWorkerInstance::Status old_status) { | |
| 792 if (IsInstalled(status())) { | 796 if (IsInstalled(status())) { |
| 793 ServiceWorkerMetrics::RecordWorkerStopped( | 797 ServiceWorkerMetrics::RecordWorkerStopped( |
| 794 ServiceWorkerMetrics::StopStatus::DETACH_BY_REGISTRY); | 798 ServiceWorkerMetrics::StopStatus::DETACH_BY_REGISTRY); |
| 795 } | 799 } |
| 796 OnStoppedInternal(old_status); | 800 OnStoppedInternal(old_status); |
| 797 } | 801 } |
| 798 | 802 |
| 799 void ServiceWorkerVersion::OnScriptLoaded() { | 803 void ServiceWorkerVersion::OnScriptLoaded() { |
| 800 if (IsInstalled(status())) | 804 if (IsInstalled(status())) |
| 801 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.ScriptLoadSuccess", true); | 805 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.ScriptLoadSuccess", true); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 892 | 896 |
| 893 void ServiceWorkerVersion::OnGetClientFinished( | 897 void ServiceWorkerVersion::OnGetClientFinished( |
| 894 int request_id, | 898 int request_id, |
| 895 const ServiceWorkerClientInfo& client_info) { | 899 const ServiceWorkerClientInfo& client_info) { |
| 896 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 900 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 897 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::OnGetClient", | 901 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::OnGetClient", |
| 898 request_id, "client_type", client_info.client_type); | 902 request_id, "client_type", client_info.client_type); |
| 899 | 903 |
| 900 // When Clients.get() is called on the script evaluation phase, the running | 904 // When Clients.get() is called on the script evaluation phase, the running |
| 901 // status can be STARTING here. | 905 // status can be STARTING here. |
| 902 if (running_status() != STARTING && running_status() != RUNNING) | 906 if (running_status() != EmbeddedWorkerStatus::STARTING && |
| 907 running_status() != EmbeddedWorkerStatus::RUNNING) { |
| 903 return; | 908 return; |
| 909 } |
| 904 | 910 |
| 905 embedded_worker_->SendMessage( | 911 embedded_worker_->SendMessage( |
| 906 ServiceWorkerMsg_DidGetClient(request_id, client_info)); | 912 ServiceWorkerMsg_DidGetClient(request_id, client_info)); |
| 907 } | 913 } |
| 908 | 914 |
| 909 void ServiceWorkerVersion::OnGetClients( | 915 void ServiceWorkerVersion::OnGetClients( |
| 910 int request_id, | 916 int request_id, |
| 911 const ServiceWorkerClientQueryOptions& options) { | 917 const ServiceWorkerClientQueryOptions& options) { |
| 912 TRACE_EVENT_ASYNC_BEGIN2( | 918 TRACE_EVENT_ASYNC_BEGIN2( |
| 913 "ServiceWorker", "ServiceWorkerVersion::OnGetClients", request_id, | 919 "ServiceWorker", "ServiceWorkerVersion::OnGetClients", request_id, |
| 914 "client_type", options.client_type, "include_uncontrolled", | 920 "client_type", options.client_type, "include_uncontrolled", |
| 915 options.include_uncontrolled); | 921 options.include_uncontrolled); |
| 916 service_worker_client_utils::GetClients( | 922 service_worker_client_utils::GetClients( |
| 917 weak_factory_.GetWeakPtr(), options, | 923 weak_factory_.GetWeakPtr(), options, |
| 918 base::Bind(&ServiceWorkerVersion::OnGetClientsFinished, | 924 base::Bind(&ServiceWorkerVersion::OnGetClientsFinished, |
| 919 weak_factory_.GetWeakPtr(), request_id)); | 925 weak_factory_.GetWeakPtr(), request_id)); |
| 920 } | 926 } |
| 921 | 927 |
| 922 void ServiceWorkerVersion::OnGetClientsFinished(int request_id, | 928 void ServiceWorkerVersion::OnGetClientsFinished(int request_id, |
| 923 ServiceWorkerClients* clients) { | 929 ServiceWorkerClients* clients) { |
| 924 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 930 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 925 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::OnGetClients", | 931 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::OnGetClients", |
| 926 request_id, "The number of clients", clients->size()); | 932 request_id, "The number of clients", clients->size()); |
| 927 | 933 |
| 928 // When Clients.matchAll() is called on the script evaluation phase, the | 934 // When Clients.matchAll() is called on the script evaluation phase, the |
| 929 // running status can be STARTING here. | 935 // running status can be STARTING here. |
| 930 if (running_status() != STARTING && running_status() != RUNNING) | 936 if (running_status() != EmbeddedWorkerStatus::STARTING && |
| 937 running_status() != EmbeddedWorkerStatus::RUNNING) { |
| 931 return; | 938 return; |
| 939 } |
| 932 | 940 |
| 933 embedded_worker_->SendMessage( | 941 embedded_worker_->SendMessage( |
| 934 ServiceWorkerMsg_DidGetClients(request_id, *clients)); | 942 ServiceWorkerMsg_DidGetClients(request_id, *clients)); |
| 935 } | 943 } |
| 936 | 944 |
| 937 void ServiceWorkerVersion::OnSimpleEventResponse( | 945 void ServiceWorkerVersion::OnSimpleEventResponse( |
| 938 int request_id, | 946 int request_id, |
| 939 blink::WebServiceWorkerEventResult result) { | 947 blink::WebServiceWorkerEventResult result) { |
| 940 // Copy error callback before calling FinishRequest. | 948 // Copy error callback before calling FinishRequest. |
| 941 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); | 949 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 986 base::Bind(&ServiceWorkerVersion::OnOpenWindowFinished, | 994 base::Bind(&ServiceWorkerVersion::OnOpenWindowFinished, |
| 987 weak_factory_.GetWeakPtr(), request_id)); | 995 weak_factory_.GetWeakPtr(), request_id)); |
| 988 } | 996 } |
| 989 | 997 |
| 990 void ServiceWorkerVersion::OnOpenWindowFinished( | 998 void ServiceWorkerVersion::OnOpenWindowFinished( |
| 991 int request_id, | 999 int request_id, |
| 992 ServiceWorkerStatusCode status, | 1000 ServiceWorkerStatusCode status, |
| 993 const ServiceWorkerClientInfo& client_info) { | 1001 const ServiceWorkerClientInfo& client_info) { |
| 994 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1002 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 995 | 1003 |
| 996 if (running_status() != RUNNING) | 1004 if (running_status() != EmbeddedWorkerStatus::RUNNING) |
| 997 return; | 1005 return; |
| 998 | 1006 |
| 999 if (status != SERVICE_WORKER_OK) { | 1007 if (status != SERVICE_WORKER_OK) { |
| 1000 embedded_worker_->SendMessage(ServiceWorkerMsg_OpenWindowError( | 1008 embedded_worker_->SendMessage(ServiceWorkerMsg_OpenWindowError( |
| 1001 request_id, "Something went wrong while trying to open the window.")); | 1009 request_id, "Something went wrong while trying to open the window.")); |
| 1002 return; | 1010 return; |
| 1003 } | 1011 } |
| 1004 | 1012 |
| 1005 embedded_worker_->SendMessage( | 1013 embedded_worker_->SendMessage( |
| 1006 ServiceWorkerMsg_OpenWindowResponse(request_id, client_info)); | 1014 ServiceWorkerMsg_OpenWindowResponse(request_id, client_info)); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1094 service_worker_client_utils::FocusWindowClient( | 1102 service_worker_client_utils::FocusWindowClient( |
| 1095 provider_host, base::Bind(&ServiceWorkerVersion::OnFocusClientFinished, | 1103 provider_host, base::Bind(&ServiceWorkerVersion::OnFocusClientFinished, |
| 1096 weak_factory_.GetWeakPtr(), request_id)); | 1104 weak_factory_.GetWeakPtr(), request_id)); |
| 1097 } | 1105 } |
| 1098 | 1106 |
| 1099 void ServiceWorkerVersion::OnFocusClientFinished( | 1107 void ServiceWorkerVersion::OnFocusClientFinished( |
| 1100 int request_id, | 1108 int request_id, |
| 1101 const ServiceWorkerClientInfo& client_info) { | 1109 const ServiceWorkerClientInfo& client_info) { |
| 1102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1110 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1103 | 1111 |
| 1104 if (running_status() != RUNNING) | 1112 if (running_status() != EmbeddedWorkerStatus::RUNNING) |
| 1105 return; | 1113 return; |
| 1106 | 1114 |
| 1107 embedded_worker_->SendMessage( | 1115 embedded_worker_->SendMessage( |
| 1108 ServiceWorkerMsg_FocusClientResponse(request_id, client_info)); | 1116 ServiceWorkerMsg_FocusClientResponse(request_id, client_info)); |
| 1109 } | 1117 } |
| 1110 | 1118 |
| 1111 void ServiceWorkerVersion::OnNavigateClient(int request_id, | 1119 void ServiceWorkerVersion::OnNavigateClient(int request_id, |
| 1112 const std::string& client_uuid, | 1120 const std::string& client_uuid, |
| 1113 const GURL& url) { | 1121 const GURL& url) { |
| 1114 if (!context_) | 1122 if (!context_) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1150 context_, base::Bind(&ServiceWorkerVersion::OnNavigateClientFinished, | 1158 context_, base::Bind(&ServiceWorkerVersion::OnNavigateClientFinished, |
| 1151 weak_factory_.GetWeakPtr(), request_id)); | 1159 weak_factory_.GetWeakPtr(), request_id)); |
| 1152 } | 1160 } |
| 1153 | 1161 |
| 1154 void ServiceWorkerVersion::OnNavigateClientFinished( | 1162 void ServiceWorkerVersion::OnNavigateClientFinished( |
| 1155 int request_id, | 1163 int request_id, |
| 1156 ServiceWorkerStatusCode status, | 1164 ServiceWorkerStatusCode status, |
| 1157 const ServiceWorkerClientInfo& client_info) { | 1165 const ServiceWorkerClientInfo& client_info) { |
| 1158 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1166 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1159 | 1167 |
| 1160 if (running_status() != RUNNING) | 1168 if (running_status() != EmbeddedWorkerStatus::RUNNING) |
| 1161 return; | 1169 return; |
| 1162 | 1170 |
| 1163 if (status != SERVICE_WORKER_OK) { | 1171 if (status != SERVICE_WORKER_OK) { |
| 1164 embedded_worker_->SendMessage( | 1172 embedded_worker_->SendMessage( |
| 1165 ServiceWorkerMsg_NavigateClientError(request_id, GURL())); | 1173 ServiceWorkerMsg_NavigateClientError(request_id, GURL())); |
| 1166 return; | 1174 return; |
| 1167 } | 1175 } |
| 1168 | 1176 |
| 1169 embedded_worker_->SendMessage( | 1177 embedded_worker_->SendMessage( |
| 1170 ServiceWorkerMsg_NavigateClientResponse(request_id, client_info)); | 1178 ServiceWorkerMsg_NavigateClientResponse(request_id, client_info)); |
| 1171 } | 1179 } |
| 1172 | 1180 |
| 1173 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { | 1181 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { |
| 1174 skip_waiting_ = true; | 1182 skip_waiting_ = true; |
| 1175 if (status_ != INSTALLED) | 1183 if (status_ != INSTALLED) |
| 1176 return DidSkipWaiting(request_id); | 1184 return DidSkipWaiting(request_id); |
| 1177 | 1185 |
| 1178 if (!context_) | 1186 if (!context_) |
| 1179 return; | 1187 return; |
| 1180 ServiceWorkerRegistration* registration = | 1188 ServiceWorkerRegistration* registration = |
| 1181 context_->GetLiveRegistration(registration_id_); | 1189 context_->GetLiveRegistration(registration_id_); |
| 1182 if (!registration) | 1190 if (!registration) |
| 1183 return; | 1191 return; |
| 1184 pending_skip_waiting_requests_.push_back(request_id); | 1192 pending_skip_waiting_requests_.push_back(request_id); |
| 1185 if (pending_skip_waiting_requests_.size() == 1) | 1193 if (pending_skip_waiting_requests_.size() == 1) |
| 1186 registration->ActivateWaitingVersionWhenReady(); | 1194 registration->ActivateWaitingVersionWhenReady(); |
| 1187 } | 1195 } |
| 1188 | 1196 |
| 1189 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { | 1197 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { |
| 1190 if (running_status() == STARTING || running_status() == RUNNING) | 1198 if (running_status() == EmbeddedWorkerStatus::STARTING || |
| 1199 running_status() == EmbeddedWorkerStatus::RUNNING) { |
| 1191 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); | 1200 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); |
| 1201 } |
| 1192 } | 1202 } |
| 1193 | 1203 |
| 1194 void ServiceWorkerVersion::OnClaimClients(int request_id) { | 1204 void ServiceWorkerVersion::OnClaimClients(int request_id) { |
| 1195 if (status_ != ACTIVATING && status_ != ACTIVATED) { | 1205 if (status_ != ACTIVATING && status_ != ACTIVATED) { |
| 1196 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( | 1206 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
| 1197 request_id, blink::WebServiceWorkerError::ErrorTypeState, | 1207 request_id, blink::WebServiceWorkerError::ErrorTypeState, |
| 1198 base::ASCIIToUTF16(kClaimClientsStateErrorMesage))); | 1208 base::ASCIIToUTF16(kClaimClientsStateErrorMesage))); |
| 1199 return; | 1209 return; |
| 1200 } | 1210 } |
| 1201 if (context_) { | 1211 if (context_) { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1280 RecordStartWorkerResult(purpose, prestart_status, kInvalidTraceId, | 1290 RecordStartWorkerResult(purpose, prestart_status, kInvalidTraceId, |
| 1281 is_browser_startup_complete, | 1291 is_browser_startup_complete, |
| 1282 SERVICE_WORKER_ERROR_REDUNDANT); | 1292 SERVICE_WORKER_ERROR_REDUNDANT); |
| 1283 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_REDUNDANT)); | 1293 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_REDUNDANT)); |
| 1284 return; | 1294 return; |
| 1285 } | 1295 } |
| 1286 | 1296 |
| 1287 MarkIfStale(); | 1297 MarkIfStale(); |
| 1288 | 1298 |
| 1289 switch (running_status()) { | 1299 switch (running_status()) { |
| 1290 case RUNNING: | 1300 case EmbeddedWorkerStatus::RUNNING: |
| 1291 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); | 1301 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); |
| 1292 return; | 1302 return; |
| 1293 case STARTING: | 1303 case EmbeddedWorkerStatus::STARTING: |
| 1294 DCHECK(!start_callbacks_.empty()); | 1304 DCHECK(!start_callbacks_.empty()); |
| 1295 break; | 1305 break; |
| 1296 case STOPPING: | 1306 case EmbeddedWorkerStatus::STOPPING: |
| 1297 case STOPPED: | 1307 case EmbeddedWorkerStatus::STOPPED: |
| 1298 if (start_callbacks_.empty()) { | 1308 if (start_callbacks_.empty()) { |
| 1299 int trace_id = NextTraceId(); | 1309 int trace_id = NextTraceId(); |
| 1300 TRACE_EVENT_ASYNC_BEGIN2( | 1310 TRACE_EVENT_ASYNC_BEGIN2( |
| 1301 "ServiceWorker", "ServiceWorkerVersion::StartWorker", trace_id, | 1311 "ServiceWorker", "ServiceWorkerVersion::StartWorker", trace_id, |
| 1302 "Script", script_url_.spec(), "Purpose", | 1312 "Script", script_url_.spec(), "Purpose", |
| 1303 ServiceWorkerMetrics::EventTypeToString(purpose)); | 1313 ServiceWorkerMetrics::EventTypeToString(purpose)); |
| 1304 start_callbacks_.push_back( | 1314 start_callbacks_.push_back( |
| 1305 base::Bind(&ServiceWorkerVersion::RecordStartWorkerResult, | 1315 base::Bind(&ServiceWorkerVersion::RecordStartWorkerResult, |
| 1306 weak_factory_.GetWeakPtr(), purpose, prestart_status, | 1316 weak_factory_.GetWeakPtr(), purpose, prestart_status, |
| 1307 trace_id, is_browser_startup_complete)); | 1317 trace_id, is_browser_startup_complete)); |
| 1308 } | 1318 } |
| 1309 break; | 1319 break; |
| 1310 } | 1320 } |
| 1311 | 1321 |
| 1312 // Keep the live registration while starting the worker. | 1322 // Keep the live registration while starting the worker. |
| 1313 start_callbacks_.push_back( | 1323 start_callbacks_.push_back( |
| 1314 base::Bind(&RunStartWorkerCallback, callback, protect)); | 1324 base::Bind(&RunStartWorkerCallback, callback, protect)); |
| 1315 | 1325 |
| 1316 if (running_status() == STOPPED) | 1326 if (running_status() == EmbeddedWorkerStatus::STOPPED) |
| 1317 StartWorkerInternal(); | 1327 StartWorkerInternal(); |
| 1318 DCHECK(timeout_timer_.IsRunning()); | 1328 DCHECK(timeout_timer_.IsRunning()); |
| 1319 } | 1329 } |
| 1320 | 1330 |
| 1321 void ServiceWorkerVersion::StartWorkerInternal() { | 1331 void ServiceWorkerVersion::StartWorkerInternal() { |
| 1322 DCHECK_EQ(STOPPED, running_status()); | 1332 DCHECK_EQ(EmbeddedWorkerStatus::STOPPED, running_status()); |
| 1323 | 1333 |
| 1324 DCHECK(!metrics_); | 1334 DCHECK(!metrics_); |
| 1325 metrics_.reset(new Metrics(this)); | 1335 metrics_.reset(new Metrics(this)); |
| 1326 | 1336 |
| 1327 StartTimeoutTimer(); | 1337 StartTimeoutTimer(); |
| 1328 | 1338 |
| 1329 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 1339 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 1330 new EmbeddedWorkerMsg_StartWorker_Params()); | 1340 new EmbeddedWorkerMsg_StartWorker_Params()); |
| 1331 params->service_worker_version_id = version_id_; | 1341 params->service_worker_version_id = version_id_; |
| 1332 params->scope = scope_; | 1342 params->scope = scope_; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1378 void ServiceWorkerVersion::SetTimeoutTimerInterval(base::TimeDelta interval) { | 1388 void ServiceWorkerVersion::SetTimeoutTimerInterval(base::TimeDelta interval) { |
| 1379 DCHECK(timeout_timer_.IsRunning()); | 1389 DCHECK(timeout_timer_.IsRunning()); |
| 1380 if (timeout_timer_.GetCurrentDelay() != interval) { | 1390 if (timeout_timer_.GetCurrentDelay() != interval) { |
| 1381 timeout_timer_.Stop(); | 1391 timeout_timer_.Stop(); |
| 1382 timeout_timer_.Start(FROM_HERE, interval, this, | 1392 timeout_timer_.Start(FROM_HERE, interval, this, |
| 1383 &ServiceWorkerVersion::OnTimeoutTimer); | 1393 &ServiceWorkerVersion::OnTimeoutTimer); |
| 1384 } | 1394 } |
| 1385 } | 1395 } |
| 1386 | 1396 |
| 1387 void ServiceWorkerVersion::OnTimeoutTimer() { | 1397 void ServiceWorkerVersion::OnTimeoutTimer() { |
| 1388 DCHECK(running_status() == STARTING || running_status() == RUNNING || | 1398 DCHECK(running_status() == EmbeddedWorkerStatus::STARTING || |
| 1389 running_status() == STOPPING) | 1399 running_status() == EmbeddedWorkerStatus::RUNNING || |
| 1390 << running_status(); | 1400 running_status() == EmbeddedWorkerStatus::STOPPING) |
| 1401 << static_cast<int>(running_status()); |
| 1391 | 1402 |
| 1392 if (!context_) | 1403 if (!context_) |
| 1393 return; | 1404 return; |
| 1394 | 1405 |
| 1395 MarkIfStale(); | 1406 MarkIfStale(); |
| 1396 | 1407 |
| 1397 // Stopping the worker hasn't finished within a certain period. | 1408 // Stopping the worker hasn't finished within a certain period. |
| 1398 if (GetTickDuration(stop_time_) > | 1409 if (GetTickDuration(stop_time_) > |
| 1399 base::TimeDelta::FromSeconds(kStopWorkerTimeoutSeconds)) { | 1410 base::TimeDelta::FromSeconds(kStopWorkerTimeoutSeconds)) { |
| 1400 DCHECK_EQ(STOPPING, running_status()); | 1411 DCHECK_EQ(EmbeddedWorkerStatus::STOPPING, running_status()); |
| 1401 if (IsInstalled(status())) { | 1412 if (IsInstalled(status())) { |
| 1402 ServiceWorkerMetrics::RecordWorkerStopped( | 1413 ServiceWorkerMetrics::RecordWorkerStopped( |
| 1403 ServiceWorkerMetrics::StopStatus::TIMEOUT); | 1414 ServiceWorkerMetrics::StopStatus::TIMEOUT); |
| 1404 } | 1415 } |
| 1405 ReportError(SERVICE_WORKER_ERROR_TIMEOUT, "DETACH_STALLED_IN_STOPPING"); | 1416 ReportError(SERVICE_WORKER_ERROR_TIMEOUT, "DETACH_STALLED_IN_STOPPING"); |
| 1406 | 1417 |
| 1407 // Detach the worker. Remove |this| as a listener first; otherwise | 1418 // Detach the worker. Remove |this| as a listener first; otherwise |
| 1408 // OnStoppedInternal might try to restart before the new worker | 1419 // OnStoppedInternal might try to restart before the new worker |
| 1409 // is created. | 1420 // is created. |
| 1410 embedded_worker_->RemoveListener(this); | 1421 embedded_worker_->RemoveListener(this); |
| 1411 embedded_worker_->Detach(); | 1422 embedded_worker_->Detach(); |
| 1412 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); | 1423 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); |
| 1413 embedded_worker_->AddListener(this); | 1424 embedded_worker_->AddListener(this); |
| 1414 | 1425 |
| 1415 // Call OnStoppedInternal to fail callbacks and possibly restart. | 1426 // Call OnStoppedInternal to fail callbacks and possibly restart. |
| 1416 OnStoppedInternal(EmbeddedWorkerInstance::STOPPING); | 1427 OnStoppedInternal(EmbeddedWorkerStatus::STOPPING); |
| 1417 return; | 1428 return; |
| 1418 } | 1429 } |
| 1419 | 1430 |
| 1420 // Trigger update if worker is stale and we waited long enough for it to go | 1431 // Trigger update if worker is stale and we waited long enough for it to go |
| 1421 // idle. | 1432 // idle. |
| 1422 if (GetTickDuration(stale_time_) > | 1433 if (GetTickDuration(stale_time_) > |
| 1423 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes)) { | 1434 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes)) { |
| 1424 ClearTick(&stale_time_); | 1435 ClearTick(&stale_time_); |
| 1425 if (!update_timer_.IsRunning()) | 1436 if (!update_timer_.IsRunning()) |
| 1426 ScheduleUpdate(); | 1437 ScheduleUpdate(); |
| 1427 } | 1438 } |
| 1428 | 1439 |
| 1429 // Starting a worker hasn't finished within a certain period. | 1440 // Starting a worker hasn't finished within a certain period. |
| 1430 const base::TimeDelta start_limit = | 1441 const base::TimeDelta start_limit = |
| 1431 IsInstalled(status()) | 1442 IsInstalled(status()) |
| 1432 ? base::TimeDelta::FromSeconds(kStartInstalledWorkerTimeoutSeconds) | 1443 ? base::TimeDelta::FromSeconds(kStartInstalledWorkerTimeoutSeconds) |
| 1433 : base::TimeDelta::FromMinutes(kStartNewWorkerTimeoutMinutes); | 1444 : base::TimeDelta::FromMinutes(kStartNewWorkerTimeoutMinutes); |
| 1434 if (GetTickDuration(start_time_) > start_limit) { | 1445 if (GetTickDuration(start_time_) > start_limit) { |
| 1435 DCHECK(running_status() == STARTING || running_status() == STOPPING) | 1446 DCHECK(running_status() == EmbeddedWorkerStatus::STARTING || |
| 1436 << running_status(); | 1447 running_status() == EmbeddedWorkerStatus::STOPPING) |
| 1448 << static_cast<int>(running_status()); |
| 1437 scoped_refptr<ServiceWorkerVersion> protect(this); | 1449 scoped_refptr<ServiceWorkerVersion> protect(this); |
| 1438 RunCallbacks(this, &start_callbacks_, SERVICE_WORKER_ERROR_TIMEOUT); | 1450 RunCallbacks(this, &start_callbacks_, SERVICE_WORKER_ERROR_TIMEOUT); |
| 1439 if (running_status() == STARTING) | 1451 if (running_status() == EmbeddedWorkerStatus::STARTING) |
| 1440 embedded_worker_->Stop(); | 1452 embedded_worker_->Stop(); |
| 1441 return; | 1453 return; |
| 1442 } | 1454 } |
| 1443 | 1455 |
| 1444 // Requests have not finished before their expiration. | 1456 // Requests have not finished before their expiration. |
| 1445 bool stop_for_timeout = false; | 1457 bool stop_for_timeout = false; |
| 1446 while (!requests_.empty()) { | 1458 while (!requests_.empty()) { |
| 1447 RequestInfo info = requests_.top(); | 1459 RequestInfo info = requests_.top(); |
| 1448 if (!RequestExpired(info.expiration)) | 1460 if (!RequestExpired(info.expiration)) |
| 1449 break; | 1461 break; |
| 1450 if (MaybeTimeOutRequest(info)) { | 1462 if (MaybeTimeOutRequest(info)) { |
| 1451 stop_for_timeout = | 1463 stop_for_timeout = |
| 1452 stop_for_timeout || info.timeout_behavior == KILL_ON_TIMEOUT; | 1464 stop_for_timeout || info.timeout_behavior == KILL_ON_TIMEOUT; |
| 1453 ServiceWorkerMetrics::RecordEventTimeout(info.event_type); | 1465 ServiceWorkerMetrics::RecordEventTimeout(info.event_type); |
| 1454 } | 1466 } |
| 1455 requests_.pop(); | 1467 requests_.pop(); |
| 1456 } | 1468 } |
| 1457 if (stop_for_timeout && running_status() != STOPPING) | 1469 if (stop_for_timeout && running_status() != EmbeddedWorkerStatus::STOPPING) |
| 1458 embedded_worker_->Stop(); | 1470 embedded_worker_->Stop(); |
| 1459 | 1471 |
| 1460 // For the timeouts below, there are no callbacks to timeout so there is | 1472 // For the timeouts below, there are no callbacks to timeout so there is |
| 1461 // nothing more to do if the worker is already stopping. | 1473 // nothing more to do if the worker is already stopping. |
| 1462 if (running_status() == STOPPING) | 1474 if (running_status() == EmbeddedWorkerStatus::STOPPING) |
| 1463 return; | 1475 return; |
| 1464 | 1476 |
| 1465 // The worker has been idle for longer than a certain period. | 1477 // The worker has been idle for longer than a certain period. |
| 1466 if (GetTickDuration(idle_time_) > | 1478 if (GetTickDuration(idle_time_) > |
| 1467 base::TimeDelta::FromSeconds(kIdleWorkerTimeoutSeconds)) { | 1479 base::TimeDelta::FromSeconds(kIdleWorkerTimeoutSeconds)) { |
| 1468 StopWorkerIfIdle(); | 1480 StopWorkerIfIdle(); |
| 1469 return; | 1481 return; |
| 1470 } | 1482 } |
| 1471 | 1483 |
| 1472 // Check ping status. | 1484 // Check ping status. |
| 1473 ping_controller_->CheckPingStatus(); | 1485 ping_controller_->CheckPingStatus(); |
| 1474 } | 1486 } |
| 1475 | 1487 |
| 1476 ServiceWorkerStatusCode ServiceWorkerVersion::PingWorker() { | 1488 ServiceWorkerStatusCode ServiceWorkerVersion::PingWorker() { |
| 1477 DCHECK(running_status() == STARTING || running_status() == RUNNING); | 1489 DCHECK(running_status() == EmbeddedWorkerStatus::STARTING || |
| 1490 running_status() == EmbeddedWorkerStatus::RUNNING); |
| 1478 return embedded_worker_->SendMessage(ServiceWorkerMsg_Ping()); | 1491 return embedded_worker_->SendMessage(ServiceWorkerMsg_Ping()); |
| 1479 } | 1492 } |
| 1480 | 1493 |
| 1481 void ServiceWorkerVersion::OnPingTimeout() { | 1494 void ServiceWorkerVersion::OnPingTimeout() { |
| 1482 DCHECK(running_status() == STARTING || running_status() == RUNNING); | 1495 DCHECK(running_status() == EmbeddedWorkerStatus::STARTING || |
| 1496 running_status() == EmbeddedWorkerStatus::RUNNING); |
| 1483 // TODO(falken): Show a message to the developer that the SW was stopped due | 1497 // TODO(falken): Show a message to the developer that the SW was stopped due |
| 1484 // to timeout (crbug.com/457968). Also, change the error code to | 1498 // to timeout (crbug.com/457968). Also, change the error code to |
| 1485 // SERVICE_WORKER_ERROR_TIMEOUT. | 1499 // SERVICE_WORKER_ERROR_TIMEOUT. |
| 1486 StopWorkerIfIdle(); | 1500 StopWorkerIfIdle(); |
| 1487 } | 1501 } |
| 1488 | 1502 |
| 1489 void ServiceWorkerVersion::StopWorkerIfIdle() { | 1503 void ServiceWorkerVersion::StopWorkerIfIdle() { |
| 1490 if (HasInflightRequests() && !ping_controller_->IsTimedOut()) | 1504 if (HasInflightRequests() && !ping_controller_->IsTimedOut()) |
| 1491 return; | 1505 return; |
| 1492 if (running_status() == STOPPED || running_status() == STOPPING || | 1506 if (running_status() == EmbeddedWorkerStatus::STOPPED || |
| 1507 running_status() == EmbeddedWorkerStatus::STOPPING || |
| 1493 !stop_callbacks_.empty()) { | 1508 !stop_callbacks_.empty()) { |
| 1494 return; | 1509 return; |
| 1495 } | 1510 } |
| 1496 | 1511 |
| 1497 embedded_worker_->StopIfIdle(); | 1512 embedded_worker_->StopIfIdle(); |
| 1498 } | 1513 } |
| 1499 | 1514 |
| 1500 bool ServiceWorkerVersion::HasInflightRequests() const { | 1515 bool ServiceWorkerVersion::HasInflightRequests() const { |
| 1501 return !custom_requests_.IsEmpty() || !streaming_url_request_jobs_.empty(); | 1516 return !custom_requests_.IsEmpty() || !streaming_url_request_jobs_.empty(); |
| 1502 } | 1517 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1527 GetTickDuration(start_time), IsInstalled(prestart_status), | 1542 GetTickDuration(start_time), IsInstalled(prestart_status), |
| 1528 ServiceWorkerMetrics::GetStartSituation( | 1543 ServiceWorkerMetrics::GetStartSituation( |
| 1529 is_browser_startup_complete, embedded_worker_->is_new_process()), | 1544 is_browser_startup_complete, embedded_worker_->is_new_process()), |
| 1530 purpose); | 1545 purpose); |
| 1531 } | 1546 } |
| 1532 | 1547 |
| 1533 if (status != SERVICE_WORKER_ERROR_TIMEOUT) | 1548 if (status != SERVICE_WORKER_ERROR_TIMEOUT) |
| 1534 return; | 1549 return; |
| 1535 EmbeddedWorkerInstance::StartingPhase phase = | 1550 EmbeddedWorkerInstance::StartingPhase phase = |
| 1536 EmbeddedWorkerInstance::NOT_STARTING; | 1551 EmbeddedWorkerInstance::NOT_STARTING; |
| 1537 EmbeddedWorkerInstance::Status running_status = embedded_worker_->status(); | 1552 EmbeddedWorkerStatus running_status = embedded_worker_->status(); |
| 1538 // Build an artifical JavaScript exception to show in the ServiceWorker | 1553 // Build an artifical JavaScript exception to show in the ServiceWorker |
| 1539 // log for developers; it's not user-facing so it's not a localized resource. | 1554 // log for developers; it's not user-facing so it's not a localized resource. |
| 1540 std::string message = "ServiceWorker startup timed out. "; | 1555 std::string message = "ServiceWorker startup timed out. "; |
| 1541 if (running_status != EmbeddedWorkerInstance::STARTING) { | 1556 if (running_status != EmbeddedWorkerStatus::STARTING) { |
| 1542 message.append("The worker had unexpected status: "); | 1557 message.append("The worker had unexpected status: "); |
| 1543 message.append(EmbeddedWorkerInstance::StatusToString(running_status)); | 1558 message.append(EmbeddedWorkerInstance::StatusToString(running_status)); |
| 1544 } else { | 1559 } else { |
| 1545 phase = embedded_worker_->starting_phase(); | 1560 phase = embedded_worker_->starting_phase(); |
| 1546 message.append("The worker was in startup phase: "); | 1561 message.append("The worker was in startup phase: "); |
| 1547 message.append(EmbeddedWorkerInstance::StartingPhaseToString(phase)); | 1562 message.append(EmbeddedWorkerInstance::StartingPhaseToString(phase)); |
| 1548 } | 1563 } |
| 1549 message.append("."); | 1564 message.append("."); |
| 1550 OnReportException(base::UTF8ToUTF16(message), -1, -1, GURL()); | 1565 OnReportException(base::UTF8ToUTF16(message), -1, -1, GURL()); |
| 1551 DVLOG(1) << message; | 1566 DVLOG(1) << message; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1630 context_->UnprotectVersion(version_id_); | 1645 context_->UnprotectVersion(version_id_); |
| 1631 is_update_scheduled_ = false; | 1646 is_update_scheduled_ = false; |
| 1632 } | 1647 } |
| 1633 | 1648 |
| 1634 if (status != SERVICE_WORKER_OK || registration->active_version() != this) | 1649 if (status != SERVICE_WORKER_OK || registration->active_version() != this) |
| 1635 return; | 1650 return; |
| 1636 context_->UpdateServiceWorker(registration.get(), | 1651 context_->UpdateServiceWorker(registration.get(), |
| 1637 false /* force_bypass_cache */); | 1652 false /* force_bypass_cache */); |
| 1638 } | 1653 } |
| 1639 | 1654 |
| 1640 void ServiceWorkerVersion::OnStoppedInternal( | 1655 void ServiceWorkerVersion::OnStoppedInternal(EmbeddedWorkerStatus old_status) { |
| 1641 EmbeddedWorkerInstance::Status old_status) { | 1656 DCHECK_EQ(EmbeddedWorkerStatus::STOPPED, running_status()); |
| 1642 DCHECK_EQ(STOPPED, running_status()); | |
| 1643 scoped_refptr<ServiceWorkerVersion> protect; | 1657 scoped_refptr<ServiceWorkerVersion> protect; |
| 1644 if (!in_dtor_) | 1658 if (!in_dtor_) |
| 1645 protect = this; | 1659 protect = this; |
| 1646 | 1660 |
| 1647 DCHECK(metrics_); | 1661 DCHECK(metrics_); |
| 1648 metrics_.reset(); | 1662 metrics_.reset(); |
| 1649 | 1663 |
| 1650 bool should_restart = !is_redundant() && !start_callbacks_.empty() && | 1664 bool should_restart = !is_redundant() && !start_callbacks_.empty() && |
| 1651 (old_status != EmbeddedWorkerInstance::STARTING) && | 1665 (old_status != EmbeddedWorkerStatus::STARTING) && |
| 1652 !in_dtor_ && !ping_controller_->IsTimedOut(); | 1666 !in_dtor_ && !ping_controller_->IsTimedOut(); |
| 1653 | 1667 |
| 1654 if (!stop_time_.is_null()) { | 1668 if (!stop_time_.is_null()) { |
| 1655 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::StopWorker", | 1669 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::StopWorker", |
| 1656 stop_time_.ToInternalValue(), "Restart", | 1670 stop_time_.ToInternalValue(), "Restart", |
| 1657 should_restart); | 1671 should_restart); |
| 1658 ClearTick(&stop_time_); | 1672 ClearTick(&stop_time_); |
| 1659 } | 1673 } |
| 1660 StopTimeoutTimer(); | 1674 StopTimeoutTimer(); |
| 1661 | 1675 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1695 StartWorkerInternal(); | 1709 StartWorkerInternal(); |
| 1696 } | 1710 } |
| 1697 | 1711 |
| 1698 void ServiceWorkerVersion::OnMojoConnectionError(const char* service_name) { | 1712 void ServiceWorkerVersion::OnMojoConnectionError(const char* service_name) { |
| 1699 // Simply deleting the service will cause error callbacks to be called from | 1713 // Simply deleting the service will cause error callbacks to be called from |
| 1700 // the destructor of the MojoServiceWrapper instance. | 1714 // the destructor of the MojoServiceWrapper instance. |
| 1701 mojo_services_.erase(service_name); | 1715 mojo_services_.erase(service_name); |
| 1702 } | 1716 } |
| 1703 | 1717 |
| 1704 void ServiceWorkerVersion::OnBeginEvent() { | 1718 void ServiceWorkerVersion::OnBeginEvent() { |
| 1705 if (should_exclude_from_uma_ || running_status() != RUNNING || | 1719 if (should_exclude_from_uma_ || |
| 1720 running_status() != EmbeddedWorkerStatus::RUNNING || |
| 1706 idle_time_.is_null()) { | 1721 idle_time_.is_null()) { |
| 1707 return; | 1722 return; |
| 1708 } | 1723 } |
| 1709 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - | 1724 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - |
| 1710 idle_time_); | 1725 idle_time_); |
| 1711 } | 1726 } |
| 1712 | 1727 |
| 1713 } // namespace content | 1728 } // namespace content |
| OLD | NEW |