| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 CallbackArray callbacks; | 84 CallbackArray callbacks; |
| 85 callbacks.swap(*callbacks_ptr); | 85 callbacks.swap(*callbacks_ptr); |
| 86 for (const auto& callback : callbacks) | 86 for (const auto& callback : callbacks) |
| 87 callback.Run(arg); | 87 callback.Run(arg); |
| 88 } | 88 } |
| 89 | 89 |
| 90 template <typename IDMAP, typename... Params> | 90 template <typename IDMAP, typename... Params> |
| 91 void RunIDMapCallbacks(IDMAP* requests, const Params&... params) { | 91 void RunIDMapCallbacks(IDMAP* requests, const Params&... params) { |
| 92 typename IDMAP::iterator iter(requests); | 92 typename IDMAP::iterator iter(requests); |
| 93 while (!iter.IsAtEnd()) { | 93 while (!iter.IsAtEnd()) { |
| 94 TRACE_EVENT_ASYNC_END0("ServiceWorker", "ServiceWorkerVersion::Request", |
| 95 iter.GetCurrentValue()); |
| 94 iter.GetCurrentValue()->callback.Run(params...); | 96 iter.GetCurrentValue()->callback.Run(params...); |
| 95 iter.Advance(); | 97 iter.Advance(); |
| 96 } | 98 } |
| 97 requests->Clear(); | 99 requests->Clear(); |
| 98 } | 100 } |
| 99 | 101 |
| 100 template <typename CallbackType, typename... Params> | 102 template <typename CallbackType, typename... Params> |
| 101 bool RunIDMapCallback(IDMap<CallbackType, IDMapOwnPointer>* requests, | 103 bool RunIDMapCallback(IDMap<CallbackType, IDMapOwnPointer>* requests, |
| 102 int request_id, | 104 int request_id, |
| 103 const Params&... params) { | 105 const Params&... params) { |
| 104 CallbackType* request = requests->Lookup(request_id); | 106 CallbackType* request = requests->Lookup(request_id); |
| 105 if (!request) | 107 if (!request) |
| 106 return false; | 108 return false; |
| 107 | 109 |
| 110 TRACE_EVENT_ASYNC_END0("ServiceWorker", "ServiceWorkerVersion::Request", |
| 111 request); |
| 108 request->callback.Run(params...); | 112 request->callback.Run(params...); |
| 109 requests->Remove(request_id); | 113 requests->Remove(request_id); |
| 110 return true; | 114 return true; |
| 111 } | 115 } |
| 112 | 116 |
| 113 void RunStartWorkerCallback( | 117 void RunStartWorkerCallback( |
| 114 const StatusCallback& callback, | 118 const StatusCallback& callback, |
| 115 scoped_refptr<ServiceWorkerRegistration> protect, | 119 scoped_refptr<ServiceWorkerRegistration> protect, |
| 116 ServiceWorkerStatusCode status) { | 120 ServiceWorkerStatusCode status) { |
| 117 callback.Run(status); | 121 callback.Run(status); |
| (...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 ServiceWorkerVersion* worker, | 875 ServiceWorkerVersion* worker, |
| 872 const char* service_name) | 876 const char* service_name) |
| 873 : worker_(worker), service_name_(service_name) {} | 877 : worker_(worker), service_name_(service_name) {} |
| 874 | 878 |
| 875 ServiceWorkerVersion::BaseMojoServiceWrapper::~BaseMojoServiceWrapper() { | 879 ServiceWorkerVersion::BaseMojoServiceWrapper::~BaseMojoServiceWrapper() { |
| 876 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer>::iterator iter( | 880 IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer>::iterator iter( |
| 877 &worker_->custom_requests_); | 881 &worker_->custom_requests_); |
| 878 while (!iter.IsAtEnd()) { | 882 while (!iter.IsAtEnd()) { |
| 879 PendingRequest<StatusCallback>* request = iter.GetCurrentValue(); | 883 PendingRequest<StatusCallback>* request = iter.GetCurrentValue(); |
| 880 if (request->mojo_service == service_name_) { | 884 if (request->mojo_service == service_name_) { |
| 885 TRACE_EVENT_ASYNC_END0("ServiceWorker", "ServiceWorkerVersion::Request", |
| 886 request); |
| 881 request->callback.Run(SERVICE_WORKER_ERROR_FAILED); | 887 request->callback.Run(SERVICE_WORKER_ERROR_FAILED); |
| 882 worker_->custom_requests_.Remove(iter.GetCurrentKey()); | 888 worker_->custom_requests_.Remove(iter.GetCurrentKey()); |
| 883 } | 889 } |
| 884 iter.Advance(); | 890 iter.Advance(); |
| 885 } | 891 } |
| 886 } | 892 } |
| 887 | 893 |
| 888 void ServiceWorkerVersion::OnThreadStarted() { | 894 void ServiceWorkerVersion::OnThreadStarted() { |
| 889 if (running_status() == STOPPING) | 895 if (running_status() == STOPPING) |
| 890 return; | 896 return; |
| (...skipping 892 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1783 DVLOG(1) << message; | 1789 DVLOG(1) << message; |
| 1784 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StartWorker.TimeoutPhase", | 1790 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StartWorker.TimeoutPhase", |
| 1785 phase, | 1791 phase, |
| 1786 EmbeddedWorkerInstance::STARTING_PHASE_MAX_VALUE); | 1792 EmbeddedWorkerInstance::STARTING_PHASE_MAX_VALUE); |
| 1787 } | 1793 } |
| 1788 | 1794 |
| 1789 template <typename IDMAP> | 1795 template <typename IDMAP> |
| 1790 void ServiceWorkerVersion::RemoveCallbackAndStopIfRedundant(IDMAP* callbacks, | 1796 void ServiceWorkerVersion::RemoveCallbackAndStopIfRedundant(IDMAP* callbacks, |
| 1791 int request_id) { | 1797 int request_id) { |
| 1792 RestartTick(&idle_time_); | 1798 RestartTick(&idle_time_); |
| 1799 auto* request = callbacks->Lookup(request_id); |
| 1800 if (request) { |
| 1801 TRACE_EVENT_ASYNC_END0("ServiceWorker", "ServiceWorkerVersion::Request", |
| 1802 request); |
| 1803 } |
| 1793 callbacks->Remove(request_id); | 1804 callbacks->Remove(request_id); |
| 1794 if (is_redundant()) { | 1805 if (is_redundant()) { |
| 1795 // The stop should be already scheduled, but try to stop immediately, in | 1806 // The stop should be already scheduled, but try to stop immediately, in |
| 1796 // order to release worker resources soon. | 1807 // order to release worker resources soon. |
| 1797 StopWorkerIfIdle(); | 1808 StopWorkerIfIdle(); |
| 1798 } | 1809 } |
| 1799 } | 1810 } |
| 1800 | 1811 |
| 1801 template <typename CallbackType> | 1812 template <typename CallbackType> |
| 1802 int ServiceWorkerVersion::AddRequest( | 1813 int ServiceWorkerVersion::AddRequest( |
| 1803 const CallbackType& callback, | 1814 const CallbackType& callback, |
| 1804 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, | 1815 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, |
| 1805 RequestType request_type, | 1816 RequestType request_type, |
| 1806 ServiceWorkerMetrics::EventType event_type) { | 1817 ServiceWorkerMetrics::EventType event_type) { |
| 1807 base::TimeTicks expiration_time = | 1818 base::TimeTicks expiration_time = |
| 1808 base::TimeTicks::Now() + | 1819 base::TimeTicks::Now() + |
| 1809 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes); | 1820 base::TimeDelta::FromMinutes(kRequestTimeoutMinutes); |
| 1810 return AddRequestWithExpiration(callback, callback_map, request_type, | 1821 return AddRequestWithExpiration(callback, callback_map, request_type, |
| 1811 event_type, expiration_time, KILL_ON_TIMEOUT); | 1822 event_type, expiration_time, KILL_ON_TIMEOUT); |
| 1812 } | 1823 } |
| 1813 | 1824 |
| 1814 template <typename CallbackType> | 1825 template <typename CallbackType> |
| 1815 int ServiceWorkerVersion::AddRequestWithExpiration( | 1826 int ServiceWorkerVersion::AddRequestWithExpiration( |
| 1816 const CallbackType& callback, | 1827 const CallbackType& callback, |
| 1817 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, | 1828 IDMap<PendingRequest<CallbackType>, IDMapOwnPointer>* callback_map, |
| 1818 RequestType request_type, | 1829 RequestType request_type, |
| 1819 ServiceWorkerMetrics::EventType event_type, | 1830 ServiceWorkerMetrics::EventType event_type, |
| 1820 base::TimeTicks expiration, | 1831 base::TimeTicks expiration, |
| 1821 TimeoutBehavior timeout_behavior) { | 1832 TimeoutBehavior timeout_behavior) { |
| 1822 int request_id = callback_map->Add(new PendingRequest<CallbackType>( | 1833 PendingRequest<CallbackType>* request = new PendingRequest<CallbackType>( |
| 1823 callback, base::TimeTicks::Now(), event_type)); | 1834 callback, base::TimeTicks::Now(), event_type); |
| 1835 int request_id = callback_map->Add(request); |
| 1836 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", "ServiceWorkerVersion::Request", |
| 1837 request, "Request id", request_id, "Event type", |
| 1838 ServiceWorkerMetrics::EventTypeToString(event_type)); |
| 1824 requests_.push(RequestInfo(request_id, request_type, event_type, expiration, | 1839 requests_.push(RequestInfo(request_id, request_type, event_type, expiration, |
| 1825 timeout_behavior)); | 1840 timeout_behavior)); |
| 1826 return request_id; | 1841 return request_id; |
| 1827 } | 1842 } |
| 1828 | 1843 |
| 1829 bool ServiceWorkerVersion::MaybeTimeOutRequest(const RequestInfo& info) { | 1844 bool ServiceWorkerVersion::MaybeTimeOutRequest(const RequestInfo& info) { |
| 1830 switch (info.type) { | 1845 switch (info.type) { |
| 1831 case REQUEST_ACTIVATE: | 1846 case REQUEST_ACTIVATE: |
| 1832 return RunIDMapCallback(&activate_requests_, info.id, | 1847 return RunIDMapCallback(&activate_requests_, info.id, |
| 1833 SERVICE_WORKER_ERROR_TIMEOUT); | 1848 SERVICE_WORKER_ERROR_TIMEOUT); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1989 void ServiceWorkerVersion::OnBeginEvent() { | 2004 void ServiceWorkerVersion::OnBeginEvent() { |
| 1990 if (should_exclude_from_uma_ || running_status() != RUNNING || | 2005 if (should_exclude_from_uma_ || running_status() != RUNNING || |
| 1991 idle_time_.is_null()) { | 2006 idle_time_.is_null()) { |
| 1992 return; | 2007 return; |
| 1993 } | 2008 } |
| 1994 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - | 2009 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - |
| 1995 idle_time_); | 2010 idle_time_); |
| 1996 } | 2011 } |
| 1997 | 2012 |
| 1998 } // namespace content | 2013 } // namespace content |
| OLD | NEW |