1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_metrics.h"
7 #include "base/metrics/histogram_macros.h"
8 #include "base/metrics/sparse_histogram.h"
9 #include "base/strings/string_util.h"
10 #include "content/common/service_worker/service_worker_types.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/content_browser_client.h"
13 #include "content/public/common/content_client.h"
19 void RecordURLMetricOnUI(const GURL
& url
) {
20 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
21 GetContentClient()->browser()->RecordURLMetric(
22 "ServiceWorker.ControlledPageUrl", url
);
25 ServiceWorkerMetrics::Site
SiteFromURL(const GURL
& gurl
) {
26 // UIThreadSearchTermsData::GoogleBaseURLValue() returns the google base
27 // URL, but not available in content layer.
28 static const char google_like_scope_prefix
[] = "https://www.google.";
29 static const char ntp_scope_path
[] = "/_/chrome/";
30 if (base::StartsWith(gurl
.spec(), google_like_scope_prefix
,
31 base::CompareCase::INSENSITIVE_ASCII
) &&
32 base::StartsWith(gurl
.path(), ntp_scope_path
,
33 base::CompareCase::SENSITIVE
)) {
34 return ServiceWorkerMetrics::Site::NEW_TAB_PAGE
;
37 return ServiceWorkerMetrics::Site::OTHER
;
40 enum EventHandledRatioType
{
44 NUM_EVENT_HANDLED_RATIO_TYPE
,
49 bool ServiceWorkerMetrics::ShouldExcludeSiteFromHistogram(Site site
) {
50 return site
== ServiceWorkerMetrics::Site::NEW_TAB_PAGE
;
53 bool ServiceWorkerMetrics::ShouldExcludeURLFromHistogram(const GURL
& url
) {
54 return ShouldExcludeSiteFromHistogram(SiteFromURL(url
));
57 void ServiceWorkerMetrics::CountInitDiskCacheResult(bool result
) {
58 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.DiskCache.InitResult", result
);
61 void ServiceWorkerMetrics::CountReadResponseResult(
62 ServiceWorkerMetrics::ReadResponseResult result
) {
63 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.DiskCache.ReadResponseResult",
64 result
, NUM_READ_RESPONSE_RESULT_TYPES
);
67 void ServiceWorkerMetrics::CountWriteResponseResult(
68 ServiceWorkerMetrics::WriteResponseResult result
) {
69 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.DiskCache.WriteResponseResult",
70 result
, NUM_WRITE_RESPONSE_RESULT_TYPES
);
73 void ServiceWorkerMetrics::CountOpenDatabaseResult(
74 ServiceWorkerDatabase::Status status
) {
75 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.OpenResult",
76 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
79 void ServiceWorkerMetrics::CountReadDatabaseResult(
80 ServiceWorkerDatabase::Status status
) {
81 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.ReadResult",
82 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
85 void ServiceWorkerMetrics::CountWriteDatabaseResult(
86 ServiceWorkerDatabase::Status status
) {
87 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.WriteResult",
88 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
91 void ServiceWorkerMetrics::RecordDestroyDatabaseResult(
92 ServiceWorkerDatabase::Status status
) {
93 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.DestroyDatabaseResult",
94 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
97 void ServiceWorkerMetrics::RecordPurgeResourceResult(int net_error
) {
98 UMA_HISTOGRAM_SPARSE_SLOWLY("ServiceWorker.Storage.PurgeResourceResult",
102 void ServiceWorkerMetrics::RecordDiskCacheMigrationResult(
103 DiskCacheMigrationResult result
) {
104 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Storage.DiskCacheMigrationResult",
105 result
, NUM_MIGRATION_RESULT_TYPES
);
108 void ServiceWorkerMetrics::RecordDeleteAndStartOverResult(
109 DeleteAndStartOverResult result
) {
110 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Storage.DeleteAndStartOverResult",
111 result
, NUM_DELETE_AND_START_OVER_RESULT_TYPES
);
114 void ServiceWorkerMetrics::CountControlledPageLoad(const GURL
& url
) {
115 Site site
= SiteFromURL(url
);
116 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.PageLoad", static_cast<int>(site
),
117 static_cast<int>(Site::NUM_TYPES
));
119 if (ShouldExcludeSiteFromHistogram(site
))
121 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
122 base::Bind(&RecordURLMetricOnUI
, url
));
125 void ServiceWorkerMetrics::RecordStartWorkerStatus(
126 ServiceWorkerStatusCode status
,
129 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StartWorker.Status", status
,
130 SERVICE_WORKER_ERROR_MAX_VALUE
);
132 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StartNewWorker.Status", status
,
133 SERVICE_WORKER_ERROR_MAX_VALUE
);
137 void ServiceWorkerMetrics::RecordStartWorkerTime(const base::TimeDelta
& time
,
140 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.StartWorker.Time", time
);
142 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.StartNewWorker.Time", time
);
145 void ServiceWorkerMetrics::RecordStopWorkerStatus(StopWorkerStatus status
) {
146 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StopWorker.Status", status
,
147 NUM_STOP_STATUS_TYPES
);
150 void ServiceWorkerMetrics::RecordStopWorkerTime(const base::TimeDelta
& time
) {
151 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.StopWorker.Time", time
);
154 void ServiceWorkerMetrics::RecordActivateEventStatus(
155 ServiceWorkerStatusCode status
) {
156 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.ActivateEventStatus", status
,
157 SERVICE_WORKER_ERROR_MAX_VALUE
);
160 void ServiceWorkerMetrics::RecordInstallEventStatus(
161 ServiceWorkerStatusCode status
) {
162 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.InstallEventStatus", status
,
163 SERVICE_WORKER_ERROR_MAX_VALUE
);
166 void ServiceWorkerMetrics::RecordEventHandledRatio(EventType event
,
167 size_t handled_events
,
168 size_t fired_events
) {
171 EventHandledRatioType type
= EVENT_HANDLED_SOME
;
172 if (fired_events
== handled_events
)
173 type
= EVENT_HANDLED_ALL
;
174 else if (handled_events
== 0)
175 type
= EVENT_HANDLED_NONE
;
176 // For now Fetch is the only type that is recorded.
177 DCHECK_EQ(EVENT_TYPE_FETCH
, event
);
178 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.EventHandledRatioType.Fetch", type
,
179 NUM_EVENT_HANDLED_RATIO_TYPE
);
182 void ServiceWorkerMetrics::RecordFetchEventStatus(
183 bool is_main_resource
,
184 ServiceWorkerStatusCode status
) {
185 if (is_main_resource
) {
186 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.FetchEvent.MainResource.Status",
187 status
, SERVICE_WORKER_ERROR_MAX_VALUE
);
189 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.FetchEvent.Subresource.Status",
190 status
, SERVICE_WORKER_ERROR_MAX_VALUE
);
194 void ServiceWorkerMetrics::RecordFetchEventTime(
195 ServiceWorkerFetchEventResult result
,
196 const base::TimeDelta
& time
) {
198 case ServiceWorkerFetchEventResult::
199 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK
:
200 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.FetchEvent.Fallback.Time",
203 case ServiceWorkerFetchEventResult::
204 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE
:
205 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.FetchEvent.HasResponse.Time",
211 void ServiceWorkerMetrics::RecordURLRequestJobResult(
212 bool is_main_resource
,
213 URLRequestJobResult result
) {
214 if (is_main_resource
) {
215 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.URLRequestJob.MainResource.Result",
216 result
, NUM_REQUEST_JOB_RESULT_TYPES
);
218 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.URLRequestJob.Subresource.Result",
219 result
, NUM_REQUEST_JOB_RESULT_TYPES
);
223 void ServiceWorkerMetrics::RecordStatusZeroResponseError(
224 bool is_main_resource
,
225 blink::WebServiceWorkerResponseError error
) {
226 if (is_main_resource
) {
227 UMA_HISTOGRAM_ENUMERATION(
228 "ServiceWorker.URLRequestJob.MainResource.StatusZeroError", error
,
229 blink::WebServiceWorkerResponseErrorLast
+ 1);
231 UMA_HISTOGRAM_ENUMERATION(
232 "ServiceWorker.URLRequestJob.Subresource.StatusZeroError", error
,
233 blink::WebServiceWorkerResponseErrorLast
+ 1);
237 void ServiceWorkerMetrics::RecordFallbackedRequestMode(FetchRequestMode mode
) {
238 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.URLRequestJob.FallbackedRequestMode",
239 mode
, FETCH_REQUEST_MODE_LAST
+ 1);
242 void ServiceWorkerMetrics::RecordTimeBetweenEvents(
243 const base::TimeDelta
& time
) {
244 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.TimeBetweenEvents", time
);
247 } // namespace content