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/public/browser/browser_thread.h"
12 #include "content/public/browser/content_browser_client.h"
13 #include "content/public/browser/user_metrics.h"
14 #include "content/public/common/content_client.h"
20 void RecordURLMetricOnUI(const GURL
& url
) {
21 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
22 GetContentClient()->browser()->RecordURLMetric(
23 "ServiceWorker.ControlledPageUrl", url
);
26 bool ShouldExcludeForHistogram(const GURL
& scope
) {
27 // Exclude NTP scope from UMA for now as it tends to dominate the stats
28 // and makes the results largely skewed.
29 // TOOD(kinuko): This should be temporary, revisit this once we have
30 // better idea about what should be excluded in the UMA.
31 // (UIThreadSearchTermsData::GoogleBaseURLValue() returns the google base
32 // URL, but not available in content layer)
33 const char google_like_scope_prefix
[] = "https://www.google.";
34 return base::StartsWith(scope
.spec(), google_like_scope_prefix
,
35 base::CompareCase::INSENSITIVE_ASCII
);
38 enum EventHandledRatioType
{
42 NUM_EVENT_HANDLED_RATIO_TYPE
,
47 void ServiceWorkerMetrics::CountInitDiskCacheResult(bool result
) {
48 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.DiskCache.InitResult", result
);
51 void ServiceWorkerMetrics::CountReadResponseResult(
52 ServiceWorkerMetrics::ReadResponseResult result
) {
53 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.DiskCache.ReadResponseResult",
54 result
, NUM_READ_RESPONSE_RESULT_TYPES
);
57 void ServiceWorkerMetrics::CountWriteResponseResult(
58 ServiceWorkerMetrics::WriteResponseResult result
) {
59 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.DiskCache.WriteResponseResult",
60 result
, NUM_WRITE_RESPONSE_RESULT_TYPES
);
63 void ServiceWorkerMetrics::CountOpenDatabaseResult(
64 ServiceWorkerDatabase::Status status
) {
65 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.OpenResult",
66 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
69 void ServiceWorkerMetrics::CountReadDatabaseResult(
70 ServiceWorkerDatabase::Status status
) {
71 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.ReadResult",
72 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
75 void ServiceWorkerMetrics::CountWriteDatabaseResult(
76 ServiceWorkerDatabase::Status status
) {
77 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.WriteResult",
78 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
81 void ServiceWorkerMetrics::RecordDestroyDatabaseResult(
82 ServiceWorkerDatabase::Status status
) {
83 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Database.DestroyDatabaseResult",
84 status
, ServiceWorkerDatabase::STATUS_ERROR_MAX
);
87 void ServiceWorkerMetrics::RecordPurgeResourceResult(int net_error
) {
88 UMA_HISTOGRAM_SPARSE_SLOWLY("ServiceWorker.Storage.PurgeResourceResult",
92 void ServiceWorkerMetrics::RecordDiskCacheMigrationResult(
93 DiskCacheMigrationResult result
) {
94 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Storage.DiskCacheMigrationResult",
95 result
, NUM_MIGRATION_RESULT_TYPES
);
98 void ServiceWorkerMetrics::RecordDeleteAndStartOverResult(
99 DeleteAndStartOverResult result
) {
100 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.Storage.DeleteAndStartOverResult",
101 result
, NUM_DELETE_AND_START_OVER_RESULT_TYPES
);
104 void ServiceWorkerMetrics::CountControlledPageLoad(const GURL
& url
) {
105 RecordAction(base::UserMetricsAction("ServiceWorker.ControlledPageLoad"));
106 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
107 base::Bind(&RecordURLMetricOnUI
, url
));
110 void ServiceWorkerMetrics::RecordStartWorkerStatus(
111 ServiceWorkerStatusCode status
,
114 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StartWorker.Status", status
,
115 SERVICE_WORKER_ERROR_MAX_VALUE
);
117 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StartNewWorker.Status", status
,
118 SERVICE_WORKER_ERROR_MAX_VALUE
);
122 void ServiceWorkerMetrics::RecordStartWorkerTime(const base::TimeDelta
& time
,
125 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.StartWorker.Time", time
);
127 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.StartNewWorker.Time", time
);
130 void ServiceWorkerMetrics::RecordStopWorkerStatus(StopWorkerStatus status
) {
131 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.StopWorker.Status", status
,
132 NUM_STOP_STATUS_TYPES
);
135 void ServiceWorkerMetrics::RecordStopWorkerTime(const base::TimeDelta
& time
) {
136 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.StopWorker.Time", time
);
139 void ServiceWorkerMetrics::RecordActivateEventStatus(
140 ServiceWorkerStatusCode status
) {
141 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.ActivateEventStatus", status
,
142 SERVICE_WORKER_ERROR_MAX_VALUE
);
145 void ServiceWorkerMetrics::RecordInstallEventStatus(
146 ServiceWorkerStatusCode status
) {
147 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.InstallEventStatus", status
,
148 SERVICE_WORKER_ERROR_MAX_VALUE
);
151 void ServiceWorkerMetrics::RecordEventHandledRatio(const GURL
& scope
,
153 size_t handled_events
,
154 size_t fired_events
) {
155 if (!fired_events
|| ShouldExcludeForHistogram(scope
))
157 EventHandledRatioType type
= EVENT_HANDLED_SOME
;
158 if (fired_events
== handled_events
)
159 type
= EVENT_HANDLED_ALL
;
160 else if (handled_events
== 0)
161 type
= EVENT_HANDLED_NONE
;
162 // For now Fetch is the only type that is recorded.
163 DCHECK_EQ(EVENT_TYPE_FETCH
, event
);
164 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.EventHandledRatioType.Fetch", type
,
165 NUM_EVENT_HANDLED_RATIO_TYPE
);
168 void ServiceWorkerMetrics::RecordFetchEventStatus(
169 bool is_main_resource
,
170 ServiceWorkerStatusCode status
) {
171 if (is_main_resource
) {
172 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.FetchEvent.MainResource.Status",
173 status
, SERVICE_WORKER_ERROR_MAX_VALUE
);
175 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.FetchEvent.Subresource.Status",
176 status
, SERVICE_WORKER_ERROR_MAX_VALUE
);
180 void ServiceWorkerMetrics::RecordFetchEventTime(
181 ServiceWorkerFetchEventResult result
,
182 const base::TimeDelta
& time
) {
184 case ServiceWorkerFetchEventResult::
185 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK
:
186 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.FetchEvent.Fallback.Time",
189 case ServiceWorkerFetchEventResult::
190 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE
:
191 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.FetchEvent.HasResponse.Time",
197 void ServiceWorkerMetrics::RecordURLRequestJobResult(
198 bool is_main_resource
,
199 URLRequestJobResult result
) {
200 if (is_main_resource
) {
201 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.URLRequestJob.MainResource.Result",
202 result
, NUM_REQUEST_JOB_RESULT_TYPES
);
204 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.URLRequestJob.Subresource.Result",
205 result
, NUM_REQUEST_JOB_RESULT_TYPES
);
209 void ServiceWorkerMetrics::RecordStatusZeroResponseError(
210 bool is_main_resource
,
211 blink::WebServiceWorkerResponseError error
) {
212 if (is_main_resource
) {
213 UMA_HISTOGRAM_ENUMERATION(
214 "ServiceWorker.URLRequestJob.MainResource.StatusZeroError", error
,
215 blink::WebServiceWorkerResponseErrorLast
+ 1);
217 UMA_HISTOGRAM_ENUMERATION(
218 "ServiceWorker.URLRequestJob.Subresource.StatusZeroError", error
,
219 blink::WebServiceWorkerResponseErrorLast
+ 1);
223 } // namespace content