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 MaybeRecordNetworkBytesOS(); | |
81 #endif | |
82 } | |
83 | |
84 void DataUseMeasurement::OnNetworkBytesSent(const net::URLRequest& request, | |
85 int64_t bytes_sent) { | |
86 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.Delegate", bytes_sent); | |
87 #if defined(OS_ANDROID) | |
88 bytes_transferred_since_last_traffic_stats_query_ += bytes_sent; | |
89 MaybeRecordNetworkBytesOS(); | |
90 #endif | |
91 } | |
92 | |
68 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, | 93 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, |
69 bool started) { | 94 bool started) { |
70 // TODO(amohammadkhan): Verify that there is no double recording in data use | 95 // TODO(amohammadkhan): Verify that there is no double recording in data use |
71 // of redirected requests. | 96 // of redirected requests. |
72 ReportDataUseUMA(request); | 97 ReportDataUseUMA(request); |
73 } | 98 } |
74 | 99 |
75 void DataUseMeasurement::ReportDataUseUMA( | 100 void DataUseMeasurement::ReportDataUseUMA( |
76 const net::URLRequest& request) const { | 101 const net::URLRequest& request) const { |
77 // Counts rely on URLRequest::GetTotalReceivedBytes() and | 102 // Counts rely on URLRequest::GetTotalReceivedBytes() and |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 return base::StringPrintf( | 180 return base::StringPrintf( |
156 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", | 181 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", |
157 app_state == BACKGROUND ? "Background" : "Foreground", | 182 app_state == BACKGROUND ? "Background" : "Foreground", |
158 is_connection_cellular ? "Cellular" : "NotCellular"); | 183 is_connection_cellular ? "Cellular" : "NotCellular"); |
159 } | 184 } |
160 | 185 |
161 #if defined(OS_ANDROID) | 186 #if defined(OS_ANDROID) |
162 void DataUseMeasurement::OnApplicationStateChange( | 187 void DataUseMeasurement::OnApplicationStateChange( |
163 base::android::ApplicationState application_state) { | 188 base::android::ApplicationState application_state) { |
164 app_state_ = application_state; | 189 app_state_ = application_state; |
190 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | |
191 MaybeRecordNetworkBytesOS(); | |
192 } | |
193 | |
194 void DataUseMeasurement::MaybeRecordNetworkBytesOS() { | |
195 // Minimum number of bytes that should be reported by the network delegate | |
196 // before Android's TrafficStats API is queried (if Chrome is not in | |
197 // background). This reduces the overhead of repeatedly calling the API. | |
sclittle
2016/09/20 20:54:13
I'm not sure 10k is much different here. If the us
tbansal1
2016/09/21 17:38:42
Done.
| |
198 static const int64_t kMaxDelegateBytes = 10000; | |
sclittle
2016/09/20 20:54:13
nit: Should this be called kMinDelegateBytes, to m
tbansal1
2016/09/21 17:38:42
Obsolete.
| |
199 if (bytes_transferred_since_last_traffic_stats_query_ < kMaxDelegateBytes && | |
200 CurrentAppState() == FOREGROUND) { | |
201 return; | |
202 } | |
203 bytes_transferred_since_last_traffic_stats_query_ = 0; | |
204 int64_t bytes = 0; | |
205 // Query Android traffic stats directly instead of registering with the | |
206 // DataUseAggregator since the latter does not provide notifications for | |
207 // the incognito traffic. | |
208 if (net::android::traffic_stats::GetCurrentUidRxBytes(&bytes)) { | |
209 if (rx_bytes_os_ != 0) { | |
210 DCHECK_GE(bytes, rx_bytes_os_); | |
211 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.OS", bytes - rx_bytes_os_); | |
212 } | |
213 rx_bytes_os_ = bytes; | |
214 } | |
215 | |
216 if (net::android::traffic_stats::GetCurrentUidTxBytes(&bytes)) { | |
217 if (tx_bytes_os_ != 0) { | |
218 DCHECK_GE(bytes, tx_bytes_os_); | |
219 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.OS", bytes - tx_bytes_os_); | |
220 } | |
221 tx_bytes_os_ = bytes; | |
222 } | |
165 } | 223 } |
166 #endif | 224 #endif |
167 | 225 |
168 void DataUseMeasurement::ReportDataUsageServices( | 226 void DataUseMeasurement::ReportDataUsageServices( |
169 DataUseUserData::ServiceName service, | 227 DataUseUserData::ServiceName service, |
170 TrafficDirection dir, | 228 TrafficDirection dir, |
171 bool is_connection_cellular, | 229 bool is_connection_cellular, |
172 int64_t message_size) const { | 230 int64_t message_size) const { |
173 RecordUMAHistogramCount( | 231 RecordUMAHistogramCount( |
174 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), | 232 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), |
175 message_size); | 233 message_size); |
176 if (message_size > 0) { | 234 if (message_size > 0) { |
177 IncreaseSparseHistogramByValue( | 235 IncreaseSparseHistogramByValue( |
178 GetHistogramName("DataUse.MessageSize.AllServices", dir, | 236 GetHistogramName("DataUse.MessageSize.AllServices", dir, |
179 is_connection_cellular), | 237 is_connection_cellular), |
180 service, message_size); | 238 service, message_size); |
181 } | 239 } |
182 } | 240 } |
183 | 241 |
184 } // namespace data_use_measurement | 242 } // namespace data_use_measurement |
OLD | NEW |