| 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 <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 CallbackArray callbacks; | 87 CallbackArray callbacks; |
| 88 callbacks.swap(*callbacks_ptr); | 88 callbacks.swap(*callbacks_ptr); |
| 89 for (const auto& callback : callbacks) | 89 for (const auto& callback : callbacks) |
| 90 callback.Run(arg); | 90 callback.Run(arg); |
| 91 } | 91 } |
| 92 | 92 |
| 93 template <typename IDMAP, typename... Params> | 93 template <typename IDMAP, typename... Params> |
| 94 void RunIDMapCallbacks(IDMAP* requests, const Params&... params) { | 94 void RunIDMapCallbacks(IDMAP* requests, const Params&... params) { |
| 95 typename IDMAP::iterator iter(requests); | 95 typename IDMAP::iterator iter(requests); |
| 96 while (!iter.IsAtEnd()) { | 96 while (!iter.IsAtEnd()) { |
| 97 TRACE_EVENT_ASYNC_END0("ServiceWorker", "ServiceWorkerVersion::Request", |
| 98 iter.GetCurrentValue()); |
| 97 iter.GetCurrentValue()->callback.Run(params...); | 99 iter.GetCurrentValue()->callback.Run(params...); |
| 98 iter.Advance(); | 100 iter.Advance(); |
| 99 } | 101 } |
| 100 requests->Clear(); | 102 requests->Clear(); |
| 101 } | 103 } |
| 102 | 104 |
| 103 template <typename CallbackType, typename... Params> | 105 template <typename CallbackType, typename... Params> |
| 104 bool RunIDMapCallback(IDMap<CallbackType, IDMapOwnPointer>* requests, | 106 bool RunIDMapCallback(IDMap<CallbackType, IDMapOwnPointer>* requests, |
| 105 int request_id, | 107 int request_id, |
| 106 const Params&... params) { | 108 const Params&... params) { |
| 107 CallbackType* request = requests->Lookup(request_id); | 109 CallbackType* request = requests->Lookup(request_id); |
| 108 if (!request) | 110 if (!request) |
| 109 return false; | 111 return false; |
| 110 | 112 |
| 113 TRACE_EVENT_ASYNC_END0("ServiceWorker", "ServiceWorkerVersion::Request", |
| 114 request); |
| 111 request->callback.Run(params...); | 115 request->callback.Run(params...); |
| 112 requests->Remove(request_id); | 116 requests->Remove(request_id); |
| 113 return true; | 117 return true; |
| 114 } | 118 } |
| 115 | 119 |
| 116 void RunStartWorkerCallback( | 120 void RunStartWorkerCallback( |
| 117 const StatusCallback& callback, | 121 const StatusCallback& callback, |
| 118 scoped_refptr<ServiceWorkerRegistration> protect, | 122 scoped_refptr<ServiceWorkerRegistration> protect, |
| 119 ServiceWorkerStatusCode status) { | 123 ServiceWorkerStatusCode status) { |
| 120 callback.Run(status); | 124 callback.Run(status); |
| (...skipping 769 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 890 ServiceWorkerVersion* worker, | 894 ServiceWorkerVersion* worker, |
| 891 const char* service_name) | 895 const char* service_name) |
| 892 : worker_(worker), service_name_(service_name) {} | 896 : worker_(worker), service_name_(service_name) {} |
| 893 | 897 |
| 894 ServiceWorkerVersion::BaseMojoServiceWrapper::~BaseMojoServiceWrapper() { | 898 ServiceWorkerVersion::BaseMojoServiceWrapper::~BaseMojoServiceWrapper() { |
| 895 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer>::iterator iter( | 899 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer>::iterator iter( |
| 896 &worker_->custom_requests_); | 900 &worker_->custom_requests_); |
| 897 while (!iter.IsAtEnd()) { | 901 while (!iter.IsAtEnd()) { |
| 898 PendingRequest<StatusCallback>* request = iter.GetCurrentValue(); | 902 PendingRequest<StatusCallback>* request = iter.GetCurrentValue(); |
| 899 if (request->mojo_service == service_name_) { | 903 if (request->mojo_service == service_name_) { |
| 904 TRACE_EVENT_ASYNC_END0("ServiceWorker", "ServiceWorkerVersion::Request", |
| 905 request); |
| 900 request->callback.Run(SERVICE_WORKER_ERROR_FAILED); | 906 request->callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 901 worker_->custom_requests_.Remove(iter.GetCurrentKey()); | 907 worker_->custom_requests_.Remove(iter.GetCurrentKey()); |
| 902 } | 908 } |
| 903 iter.Advance(); | 909 iter.Advance(); |
| 904 } | 910 } |
| 905 } | 911 } |
| 906 | 912 |
| 907 void ServiceWorkerVersion::OnThreadStarted() { | 913 void ServiceWorkerVersion::OnThreadStarted() { |
| 908 if (running_status() == STOPPING) | 914 if (running_status() == STOPPING) |
| 909 return; | 915 return; |
| (...skipping 895 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1805 DVLOG(1) << message; | 1811 DVLOG(1) << message; |
| 1806 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StartWorker.TimeoutPhase", | 1812 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StartWorker.TimeoutPhase", |
| 1807 phase, | 1813 phase, |
| 1808 EmbeddedWorkerInstance::STARTING_PHASE_MAX_VALUE); | 1814 EmbeddedWorkerInstance::STARTING_PHASE_MAX_VALUE); |
| 1809 } | 1815 } |
| 1810 | 1816 |
| 1811 template <typename IDMAP> | 1817 template <typename IDMAP> |
| 1812 void ServiceWorkerVersion::RemoveCallbackAndStopIfRedundant(IDMAP* callbacks, | 1818 void ServiceWorkerVersion::RemoveCallbackAndStopIfRedundant(IDMAP* callbacks, |
| 1813 int request_id) { | 1819 int request_id) { |
| 1814 RestartTick(&idle_time_); | 1820 RestartTick(&idle_time_); |
| 1821 auto* request = callbacks->Lookup(request_id); |
| 1822 if (request) { |
| 1823 TRACE_EVENT_ASYNC_END0("ServiceWorker", "ServiceWorkerVersion::Request", |
| 1824 request); |
| 1825 } |
| 1815 callbacks->Remove(request_id); | 1826 callbacks->Remove(request_id); |
| 1816 if (is_redundant()) { | 1827 if (is_redundant()) { |
| 1817 // The stop should be already scheduled, but try to stop immediately, in | 1828 // The stop should be already scheduled, but try to stop immediately, in |
| 1818 // order to release worker resources soon. | 1829 // order to release worker resources soon. |
| 1819 StopWorkerIfIdle(); | 1830 StopWorkerIfIdle(); |
| 1820 } | 1831 } |
| 1821 } | 1832 } |
| 1822 | 1833 |
| 1823 template <typename CallbackType> | 1834 template <typename CallbackType> |
| 1824 int ServiceWorkerVersion::AddRequest( | 1835 int ServiceWorkerVersion::AddRequest( |
| 1825 const CallbackType& callback, | 1836 const CallbackType& callback, |
| 1826 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, | 1837 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, |
| 1827 RequestType request_type, | 1838 RequestType request_type, |
| 1828 ServiceWorkerMetrics::EventType event_type) { | 1839 ServiceWorkerMetrics::EventType event_type) { |
| 1829 base::TimeTicks expiration_time = | 1840 base::TimeTicks expiration_time = |
| 1830 base::TimeTicks::Now() + | 1841 base::TimeTicks::Now() + |
| 1831 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes); | 1842 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes); |
| 1832 return AddRequestWithExpiration(callback, callback_map, request_type, | 1843 return AddRequestWithExpiration(callback, callback_map, request_type, |
| 1833 event_type, expiration_time, KILL_ON_TIMEOUT); | 1844 event_type, expiration_time, KILL_ON_TIMEOUT); |
| 1834 } | 1845 } |
| 1835 | 1846 |
| 1836 template <typename CallbackType> | 1847 template <typename CallbackType> |
| 1837 int ServiceWorkerVersion::AddRequestWithExpiration( | 1848 int ServiceWorkerVersion::AddRequestWithExpiration( |
| 1838 const CallbackType& callback, | 1849 const CallbackType& callback, |
| 1839 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, | 1850 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, |
| 1840 RequestType request_type, | 1851 RequestType request_type, |
| 1841 ServiceWorkerMetrics::EventType event_type, | 1852 ServiceWorkerMetrics::EventType event_type, |
| 1842 base::TimeTicks expiration, | 1853 base::TimeTicks expiration, |
| 1843 TimeoutBehavior timeout_behavior) { | 1854 TimeoutBehavior timeout_behavior) { |
| 1844 int request_id = callback_map->Add(new PendingRequest<CallbackType>( | 1855 PendingRequest<CallbackType>* request = new PendingRequest<CallbackType>( |
| 1845 callback, base::TimeTicks::Now(), event_type)); | 1856 callback, base::TimeTicks::Now(), event_type); |
| 1857 int request_id = callback_map->Add(request); |
| 1858 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", "ServiceWorkerVersion::Request", |
| 1859 request, "Request id", request_id, "Event type", |
| 1860 ServiceWorkerMetrics::EventTypeToString(event_type)); |
| 1846 requests_.push(RequestInfo(request_id, request_type, event_type, expiration, | 1861 requests_.push(RequestInfo(request_id, request_type, event_type, expiration, |
| 1847 timeout_behavior)); | 1862 timeout_behavior)); |
| 1848 return request_id; | 1863 return request_id; |
| 1849 } | 1864 } |
| 1850 | 1865 |
| 1851 bool ServiceWorkerVersion::MaybeTimeOutRequest(const RequestInfo& info) { | 1866 bool ServiceWorkerVersion::MaybeTimeOutRequest(const RequestInfo& info) { |
| 1852 switch (info.type) { | 1867 switch (info.type) { |
| 1853 case REQUEST_ACTIVATE: | 1868 case REQUEST_ACTIVATE: |
| 1854 return RunIDMapCallback(&activate_requests_, info.id, | 1869 return RunIDMapCallback(&activate_requests_, info.id, |
| 1855 SERVICE_WORKER_ERROR_TIMEOUT); | 1870 SERVICE_WORKER_ERROR_TIMEOUT); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2011 void ServiceWorkerVersion::OnBeginEvent() { | 2026 void ServiceWorkerVersion::OnBeginEvent() { |
| 2012 if (should_exclude_from_uma_ || running_status() != RUNNING || | 2027 if (should_exclude_from_uma_ || running_status() != RUNNING || |
| 2013 idle_time_.is_null()) { | 2028 idle_time_.is_null()) { |
| 2014 return; | 2029 return; |
| 2015 } | 2030 } |
| 2016 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - | 2031 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - |
| 2017 idle_time_); | 2032 idle_time_); |
| 2018 } | 2033 } |
| 2019 | 2034 |
| 2020 } // namespace content | 2035 } // namespace content |
| OLD | NEW |