Chromium Code Reviews| 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 "components/data_use_measurement/core/data_use_user_data.h" | |
| 11 #include "content/public/browser/resource_request_info.h" | 12 #include "content/public/browser/resource_request_info.h" |
| 12 #include "net/base/network_change_notifier.h" | 13 #include "net/base/network_change_notifier.h" |
| 13 #include "net/base/upload_data_stream.h" | 14 #include "net/base/upload_data_stream.h" |
| 14 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 15 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 16 | 17 |
| 17 #if defined(OS_ANDROID) | 18 #if defined(OS_ANDROID) |
| 18 #include "net/android/traffic_stats.h" | 19 #include "net/android/traffic_stats.h" |
| 19 #endif | 20 #endif |
| 20 | 21 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 base::Unretained(this)))), | 60 base::Unretained(this)))), |
| 60 rx_bytes_os_(0), | 61 rx_bytes_os_(0), |
| 61 tx_bytes_os_(0), | 62 tx_bytes_os_(0), |
| 62 bytes_transferred_since_last_traffic_stats_query_(0) | 63 bytes_transferred_since_last_traffic_stats_query_(0) |
| 63 #endif | 64 #endif |
| 64 { | 65 { |
| 65 } | 66 } |
| 66 | 67 |
| 67 DataUseMeasurement::~DataUseMeasurement(){}; | 68 DataUseMeasurement::~DataUseMeasurement(){}; |
| 68 | 69 |
| 70 void DataUseMeasurement::OnBeforeURLRequest(net::URLRequest* request) { | |
| 71 DataUseUserData* data_use_user_data = reinterpret_cast<DataUseUserData*>( | |
|
Not at Google. Contact bengr
2016/09/26 18:40:19
Is there a reason for reinterpret_cast here? Could
| |
| 72 request->GetUserData(DataUseUserData::kUserDataKey)); | |
| 73 if (!data_use_user_data) { | |
| 74 data_use_user_data = new DataUseUserData( | |
| 75 DataUseUserData::ServiceName::NOT_TAGGED, CurrentAppState()); | |
| 76 request->SetUserData(DataUseUserData::kUserDataKey, data_use_user_data); | |
| 77 } | |
| 78 } | |
| 79 | |
| 69 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, | 80 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, |
| 70 const GURL& new_location) { | 81 const GURL& new_location) { |
| 71 // Recording data use of request on redirects. | 82 // Recording data use of request on redirects. |
| 72 ReportDataUseUMA(request); | 83 ReportDataUseUMA(request); |
| 73 } | 84 } |
| 74 | 85 |
| 75 void DataUseMeasurement::OnNetworkBytesReceived(const net::URLRequest& request, | 86 void DataUseMeasurement::OnNetworkBytesReceived(const net::URLRequest& request, |
| 76 int64_t bytes_received) { | 87 int64_t bytes_received) { |
| 77 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.Delegate", bytes_received); | 88 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.Delegate", bytes_received); |
| 78 #if defined(OS_ANDROID) | 89 #if defined(OS_ANDROID) |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 105 // network layer overhead, TLS overhead, and DNS. | 116 // network layer overhead, TLS overhead, and DNS. |
| 106 // TODO(amohammadkhan): Make these measured bytes more in line with number of | 117 // TODO(amohammadkhan): Make these measured bytes more in line with number of |
| 107 // bytes in lower levels. | 118 // bytes in lower levels. |
| 108 int64_t total_upload_bytes = request.GetTotalSentBytes(); | 119 int64_t total_upload_bytes = request.GetTotalSentBytes(); |
| 109 int64_t total_received_bytes = request.GetTotalReceivedBytes(); | 120 int64_t total_received_bytes = request.GetTotalReceivedBytes(); |
| 110 bool is_user_traffic = IsUserInitiatedRequest(request); | 121 bool is_user_traffic = IsUserInitiatedRequest(request); |
| 111 | 122 |
| 112 bool is_connection_cellular = | 123 bool is_connection_cellular = |
| 113 net::NetworkChangeNotifier::IsConnectionCellular( | 124 net::NetworkChangeNotifier::IsConnectionCellular( |
| 114 net::NetworkChangeNotifier::GetConnectionType()); | 125 net::NetworkChangeNotifier::GetConnectionType()); |
| 115 RecordUMAHistogramCount( | |
| 116 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | |
| 117 : "DataUse.TrafficSize.System", | |
| 118 UPSTREAM, is_connection_cellular), | |
| 119 total_upload_bytes); | |
| 120 RecordUMAHistogramCount( | |
| 121 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | |
| 122 : "DataUse.TrafficSize.System", | |
| 123 DOWNSTREAM, is_connection_cellular), | |
| 124 total_received_bytes); | |
| 125 | 126 |
| 126 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | 127 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( |
| 127 request.GetUserData(DataUseUserData::kUserDataKey)); | 128 request.GetUserData(DataUseUserData::kUserDataKey)); |
| 128 DataUseUserData::ServiceName service_name = | 129 DataUseUserData::ServiceName service_name = |
| 129 attached_service_data ? attached_service_data->service_name() | 130 attached_service_data ? attached_service_data->service_name() |
| 130 : DataUseUserData::NOT_TAGGED; | 131 : DataUseUserData::NOT_TAGGED; |
| 132 bool started_in_foreground = | |
| 133 attached_service_data | |
| 134 ? attached_service_data->app_state() == DataUseUserData::FOREGROUND | |
| 135 : CurrentAppState() == DataUseUserData::FOREGROUND; | |
| 136 | |
| 137 RecordUMAHistogramCount( | |
| 138 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | |
| 139 : "DataUse.TrafficSize.System", | |
| 140 UPSTREAM, started_in_foreground, is_connection_cellular), | |
| 141 total_upload_bytes); | |
| 142 RecordUMAHistogramCount( | |
| 143 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | |
| 144 : "DataUse.TrafficSize.System", | |
| 145 DOWNSTREAM, started_in_foreground, | |
| 146 is_connection_cellular), | |
| 147 total_received_bytes); | |
| 148 | |
| 131 if (!is_user_traffic) { | 149 if (!is_user_traffic) { |
| 132 ReportDataUsageServices(service_name, UPSTREAM, is_connection_cellular, | 150 ReportDataUsageServices(service_name, UPSTREAM, started_in_foreground, |
| 133 total_upload_bytes); | 151 is_connection_cellular, total_upload_bytes); |
| 134 ReportDataUsageServices(service_name, DOWNSTREAM, is_connection_cellular, | 152 ReportDataUsageServices(service_name, DOWNSTREAM, started_in_foreground, |
| 135 total_received_bytes); | 153 is_connection_cellular, total_received_bytes); |
| 136 } | 154 } |
| 137 | 155 |
| 138 // Update data use prefs for cellular connections. | 156 // Update data use prefs for cellular connections. |
| 139 if (!metrics_data_use_forwarder_.is_null()) { | 157 if (!metrics_data_use_forwarder_.is_null()) { |
| 140 metrics_data_use_forwarder_.Run( | 158 metrics_data_use_forwarder_.Run( |
| 141 attached_service_data->GetServiceNameAsString(service_name), | 159 attached_service_data->GetServiceNameAsString(service_name), |
| 142 total_upload_bytes + total_received_bytes, is_connection_cellular); | 160 total_upload_bytes + total_received_bytes, is_connection_cellular); |
| 143 } | 161 } |
| 144 } | 162 } |
| 145 | 163 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 157 return content::ResourceRequestInfo::ForRequest(&request) != nullptr; | 175 return content::ResourceRequestInfo::ForRequest(&request) != nullptr; |
| 158 } | 176 } |
| 159 | 177 |
| 160 #if defined(OS_ANDROID) | 178 #if defined(OS_ANDROID) |
| 161 void DataUseMeasurement::OnApplicationStateChangeForTesting( | 179 void DataUseMeasurement::OnApplicationStateChangeForTesting( |
| 162 base::android::ApplicationState application_state) { | 180 base::android::ApplicationState application_state) { |
| 163 app_state_ = application_state; | 181 app_state_ = application_state; |
| 164 } | 182 } |
| 165 #endif | 183 #endif |
| 166 | 184 |
| 167 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const { | 185 DataUseUserData::AppState DataUseMeasurement::CurrentAppState() const { |
| 168 #if defined(OS_ANDROID) | 186 #if defined(OS_ANDROID) |
| 169 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | 187 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
| 170 return BACKGROUND; | 188 return DataUseUserData::BACKGROUND; |
| 171 #endif | 189 #endif |
| 172 // If the OS is not Android, all the requests are considered Foreground. | 190 // If the OS is not Android, all the requests are considered Foreground. |
| 173 return FOREGROUND; | 191 return DataUseUserData::FOREGROUND; |
| 174 } | 192 } |
| 175 | 193 |
| 176 std::string DataUseMeasurement::GetHistogramName( | 194 std::string DataUseMeasurement::GetHistogramName( |
| 177 const char* prefix, | 195 const char* prefix, |
| 178 TrafficDirection dir, | 196 TrafficDirection dir, |
| 197 bool started_in_foreground, | |
| 179 bool is_connection_cellular) const { | 198 bool is_connection_cellular) const { |
| 180 AppState app_state = CurrentAppState(); | |
| 181 return base::StringPrintf( | 199 return base::StringPrintf( |
| 182 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", | 200 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", |
| 183 app_state == BACKGROUND ? "Background" : "Foreground", | 201 started_in_foreground ? "Foreground" : "Background", |
| 184 is_connection_cellular ? "Cellular" : "NotCellular"); | 202 is_connection_cellular ? "Cellular" : "NotCellular"); |
| 185 } | 203 } |
| 186 | 204 |
| 187 #if defined(OS_ANDROID) | 205 #if defined(OS_ANDROID) |
| 188 void DataUseMeasurement::OnApplicationStateChange( | 206 void DataUseMeasurement::OnApplicationStateChange( |
| 189 base::android::ApplicationState application_state) { | 207 base::android::ApplicationState application_state) { |
| 190 app_state_ = application_state; | 208 app_state_ = application_state; |
| 191 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | 209 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
| 192 MaybeRecordNetworkBytesOS(); | 210 MaybeRecordNetworkBytesOS(); |
| 193 } | 211 } |
| 194 | 212 |
| 195 void DataUseMeasurement::MaybeRecordNetworkBytesOS() { | 213 void DataUseMeasurement::MaybeRecordNetworkBytesOS() { |
| 196 // Minimum number of bytes that should be reported by the network delegate | 214 // 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 | 215 // before Android's TrafficStats API is queried (if Chrome is not in |
| 198 // background). This reduces the overhead of repeatedly calling the API. | 216 // background). This reduces the overhead of repeatedly calling the API. |
| 199 static const int64_t kMinDelegateBytes = 25000; | 217 static const int64_t kMinDelegateBytes = 25000; |
| 200 | 218 |
| 201 if (bytes_transferred_since_last_traffic_stats_query_ < kMinDelegateBytes && | 219 if (bytes_transferred_since_last_traffic_stats_query_ < kMinDelegateBytes && |
| 202 CurrentAppState() == FOREGROUND) { | 220 CurrentAppState() == DataUseUserData::FOREGROUND) { |
| 203 return; | 221 return; |
| 204 } | 222 } |
| 205 bytes_transferred_since_last_traffic_stats_query_ = 0; | 223 bytes_transferred_since_last_traffic_stats_query_ = 0; |
| 206 int64_t bytes = 0; | 224 int64_t bytes = 0; |
| 207 // Query Android traffic stats directly instead of registering with the | 225 // Query Android traffic stats directly instead of registering with the |
| 208 // DataUseAggregator since the latter does not provide notifications for | 226 // DataUseAggregator since the latter does not provide notifications for |
| 209 // the incognito traffic. | 227 // the incognito traffic. |
| 210 if (net::android::traffic_stats::GetCurrentUidRxBytes(&bytes)) { | 228 if (net::android::traffic_stats::GetCurrentUidRxBytes(&bytes)) { |
| 211 if (rx_bytes_os_ != 0) { | 229 if (rx_bytes_os_ != 0) { |
| 212 DCHECK_GE(bytes, rx_bytes_os_); | 230 DCHECK_GE(bytes, rx_bytes_os_); |
| 213 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.OS", bytes - rx_bytes_os_); | 231 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.OS", bytes - rx_bytes_os_); |
| 214 } | 232 } |
| 215 rx_bytes_os_ = bytes; | 233 rx_bytes_os_ = bytes; |
| 216 } | 234 } |
| 217 | 235 |
| 218 if (net::android::traffic_stats::GetCurrentUidTxBytes(&bytes)) { | 236 if (net::android::traffic_stats::GetCurrentUidTxBytes(&bytes)) { |
| 219 if (tx_bytes_os_ != 0) { | 237 if (tx_bytes_os_ != 0) { |
| 220 DCHECK_GE(bytes, tx_bytes_os_); | 238 DCHECK_GE(bytes, tx_bytes_os_); |
| 221 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.OS", bytes - tx_bytes_os_); | 239 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.OS", bytes - tx_bytes_os_); |
| 222 } | 240 } |
| 223 tx_bytes_os_ = bytes; | 241 tx_bytes_os_ = bytes; |
| 224 } | 242 } |
| 225 } | 243 } |
| 226 #endif | 244 #endif |
| 227 | 245 |
| 228 void DataUseMeasurement::ReportDataUsageServices( | 246 void DataUseMeasurement::ReportDataUsageServices( |
| 229 DataUseUserData::ServiceName service, | 247 DataUseUserData::ServiceName service, |
| 230 TrafficDirection dir, | 248 TrafficDirection dir, |
| 249 bool started_in_foreground, | |
| 231 bool is_connection_cellular, | 250 bool is_connection_cellular, |
| 232 int64_t message_size) const { | 251 int64_t message_size) const { |
| 233 RecordUMAHistogramCount( | 252 RecordUMAHistogramCount( |
| 234 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), | 253 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), |
| 235 message_size); | 254 message_size); |
| 236 if (message_size > 0) { | 255 if (message_size > 0) { |
| 237 IncreaseSparseHistogramByValue( | 256 IncreaseSparseHistogramByValue( |
| 238 GetHistogramName("DataUse.MessageSize.AllServices", dir, | 257 GetHistogramName("DataUse.MessageSize.AllServices", dir, |
| 239 is_connection_cellular), | 258 started_in_foreground, is_connection_cellular), |
| 240 service, message_size); | 259 service, message_size); |
| 241 } | 260 } |
| 242 } | 261 } |
| 243 | 262 |
| 244 } // namespace data_use_measurement | 263 } // namespace data_use_measurement |
| OLD | NEW |