Index: components/data_use_measurement/content/data_use_measurement.cc |
diff --git a/components/data_use_measurement/content/data_use_measurement.cc b/components/data_use_measurement/content/data_use_measurement.cc |
index dd70ad5607af5aa785fc97068c66054f5eb9af94..05c9acecebaa0aa7e578ca9782ba685526d7b6ba 100644 |
--- a/components/data_use_measurement/content/data_use_measurement.cc |
+++ b/components/data_use_measurement/content/data_use_measurement.cc |
@@ -14,6 +14,10 @@ |
#include "net/http/http_response_headers.h" |
#include "net/url_request/url_request.h" |
+#if defined(OS_ANDROID) |
+#include "net/android/traffic_stats.h" |
+#endif |
+ |
namespace data_use_measurement { |
namespace { |
@@ -52,7 +56,10 @@ DataUseMeasurement::DataUseMeasurement( |
app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), |
app_listener_(new base::android::ApplicationStatusListener( |
base::Bind(&DataUseMeasurement::OnApplicationStateChange, |
- base::Unretained(this)))) |
+ base::Unretained(this)))), |
+ rx_bytes_os_(0), |
+ tx_bytes_os_(0), |
+ bytes_transferred_since_last_traffic_stats_query_(0) |
#endif |
{ |
} |
@@ -65,6 +72,24 @@ void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, |
ReportDataUseUMA(request); |
} |
+void DataUseMeasurement::OnNetworkBytesReceived(const net::URLRequest& request, |
+ int64_t bytes_received) { |
+ UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.Delegate", bytes_received); |
+#if defined(OS_ANDROID) |
+ bytes_transferred_since_last_traffic_stats_query_ += bytes_received; |
+ MaybeRecordNetworkBytesOS(); |
+#endif |
+} |
+ |
+void DataUseMeasurement::OnNetworkBytesSent(const net::URLRequest& request, |
+ int64_t bytes_sent) { |
+ UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.Delegate", bytes_sent); |
+#if defined(OS_ANDROID) |
+ bytes_transferred_since_last_traffic_stats_query_ += bytes_sent; |
+ MaybeRecordNetworkBytesOS(); |
+#endif |
+} |
+ |
void DataUseMeasurement::OnCompleted(const net::URLRequest& request, |
bool started) { |
// TODO(amohammadkhan): Verify that there is no double recording in data use |
@@ -162,6 +187,39 @@ std::string DataUseMeasurement::GetHistogramName( |
void DataUseMeasurement::OnApplicationStateChange( |
base::android::ApplicationState application_state) { |
app_state_ = application_state; |
+ if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
+ MaybeRecordNetworkBytesOS(); |
+} |
+ |
+void DataUseMeasurement::MaybeRecordNetworkBytesOS() { |
+ // Minimum number of bytes that should be reported by the network delegate |
+ // before Android's TrafficStats API is queried (if Chrome is not in |
+ // 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.
|
+ 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.
|
+ if (bytes_transferred_since_last_traffic_stats_query_ < kMaxDelegateBytes && |
+ CurrentAppState() == FOREGROUND) { |
+ return; |
+ } |
+ bytes_transferred_since_last_traffic_stats_query_ = 0; |
+ int64_t bytes = 0; |
+ // Query Android traffic stats directly instead of registering with the |
+ // DataUseAggregator since the latter does not provide notifications for |
+ // the incognito traffic. |
+ if (net::android::traffic_stats::GetCurrentUidRxBytes(&bytes)) { |
+ if (rx_bytes_os_ != 0) { |
+ DCHECK_GE(bytes, rx_bytes_os_); |
+ UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.OS", bytes - rx_bytes_os_); |
+ } |
+ rx_bytes_os_ = bytes; |
+ } |
+ |
+ if (net::android::traffic_stats::GetCurrentUidTxBytes(&bytes)) { |
+ if (tx_bytes_os_ != 0) { |
+ DCHECK_GE(bytes, tx_bytes_os_); |
+ UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.OS", bytes - tx_bytes_os_); |
+ } |
+ tx_bytes_os_ = bytes; |
+ } |
} |
#endif |