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 namespace data_use_measurement { | 18 namespace data_use_measurement { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), | 53 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), |
| 53 app_listener_(new base::android::ApplicationStatusListener( | 54 app_listener_(new base::android::ApplicationStatusListener( |
| 54 base::Bind(&DataUseMeasurement::OnApplicationStateChange, | 55 base::Bind(&DataUseMeasurement::OnApplicationStateChange, |
| 55 base::Unretained(this)))) | 56 base::Unretained(this)))) |
| 56 #endif | 57 #endif |
| 57 { | 58 { |
| 58 } | 59 } |
| 59 | 60 |
| 60 DataUseMeasurement::~DataUseMeasurement(){}; | 61 DataUseMeasurement::~DataUseMeasurement(){}; |
| 61 | 62 |
| 63 void DataUseMeasurement::OnBeforeURLRequest(net::URLRequest* request) { | |
| 64 DataUseUserData* data_use_user_data = reinterpret_cast<DataUseUserData*>( | |
| 65 request->GetUserData(DataUseUserData::kUserDataKey)); | |
| 66 if (!data_use_user_data) { | |
| 67 data_use_user_data = | |
| 68 new DataUseUserData(DataUseUserData::ServiceName::NOT_TAGGED, | |
| 69 CurrentAppState() == FOREGROUND); | |
| 70 request->SetUserData(DataUseUserData::kUserDataKey, data_use_user_data); | |
| 71 } | |
| 72 } | |
| 73 | |
| 62 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, | 74 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, |
| 63 const GURL& new_location) { | 75 const GURL& new_location) { |
| 64 // Recording data use of request on redirects. | 76 // Recording data use of request on redirects. |
| 65 ReportDataUseUMA(request); | 77 ReportDataUseUMA(request); |
| 66 } | 78 } |
| 67 | 79 |
| 68 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, | 80 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, |
| 69 bool started) { | 81 bool started) { |
| 70 // TODO(amohammadkhan): Verify that there is no double recording in data use | 82 // TODO(amohammadkhan): Verify that there is no double recording in data use |
| 71 // of redirected requests. | 83 // of redirected requests. |
| 72 ReportDataUseUMA(request); | 84 ReportDataUseUMA(request); |
| 73 } | 85 } |
| 74 | 86 |
| 75 void DataUseMeasurement::ReportDataUseUMA( | 87 void DataUseMeasurement::ReportDataUseUMA( |
| 76 const net::URLRequest& request) const { | 88 const net::URLRequest& request) const { |
| 77 // Counts rely on URLRequest::GetTotalReceivedBytes() and | 89 // Counts rely on URLRequest::GetTotalReceivedBytes() and |
| 78 // URLRequest::GetTotalSentBytes(), which does not include the send path, | 90 // URLRequest::GetTotalSentBytes(), which does not include the send path, |
| 79 // network layer overhead, TLS overhead, and DNS. | 91 // network layer overhead, TLS overhead, and DNS. |
| 80 // TODO(amohammadkhan): Make these measured bytes more in line with number of | 92 // TODO(amohammadkhan): Make these measured bytes more in line with number of |
| 81 // bytes in lower levels. | 93 // bytes in lower levels. |
| 82 int64_t total_upload_bytes = request.GetTotalSentBytes(); | 94 int64_t total_upload_bytes = request.GetTotalSentBytes(); |
| 83 int64_t total_received_bytes = request.GetTotalReceivedBytes(); | 95 int64_t total_received_bytes = request.GetTotalReceivedBytes(); |
| 84 bool is_user_traffic = IsUserInitiatedRequest(request); | 96 bool is_user_traffic = IsUserInitiatedRequest(request); |
| 85 | 97 |
| 86 bool is_connection_cellular = | 98 bool is_connection_cellular = |
| 87 net::NetworkChangeNotifier::IsConnectionCellular( | 99 net::NetworkChangeNotifier::IsConnectionCellular( |
| 88 net::NetworkChangeNotifier::GetConnectionType()); | 100 net::NetworkChangeNotifier::GetConnectionType()); |
| 89 RecordUMAHistogramCount( | |
| 90 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | |
| 91 : "DataUse.TrafficSize.System", | |
| 92 UPSTREAM, is_connection_cellular), | |
| 93 total_upload_bytes); | |
| 94 RecordUMAHistogramCount( | |
| 95 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | |
| 96 : "DataUse.TrafficSize.System", | |
| 97 DOWNSTREAM, is_connection_cellular), | |
| 98 total_received_bytes); | |
| 99 | 101 |
| 100 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | 102 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( |
| 101 request.GetUserData(DataUseUserData::kUserDataKey)); | 103 request.GetUserData(DataUseUserData::kUserDataKey)); |
| 102 DataUseUserData::ServiceName service_name = | 104 DataUseUserData::ServiceName service_name = |
| 103 attached_service_data ? attached_service_data->service_name() | 105 attached_service_data ? attached_service_data->service_name() |
| 104 : DataUseUserData::NOT_TAGGED; | 106 : DataUseUserData::NOT_TAGGED; |
| 107 bool started_in_foreground = | |
|
bengr
2016/09/23 18:45:31
Might be better to have an "unknown" state.
Raj
2016/09/23 22:34:09
Done.
| |
| 108 attached_service_data ? attached_service_data->started_in_foreground() | |
| 109 : CurrentAppState() == FOREGROUND; | |
| 110 | |
| 111 RecordUMAHistogramCount( | |
| 112 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | |
| 113 : "DataUse.TrafficSize.System", | |
| 114 UPSTREAM, started_in_foreground, is_connection_cellular), | |
| 115 total_upload_bytes); | |
| 116 RecordUMAHistogramCount( | |
| 117 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | |
| 118 : "DataUse.TrafficSize.System", | |
| 119 DOWNSTREAM, started_in_foreground, | |
| 120 is_connection_cellular), | |
| 121 total_received_bytes); | |
| 122 | |
| 105 if (!is_user_traffic) { | 123 if (!is_user_traffic) { |
| 106 ReportDataUsageServices(service_name, UPSTREAM, is_connection_cellular, | 124 ReportDataUsageServices(service_name, UPSTREAM, started_in_foreground, |
| 107 total_upload_bytes); | 125 is_connection_cellular, total_upload_bytes); |
| 108 ReportDataUsageServices(service_name, DOWNSTREAM, is_connection_cellular, | 126 ReportDataUsageServices(service_name, DOWNSTREAM, started_in_foreground, |
| 109 total_received_bytes); | 127 is_connection_cellular, total_received_bytes); |
| 110 } | 128 } |
| 111 | 129 |
| 112 // Update data use prefs for cellular connections. | 130 // Update data use prefs for cellular connections. |
| 113 if (!metrics_data_use_forwarder_.is_null()) { | 131 if (!metrics_data_use_forwarder_.is_null()) { |
| 114 metrics_data_use_forwarder_.Run( | 132 metrics_data_use_forwarder_.Run( |
| 115 attached_service_data->GetServiceNameAsString(service_name), | 133 attached_service_data->GetServiceNameAsString(service_name), |
| 116 total_upload_bytes + total_received_bytes, is_connection_cellular); | 134 total_upload_bytes + total_received_bytes, is_connection_cellular); |
| 117 } | 135 } |
| 118 } | 136 } |
| 119 | 137 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 143 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | 161 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
| 144 return BACKGROUND; | 162 return BACKGROUND; |
| 145 #endif | 163 #endif |
| 146 // If the OS is not Android, all the requests are considered Foreground. | 164 // If the OS is not Android, all the requests are considered Foreground. |
| 147 return FOREGROUND; | 165 return FOREGROUND; |
| 148 } | 166 } |
| 149 | 167 |
| 150 std::string DataUseMeasurement::GetHistogramName( | 168 std::string DataUseMeasurement::GetHistogramName( |
| 151 const char* prefix, | 169 const char* prefix, |
| 152 TrafficDirection dir, | 170 TrafficDirection dir, |
| 171 bool started_in_foreground, | |
| 153 bool is_connection_cellular) const { | 172 bool is_connection_cellular) const { |
| 154 AppState app_state = CurrentAppState(); | |
| 155 return base::StringPrintf( | 173 return base::StringPrintf( |
| 156 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", | 174 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", |
| 157 app_state == BACKGROUND ? "Background" : "Foreground", | 175 started_in_foreground ? "Foreground" : "Background", |
| 158 is_connection_cellular ? "Cellular" : "NotCellular"); | 176 is_connection_cellular ? "Cellular" : "NotCellular"); |
| 159 } | 177 } |
| 160 | 178 |
| 161 #if defined(OS_ANDROID) | 179 #if defined(OS_ANDROID) |
| 162 void DataUseMeasurement::OnApplicationStateChange( | 180 void DataUseMeasurement::OnApplicationStateChange( |
| 163 base::android::ApplicationState application_state) { | 181 base::android::ApplicationState application_state) { |
| 164 app_state_ = application_state; | 182 app_state_ = application_state; |
| 165 } | 183 } |
| 166 #endif | 184 #endif |
| 167 | 185 |
| 168 void DataUseMeasurement::ReportDataUsageServices( | 186 void DataUseMeasurement::ReportDataUsageServices( |
| 169 DataUseUserData::ServiceName service, | 187 DataUseUserData::ServiceName service, |
| 170 TrafficDirection dir, | 188 TrafficDirection dir, |
| 189 bool started_in_foreground, | |
| 171 bool is_connection_cellular, | 190 bool is_connection_cellular, |
| 172 int64_t message_size) const { | 191 int64_t message_size) const { |
| 173 RecordUMAHistogramCount( | 192 RecordUMAHistogramCount( |
| 174 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), | 193 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), |
| 175 message_size); | 194 message_size); |
| 176 if (message_size > 0) { | 195 if (message_size > 0) { |
| 177 IncreaseSparseHistogramByValue( | 196 IncreaseSparseHistogramByValue( |
| 178 GetHistogramName("DataUse.MessageSize.AllServices", dir, | 197 GetHistogramName("DataUse.MessageSize.AllServices", dir, |
| 179 is_connection_cellular), | 198 started_in_foreground, is_connection_cellular), |
| 180 service, message_size); | 199 service, message_size); |
| 181 } | 200 } |
| 182 } | 201 } |
| 183 | 202 |
| 184 } // namespace data_use_measurement | 203 } // namespace data_use_measurement |
| OLD | NEW |