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