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/metrics/user_metrics_action.h"
10 #include "base/strings/string_util.h"
11 #include "content/common/service_worker/service_worker_types.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/content_browser_client.h"
14 #include "content/public/browser/user_metrics.h"
15 #include "content/public/common/content_client.h"
21 void RecordURLMetricOnUI(const GURL
& url
) {
22 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
23 GetContentClient()->browser()->RecordURLMetric(
24 "ServiceWorker.ControlledPageUrl", url
);
27 bool ShouldExcludeForHistogram(const GURL
& scope
) {
28 // Exclude NTP scope from UMA for now as it tends to dominate the stats
29 // and makes the results largely skewed.
30 // TOOD(kinuko): This should be temporary, revisit this once we have
31 // better idea about what should be excluded in the UMA.
32 // (UIThreadSearchTermsData::GoogleBaseURLValue() returns the google base
33 // URL, but not available in content layer)
34 const char google_like_scope_prefix
[] = "https://www.google.";
35 return base::StartsWith(scope
.spec(), google_like_scope_prefix
,
36 base::CompareCase::INSENSITIVE_ASCII
);
39 enum EventHandledRatioType
{
43 NUM_EVENT_HANDLED_RATIO_TYPE
,
48 void ServiceWorkerMetrics::CountInitDiskCacheResult(bool result
) {
49 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.DiskCache.InitResult", result
);
52 void ServiceWorkerMetrics::CountReadResponseResult(
53 ServiceWorkerMetrics::ReadResponseResult result
) {
54 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.DiskCache.ReadResponseResult",
55 result
, NUM_READ_RESPONSE_RESULT_TYPES
);
58 void ServiceWorkerMetrics::CountWriteResponseResult(
59 ServiceWorkerMetrics::WriteResponseResult result
) {
60 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.DiskCache.WriteResponseResult",
61 result
, NUM_WRITE_RESPONSE_RESULT_TYPES
);
64 void ServiceWorkerMetrics::CountOpenDatabaseResult(
65 ServiceWorkerDatabase::Status status
) {
66 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.OpenResult",
67 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
70 void ServiceWorkerMetrics::CountReadDatabaseResult(
71 ServiceWorkerDatabase::Status status
) {
72 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.ReadResult",
73 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
76 void ServiceWorkerMetrics::CountWriteDatabaseResult(
77 ServiceWorkerDatabase::Status status
) {
78 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.WriteResult",
79 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
82 void ServiceWorkerMetrics::RecordDestroyDatabaseResult(
83 ServiceWorkerDatabase::Status status
) {
84 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.DestroyDatabaseResult",
85 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
88 void ServiceWorkerMetrics::RecordPurgeResourceResult(int net_error
) {
89 UMA_HISTOGRAM_SPARSE_SLOWLY("ServiceWorker.Storage.PurgeResourceResult",
93 void ServiceWorkerMetrics::RecordDiskCacheMigrationResult(
94 DiskCacheMigrationResult result
) {
95 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Storage.DiskCacheMigrationResult",
96 result
, NUM_MIGRATION_RESULT_TYPES
);
99 void ServiceWorkerMetrics::RecordDeleteAndStartOverResult(
100 DeleteAndStartOverResult result
) {
101 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Storage.DeleteAndStartOverResult",
102 result
, NUM_DELETE_AND_START_OVER_RESULT_TYPES
);
105 void ServiceWorkerMetrics::CountControlledPageLoad(const GURL
& url
) {
106 RecordAction(base::UserMetricsAction("ServiceWorker.ControlledPageLoad"));
107 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
108 base::Bind(&RecordURLMetricOnUI
, url
));
111 void ServiceWorkerMetrics::RecordStartWorkerStatus(
112 ServiceWorkerStatusCode status
,
115 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StartWorker.Status", status
,
116 SERVICE_WORKER_ERROR_MAX_VALUE
);
118 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StartNewWorker.Status", status
,
119 SERVICE_WORKER_ERROR_MAX_VALUE
);
123 void ServiceWorkerMetrics::RecordStartWorkerTime(const base::TimeDelta
& time
,
126 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.StartWorker.Time", time
);
128 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.StartNewWorker.Time", time
);
131 void ServiceWorkerMetrics::RecordStopWorkerStatus(StopWorkerStatus status
) {
132 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StopWorker.Status", status
,
133 NUM_STOP_STATUS_TYPES
);
136 void ServiceWorkerMetrics::RecordStopWorkerTime(const base::TimeDelta
& time
) {
137 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.StopWorker.Time", time
);
140 void ServiceWorkerMetrics::RecordActivateEventStatus(
141 ServiceWorkerStatusCode status
) {
142 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.ActivateEventStatus", status
,
143 SERVICE_WORKER_ERROR_MAX_VALUE
);
146 void ServiceWorkerMetrics::RecordInstallEventStatus(
147 ServiceWorkerStatusCode status
) {
148 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.InstallEventStatus", status
,
149 SERVICE_WORKER_ERROR_MAX_VALUE
);
152 void ServiceWorkerMetrics::RecordEventHandledRatio(const GURL
& scope
,
154 size_t handled_events
,
155 size_t fired_events
) {
156 if (!fired_events
|| ShouldExcludeForHistogram(scope
))
158 EventHandledRatioType type
= EVENT_HANDLED_SOME
;
159 if (fired_events
== handled_events
)
160 type
= EVENT_HANDLED_ALL
;
161 else if (handled_events
== 0)
162 type
= EVENT_HANDLED_NONE
;
163 // For now Fetch is the only type that is recorded.
164 DCHECK_EQ(EVENT_TYPE_FETCH
, event
);
165 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.EventHandledRatioType.Fetch", type
,
166 NUM_EVENT_HANDLED_RATIO_TYPE
);
169 void ServiceWorkerMetrics::RecordFetchEventStatus(
170 bool is_main_resource
,
171 ServiceWorkerStatusCode status
) {
172 if (is_main_resource
) {
173 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.FetchEvent.MainResource.Status",
174 status
, SERVICE_WORKER_ERROR_MAX_VALUE
);
176 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.FetchEvent.Subresource.Status",
177 status
, SERVICE_WORKER_ERROR_MAX_VALUE
);
181 void ServiceWorkerMetrics::RecordFetchEventTime(
182 ServiceWorkerFetchEventResult result
,
183 const base::TimeDelta
& time
) {
185 case ServiceWorkerFetchEventResult::
186 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK
:
187 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.FetchEvent.Fallback.Time",
190 case ServiceWorkerFetchEventResult::
191 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE
:
192 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.FetchEvent.HasResponse.Time",
198 void ServiceWorkerMetrics::RecordURLRequestJobResult(
199 bool is_main_resource
,
200 URLRequestJobResult result
) {
201 if (is_main_resource
) {
202 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.URLRequestJob.MainResource.Result",
203 result
, NUM_REQUEST_JOB_RESULT_TYPES
);
205 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.URLRequestJob.Subresource.Result",
206 result
, NUM_REQUEST_JOB_RESULT_TYPES
);
210 void ServiceWorkerMetrics::RecordStatusZeroResponseError(
211 bool is_main_resource
,
212 blink::WebServiceWorkerResponseError error
) {
213 if (is_main_resource
) {
214 UMA_HISTOGRAM_ENUMERATION(
215 "ServiceWorker.URLRequestJob.MainResource.StatusZeroError", error
,
216 blink::WebServiceWorkerResponseErrorLast
+ 1);
218 UMA_HISTOGRAM_ENUMERATION(
219 "ServiceWorker.URLRequestJob.Subresource.StatusZeroError", error
,
220 blink::WebServiceWorkerResponseErrorLast
+ 1);
224 void ServiceWorkerMetrics::RecordFallbackedRequestMode(FetchRequestMode mode
) {
225 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.URLRequestJob.FallbackedRequestMode",
226 mode
, FETCH_REQUEST_MODE_LAST
+ 1);
229 } // namespace content