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/core/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_ascriber.h" | |
| 12 #include "components/data_use_measurement/core/data_use_recorder.h" | |
| 11 #include "components/data_use_measurement/core/data_use_user_data.h" | 13 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 12 #include "components/data_use_measurement/core/url_request_classifier.h" | 14 #include "components/data_use_measurement/core/url_request_classifier.h" |
| 13 #include "components/domain_reliability/uploader.h" | 15 #include "components/domain_reliability/uploader.h" |
| 14 #include "net/base/network_change_notifier.h" | 16 #include "net/base/network_change_notifier.h" |
| 15 #include "net/base/upload_data_stream.h" | 17 #include "net/base/upload_data_stream.h" |
| 16 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 17 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 18 | 20 |
| 19 #if defined(OS_ANDROID) | 21 #if defined(OS_ANDROID) |
| 20 #include "net/android/traffic_stats.h" | 22 #include "net/android/traffic_stats.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 base::TimeDelta::FromHours(1), // Maximum sample | 60 base::TimeDelta::FromHours(1), // Maximum sample |
| 59 50, // Bucket count. | 61 50, // Bucket count. |
| 60 base::HistogramBase::kUmaTargetedHistogramFlag); | 62 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 61 histogram_pointer->AddCount(latency.InMilliseconds(), count); | 63 histogram_pointer->AddCount(latency.InMilliseconds(), count); |
| 62 } | 64 } |
| 63 #endif | 65 #endif |
| 64 | 66 |
| 65 } // namespace | 67 } // namespace |
| 66 | 68 |
| 67 DataUseMeasurement::DataUseMeasurement( | 69 DataUseMeasurement::DataUseMeasurement( |
| 70 DataUseAscriber* ascriber, | |
|
RyanSturm
2016/12/16 16:26:10
nit: can you order the arguments in the same order
Raj
2016/12/16 19:59:10
Done.
| |
| 68 std::unique_ptr<URLRequestClassifier> url_request_classifier, | 71 std::unique_ptr<URLRequestClassifier> url_request_classifier, |
| 69 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder) | 72 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder) |
| 70 : url_request_classifier_(std::move(url_request_classifier)), | 73 : url_request_classifier_(std::move(url_request_classifier)), |
| 71 metrics_data_use_forwarder_(metrics_data_use_forwarder) | 74 metrics_data_use_forwarder_(metrics_data_use_forwarder), |
| 75 ascriber_(ascriber) | |
| 72 #if defined(OS_ANDROID) | 76 #if defined(OS_ANDROID) |
| 73 , | 77 , |
| 74 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), | 78 app_state_(base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES), |
| 75 app_listener_(new base::android::ApplicationStatusListener( | 79 app_listener_(new base::android::ApplicationStatusListener( |
| 76 base::Bind(&DataUseMeasurement::OnApplicationStateChange, | 80 base::Bind(&DataUseMeasurement::OnApplicationStateChange, |
| 77 base::Unretained(this)))), | 81 base::Unretained(this)))), |
| 78 rx_bytes_os_(0), | 82 rx_bytes_os_(0), |
| 79 tx_bytes_os_(0), | 83 tx_bytes_os_(0), |
| 80 bytes_transferred_since_last_traffic_stats_query_(0), | 84 bytes_transferred_since_last_traffic_stats_query_(0), |
| 81 no_reads_since_background_(false) | 85 no_reads_since_background_(false) |
| 82 #endif | 86 #endif |
| 83 { | 87 { |
| 88 DCHECK(ascriber_); | |
| 84 DCHECK(url_request_classifier_); | 89 DCHECK(url_request_classifier_); |
| 85 } | 90 } |
| 86 | 91 |
| 87 DataUseMeasurement::~DataUseMeasurement(){}; | 92 DataUseMeasurement::~DataUseMeasurement(){}; |
| 88 | 93 |
| 89 void DataUseMeasurement::OnBeforeURLRequest(net::URLRequest* request) { | 94 void DataUseMeasurement::OnBeforeURLRequest(net::URLRequest* request) { |
| 90 DataUseUserData* data_use_user_data = reinterpret_cast<DataUseUserData*>( | 95 DataUseUserData* data_use_user_data = reinterpret_cast<DataUseUserData*>( |
| 91 request->GetUserData(DataUseUserData::kUserDataKey)); | 96 request->GetUserData(DataUseUserData::kUserDataKey)); |
| 92 if (!data_use_user_data) { | 97 if (!data_use_user_data) { |
| 93 DataUseUserData::ServiceName service_name = | 98 DataUseUserData::ServiceName service_name = |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 time_since_background, bytes); | 188 time_since_background, bytes); |
| 184 if (no_reads_since_background_) { | 189 if (no_reads_since_background_) { |
| 185 no_reads_since_background_ = false; | 190 no_reads_since_background_ = false; |
| 186 IncrementLatencyHistogramByCount( | 191 IncrementLatencyHistogramByCount( |
| 187 is_user_traffic ? "DataUse.BackgroundToFirstDownstream.User" | 192 is_user_traffic ? "DataUse.BackgroundToFirstDownstream.User" |
| 188 : "DataUse.BackgroundToFirstDownstream.System", | 193 : "DataUse.BackgroundToFirstDownstream.System", |
| 189 time_since_background, 1); | 194 time_since_background, 1); |
| 190 } | 195 } |
| 191 } | 196 } |
| 192 #endif | 197 #endif |
| 198 | |
| 199 if (is_user_traffic) { | |
| 200 const auto recorder = ascriber_->GetDataUseRecorderIfExists(&request); | |
|
RyanSturm
2016/12/16 16:26:10
nit: s/auto/DataUseRecorder*/
It's a little fuzzy
Raj
2016/12/16 19:59:10
Done.
| |
| 201 if (recorder) { | |
| 202 RecordTabStateHistogram( | |
| 203 dir, new_app_state, | |
| 204 recorder->is_visible() ? TabState::FOREGROUND : TabState::BACKGROUND, | |
| 205 bytes); | |
| 206 } | |
| 207 } | |
| 193 } | 208 } |
| 194 | 209 |
| 195 void DataUseMeasurement::UpdateDataUsePrefs( | 210 void DataUseMeasurement::UpdateDataUsePrefs( |
| 196 const net::URLRequest& request) const { | 211 const net::URLRequest& request) const { |
| 197 bool is_connection_cellular = | 212 bool is_connection_cellular = |
| 198 net::NetworkChangeNotifier::IsConnectionCellular( | 213 net::NetworkChangeNotifier::IsConnectionCellular( |
| 199 net::NetworkChangeNotifier::GetConnectionType()); | 214 net::NetworkChangeNotifier::GetConnectionType()); |
| 200 | 215 |
| 201 DataUseUserData* attached_service_data = static_cast<DataUseUserData*>( | 216 DataUseUserData* attached_service_data = static_cast<DataUseUserData*>( |
| 202 request.GetUserData(DataUseUserData::kUserDataKey)); | 217 request.GetUserData(DataUseUserData::kUserDataKey)); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 323 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), | 338 "DataUse.MessageSize." + DataUseUserData::GetServiceNameAsString(service), |
| 324 message_size); | 339 message_size); |
| 325 if (message_size > 0) { | 340 if (message_size > 0) { |
| 326 IncreaseSparseHistogramByValue( | 341 IncreaseSparseHistogramByValue( |
| 327 GetHistogramName("DataUse.MessageSize.AllServices", dir, app_state, | 342 GetHistogramName("DataUse.MessageSize.AllServices", dir, app_state, |
| 328 is_connection_cellular), | 343 is_connection_cellular), |
| 329 service, message_size); | 344 service, message_size); |
| 330 } | 345 } |
| 331 } | 346 } |
| 332 | 347 |
| 348 void DataUseMeasurement::RecordTabStateHistogram( | |
| 349 TrafficDirection dir, | |
| 350 DataUseUserData::AppState app_state, | |
| 351 TabState tab_state, | |
| 352 int64_t bytes) { | |
| 353 if (app_state == DataUseUserData::UNKNOWN || tab_state == TabState::UNKNOWN) | |
| 354 return; | |
| 355 | |
| 356 RecordUMAHistogramCount( | |
| 357 base::StringPrintf("DataUse.AppTabState.%s.%s", | |
|
RyanSturm
2016/12/16 16:26:10
This expression might be a little more readable if
Raj
2016/12/16 19:59:10
Done.
RyanSturm
2016/12/19 19:31:49
Thanks.
| |
| 358 dir == UPSTREAM ? "Upstream" : "Downstream", | |
| 359 app_state == DataUseUserData::BACKGROUND | |
| 360 ? "AppBackground" | |
| 361 : tab_state == TabState::FOREGROUND | |
| 362 ? "AppForeground.TabForeground" | |
| 363 : "AppForeground.TabBackground"), | |
| 364 bytes); | |
| 365 } | |
| 366 | |
| 333 } // namespace data_use_measurement | 367 } // namespace data_use_measurement |
| OLD | NEW |