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/content/data_use_measurement.h" | 5 #include "components/data_use_measurement/content/data_use_measurement.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.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" |
11 #include "content/public/browser/resource_request_info.h" | 11 #include "content/public/browser/resource_request_info.h" |
12 #include "net/base/network_change_notifier.h" | 12 #include "net/base/network_change_notifier.h" |
13 #include "net/base/upload_data_stream.h" | 13 #include "net/base/upload_data_stream.h" |
14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
15 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
16 | 16 |
| 17 #if defined(OS_ANDROID) |
| 18 #include "net/android/traffic_stats.h" |
| 19 #endif |
| 20 |
17 namespace data_use_measurement { | 21 namespace data_use_measurement { |
18 | 22 |
19 namespace { | 23 namespace { |
20 | 24 |
21 // Records the occurrence of |sample| in |name| histogram. Conventional UMA | 25 // Records the occurrence of |sample| in |name| histogram. Conventional UMA |
22 // histograms are not used because the |name| is not static. | 26 // histograms are not used because the |name| is not static. |
23 void RecordUMAHistogramCount(const std::string& name, int64_t sample) { | 27 void RecordUMAHistogramCount(const std::string& name, int64_t sample) { |
24 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( | 28 base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet( |
25 name, | 29 name, |
26 1, // Minimum sample size in bytes. | 30 1, // Minimum sample size in bytes. |
(...skipping 18 matching lines...) Expand all Loading... |
45 } // namespace | 49 } // namespace |
46 | 50 |
47 DataUseMeasurement::DataUseMeasurement( | 51 DataUseMeasurement::DataUseMeasurement( |
48 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder) | 52 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder) |
49 : metrics_data_use_forwarder_(metrics_data_use_forwarder) | 53 : metrics_data_use_forwarder_(metrics_data_use_forwarder) |
50 #if defined(OS_ANDROID) | 54 #if defined(OS_ANDROID) |
51 , | 55 , |
52 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), | 56 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), |
53 app_listener_(new base::android::ApplicationStatusListener( | 57 app_listener_(new base::android::ApplicationStatusListener( |
54 base::Bind(&DataUseMeasurement::OnApplicationStateChange, | 58 base::Bind(&DataUseMeasurement::OnApplicationStateChange, |
55 base::Unretained(this)))) | 59 base::Unretained(this)))), |
| 60 rx_bytes_os_(0), |
| 61 tx_bytes_os_(0), |
| 62 bytes_transferred_since_last_traffic_stats_query_(0) |
56 #endif | 63 #endif |
57 { | 64 { |
58 } | 65 } |
59 | 66 |
60 DataUseMeasurement::~DataUseMeasurement(){}; | 67 DataUseMeasurement::~DataUseMeasurement(){}; |
61 | 68 |
62 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, | 69 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, |
63 const GURL& new_location) { | 70 const GURL& new_location) { |
64 // Recording data use of request on redirects. | 71 // Recording data use of request on redirects. |
65 ReportDataUseUMA(request); | 72 ReportDataUseUMA(request); |
66 } | 73 } |
67 | 74 |
| 75 void DataUseMeasurement::OnNetworkBytesReceived(const net::URLRequest& request, |
| 76 int64_t bytes_received) { |
| 77 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.Delegate", bytes_received); |
| 78 #if defined(OS_ANDROID) |
| 79 bytes_transferred_since_last_traffic_stats_query_ += bytes_received; |
| 80 #endif |
| 81 } |
| 82 |
| 83 void DataUseMeasurement::OnNetworkBytesSent(const net::URLRequest& request, |
| 84 int64_t bytes_sent) { |
| 85 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.Delegate", bytes_sent); |
| 86 #if defined(OS_ANDROID) |
| 87 bytes_transferred_since_last_traffic_stats_query_ += bytes_sent; |
| 88 #endif |
| 89 } |
| 90 |
68 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, | 91 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, |
69 bool started) { | 92 bool started) { |
70 // TODO(amohammadkhan): Verify that there is no double recording in data use | 93 // TODO(amohammadkhan): Verify that there is no double recording in data use |
71 // of redirected requests. | 94 // of redirected requests. |
72 ReportDataUseUMA(request); | 95 ReportDataUseUMA(request); |
| 96 #if defined(OS_ANDROID) |
| 97 MaybeRecordNetworkBytesOS(); |
| 98 #endif |
73 } | 99 } |
74 | 100 |
75 void DataUseMeasurement::ReportDataUseUMA( | 101 void DataUseMeasurement::ReportDataUseUMA( |
76 const net::URLRequest& request) const { | 102 const net::URLRequest& request) const { |
77 // Counts rely on URLRequest::GetTotalReceivedBytes() and | 103 // Counts rely on URLRequest::GetTotalReceivedBytes() and |
78 // URLRequest::GetTotalSentBytes(), which does not include the send path, | 104 // URLRequest::GetTotalSentBytes(), which does not include the send path, |
79 // network layer overhead, TLS overhead, and DNS. | 105 // network layer overhead, TLS overhead, and DNS. |
80 // TODO(amohammadkhan): Make these measured bytes more in line with number of | 106 // TODO(amohammadkhan): Make these measured bytes more in line with number of |
81 // bytes in lower levels. | 107 // bytes in lower levels. |
82 int64_t total_upload_bytes = request.GetTotalSentBytes(); | 108 int64_t total_upload_bytes = request.GetTotalSentBytes(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 return base::StringPrintf( | 181 return base::StringPrintf( |
156 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", | 182 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", |
157 app_state == BACKGROUND ? "Background" : "Foreground", | 183 app_state == BACKGROUND ? "Background" : "Foreground", |
158 is_connection_cellular ? "Cellular" : "NotCellular"); | 184 is_connection_cellular ? "Cellular" : "NotCellular"); |
159 } | 185 } |
160 | 186 |
161 #if defined(OS_ANDROID) | 187 #if defined(OS_ANDROID) |
162 void DataUseMeasurement::OnApplicationStateChange( | 188 void DataUseMeasurement::OnApplicationStateChange( |
163 base::android::ApplicationState application_state) { | 189 base::android::ApplicationState application_state) { |
164 app_state_ = application_state; | 190 app_state_ = application_state; |
| 191 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
| 192 MaybeRecordNetworkBytesOS(); |
| 193 } |
| 194 |
| 195 void DataUseMeasurement::MaybeRecordNetworkBytesOS() { |
| 196 // Minimum number of bytes that should be reported by the network delegate |
| 197 // before Android's TrafficStats API is queried (if Chrome is not in |
| 198 // background). This reduces the overhead of repeatedly calling the API. |
| 199 static const int64_t kMinDelegateBytes = 25000; |
| 200 |
| 201 if (bytes_transferred_since_last_traffic_stats_query_ < kMinDelegateBytes && |
| 202 CurrentAppState() == FOREGROUND) { |
| 203 return; |
| 204 } |
| 205 bytes_transferred_since_last_traffic_stats_query_ = 0; |
| 206 int64_t bytes = 0; |
| 207 // Query Android traffic stats directly instead of registering with the |
| 208 // DataUseAggregator since the latter does not provide notifications for |
| 209 // the incognito traffic. |
| 210 if (net::android::traffic_stats::GetCurrentUidRxBytes(&bytes)) { |
| 211 if (rx_bytes_os_ != 0) { |
| 212 DCHECK_GE(bytes, rx_bytes_os_); |
| 213 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.OS", bytes - rx_bytes_os_); |
| 214 } |
| 215 rx_bytes_os_ = bytes; |
| 216 } |
| 217 |
| 218 if (net::android::traffic_stats::GetCurrentUidTxBytes(&bytes)) { |
| 219 if (tx_bytes_os_ != 0) { |
| 220 DCHECK_GE(bytes, tx_bytes_os_); |
| 221 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.OS", bytes - tx_bytes_os_); |
| 222 } |
| 223 tx_bytes_os_ = bytes; |
| 224 } |
165 } | 225 } |
166 #endif | 226 #endif |
167 | 227 |
168 void DataUseMeasurement::ReportDataUsageServices( | 228 void DataUseMeasurement::ReportDataUsageServices( |
169 DataUseUserData::ServiceName service, | 229 DataUseUserData::ServiceName service, |
170 TrafficDirection dir, | 230 TrafficDirection dir, |
171 bool is_connection_cellular, | 231 bool is_connection_cellular, |
172 int64_t message_size) const { | 232 int64_t message_size) const { |
173 RecordUMAHistogramCount( | 233 RecordUMAHistogramCount( |
174 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), | 234 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), |
175 message_size); | 235 message_size); |
176 if (message_size > 0) { | 236 if (message_size > 0) { |
177 IncreaseSparseHistogramByValue( | 237 IncreaseSparseHistogramByValue( |
178 GetHistogramName("DataUse.MessageSize.AllServices", dir, | 238 GetHistogramName("DataUse.MessageSize.AllServices", dir, |
179 is_connection_cellular), | 239 is_connection_cellular), |
180 service, message_size); | 240 service, message_size); |
181 } | 241 } |
182 } | 242 } |
183 | 243 |
184 } // namespace data_use_measurement | 244 } // namespace data_use_measurement |
OLD | NEW |