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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 if (!data_use_user_data) { | 73 if (!data_use_user_data) { |
74 data_use_user_data = new DataUseUserData( | 74 data_use_user_data = new DataUseUserData( |
75 DataUseUserData::ServiceName::NOT_TAGGED, CurrentAppState()); | 75 DataUseUserData::ServiceName::NOT_TAGGED, CurrentAppState()); |
76 request->SetUserData(DataUseUserData::kUserDataKey, data_use_user_data); | 76 request->SetUserData(DataUseUserData::kUserDataKey, data_use_user_data); |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, | 80 void DataUseMeasurement::OnBeforeRedirect(const net::URLRequest& request, |
81 const GURL& new_location) { | 81 const GURL& new_location) { |
82 // Recording data use of request on redirects. | 82 // Recording data use of request on redirects. |
83 ReportDataUseUMA(request); | 83 ReportDataUseUMA(request); |
Not at Google. Contact bengr
2016/10/06 22:17:48
Is this called here as well as in OnCompleted beca
Raj
2016/10/06 23:17:51
Added comment.
| |
84 } | 84 } |
85 | 85 |
86 void DataUseMeasurement::OnNetworkBytesReceived(const net::URLRequest& request, | 86 void DataUseMeasurement::OnNetworkBytesReceived(const net::URLRequest& request, |
87 int64_t bytes_received) { | 87 int64_t bytes_received) { |
88 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.Delegate", bytes_received); | 88 UMA_HISTOGRAM_COUNTS("DataUse.BytesReceived.Delegate", bytes_received); |
89 ReportDataUseUMAOnNetworkAccess(request, DOWNSTREAM, bytes_received); | |
89 #if defined(OS_ANDROID) | 90 #if defined(OS_ANDROID) |
90 bytes_transferred_since_last_traffic_stats_query_ += bytes_received; | 91 bytes_transferred_since_last_traffic_stats_query_ += bytes_received; |
91 #endif | 92 #endif |
92 } | 93 } |
93 | 94 |
94 void DataUseMeasurement::OnNetworkBytesSent(const net::URLRequest& request, | 95 void DataUseMeasurement::OnNetworkBytesSent(const net::URLRequest& request, |
95 int64_t bytes_sent) { | 96 int64_t bytes_sent) { |
96 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.Delegate", bytes_sent); | 97 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.Delegate", bytes_sent); |
98 ReportDataUseUMAOnNetworkAccess(request, UPSTREAM, bytes_sent); | |
97 #if defined(OS_ANDROID) | 99 #if defined(OS_ANDROID) |
98 bytes_transferred_since_last_traffic_stats_query_ += bytes_sent; | 100 bytes_transferred_since_last_traffic_stats_query_ += bytes_sent; |
99 #endif | 101 #endif |
100 } | 102 } |
101 | 103 |
102 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, | 104 void DataUseMeasurement::OnCompleted(const net::URLRequest& request, |
103 bool started) { | 105 bool started) { |
104 // TODO(amohammadkhan): Verify that there is no double recording in data use | 106 // TODO(amohammadkhan): Verify that there is no double recording in data use |
105 // of redirected requests. | 107 // of redirected requests. |
106 ReportDataUseUMA(request); | 108 ReportDataUseUMA(request); |
107 #if defined(OS_ANDROID) | 109 #if defined(OS_ANDROID) |
108 MaybeRecordNetworkBytesOS(); | 110 MaybeRecordNetworkBytesOS(); |
109 #endif | 111 #endif |
110 } | 112 } |
111 | 113 |
112 void DataUseMeasurement::ReportDataUseUMA( | 114 void DataUseMeasurement::ReportDataUseUMAOnNetworkAccess( |
113 const net::URLRequest& request) const { | 115 const net::URLRequest& request, |
114 // Counts rely on URLRequest::GetTotalReceivedBytes() and | 116 TrafficDirection dir, |
115 // URLRequest::GetTotalSentBytes(), which does not include the send path, | 117 int64_t bytes) const { |
116 // network layer overhead, TLS overhead, and DNS. | |
117 // TODO(amohammadkhan): Make these measured bytes more in line with number of | |
118 // bytes in lower levels. | |
119 int64_t total_upload_bytes = request.GetTotalSentBytes(); | |
120 int64_t total_received_bytes = request.GetTotalReceivedBytes(); | |
121 bool is_user_traffic = IsUserInitiatedRequest(request); | 118 bool is_user_traffic = IsUserInitiatedRequest(request); |
122 | |
123 bool is_connection_cellular = | 119 bool is_connection_cellular = |
124 net::NetworkChangeNotifier::IsConnectionCellular( | 120 net::NetworkChangeNotifier::IsConnectionCellular( |
125 net::NetworkChangeNotifier::GetConnectionType()); | 121 net::NetworkChangeNotifier::GetConnectionType()); |
126 | 122 |
127 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | 123 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( |
128 request.GetUserData(DataUseUserData::kUserDataKey)); | 124 request.GetUserData(DataUseUserData::kUserDataKey)); |
129 DataUseUserData::ServiceName service_name = | 125 DataUseUserData::ServiceName service_name = |
Not at Google. Contact bengr
2016/10/06 22:17:47
To avoid repeating the ternary operator, suggest:
Raj
2016/10/06 23:17:51
Done.
| |
130 attached_service_data ? attached_service_data->service_name() | 126 attached_service_data ? attached_service_data->service_name() |
131 : DataUseUserData::NOT_TAGGED; | 127 : DataUseUserData::NOT_TAGGED; |
132 bool started_in_foreground = | 128 DataUseUserData::AppState old_app_state = |
133 attached_service_data | 129 attached_service_data ? attached_service_data->app_state() |
134 ? attached_service_data->app_state() == DataUseUserData::FOREGROUND | 130 : DataUseUserData::FOREGROUND; |
135 : CurrentAppState() == DataUseUserData::FOREGROUND; | 131 DataUseUserData::AppState new_app_state = old_app_state == CurrentAppState() |
Not at Google. Contact bengr
2016/10/06 22:17:48
Why not just use CurrentAppState()?
Raj
2016/10/06 23:17:51
CurrentAppState() only returns FOREGROUND or BACKG
| |
132 ? old_app_state | |
133 : DataUseUserData::UNKNOWN; | |
134 | |
135 if (attached_service_data && old_app_state != new_app_state) | |
136 attached_service_data->set_app_state(CurrentAppState()); | |
136 | 137 |
137 RecordUMAHistogramCount( | 138 RecordUMAHistogramCount( |
138 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | 139 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" |
139 : "DataUse.TrafficSize.System", | 140 : "DataUse.TrafficSize.System", |
140 UPSTREAM, started_in_foreground, is_connection_cellular), | 141 dir, new_app_state, is_connection_cellular), |
141 total_upload_bytes); | 142 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 | 143 |
149 if (!is_user_traffic) { | 144 if (!is_user_traffic) { |
150 ReportDataUsageServices(service_name, UPSTREAM, started_in_foreground, | 145 ReportDataUsageServices(service_name, dir, new_app_state, |
151 is_connection_cellular, total_upload_bytes); | 146 is_connection_cellular, bytes); |
152 ReportDataUsageServices(service_name, DOWNSTREAM, started_in_foreground, | |
153 is_connection_cellular, total_received_bytes); | |
154 } | 147 } |
148 } | |
149 | |
150 void DataUseMeasurement::ReportDataUseUMA( | |
Not at Google. Contact bengr
2016/10/06 22:17:47
Looks like this is just updating prefs and not rep
Raj
2016/10/06 23:17:51
Done.
| |
151 const net::URLRequest& request) const { | |
152 bool is_connection_cellular = | |
153 net::NetworkChangeNotifier::IsConnectionCellular( | |
154 net::NetworkChangeNotifier::GetConnectionType()); | |
155 | |
156 DataUseUserData* attached_service_data = reinterpret_cast<DataUseUserData*>( | |
Not at Google. Contact bengr
2016/10/06 22:17:47
Can this be a static_cast?
Raj
2016/10/06 23:17:51
Done.
| |
157 request.GetUserData(DataUseUserData::kUserDataKey)); | |
158 DataUseUserData::ServiceName service_name = | |
159 attached_service_data ? attached_service_data->service_name() | |
160 : DataUseUserData::NOT_TAGGED; | |
155 | 161 |
156 // Update data use prefs for cellular connections. | 162 // Update data use prefs for cellular connections. |
157 if (!metrics_data_use_forwarder_.is_null()) { | 163 if (!metrics_data_use_forwarder_.is_null()) { |
158 metrics_data_use_forwarder_.Run( | 164 metrics_data_use_forwarder_.Run( |
159 attached_service_data->GetServiceNameAsString(service_name), | 165 DataUseUserData::GetServiceNameAsString(service_name), |
160 total_upload_bytes + total_received_bytes, is_connection_cellular); | 166 request.GetTotalSentBytes() + request.GetTotalReceivedBytes(), |
167 is_connection_cellular); | |
161 } | 168 } |
162 } | 169 } |
163 | 170 |
164 // static | 171 // static |
165 bool DataUseMeasurement::IsUserInitiatedRequest( | 172 bool DataUseMeasurement::IsUserInitiatedRequest( |
166 const net::URLRequest& request) { | 173 const net::URLRequest& request) { |
167 // Having ResourceRequestInfo in the URL request is a sign that the request is | 174 // Having ResourceRequestInfo in the URL request is a sign that the request is |
168 // for a web content from user. For now we could add a condition to check | 175 // for a web content from user. For now we could add a condition to check |
169 // ProcessType in info is content::PROCESS_TYPE_RENDERER, but it won't be | 176 // ProcessType in info is content::PROCESS_TYPE_RENDERER, but it won't be |
170 // compatible with upcoming PlzNavigate architecture. So just existence of | 177 // compatible with upcoming PlzNavigate architecture. So just existence of |
(...skipping 16 matching lines...) Expand all Loading... | |
187 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | 194 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
188 return DataUseUserData::BACKGROUND; | 195 return DataUseUserData::BACKGROUND; |
189 #endif | 196 #endif |
190 // If the OS is not Android, all the requests are considered Foreground. | 197 // If the OS is not Android, all the requests are considered Foreground. |
191 return DataUseUserData::FOREGROUND; | 198 return DataUseUserData::FOREGROUND; |
192 } | 199 } |
193 | 200 |
194 std::string DataUseMeasurement::GetHistogramName( | 201 std::string DataUseMeasurement::GetHistogramName( |
195 const char* prefix, | 202 const char* prefix, |
196 TrafficDirection dir, | 203 TrafficDirection dir, |
197 bool started_in_foreground, | 204 DataUseUserData::AppState app_state, |
198 bool is_connection_cellular) const { | 205 bool is_connection_cellular) const { |
199 return base::StringPrintf( | 206 return base::StringPrintf( |
200 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", | 207 "%s.%s.%s.%s", prefix, dir == UPSTREAM ? "Upstream" : "Downstream", |
201 started_in_foreground ? "Foreground" : "Background", | 208 app_state == DataUseUserData::UNKNOWN |
209 ? "Unknown" | |
210 : (app_state == DataUseUserData::FOREGROUND ? "Foreground" | |
211 : "Background"), | |
202 is_connection_cellular ? "Cellular" : "NotCellular"); | 212 is_connection_cellular ? "Cellular" : "NotCellular"); |
203 } | 213 } |
204 | 214 |
205 #if defined(OS_ANDROID) | 215 #if defined(OS_ANDROID) |
206 void DataUseMeasurement::OnApplicationStateChange( | 216 void DataUseMeasurement::OnApplicationStateChange( |
207 base::android::ApplicationState application_state) { | 217 base::android::ApplicationState application_state) { |
208 app_state_ = application_state; | 218 app_state_ = application_state; |
209 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | 219 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
210 MaybeRecordNetworkBytesOS(); | 220 MaybeRecordNetworkBytesOS(); |
211 } | 221 } |
(...skipping 27 matching lines...) Expand all Loading... | |
239 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.OS", bytes - tx_bytes_os_); | 249 UMA_HISTOGRAM_COUNTS("DataUse.BytesSent.OS", bytes - tx_bytes_os_); |
240 } | 250 } |
241 tx_bytes_os_ = bytes; | 251 tx_bytes_os_ = bytes; |
242 } | 252 } |
243 } | 253 } |
244 #endif | 254 #endif |
245 | 255 |
246 void DataUseMeasurement::ReportDataUsageServices( | 256 void DataUseMeasurement::ReportDataUsageServices( |
247 DataUseUserData::ServiceName service, | 257 DataUseUserData::ServiceName service, |
248 TrafficDirection dir, | 258 TrafficDirection dir, |
249 bool started_in_foreground, | 259 DataUseUserData::AppState app_state, |
250 bool is_connection_cellular, | 260 bool is_connection_cellular, |
251 int64_t message_size) const { | 261 int64_t message_size) const { |
252 RecordUMAHistogramCount( | 262 RecordUMAHistogramCount( |
253 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), | 263 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), |
254 message_size); | 264 message_size); |
255 if (message_size > 0) { | 265 if (message_size > 0) { |
256 IncreaseSparseHistogramByValue( | 266 IncreaseSparseHistogramByValue( |
257 GetHistogramName("DataUse.MessageSize.AllServices", dir, | 267 GetHistogramName("DataUse.MessageSize.AllServices", dir, app_state, |
258 started_in_foreground, is_connection_cellular), | 268 is_connection_cellular), |
259 service, message_size); | 269 service, message_size); |
260 } | 270 } |
261 } | 271 } |
262 | 272 |
263 } // namespace data_use_measurement | 273 } // namespace data_use_measurement |
OLD | NEW |