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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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::ReportDataUseUMA( | 62 void DataUseMeasurement::ReportDataUseUMA( |
63 const net::URLRequest* request) const { | 63 const net::URLRequest* request) const { |
64 const content::ResourceRequestInfo* info = | |
65 content::ResourceRequestInfo::ForRequest(request); | |
66 // Having |info| is the sign of a request for a web content from user. For now | |
67 // we could add a condition to check ProcessType in info is | |
68 // content::PROCESS_TYPE_RENDERER, but it won't be compatible with upcoming | |
69 // PlzNavigate architecture. So just existence of |info| is verified, and the | |
70 // current check should be compatible with upcoming changes in PlzNavigate. | |
71 bool is_user_traffic = info != nullptr; | |
72 | 64 |
73 // Counts rely on URLRequest::GetTotalReceivedBytes() and | 65 // Counts rely on URLRequest::GetTotalReceivedBytes() and |
74 // URLRequest::GetTotalSentBytes(), which does not include the send path, | 66 // URLRequest::GetTotalSentBytes(), which does not include the send path, |
75 // network layer overhead, TLS overhead, and DNS. | 67 // network layer overhead, TLS overhead, and DNS. |
76 // TODO(amohammadkhan): Make these measured bytes more in line with number of | 68 // TODO(amohammadkhan): Make these measured bytes more in line with number of |
77 // bytes in lower levels. | 69 // bytes in lower levels. |
78 int64_t total_upload_bytes = request->GetTotalSentBytes(); | 70 int64_t total_upload_bytes = request->GetTotalSentBytes(); |
79 int64_t total_received_bytes = request->GetTotalReceivedBytes(); | 71 int64_t total_received_bytes = request->GetTotalReceivedBytes(); |
| 72 bool is_user_traffic = IsUserInitiatedRequest(request); |
80 | 73 |
81 bool is_connection_cellular = | 74 bool is_connection_cellular = |
82 net::NetworkChangeNotifier::IsConnectionCellular( | 75 net::NetworkChangeNotifier::IsConnectionCellular( |
83 net::NetworkChangeNotifier::GetConnectionType()); | 76 net::NetworkChangeNotifier::GetConnectionType()); |
84 RecordUMAHistogramCount( | 77 RecordUMAHistogramCount( |
85 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" | 78 GetHistogramName(is_user_traffic ? "DataUse.TrafficSize.User" |
86 : "DataUse.TrafficSize.System", | 79 : "DataUse.TrafficSize.System", |
87 UPSTREAM, is_connection_cellular), | 80 UPSTREAM, is_connection_cellular), |
88 total_upload_bytes); | 81 total_upload_bytes); |
89 RecordUMAHistogramCount( | 82 RecordUMAHistogramCount( |
(...skipping 15 matching lines...) Expand all Loading... |
105 } | 98 } |
106 | 99 |
107 // Update data use prefs for cellular connections. | 100 // Update data use prefs for cellular connections. |
108 if (!metrics_data_use_forwarder_.is_null()) { | 101 if (!metrics_data_use_forwarder_.is_null()) { |
109 metrics_data_use_forwarder_.Run( | 102 metrics_data_use_forwarder_.Run( |
110 attached_service_data->GetServiceNameAsString(service_name), | 103 attached_service_data->GetServiceNameAsString(service_name), |
111 total_upload_bytes + total_received_bytes, is_connection_cellular); | 104 total_upload_bytes + total_received_bytes, is_connection_cellular); |
112 } | 105 } |
113 } | 106 } |
114 | 107 |
| 108 // static |
| 109 bool DataUseMeasurement::IsUserInitiatedRequest( |
| 110 const net::URLRequest* request) { |
| 111 // 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 |
| 113 // ProcessType in info is content::PROCESS_TYPE_RENDERER, but it won't be |
| 114 // compatible with upcoming PlzNavigate architecture. So just existence of |
| 115 // ResourceRequestInfo is verified, and the current check should be compatible |
| 116 // with upcoming changes in PlzNavigate. |
| 117 // TODO(rajendrant): Verify this condition for different use cases. See |
| 118 // crbug.com/626063. |
| 119 return content::ResourceRequestInfo::ForRequest(request) != nullptr; |
| 120 } |
| 121 |
115 #if defined(OS_ANDROID) | 122 #if defined(OS_ANDROID) |
116 void DataUseMeasurement::OnApplicationStateChangeForTesting( | 123 void DataUseMeasurement::OnApplicationStateChangeForTesting( |
117 base::android::ApplicationState application_state) { | 124 base::android::ApplicationState application_state) { |
118 app_state_ = application_state; | 125 app_state_ = application_state; |
119 } | 126 } |
120 #endif | 127 #endif |
121 | 128 |
122 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const { | 129 DataUseMeasurement::AppState DataUseMeasurement::CurrentAppState() const { |
123 #if defined(OS_ANDROID) | 130 #if defined(OS_ANDROID) |
124 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) | 131 if (app_state_ != base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 message_size); | 163 message_size); |
157 if (message_size > 0) { | 164 if (message_size > 0) { |
158 IncreaseSparseHistogramByValue( | 165 IncreaseSparseHistogramByValue( |
159 GetHistogramName("DataUse.MessageSize.AllServices", dir, | 166 GetHistogramName("DataUse.MessageSize.AllServices", dir, |
160 is_connection_cellular), | 167 is_connection_cellular), |
161 service, message_size); | 168 service, message_size); |
162 } | 169 } |
163 } | 170 } |
164 | 171 |
165 } // namespace data_use_measurement | 172 } // namespace data_use_measurement |
OLD | NEW |