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> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/guid.h" | 14 #include "base/guid.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
20 #include "base/strings/string16.h" | 20 #include "base/strings/string16.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "base/time/default_clock.h" |
23 #include "base/time/default_tick_clock.h" | 24 #include "base/time/default_tick_clock.h" |
24 #include "content/browser/bad_message.h" | 25 #include "content/browser/bad_message.h" |
25 #include "content/browser/child_process_security_policy_impl.h" | 26 #include "content/browser/child_process_security_policy_impl.h" |
26 #include "content/browser/service_worker/embedded_worker_registry.h" | 27 #include "content/browser/service_worker/embedded_worker_registry.h" |
27 #include "content/browser/service_worker/service_worker_client_utils.h" | 28 #include "content/browser/service_worker/service_worker_client_utils.h" |
28 #include "content/browser/service_worker/service_worker_context_core.h" | 29 #include "content/browser/service_worker/service_worker_context_core.h" |
29 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 30 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
30 #include "content/browser/service_worker/service_worker_installed_scripts_sender
.h" | 31 #include "content/browser/service_worker/service_worker_installed_scripts_sender
.h" |
31 #include "content/browser/service_worker/service_worker_registration.h" | 32 #include "content/browser/service_worker/service_worker_registration.h" |
32 #include "content/common/origin_trials/trial_token_validator.h" | 33 #include "content/common/origin_trials/trial_token_validator.h" |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 int64_t version_id, | 284 int64_t version_id, |
284 base::WeakPtr<ServiceWorkerContextCore> context) | 285 base::WeakPtr<ServiceWorkerContextCore> context) |
285 : version_id_(version_id), | 286 : version_id_(version_id), |
286 registration_id_(registration->id()), | 287 registration_id_(registration->id()), |
287 script_url_(script_url), | 288 script_url_(script_url), |
288 scope_(registration->pattern()), | 289 scope_(registration->pattern()), |
289 fetch_handler_existence_(FetchHandlerExistence::UNKNOWN), | 290 fetch_handler_existence_(FetchHandlerExistence::UNKNOWN), |
290 site_for_uma_(ServiceWorkerMetrics::SiteFromURL(scope_)), | 291 site_for_uma_(ServiceWorkerMetrics::SiteFromURL(scope_)), |
291 context_(context), | 292 context_(context), |
292 script_cache_map_(this, context), | 293 script_cache_map_(this, context), |
293 tick_clock_(base::WrapUnique(new base::DefaultTickClock)), | 294 tick_clock_(base::MakeUnique<base::DefaultTickClock>()), |
| 295 clock_(base::MakeUnique<base::DefaultClock>()), |
294 ping_controller_(new PingController(this)), | 296 ping_controller_(new PingController(this)), |
295 weak_factory_(this) { | 297 weak_factory_(this) { |
296 DCHECK_NE(kInvalidServiceWorkerVersionId, version_id); | 298 DCHECK_NE(kInvalidServiceWorkerVersionId, version_id); |
297 DCHECK(context_); | 299 DCHECK(context_); |
298 DCHECK(registration); | 300 DCHECK(registration); |
299 DCHECK(script_url_.is_valid()); | 301 DCHECK(script_url_.is_valid()); |
300 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); | 302 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); |
301 embedded_worker_->AddListener(this); | 303 embedded_worker_->AddListener(this); |
302 context_->AddLiveVersion(this); | 304 context_->AddLiveVersion(this); |
303 } | 305 } |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status()) | 547 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status()) |
546 << "Can only start a request with a running worker."; | 548 << "Can only start a request with a running worker."; |
547 DCHECK(event_type == ServiceWorkerMetrics::EventType::INSTALL || | 549 DCHECK(event_type == ServiceWorkerMetrics::EventType::INSTALL || |
548 event_type == ServiceWorkerMetrics::EventType::ACTIVATE || | 550 event_type == ServiceWorkerMetrics::EventType::ACTIVATE || |
549 event_type == ServiceWorkerMetrics::EventType::MESSAGE || | 551 event_type == ServiceWorkerMetrics::EventType::MESSAGE || |
550 status() == ACTIVATED) | 552 status() == ACTIVATED) |
551 << "Event of type " << static_cast<int>(event_type) | 553 << "Event of type " << static_cast<int>(event_type) |
552 << " can only be dispatched to an active worker: " << status(); | 554 << " can only be dispatched to an active worker: " << status(); |
553 | 555 |
554 int request_id = pending_requests_.Add(base::MakeUnique<PendingRequest>( | 556 int request_id = pending_requests_.Add(base::MakeUnique<PendingRequest>( |
555 error_callback, base::Time::Now(), tick_clock_->NowTicks(), event_type)); | 557 error_callback, clock_->Now(), tick_clock_->NowTicks(), event_type)); |
556 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", "ServiceWorkerVersion::Request", | 558 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", "ServiceWorkerVersion::Request", |
557 pending_requests_.Lookup(request_id), "Request id", | 559 pending_requests_.Lookup(request_id), "Request id", |
558 request_id, "Event type", | 560 request_id, "Event type", |
559 ServiceWorkerMetrics::EventTypeToString(event_type)); | 561 ServiceWorkerMetrics::EventTypeToString(event_type)); |
560 base::TimeTicks expiration_time = tick_clock_->NowTicks() + timeout; | 562 base::TimeTicks expiration_time = tick_clock_->NowTicks() + timeout; |
561 timeout_queue_.push( | 563 timeout_queue_.push( |
562 RequestInfo(request_id, event_type, expiration_time, timeout_behavior)); | 564 RequestInfo(request_id, event_type, expiration_time, timeout_behavior)); |
563 if (expiration_time > max_request_expiration_time_) | 565 if (expiration_time > max_request_expiration_time_) |
564 max_request_expiration_time_ = expiration_time; | 566 max_request_expiration_time_ = expiration_time; |
565 return request_id; | 567 return request_id; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 // It's possible that the renderer is lying or the version started stopping | 617 // It's possible that the renderer is lying or the version started stopping |
616 // right around the time of the IPC. | 618 // right around the time of the IPC. |
617 if (running_status() != EmbeddedWorkerStatus::RUNNING) | 619 if (running_status() != EmbeddedWorkerStatus::RUNNING) |
618 return false; | 620 return false; |
619 | 621 |
620 RequestUUIDToRequestIDMap::iterator iter = | 622 RequestUUIDToRequestIDMap::iterator iter = |
621 external_request_uuid_to_request_id_.find(request_uuid); | 623 external_request_uuid_to_request_id_.find(request_uuid); |
622 if (iter != external_request_uuid_to_request_id_.end()) { | 624 if (iter != external_request_uuid_to_request_id_.end()) { |
623 int request_id = iter->second; | 625 int request_id = iter->second; |
624 external_request_uuid_to_request_id_.erase(iter); | 626 external_request_uuid_to_request_id_.erase(iter); |
625 return FinishRequest(request_id, true, base::Time::Now()); | 627 return FinishRequest(request_id, true, clock_->Now()); |
626 } | 628 } |
627 | 629 |
628 // It is possible that the request was cancelled or timed out before and we | 630 // It is possible that the request was cancelled or timed out before and we |
629 // won't find it in |external_request_uuid_to_request_id_|. | 631 // won't find it in |external_request_uuid_to_request_id_|. |
630 // Return true so we don't kill the process. | 632 // Return true so we don't kill the process. |
631 return true; | 633 return true; |
632 } | 634 } |
633 | 635 |
634 ServiceWorkerVersion::SimpleEventCallback | 636 ServiceWorkerVersion::SimpleEventCallback |
635 ServiceWorkerVersion::CreateSimpleEventCallback(int request_id) { | 637 ServiceWorkerVersion::CreateSimpleEventCallback(int request_id) { |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
736 } | 738 } |
737 if (!context_) | 739 if (!context_) |
738 return; | 740 return; |
739 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | 741 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
740 script_cache_map_.GetResources(&resources); | 742 script_cache_map_.GetResources(&resources); |
741 context_->storage()->PurgeResources(resources); | 743 context_->storage()->PurgeResources(resources); |
742 } | 744 } |
743 | 745 |
744 void ServiceWorkerVersion::SetValidOriginTrialTokens( | 746 void ServiceWorkerVersion::SetValidOriginTrialTokens( |
745 const TrialTokenValidator::FeatureToTokensMap& tokens) { | 747 const TrialTokenValidator::FeatureToTokensMap& tokens) { |
746 origin_trial_tokens_ = | 748 origin_trial_tokens_ = TrialTokenValidator::GetValidTokens( |
747 TrialTokenValidator::GetValidTokens(url::Origin(scope()), tokens); | 749 url::Origin(scope()), tokens, clock_->Now()); |
748 } | 750 } |
749 | 751 |
750 void ServiceWorkerVersion::SetDevToolsAttached(bool attached) { | 752 void ServiceWorkerVersion::SetDevToolsAttached(bool attached) { |
751 embedded_worker()->SetDevToolsAttached(attached); | 753 embedded_worker()->SetDevToolsAttached(attached); |
752 | 754 |
753 if (stop_when_devtools_detached_ && !attached) { | 755 if (stop_when_devtools_detached_ && !attached) { |
754 DCHECK_EQ(REDUNDANT, status()); | 756 DCHECK_EQ(REDUNDANT, status()); |
755 if (running_status() == EmbeddedWorkerStatus::STARTING || | 757 if (running_status() == EmbeddedWorkerStatus::STARTING || |
756 running_status() == EmbeddedWorkerStatus::RUNNING) { | 758 running_status() == EmbeddedWorkerStatus::RUNNING) { |
757 embedded_worker_->Stop(); | 759 embedded_worker_->Stop(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 main_script_http_info_.reset(new net::HttpResponseInfo(http_info)); | 791 main_script_http_info_.reset(new net::HttpResponseInfo(http_info)); |
790 | 792 |
791 // Updates |origin_trial_tokens_| if it is not set yet. This happens when: | 793 // Updates |origin_trial_tokens_| if it is not set yet. This happens when: |
792 // 1) The worker is a new one. | 794 // 1) The worker is a new one. |
793 // OR | 795 // OR |
794 // 2) The worker is an existing one but the entry in ServiceWorkerDatabase | 796 // 2) The worker is an existing one but the entry in ServiceWorkerDatabase |
795 // was written by old version Chrome (< M56), so |origin_trial_tokens| | 797 // was written by old version Chrome (< M56), so |origin_trial_tokens| |
796 // wasn't set in the entry. | 798 // wasn't set in the entry. |
797 if (!origin_trial_tokens_) { | 799 if (!origin_trial_tokens_) { |
798 origin_trial_tokens_ = TrialTokenValidator::GetValidTokensFromHeaders( | 800 origin_trial_tokens_ = TrialTokenValidator::GetValidTokensFromHeaders( |
799 url::Origin(scope()), http_info.headers.get()); | 801 url::Origin(scope()), http_info.headers.get(), clock_->Now()); |
800 } | 802 } |
801 | 803 |
802 for (auto& observer : listeners_) | 804 for (auto& observer : listeners_) |
803 observer.OnMainScriptHttpResponseInfoSet(this); | 805 observer.OnMainScriptHttpResponseInfoSet(this); |
804 } | 806 } |
805 | 807 |
806 void ServiceWorkerVersion::SimulatePingTimeoutForTesting() { | 808 void ServiceWorkerVersion::SimulatePingTimeoutForTesting() { |
807 ping_controller_->SimulateTimeoutForTesting(); | 809 ping_controller_->SimulateTimeoutForTesting(); |
808 } | 810 } |
809 | 811 |
810 void ServiceWorkerVersion::SetTickClockForTesting( | 812 void ServiceWorkerVersion::SetTickClockForTesting( |
811 std::unique_ptr<base::TickClock> tick_clock) { | 813 std::unique_ptr<base::TickClock> tick_clock) { |
812 tick_clock_ = std::move(tick_clock); | 814 tick_clock_ = std::move(tick_clock); |
813 } | 815 } |
814 | 816 |
| 817 void ServiceWorkerVersion::SetClockForTesting( |
| 818 std::unique_ptr<base::Clock> clock) { |
| 819 clock_ = std::move(clock); |
| 820 } |
| 821 |
815 const net::HttpResponseInfo* | 822 const net::HttpResponseInfo* |
816 ServiceWorkerVersion::GetMainScriptHttpResponseInfo() { | 823 ServiceWorkerVersion::GetMainScriptHttpResponseInfo() { |
817 return main_script_http_info_.get(); | 824 return main_script_http_info_.get(); |
818 } | 825 } |
819 | 826 |
820 ServiceWorkerVersion::RequestInfo::RequestInfo( | 827 ServiceWorkerVersion::RequestInfo::RequestInfo( |
821 int id, | 828 int id, |
822 ServiceWorkerMetrics::EventType event_type, | 829 ServiceWorkerMetrics::EventType event_type, |
823 const base::TimeTicks& expiration, | 830 const base::TimeTicks& expiration, |
824 TimeoutBehavior timeout_behavior) | 831 TimeoutBehavior timeout_behavior) |
(...skipping 967 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1792 void ServiceWorkerVersion::MarkIfStale() { | 1799 void ServiceWorkerVersion::MarkIfStale() { |
1793 if (!context_) | 1800 if (!context_) |
1794 return; | 1801 return; |
1795 if (update_timer_.IsRunning() || !stale_time_.is_null()) | 1802 if (update_timer_.IsRunning() || !stale_time_.is_null()) |
1796 return; | 1803 return; |
1797 ServiceWorkerRegistration* registration = | 1804 ServiceWorkerRegistration* registration = |
1798 context_->GetLiveRegistration(registration_id_); | 1805 context_->GetLiveRegistration(registration_id_); |
1799 if (!registration || registration->active_version() != this) | 1806 if (!registration || registration->active_version() != this) |
1800 return; | 1807 return; |
1801 base::TimeDelta time_since_last_check = | 1808 base::TimeDelta time_since_last_check = |
1802 base::Time::Now() - registration->last_update_check(); | 1809 clock_->Now() - registration->last_update_check(); |
1803 if (time_since_last_check > kServiceWorkerScriptMaxCacheAge) | 1810 if (time_since_last_check > kServiceWorkerScriptMaxCacheAge) |
1804 RestartTick(&stale_time_); | 1811 RestartTick(&stale_time_); |
1805 } | 1812 } |
1806 | 1813 |
1807 void ServiceWorkerVersion::FoundRegistrationForUpdate( | 1814 void ServiceWorkerVersion::FoundRegistrationForUpdate( |
1808 ServiceWorkerStatusCode status, | 1815 ServiceWorkerStatusCode status, |
1809 scoped_refptr<ServiceWorkerRegistration> registration) { | 1816 scoped_refptr<ServiceWorkerRegistration> registration) { |
1810 if (!context_) | 1817 if (!context_) |
1811 return; | 1818 return; |
1812 | 1819 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1898 | 1905 |
1899 void ServiceWorkerVersion::CleanUpExternalRequest( | 1906 void ServiceWorkerVersion::CleanUpExternalRequest( |
1900 const std::string& request_uuid, | 1907 const std::string& request_uuid, |
1901 ServiceWorkerStatusCode status) { | 1908 ServiceWorkerStatusCode status) { |
1902 if (status == SERVICE_WORKER_OK) | 1909 if (status == SERVICE_WORKER_OK) |
1903 return; | 1910 return; |
1904 external_request_uuid_to_request_id_.erase(request_uuid); | 1911 external_request_uuid_to_request_id_.erase(request_uuid); |
1905 } | 1912 } |
1906 | 1913 |
1907 } // namespace content | 1914 } // namespace content |
OLD | NEW |