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) | |
56 #endif | 62 #endif |
57 { | 63 { |
58 } | 64 } |
59 | 65 |
60 DataUseMeasurement::~DataUseMeasurement(){}; | 66 DataUseMeasurement::~DataUseMeasurement(){}; |
61 | 67 |
62 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, | 68 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, |
63 const GURL& new_location) { | 69 const GURL& new_location) { |
64 // Recording data use of request on redirects. | 70 // Recording data use of request on redirects. |
65 ReportDataUseUMA(request); | 71 ReportDataUseUMA(request); |
66 } | 72 } |
67 | 73 |
74 void DataUseMeasurement::OnNetworkBytesReceived(const net::URLRequest& request, | |
75 int64_t bytes_received) { | |
76 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.Delegate", bytes_received); | |
77 RecordNetworkBytesReceivedOS(); | |
sclittle
2016/09/14 21:28:13
Is it OK for performance to call this so often? No
tbansal1
2016/09/15 17:46:04
Good catch. Fixed.
| |
78 } | |
79 | |
80 void DataUseMeasurement::OnNetworkBytesSent(const net::URLRequest& request, | |
81 int64_t bytes_sent) { | |
82 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.Delegate", bytes_sent); | |
83 RecordNetworkBytesSentOS(); | |
84 } | |
85 | |
68 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, | 86 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, |
69 bool started) { | 87 bool started) { |
70 // TODO(amohammadkhan): Verify that there is no double recording in data use | 88 // TODO(amohammadkhan): Verify that there is no double recording in data use |
71 // of redirected requests. | 89 // of redirected requests. |
72 ReportDataUseUMA(request); | 90 ReportDataUseUMA(request); |
73 } | 91 } |
74 | 92 |
75 void DataUseMeasurement::ReportDataUseUMA( | 93 void DataUseMeasurement::ReportDataUseUMA( |
76 const net::URLRequest& request) const { | 94 const net::URLRequest& request) const { |
77 // Counts rely on URLRequest::GetTotalReceivedBytes() and | 95 // Counts rely on URLRequest::GetTotalReceivedBytes() and |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), | 192 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), |
175 message_size); | 193 message_size); |
176 if (message_size > 0) { | 194 if (message_size > 0) { |
177 IncreaseSparseHistogramByValue( | 195 IncreaseSparseHistogramByValue( |
178 GetHistogramName("DataUse.MessageSize.AllServices", dir, | 196 GetHistogramName("DataUse.MessageSize.AllServices", dir, |
179 is_connection_cellular), | 197 is_connection_cellular), |
180 service, message_size); | 198 service, message_size); |
181 } | 199 } |
182 } | 200 } |
183 | 201 |
202 void DataUseMeasurement::RecordNetworkBytesReceivedOS() { | |
sclittle
2016/09/14 21:28:13
Is there a timing issue here? E.g., suppose there'
tbansal1
2016/09/15 17:46:04
No, because I am not attributing traffic stats byt
| |
203 #if defined(OS_ANDROID) | |
204 int64_t bytes = 0; | |
205 if (net::android::traffic_stats::GetCurrentUidRxBytes(&bytes)) { | |
206 if (rx_bytes_os_ != 0) { | |
207 DCHECK_GE(bytes, rx_bytes_os_); | |
sclittle
2016/09/14 21:28:13
Are you sure you want to crash if this condition d
tbansal1
2016/09/15 17:46:04
Yes, android provides guarantee that this is alway
| |
208 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.OS", bytes - rx_bytes_os_); | |
209 } | |
210 rx_bytes_os_ = bytes; | |
211 } | |
212 #endif | |
213 } | |
214 | |
215 void DataUseMeasurement::RecordNetworkBytesSentOS() { | |
216 #if defined(OS_ANDROID) | |
217 int64_t bytes = 0; | |
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 } | |
225 #endif | |
226 } | |
227 | |
184 } // namespace data_use_measurement | 228 } // namespace data_use_measurement |
OLD | NEW |