| 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" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), | 52 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), |
| 53 app_listener_(new base::android::ApplicationStatusListener( | 53 app_listener_(new base::android::ApplicationStatusListener( |
| 54 base::Bind(&DataUseMeasurement::OnApplicationStateChange, | 54 base::Bind(&DataUseMeasurement::OnApplicationStateChange, |
| 55 base::Unretained(this)))) | 55 base::Unretained(this)))) |
| 56 #endif | 56 #endif |
| 57 { | 57 { |
| 58 } | 58 } |
| 59 | 59 |
| 60 DataUseMeasurement::~DataUseMeasurement(){}; | 60 DataUseMeasurement::~DataUseMeasurement(){}; |
| 61 | 61 |
| 62 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, |
| 63 const GURL& new_location) { |
| 64 // Recording data use of request on redirects. |
| 65 ReportDataUseUMA(request); |
| 66 } |
| 67 |
| 68 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, |
| 69 bool started) { |
| 70 // TODO(amohammadkhan): Verify that there is no double recording in data use |
| 71 // of redirected requests. |
| 72 ReportDataUseUMA(request); |
| 73 } |
| 74 |
| 62 void DataUseMeasurement::ReportDataUseUMA( | 75 void DataUseMeasurement::ReportDataUseUMA( |
| 63 const net::URLRequest* request) const { | 76 const net::URLRequest& request) const { |
| 64 | |
| 65 // Counts rely on URLRequest::GetTotalReceivedBytes() and | 77 // Counts rely on URLRequest::GetTotalReceivedBytes() and |
| 66 // URLRequest::GetTotalSentBytes(), which does not include the send path, | 78 // URLRequest::GetTotalSentBytes(), which does not include the send path, |
| 67 // network layer overhead, TLS overhead, and DNS. | 79 // network layer overhead, TLS overhead, and DNS. |
| 68 // TODO(amohammadkhan): Make these measured bytes more in line with number of | 80 // TODO(amohammadkhan): Make these measured bytes more in line with number of |
| 69 // bytes in lower levels. | 81 // bytes in lower levels. |
| 70 int64_t total_upload_bytes = request->GetTotalSentBytes(); | 82 int64_t total_upload_bytes = request.GetTotalSentBytes(); |
| 71 int64_t total_received_bytes = request->GetTotalReceivedBytes(); | 83 int64_t total_received_bytes = request.GetTotalReceivedBytes(); |
| 72 bool is_user_traffic = IsUserInitiatedRequest(request); | 84 bool is_user_traffic = IsUserInitiatedRequest(request); |
| 73 | 85 |
| 74 bool is_connection_cellular = | 86 bool is_connection_cellular = |
| 75 net::NetworkChangeNotifier::IsConnectionCellular( | 87 net::NetworkChangeNotifier::IsConnectionCellular( |
| 76 net::NetworkChangeNotifier::GetConnectionType()); | 88 net::NetworkChangeNotifier::GetConnectionType()); |
| 77 RecordUMAHistogramCount( | 89 RecordUMAHistogramCount( |
| 78 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | 90 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" |
| 79 : "DataUse.TrafficSize.System", | 91 : "DataUse.TrafficSize.System", |
| 80 UPSTREAM, is_connection_cellular), | 92 UPSTREAM, is_connection_cellular), |
| 81 total_upload_bytes); | 93 total_upload_bytes); |
| 82 RecordUMAHistogramCount( | 94 RecordUMAHistogramCount( |
| 83 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | 95 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" |
| 84 : "DataUse.TrafficSize.System", | 96 : "DataUse.TrafficSize.System", |
| 85 DOWNSTREAM, is_connection_cellular), | 97 DOWNSTREAM, is_connection_cellular), |
| 86 total_received_bytes); | 98 total_received_bytes); |
| 87 | 99 |
| 88 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | 100 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( |
| 89 request->GetUserData(DataUseUserData::kUserDataKey)); | 101 request.GetUserData(DataUseUserData::kUserDataKey)); |
| 90 DataUseUserData::ServiceName service_name = | 102 DataUseUserData::ServiceName service_name = |
| 91 attached_service_data ? attached_service_data->service_name() | 103 attached_service_data ? attached_service_data->service_name() |
| 92 : DataUseUserData::NOT_TAGGED; | 104 : DataUseUserData::NOT_TAGGED; |
| 93 if (!is_user_traffic) { | 105 if (!is_user_traffic) { |
| 94 ReportDataUsageServices(service_name, UPSTREAM, is_connection_cellular, | 106 ReportDataUsageServices(service_name, UPSTREAM, is_connection_cellular, |
| 95 total_upload_bytes); | 107 total_upload_bytes); |
| 96 ReportDataUsageServices(service_name, DOWNSTREAM, is_connection_cellular, | 108 ReportDataUsageServices(service_name, DOWNSTREAM, is_connection_cellular, |
| 97 total_received_bytes); | 109 total_received_bytes); |
| 98 } | 110 } |
| 99 | 111 |
| 100 // Update data use prefs for cellular connections. | 112 // Update data use prefs for cellular connections. |
| 101 if (!metrics_data_use_forwarder_.is_null()) { | 113 if (!metrics_data_use_forwarder_.is_null()) { |
| 102 metrics_data_use_forwarder_.Run( | 114 metrics_data_use_forwarder_.Run( |
| 103 attached_service_data->GetServiceNameAsString(service_name), | 115 attached_service_data->GetServiceNameAsString(service_name), |
| 104 total_upload_bytes + total_received_bytes, is_connection_cellular); | 116 total_upload_bytes + total_received_bytes, is_connection_cellular); |
| 105 } | 117 } |
| 106 } | 118 } |
| 107 | 119 |
| 108 // static | 120 // static |
| 109 bool DataUseMeasurement::IsUserInitiatedRequest( | 121 bool DataUseMeasurement::IsUserInitiatedRequest( |
| 110 const net::URLRequest* request) { | 122 const net::URLRequest& request) { |
| 111 // Having ResourceRequestInfo in the URL request is a sign that the request is | 123 // Having ResourceRequestInfo in the URL request is a sign that the request is |
| 112 // for a web content from user. For now we could add a condition to check | 124 // for a web content from user. For now we could add a condition to check |
| 113 // ProcessType in info is content::PROCESS_TYPE_RENDERER, but it won't be | 125 // ProcessType in info is content::PROCESS_TYPE_RENDERER, but it won't be |
| 114 // compatible with upcoming PlzNavigate architecture. So just existence of | 126 // compatible with upcoming PlzNavigate architecture. So just existence of |
| 115 // ResourceRequestInfo is verified, and the current check should be compatible | 127 // ResourceRequestInfo is verified, and the current check should be compatible |
| 116 // with upcoming changes in PlzNavigate. | 128 // with upcoming changes in PlzNavigate. |
| 117 // TODO(rajendrant): Verify this condition for different use cases. See | 129 // TODO(rajendrant): Verify this condition for different use cases. See |
| 118 // crbug.com/626063. | 130 // crbug.com/626063. |
| 119 return content::ResourceRequestInfo::ForRequest(request) != nullptr; | 131 return content::ResourceRequestInfo::ForRequest(&request) != nullptr; |
| 120 } | 132 } |
| 121 | 133 |
| 122 #if defined(OS_ANDROID) | 134 #if defined(OS_ANDROID) |
| 123 void DataUseMeasurement::OnApplicationStateChangeForTesting( | 135 void DataUseMeasurement::OnApplicationStateChangeForTesting( |
| 124 base::android::ApplicationState application_state) { | 136 base::android::ApplicationState application_state) { |
| 125 app_state_ = application_state; | 137 app_state_ = application_state; |
| 126 } | 138 } |
| 127 #endif | 139 #endif |
| 128 | 140 |
| 129 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const { | 141 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 message_size); | 175 message_size); |
| 164 if (message_size > 0) { | 176 if (message_size > 0) { |
| 165 IncreaseSparseHistogramByValue( | 177 IncreaseSparseHistogramByValue( |
| 166 GetHistogramName("DataUse.MessageSize.AllServices", dir, | 178 GetHistogramName("DataUse.MessageSize.AllServices", dir, |
| 167 is_connection_cellular), | 179 is_connection_cellular), |
| 168 service, message_size); | 180 service, message_size); |
| 169 } | 181 } |
| 170 } | 182 } |
| 171 | 183 |
| 172 } // namespace data_use_measurement | 184 } // namespace data_use_measurement |
| OLD | NEW |