OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/data_use_measurement/core/data_use_measurement.h" | 5 #include "components/data_use_measurement/core/data_use_measurement.h" |
6 | 6 |
7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "base/metrics/sparse_histogram.h" | 8 #include "base/metrics/sparse_histogram.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 base::HistogramBase* histogram_pointer = base::Histogram::FactoryTimeGet( | 57 base::HistogramBase* histogram_pointer = base::Histogram::FactoryTimeGet( |
58 name, | 58 name, |
59 base::TimeDelta::FromMilliseconds(1), // Minimum sample | 59 base::TimeDelta::FromMilliseconds(1), // Minimum sample |
60 base::TimeDelta::FromHours(1), // Maximum sample | 60 base::TimeDelta::FromHours(1), // Maximum sample |
61 50, // Bucket count. | 61 50, // Bucket count. |
62 base::HistogramBase::kUmaTargetedHistogramFlag); | 62 base::HistogramBase::kUmaTargetedHistogramFlag); |
63 histogram_pointer->AddCount(latency.InMilliseconds(), count); | 63 histogram_pointer->AddCount(latency.InMilliseconds(), count); |
64 } | 64 } |
65 #endif | 65 #endif |
66 | 66 |
67 // Data use broken by content type. This enum must remain synchronized | |
68 // with the enum of the same name in metrics/histograms/histograms.xml. | |
69 enum DataUseContenType { | |
RyanSturm
2016/12/20 19:37:52
nit: This seems duplicative. Maybe you could conso
Raj
2016/12/22 18:44:38
Done.
| |
70 MAIN_FRAME_HTML = 0, | |
71 NON_MAIN_FRAME_HTML, | |
72 CSS, | |
73 IMAGE, | |
74 JAVASCRIPT, | |
75 FONT, | |
76 AUDIO_APPBACKGROUND, | |
77 AUDIO_TABBACKGROUND, | |
78 AUDIO_TABFOREGROUND, | |
79 VIDEO_APPBACKGROUND, | |
80 VIDEO_TABBACKGROUND, | |
81 VIDEO_TABFOREGROUND, | |
82 OTHER, | |
83 TYPE_MAX, | |
84 }; | |
85 | |
86 DataUseContenType GetDataUseContentType( | |
87 DataUseUserData::ContentType content_type, | |
88 bool is_app_foreground, | |
89 bool is_tab_visible) { | |
90 switch (content_type) { | |
91 case DataUseUserData::ContentType::MAIN_FRAME_HTML: | |
92 return MAIN_FRAME_HTML; | |
93 case DataUseUserData::ContentType::NON_MAIN_FRAME_HTML: | |
94 return NON_MAIN_FRAME_HTML; | |
95 case DataUseUserData::ContentType::CSS: | |
96 return CSS; | |
97 case DataUseUserData::ContentType::IMAGE: | |
98 return IMAGE; | |
99 case DataUseUserData::ContentType::FONT: | |
100 return FONT; | |
101 case DataUseUserData::ContentType::AUDIO: | |
102 return !is_app_foreground | |
103 ? AUDIO_APPBACKGROUND | |
104 : (is_tab_visible ? AUDIO_TABFOREGROUND : AUDIO_TABBACKGROUND); | |
105 case DataUseUserData::ContentType::VIDEO: | |
106 return !is_app_foreground | |
107 ? VIDEO_APPBACKGROUND | |
108 : (is_tab_visible ? VIDEO_TABFOREGROUND : VIDEO_TABBACKGROUND); | |
109 case DataUseUserData::ContentType::OTHER: | |
110 return OTHER; | |
111 default: | |
112 NOTREACHED(); | |
113 } | |
114 return OTHER; | |
115 } | |
116 | |
67 } // namespace | 117 } // namespace |
68 | 118 |
69 DataUseMeasurement::DataUseMeasurement( | 119 DataUseMeasurement::DataUseMeasurement( |
70 std::unique_ptr<URLRequestClassifier> url_request_classifier, | 120 std::unique_ptr<URLRequestClassifier> url_request_classifier, |
71 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder, | 121 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder, |
72 DataUseAscriber* ascriber) | 122 DataUseAscriber* ascriber) |
73 : url_request_classifier_(std::move(url_request_classifier)), | 123 : url_request_classifier_(std::move(url_request_classifier)), |
74 metrics_data_use_forwarder_(metrics_data_use_forwarder), | 124 metrics_data_use_forwarder_(metrics_data_use_forwarder), |
75 ascriber_(ascriber) | 125 ascriber_(ascriber) |
76 #if defined(OS_ANDROID) | 126 #if defined(OS_ANDROID) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 } | 162 } |
113 | 163 |
114 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, | 164 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, |
115 const GURL& new_location) { | 165 const GURL& new_location) { |
116 // Recording data use of request on redirects. | 166 // Recording data use of request on redirects. |
117 // TODO(rajendrant): May not be needed when http://crbug/651957 is fixed. | 167 // TODO(rajendrant): May not be needed when http://crbug/651957 is fixed. |
118 UpdateDataUsePrefs(request); | 168 UpdateDataUsePrefs(request); |
119 ReportServicesMessageSizeUMA(request); | 169 ReportServicesMessageSizeUMA(request); |
120 } | 170 } |
121 | 171 |
172 void DataUseMeasurement::OnHeadersReceived( | |
173 net::URLRequest* request, | |
174 const net::HttpResponseHeaders* response_headers) { | |
175 DataUseUserData* data_use_user_data = reinterpret_cast<DataUseUserData*>( | |
176 request->GetUserData(DataUseUserData::kUserDataKey)); | |
177 if (data_use_user_data) { | |
178 data_use_user_data->set_content_type( | |
179 url_request_classifier_->GetContentType(*request, *response_headers)); | |
180 } | |
181 } | |
182 | |
122 void DataUseMeasurement::OnNetworkBytesReceived(const net::URLRequest& request, | 183 void DataUseMeasurement::OnNetworkBytesReceived(const net::URLRequest& request, |
123 int64_t bytes_received) { | 184 int64_t bytes_received) { |
124 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.Delegate", bytes_received); | 185 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.Delegate", bytes_received); |
125 ReportDataUseUMA(request, DOWNSTREAM, bytes_received); | 186 ReportDataUseUMA(request, DOWNSTREAM, bytes_received); |
126 #if defined(OS_ANDROID) | 187 #if defined(OS_ANDROID) |
127 bytes_transferred_since_last_traffic_stats_query_ += bytes_received; | 188 bytes_transferred_since_last_traffic_stats_query_ += bytes_received; |
128 #endif | 189 #endif |
129 } | 190 } |
130 | 191 |
131 void DataUseMeasurement::OnNetworkBytesSent(const net::URLRequest& request, | 192 void DataUseMeasurement::OnNetworkBytesSent(const net::URLRequest& request, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 if (no_reads_since_background_) { | 250 if (no_reads_since_background_) { |
190 no_reads_since_background_ = false; | 251 no_reads_since_background_ = false; |
191 IncrementLatencyHistogramByCount( | 252 IncrementLatencyHistogramByCount( |
192 is_user_traffic ? "DataUse.BackgroundToFirstDownstream.User" | 253 is_user_traffic ? "DataUse.BackgroundToFirstDownstream.User" |
193 : "DataUse.BackgroundToFirstDownstream.System", | 254 : "DataUse.BackgroundToFirstDownstream.System", |
194 time_since_background, 1); | 255 time_since_background, 1); |
195 } | 256 } |
196 } | 257 } |
197 #endif | 258 #endif |
198 | 259 |
260 bool is_tab_visible = false; | |
261 | |
199 if (is_user_traffic) { | 262 if (is_user_traffic) { |
200 const DataUseRecorder* recorder = ascriber_->GetDataUseRecorder(request); | 263 const DataUseRecorder* recorder = ascriber_->GetDataUseRecorder(request); |
201 if (recorder) { | 264 if (recorder) { |
265 is_tab_visible = recorder->is_visible(); | |
202 RecordTabStateHistogram(dir, new_app_state, recorder->is_visible(), | 266 RecordTabStateHistogram(dir, new_app_state, recorder->is_visible(), |
203 bytes); | 267 bytes); |
204 } | 268 } |
205 } | 269 } |
270 if (attached_service_data && dir == DOWNSTREAM && | |
271 new_app_state != DataUseUserData::UNKNOWN) { | |
272 RecordContentTypeHistogram(attached_service_data->content_type(), | |
RyanSturm
2016/12/20 19:37:52
nit: I just noticed now that these are sort of str
Raj
2016/12/22 18:44:37
Yes. That case is handled as follows.
These UMA ar
RyanSturm
2016/12/22 20:00:42
Awesome. Sounds like good enough granularity (FG->
| |
273 is_user_traffic, new_app_state, is_tab_visible, | |
274 bytes); | |
275 } | |
206 } | 276 } |
207 | 277 |
208 void DataUseMeasurement::UpdateDataUsePrefs( | 278 void DataUseMeasurement::UpdateDataUsePrefs( |
209 const net::URLRequest& request) const { | 279 const net::URLRequest& request) const { |
210 bool is_connection_cellular = | 280 bool is_connection_cellular = |
211 net::NetworkChangeNotifier::IsConnectionCellular( | 281 net::NetworkChangeNotifier::IsConnectionCellular( |
212 net::NetworkChangeNotifier::GetConnectionType()); | 282 net::NetworkChangeNotifier::GetConnectionType()); |
213 | 283 |
214 DataUseUserData* attached_service_data = static_cast<DataUseUserData*>( | 284 DataUseUserData* attached_service_data = static_cast<DataUseUserData*>( |
215 request.GetUserData(DataUseUserData::kUserDataKey)); | 285 request.GetUserData(DataUseUserData::kUserDataKey)); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
356 if (app_state == DataUseUserData::BACKGROUND) { | 426 if (app_state == DataUseUserData::BACKGROUND) { |
357 histogram_name.append("AppBackground"); | 427 histogram_name.append("AppBackground"); |
358 } else if (is_tab_visible) { | 428 } else if (is_tab_visible) { |
359 histogram_name.append("AppForeground.TabForeground"); | 429 histogram_name.append("AppForeground.TabForeground"); |
360 } else { | 430 } else { |
361 histogram_name.append("AppForeground.TabBackground"); | 431 histogram_name.append("AppForeground.TabBackground"); |
362 } | 432 } |
363 RecordUMAHistogramCount(histogram_name, bytes); | 433 RecordUMAHistogramCount(histogram_name, bytes); |
364 } | 434 } |
365 | 435 |
436 void DataUseMeasurement::RecordContentTypeHistogram( | |
437 DataUseUserData::ContentType content_type, | |
438 bool is_user_traffic, | |
439 DataUseUserData::AppState app_state, | |
440 bool is_tab_visible, | |
441 int64_t bytes) { | |
442 DataUseContenType data_use_content_type = GetDataUseContentType( | |
443 content_type, app_state == DataUseUserData::FOREGROUND, is_tab_visible); | |
444 if (is_user_traffic) { | |
445 UMA_HISTOGRAM_ENUMERATION("DataUse.ContentType.UserTraffic", | |
446 data_use_content_type, | |
447 DataUseContenType::TYPE_MAX); | |
448 } else { | |
449 UMA_HISTOGRAM_ENUMERATION("DataUse.ContentType.Services", | |
450 data_use_content_type, | |
451 DataUseContenType::TYPE_MAX); | |
452 } | |
453 } | |
454 | |
366 } // namespace data_use_measurement | 455 } // namespace data_use_measurement |
OLD | NEW |