| 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/core/data_use_measurement.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.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 "components/data_use_measurement/core/data_use_user_data.h" |
| 12 #include "content/public/browser/resource_request_info.h" | |
| 13 #include "net/base/network_change_notifier.h" | 12 #include "net/base/network_change_notifier.h" |
| 14 #include "net/base/upload_data_stream.h" | 13 #include "net/base/upload_data_stream.h" |
| 15 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
| 16 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
| 17 | 16 |
| 18 #if defined(OS_ANDROID) | 17 #if defined(OS_ANDROID) |
| 19 #include "net/android/traffic_stats.h" | 18 #include "net/android/traffic_stats.h" |
| 20 #endif | 19 #endif |
| 21 | 20 |
| 22 namespace data_use_measurement { | 21 namespace data_use_measurement { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 base::TimeDelta::FromHours(1), // Maximum sample | 56 base::TimeDelta::FromHours(1), // Maximum sample |
| 58 50, // Bucket count. | 57 50, // Bucket count. |
| 59 base::HistogramBase::kUmaTargetedHistogramFlag); | 58 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 60 histogram_pointer->AddCount(latency.InMilliseconds(), count); | 59 histogram_pointer->AddCount(latency.InMilliseconds(), count); |
| 61 } | 60 } |
| 62 #endif | 61 #endif |
| 63 | 62 |
| 64 } // namespace | 63 } // namespace |
| 65 | 64 |
| 66 DataUseMeasurement::DataUseMeasurement( | 65 DataUseMeasurement::DataUseMeasurement( |
| 66 const IsUserInitiatedRequestCallbackType& |
| 67 is_user_initiated_request_callback, |
| 67 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder) | 68 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder) |
| 68 : metrics_data_use_forwarder_(metrics_data_use_forwarder) | 69 : is_user_initiated_request_callback_(is_user_initiated_request_callback), |
| 70 metrics_data_use_forwarder_(metrics_data_use_forwarder) |
| 69 #if defined(OS_ANDROID) | 71 #if defined(OS_ANDROID) |
| 70 , | 72 , |
| 71 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), | 73 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), |
| 72 app_listener_(new base::android::ApplicationStatusListener( | 74 app_listener_(new base::android::ApplicationStatusListener( |
| 73 base::Bind(&DataUseMeasurement::OnApplicationStateChange, | 75 base::Bind(&DataUseMeasurement::OnApplicationStateChange, |
| 74 base::Unretained(this)))), | 76 base::Unretained(this)))), |
| 75 rx_bytes_os_(0), | 77 rx_bytes_os_(0), |
| 76 tx_bytes_os_(0), | 78 tx_bytes_os_(0), |
| 77 bytes_transferred_since_last_traffic_stats_query_(0), | 79 bytes_transferred_since_last_traffic_stats_query_(0), |
| 78 no_reads_since_background_(false) | 80 no_reads_since_background_(false) |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // of redirected requests. | 125 // of redirected requests. |
| 124 UpdateDataUsePrefs(request); | 126 UpdateDataUsePrefs(request); |
| 125 #if defined(OS_ANDROID) | 127 #if defined(OS_ANDROID) |
| 126 MaybeRecordNetworkBytesOS(); | 128 MaybeRecordNetworkBytesOS(); |
| 127 #endif | 129 #endif |
| 128 } | 130 } |
| 129 | 131 |
| 130 void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest& request, | 132 void DataUseMeasurement::ReportDataUseUMA(const net::URLRequest& request, |
| 131 TrafficDirection dir, | 133 TrafficDirection dir, |
| 132 int64_t bytes) { | 134 int64_t bytes) { |
| 133 bool is_user_traffic = IsUserInitiatedRequest(request); | 135 bool is_user_traffic = is_user_initiated_request_callback_.Run(request); |
| 134 bool is_connection_cellular = | 136 bool is_connection_cellular = |
| 135 net::NetworkChangeNotifier::IsConnectionCellular( | 137 net::NetworkChangeNotifier::IsConnectionCellular( |
| 136 net::NetworkChangeNotifier::GetConnectionType()); | 138 net::NetworkChangeNotifier::GetConnectionType()); |
| 137 | 139 |
| 138 DataUseUserData* attached_service_data = static_cast<DataUseUserData*>( | 140 DataUseUserData* attached_service_data = static_cast<DataUseUserData*>( |
| 139 request.GetUserData(DataUseUserData::kUserDataKey)); | 141 request.GetUserData(DataUseUserData::kUserDataKey)); |
| 140 DataUseUserData::ServiceName service_name = DataUseUserData::NOT_TAGGED; | 142 DataUseUserData::ServiceName service_name = DataUseUserData::NOT_TAGGED; |
| 141 DataUseUserData::AppState old_app_state = DataUseUserData::FOREGROUND; | 143 DataUseUserData::AppState old_app_state = DataUseUserData::FOREGROUND; |
| 142 DataUseUserData::AppState new_app_state = DataUseUserData::UNKNOWN; | 144 DataUseUserData::AppState new_app_state = DataUseUserData::UNKNOWN; |
| 143 | 145 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 198 |
| 197 // Update data use prefs for cellular connections. | 199 // Update data use prefs for cellular connections. |
| 198 if (!metrics_data_use_forwarder_.is_null()) { | 200 if (!metrics_data_use_forwarder_.is_null()) { |
| 199 metrics_data_use_forwarder_.Run( | 201 metrics_data_use_forwarder_.Run( |
| 200 DataUseUserData::GetServiceNameAsString(service_name), | 202 DataUseUserData::GetServiceNameAsString(service_name), |
| 201 request.GetTotalSentBytes() + request.GetTotalReceivedBytes(), | 203 request.GetTotalSentBytes() + request.GetTotalReceivedBytes(), |
| 202 is_connection_cellular); | 204 is_connection_cellular); |
| 203 } | 205 } |
| 204 } | 206 } |
| 205 | 207 |
| 206 // static | |
| 207 bool DataUseMeasurement::IsUserInitiatedRequest( | |
| 208 const net::URLRequest& request) { | |
| 209 // Having ResourceRequestInfo in the URL request is a sign that the request is | |
| 210 // for a web content from user. For now we could add a condition to check | |
| 211 // ProcessType in info is content::PROCESS_TYPE_RENDERER, but it won't be | |
| 212 // compatible with upcoming PlzNavigate architecture. So just existence of | |
| 213 // ResourceRequestInfo is verified, and the current check should be compatible | |
| 214 // with upcoming changes in PlzNavigate. | |
| 215 // TODO(rajendrant): Verify this condition for different use cases. See | |
| 216 // crbug.com/626063. | |
| 217 return content::ResourceRequestInfo::ForRequest(&request) != nullptr; | |
| 218 } | |
| 219 | |
| 220 #if defined(OS_ANDROID) | 208 #if defined(OS_ANDROID) |
| 221 void DataUseMeasurement::OnApplicationStateChangeForTesting( | 209 void DataUseMeasurement::OnApplicationStateChangeForTesting( |
| 222 base::android::ApplicationState application_state) { | 210 base::android::ApplicationState application_state) { |
| 223 OnApplicationStateChange(application_state); | 211 OnApplicationStateChange(application_state); |
| 224 } | 212 } |
| 225 #endif | 213 #endif |
| 226 | 214 |
| 227 DataUseUserData::AppState DataUseMeasurement::CurrentAppState() const { | 215 DataUseUserData::AppState DataUseMeasurement::CurrentAppState() const { |
| 228 #if defined(OS_ANDROID) | 216 #if defined(OS_ANDROID) |
| 229 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | 217 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 message_size); | 292 message_size); |
| 305 if (message_size > 0) { | 293 if (message_size > 0) { |
| 306 IncreaseSparseHistogramByValue( | 294 IncreaseSparseHistogramByValue( |
| 307 GetHistogramName("DataUse.MessageSize.AllServices", dir, app_state, | 295 GetHistogramName("DataUse.MessageSize.AllServices", dir, app_state, |
| 308 is_connection_cellular), | 296 is_connection_cellular), |
| 309 service, message_size); | 297 service, message_size); |
| 310 } | 298 } |
| 311 } | 299 } |
| 312 | 300 |
| 313 } // namespace data_use_measurement | 301 } // namespace data_use_measurement |
| OLD | NEW |