OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/metrics/net/network_metrics_provider.h" | 5 #include "components/metrics/net/network_metrics_provider.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind_helpers.h" | |
13 #include "base/callback_forward.h" | |
12 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
13 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
14 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
15 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
17 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
18 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
19 #include "build/build_config.h" | 21 #include "build/build_config.h" |
20 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
23 #include "net/nqe/network_quality_estimator.h" | |
21 | 24 |
22 #if defined(OS_CHROMEOS) | 25 #if defined(OS_CHROMEOS) |
23 #include "components/metrics/net/wifi_access_point_info_provider_chromeos.h" | 26 #include "components/metrics/net/wifi_access_point_info_provider_chromeos.h" |
24 #endif // OS_CHROMEOS | 27 #endif // OS_CHROMEOS |
25 | 28 |
26 namespace metrics { | 29 namespace metrics { |
27 | 30 |
28 NetworkMetricsProvider::NetworkMetricsProvider(base::TaskRunner* io_task_runner) | 31 NetworkMetricsProvider::NetworkMetricsProvider(base::TaskRunner* io_task_runner) |
29 : io_task_runner_(io_task_runner), | 32 : io_task_runner_(io_task_runner), |
30 connection_type_is_ambiguous_(false), | 33 connection_type_is_ambiguous_(false), |
31 wifi_phy_layer_protocol_is_ambiguous_(false), | 34 wifi_phy_layer_protocol_is_ambiguous_(false), |
32 wifi_phy_layer_protocol_(net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN), | 35 wifi_phy_layer_protocol_(net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN), |
33 total_aborts_(0), | 36 total_aborts_(0), |
34 total_codes_(0), | 37 total_codes_(0), |
38 effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), | |
39 effective_connection_type_is_ambiguous_(false), | |
35 weak_ptr_factory_(this) { | 40 weak_ptr_factory_(this) { |
36 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 41 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
37 connection_type_ = net::NetworkChangeNotifier::GetConnectionType(); | 42 connection_type_ = net::NetworkChangeNotifier::GetConnectionType(); |
38 ProbeWifiPHYLayerProtocol(); | 43 ProbeWifiPHYLayerProtocol(); |
39 } | 44 } |
40 | 45 |
41 NetworkMetricsProvider::~NetworkMetricsProvider() { | 46 NetworkMetricsProvider::~NetworkMetricsProvider() { |
47 DCHECK(thread_checker_.CalledOnValidThread()); | |
42 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 48 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
49 if (effective_connection_type_observer_ && | |
50 !network_quality_task_runner_->DeleteSoon( | |
51 FROM_HERE, effective_connection_type_observer_.release())) { | |
52 NOTIMPLEMENTED() << " ECT observer was not deleted successfully"; | |
53 } | |
43 } | 54 } |
44 | 55 |
45 void NetworkMetricsProvider::ProvideGeneralMetrics( | 56 void NetworkMetricsProvider::ProvideGeneralMetrics( |
46 ChromeUserMetricsExtension*) { | 57 ChromeUserMetricsExtension*) { |
58 DCHECK(thread_checker_.CalledOnValidThread()); | |
47 // ProvideGeneralMetrics is called on the main thread, at the time a metrics | 59 // ProvideGeneralMetrics is called on the main thread, at the time a metrics |
48 // record is being finalized. | 60 // record is being finalized. |
49 net::NetworkChangeNotifier::FinalizingMetricsLogRecord(); | 61 net::NetworkChangeNotifier::FinalizingMetricsLogRecord(); |
50 LogAggregatedMetrics(); | 62 LogAggregatedMetrics(); |
51 } | 63 } |
52 | 64 |
53 void NetworkMetricsProvider::ProvideSystemProfileMetrics( | 65 void NetworkMetricsProvider::ProvideSystemProfileMetrics( |
54 SystemProfileProto* system_profile) { | 66 SystemProfileProto* system_profile) { |
67 DCHECK(thread_checker_.CalledOnValidThread()); | |
55 SystemProfileProto::Network* network = system_profile->mutable_network(); | 68 SystemProfileProto::Network* network = system_profile->mutable_network(); |
56 network->set_connection_type_is_ambiguous(connection_type_is_ambiguous_); | 69 network->set_connection_type_is_ambiguous(connection_type_is_ambiguous_); |
57 network->set_connection_type(GetConnectionType()); | 70 network->set_connection_type(GetConnectionType()); |
58 network->set_wifi_phy_layer_protocol_is_ambiguous( | 71 network->set_wifi_phy_layer_protocol_is_ambiguous( |
59 wifi_phy_layer_protocol_is_ambiguous_); | 72 wifi_phy_layer_protocol_is_ambiguous_); |
60 network->set_wifi_phy_layer_protocol(GetWifiPHYLayerProtocol()); | 73 network->set_wifi_phy_layer_protocol(GetWifiPHYLayerProtocol()); |
74 network->set_effective_connection_type(GetEffectiveConnectionType()); | |
61 | 75 |
62 // Update the connection type. Note that this is necessary to set the network | 76 // Update the connection type. Note that this is necessary to set the network |
63 // type to "none" if there is no network connection for an entire UMA logging | 77 // type to "none" if there is no network connection for an entire UMA logging |
64 // window, since OnConnectionTypeChanged() ignores transitions to the "none" | 78 // window, since OnConnectionTypeChanged() ignores transitions to the "none" |
65 // state. | 79 // state. |
66 connection_type_ = net::NetworkChangeNotifier::GetConnectionType(); | 80 connection_type_ = net::NetworkChangeNotifier::GetConnectionType(); |
67 // Reset the "ambiguous" flags, since a new metrics log session has started. | 81 // Reset the "ambiguous" flags, since a new metrics log session has started. |
68 connection_type_is_ambiguous_ = false; | 82 connection_type_is_ambiguous_ = false; |
69 wifi_phy_layer_protocol_is_ambiguous_ = false; | 83 wifi_phy_layer_protocol_is_ambiguous_ = false; |
84 effective_connection_type_is_ambiguous_ = false; | |
70 | 85 |
71 if (!wifi_access_point_info_provider_.get()) { | 86 if (!wifi_access_point_info_provider_.get()) { |
72 #if defined(OS_CHROMEOS) | 87 #if defined(OS_CHROMEOS) |
73 wifi_access_point_info_provider_.reset( | 88 wifi_access_point_info_provider_.reset( |
74 new WifiAccessPointInfoProviderChromeos()); | 89 new WifiAccessPointInfoProviderChromeos()); |
75 #else | 90 #else |
76 wifi_access_point_info_provider_.reset( | 91 wifi_access_point_info_provider_.reset( |
77 new WifiAccessPointInfoProvider()); | 92 new WifiAccessPointInfoProvider()); |
78 #endif // OS_CHROMEOS | 93 #endif // OS_CHROMEOS |
79 } | 94 } |
80 | 95 |
81 // Connected wifi access point information. | 96 // Connected wifi access point information. |
82 WifiAccessPointInfoProvider::WifiAccessPointInfo info; | 97 WifiAccessPointInfoProvider::WifiAccessPointInfo info; |
83 if (wifi_access_point_info_provider_->GetInfo(&info)) | 98 if (wifi_access_point_info_provider_->GetInfo(&info)) |
84 WriteWifiAccessPointProto(info, network); | 99 WriteWifiAccessPointProto(info, network); |
85 } | 100 } |
86 | 101 |
87 void NetworkMetricsProvider::OnConnectionTypeChanged( | 102 void NetworkMetricsProvider::OnConnectionTypeChanged( |
88 net::NetworkChangeNotifier::ConnectionType type) { | 103 net::NetworkChangeNotifier::ConnectionType type) { |
104 DCHECK(thread_checker_.CalledOnValidThread()); | |
89 // To avoid reporting an ambiguous connection type for users on flaky | 105 // To avoid reporting an ambiguous connection type for users on flaky |
90 // connections, ignore transitions to the "none" state. Note that the | 106 // connections, ignore transitions to the "none" state. Note that the |
91 // connection type is refreshed in ProvideSystemProfileMetrics() each time a | 107 // connection type is refreshed in ProvideSystemProfileMetrics() each time a |
92 // new UMA logging window begins, so users who genuinely transition to offline | 108 // new UMA logging window begins, so users who genuinely transition to offline |
93 // mode for an extended duration will still be at least partially represented | 109 // mode for an extended duration will still be at least partially represented |
94 // in the metrics logs. | 110 // in the metrics logs. |
95 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) | 111 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) |
96 return; | 112 return; |
97 | 113 |
98 if (type != connection_type_ && | 114 if (type != connection_type_ && |
99 connection_type_ != net::NetworkChangeNotifier::CONNECTION_NONE) { | 115 connection_type_ != net::NetworkChangeNotifier::CONNECTION_NONE) { |
100 connection_type_is_ambiguous_ = true; | 116 connection_type_is_ambiguous_ = true; |
101 } | 117 } |
102 connection_type_ = type; | 118 connection_type_ = type; |
103 | 119 |
104 ProbeWifiPHYLayerProtocol(); | 120 ProbeWifiPHYLayerProtocol(); |
105 } | 121 } |
106 | 122 |
107 SystemProfileProto::Network::ConnectionType | 123 SystemProfileProto::Network::ConnectionType |
108 NetworkMetricsProvider::GetConnectionType() const { | 124 NetworkMetricsProvider::GetConnectionType() const { |
125 DCHECK(thread_checker_.CalledOnValidThread()); | |
109 switch (connection_type_) { | 126 switch (connection_type_) { |
110 case net::NetworkChangeNotifier::CONNECTION_NONE: | 127 case net::NetworkChangeNotifier::CONNECTION_NONE: |
111 return SystemProfileProto::Network::CONNECTION_NONE; | 128 return SystemProfileProto::Network::CONNECTION_NONE; |
112 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN: | 129 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN: |
113 return SystemProfileProto::Network::CONNECTION_UNKNOWN; | 130 return SystemProfileProto::Network::CONNECTION_UNKNOWN; |
114 case net::NetworkChangeNotifier::CONNECTION_ETHERNET: | 131 case net::NetworkChangeNotifier::CONNECTION_ETHERNET: |
115 return SystemProfileProto::Network::CONNECTION_ETHERNET; | 132 return SystemProfileProto::Network::CONNECTION_ETHERNET; |
116 case net::NetworkChangeNotifier::CONNECTION_WIFI: | 133 case net::NetworkChangeNotifier::CONNECTION_WIFI: |
117 return SystemProfileProto::Network::CONNECTION_WIFI; | 134 return SystemProfileProto::Network::CONNECTION_WIFI; |
118 case net::NetworkChangeNotifier::CONNECTION_2G: | 135 case net::NetworkChangeNotifier::CONNECTION_2G: |
119 return SystemProfileProto::Network::CONNECTION_2G; | 136 return SystemProfileProto::Network::CONNECTION_2G; |
120 case net::NetworkChangeNotifier::CONNECTION_3G: | 137 case net::NetworkChangeNotifier::CONNECTION_3G: |
121 return SystemProfileProto::Network::CONNECTION_3G; | 138 return SystemProfileProto::Network::CONNECTION_3G; |
122 case net::NetworkChangeNotifier::CONNECTION_4G: | 139 case net::NetworkChangeNotifier::CONNECTION_4G: |
123 return SystemProfileProto::Network::CONNECTION_4G; | 140 return SystemProfileProto::Network::CONNECTION_4G; |
124 case net::NetworkChangeNotifier::CONNECTION_BLUETOOTH: | 141 case net::NetworkChangeNotifier::CONNECTION_BLUETOOTH: |
125 return SystemProfileProto::Network::CONNECTION_BLUETOOTH; | 142 return SystemProfileProto::Network::CONNECTION_BLUETOOTH; |
126 } | 143 } |
127 NOTREACHED(); | 144 NOTREACHED(); |
128 return SystemProfileProto::Network::CONNECTION_UNKNOWN; | 145 return SystemProfileProto::Network::CONNECTION_UNKNOWN; |
129 } | 146 } |
130 | 147 |
131 SystemProfileProto::Network::WifiPHYLayerProtocol | 148 SystemProfileProto::Network::WifiPHYLayerProtocol |
132 NetworkMetricsProvider::GetWifiPHYLayerProtocol() const { | 149 NetworkMetricsProvider::GetWifiPHYLayerProtocol() const { |
150 DCHECK(thread_checker_.CalledOnValidThread()); | |
133 switch (wifi_phy_layer_protocol_) { | 151 switch (wifi_phy_layer_protocol_) { |
134 case net::WIFI_PHY_LAYER_PROTOCOL_NONE: | 152 case net::WIFI_PHY_LAYER_PROTOCOL_NONE: |
135 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_NONE; | 153 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_NONE; |
136 case net::WIFI_PHY_LAYER_PROTOCOL_ANCIENT: | 154 case net::WIFI_PHY_LAYER_PROTOCOL_ANCIENT: |
137 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_ANCIENT; | 155 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_ANCIENT; |
138 case net::WIFI_PHY_LAYER_PROTOCOL_A: | 156 case net::WIFI_PHY_LAYER_PROTOCOL_A: |
139 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_A; | 157 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_A; |
140 case net::WIFI_PHY_LAYER_PROTOCOL_B: | 158 case net::WIFI_PHY_LAYER_PROTOCOL_B: |
141 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_B; | 159 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_B; |
142 case net::WIFI_PHY_LAYER_PROTOCOL_G: | 160 case net::WIFI_PHY_LAYER_PROTOCOL_G: |
143 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_G; | 161 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_G; |
144 case net::WIFI_PHY_LAYER_PROTOCOL_N: | 162 case net::WIFI_PHY_LAYER_PROTOCOL_N: |
145 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_N; | 163 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_N; |
146 case net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN: | 164 case net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN: |
147 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | 165 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; |
148 } | 166 } |
149 NOTREACHED(); | 167 NOTREACHED(); |
150 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | 168 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; |
151 } | 169 } |
152 | 170 |
171 SystemProfileProto::Network::EffectiveConnectionType | |
172 NetworkMetricsProvider::GetEffectiveConnectionType() const { | |
173 DCHECK(thread_checker_.CalledOnValidThread()); | |
174 | |
175 if (effective_connection_type_is_ambiguous_) | |
176 return SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_AMBIGUOUS; | |
177 | |
178 switch (effective_connection_type_) { | |
179 case net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN: | |
180 return SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; | |
181 case net::EFFECTIVE_CONNECTION_TYPE_OFFLINE: | |
182 return SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_OFFLINE; | |
183 case net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G: | |
184 return SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_SLOW_2G; | |
185 case net::EFFECTIVE_CONNECTION_TYPE_2G: | |
186 return SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G; | |
187 case net::EFFECTIVE_CONNECTION_TYPE_3G: | |
188 return SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_3G; | |
189 case net::EFFECTIVE_CONNECTION_TYPE_4G: | |
190 return SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_4G; | |
191 case net::EFFECTIVE_CONNECTION_TYPE_LAST: | |
192 NOTREACHED(); | |
193 return SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; | |
194 } | |
195 NOTREACHED(); | |
196 return SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; | |
197 } | |
198 | |
153 void NetworkMetricsProvider::ProbeWifiPHYLayerProtocol() { | 199 void NetworkMetricsProvider::ProbeWifiPHYLayerProtocol() { |
200 DCHECK(thread_checker_.CalledOnValidThread()); | |
154 PostTaskAndReplyWithResult( | 201 PostTaskAndReplyWithResult( |
155 io_task_runner_, | 202 io_task_runner_, |
156 FROM_HERE, | 203 FROM_HERE, |
157 base::Bind(&net::GetWifiPHYLayerProtocol), | 204 base::Bind(&net::GetWifiPHYLayerProtocol), |
158 base::Bind(&NetworkMetricsProvider::OnWifiPHYLayerProtocolResult, | 205 base::Bind(&NetworkMetricsProvider::OnWifiPHYLayerProtocolResult, |
159 weak_ptr_factory_.GetWeakPtr())); | 206 weak_ptr_factory_.GetWeakPtr())); |
160 } | 207 } |
161 | 208 |
162 void NetworkMetricsProvider::OnWifiPHYLayerProtocolResult( | 209 void NetworkMetricsProvider::OnWifiPHYLayerProtocolResult( |
163 net::WifiPHYLayerProtocol mode) { | 210 net::WifiPHYLayerProtocol mode) { |
211 DCHECK(thread_checker_.CalledOnValidThread()); | |
164 if (wifi_phy_layer_protocol_ != net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN && | 212 if (wifi_phy_layer_protocol_ != net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN && |
165 mode != wifi_phy_layer_protocol_) { | 213 mode != wifi_phy_layer_protocol_) { |
166 wifi_phy_layer_protocol_is_ambiguous_ = true; | 214 wifi_phy_layer_protocol_is_ambiguous_ = true; |
167 } | 215 } |
168 wifi_phy_layer_protocol_ = mode; | 216 wifi_phy_layer_protocol_ = mode; |
169 } | 217 } |
170 | 218 |
171 void NetworkMetricsProvider::WriteWifiAccessPointProto( | 219 void NetworkMetricsProvider::WriteWifiAccessPointProto( |
172 const WifiAccessPointInfoProvider::WifiAccessPointInfo& info, | 220 const WifiAccessPointInfoProvider::WifiAccessPointInfo& info, |
173 SystemProfileProto::Network* network_proto) { | 221 SystemProfileProto::Network* network_proto) { |
222 DCHECK(thread_checker_.CalledOnValidThread()); | |
174 SystemProfileProto::Network::WifiAccessPoint* access_point_info = | 223 SystemProfileProto::Network::WifiAccessPoint* access_point_info = |
175 network_proto->mutable_access_point_info(); | 224 network_proto->mutable_access_point_info(); |
176 SystemProfileProto::Network::WifiAccessPoint::SecurityMode security = | 225 SystemProfileProto::Network::WifiAccessPoint::SecurityMode security = |
177 SystemProfileProto::Network::WifiAccessPoint::SECURITY_UNKNOWN; | 226 SystemProfileProto::Network::WifiAccessPoint::SECURITY_UNKNOWN; |
178 switch (info.security) { | 227 switch (info.security) { |
179 case WifiAccessPointInfoProvider::WIFI_SECURITY_NONE: | 228 case WifiAccessPointInfoProvider::WIFI_SECURITY_NONE: |
180 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_NONE; | 229 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_NONE; |
181 break; | 230 break; |
182 case WifiAccessPointInfoProvider::WIFI_SECURITY_WPA: | 231 case WifiAccessPointInfoProvider::WIFI_SECURITY_WPA: |
183 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_WPA; | 232 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_WPA; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 info.oui_list, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 288 info.oui_list, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
240 uint32_t oui; | 289 uint32_t oui; |
241 if (base::HexStringToUInt(oui_str, &oui)) | 290 if (base::HexStringToUInt(oui_str, &oui)) |
242 vendor->add_element_identifier(oui); | 291 vendor->add_element_identifier(oui); |
243 else | 292 else |
244 NOTREACHED(); | 293 NOTREACHED(); |
245 } | 294 } |
246 } | 295 } |
247 | 296 |
248 void NetworkMetricsProvider::LogAggregatedMetrics() { | 297 void NetworkMetricsProvider::LogAggregatedMetrics() { |
298 DCHECK(thread_checker_.CalledOnValidThread()); | |
249 base::HistogramBase* error_codes = base::SparseHistogram::FactoryGet( | 299 base::HistogramBase* error_codes = base::SparseHistogram::FactoryGet( |
250 "Net.ErrorCodesForMainFrame3", | 300 "Net.ErrorCodesForMainFrame3", |
251 base::HistogramBase::kUmaTargetedHistogramFlag); | 301 base::HistogramBase::kUmaTargetedHistogramFlag); |
252 std::unique_ptr<base::HistogramSamples> samples = | 302 std::unique_ptr<base::HistogramSamples> samples = |
253 error_codes->SnapshotSamples(); | 303 error_codes->SnapshotSamples(); |
254 base::HistogramBase::Count new_aborts = | 304 base::HistogramBase::Count new_aborts = |
255 samples->GetCount(-net::ERR_ABORTED) - total_aborts_; | 305 samples->GetCount(-net::ERR_ABORTED) - total_aborts_; |
256 base::HistogramBase::Count new_codes = samples->TotalCount() - total_codes_; | 306 base::HistogramBase::Count new_codes = samples->TotalCount() - total_codes_; |
257 if (new_codes > 0) { | 307 if (new_codes > 0) { |
258 UMA_HISTOGRAM_COUNTS("Net.ErrAborted.CountPerUpload", new_aborts); | 308 UMA_HISTOGRAM_COUNTS("Net.ErrAborted.CountPerUpload", new_aborts); |
259 UMA_HISTOGRAM_PERCENTAGE("Net.ErrAborted.ProportionPerUpload", | 309 UMA_HISTOGRAM_PERCENTAGE("Net.ErrAborted.ProportionPerUpload", |
260 (100 * new_aborts) / new_codes); | 310 (100 * new_aborts) / new_codes); |
261 total_codes_ += new_codes; | 311 total_codes_ += new_codes; |
262 total_aborts_ += new_aborts; | 312 total_aborts_ += new_aborts; |
263 } | 313 } |
264 } | 314 } |
265 | 315 |
316 void NetworkMetricsProvider::OnEffectiveConnectionTypeChanged( | |
317 net::EffectiveConnectionType type) { | |
318 DCHECK(thread_checker_.CalledOnValidThread()); | |
319 if (effective_connection_type_ != type && | |
320 type != net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN && | |
321 effective_connection_type_ != net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) { | |
322 effective_connection_type_is_ambiguous_ = true; | |
323 } | |
324 effective_connection_type_ = type; | |
325 } | |
326 | |
327 // Listens to the changes in the effective conection type. | |
328 class NetworkMetricsProvider::EffectiveConnectionTypeObserver | |
329 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { | |
330 public: | |
331 // |network_quality_estimator| is used to provide the network quality | |
332 // estimates. Guaranteed to be non-null. |callback| is run on | |
333 // |callback_task_runner|, and provides notifications about the changes in the | |
334 // effective connection type. | |
335 EffectiveConnectionTypeObserver( | |
336 base::Callback<void(net::EffectiveConnectionType)> callback, | |
337 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner) | |
338 : network_quality_estimator_(nullptr), | |
339 callback_(callback), | |
340 callback_task_runner_(callback_task_runner) { | |
341 DCHECK(callback_); | |
342 DCHECK(callback_task_runner_); | |
343 // |this| is initialized and used on the IO thread. | |
RyanSturm
2016/12/28 21:53:11
nit maybe: s/IO/network/
tbansal1
2017/01/03 17:25:13
Done.
| |
344 thread_checker_.DetachFromThread(); | |
345 } | |
346 | |
347 ~EffectiveConnectionTypeObserver() override { | |
348 DCHECK(thread_checker_.CalledOnValidThread()); | |
349 if (network_quality_estimator_) | |
350 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); | |
351 } | |
352 | |
353 // Initializes |this| on IO thread, which is same thread where | |
RyanSturm
2016/12/28 21:53:11
nit:s/same/the same/
RyanSturm
2016/12/28 21:53:11
nit:s/IO/network/
tbansal1
2017/01/03 17:25:13
Done.
| |
354 // |network_quality_estimator| lives. | |
355 void InitOnIOThread(net::NetworkQualityEstimator* network_quality_estimator) { | |
RyanSturm
2016/12/28 21:53:11
nit maybe: s/InitOnIOThread/InitOnNetworkThread/
tbansal1
2017/01/03 17:25:13
Done.
| |
356 network_quality_estimator_ = network_quality_estimator; | |
357 if (network_quality_estimator_) | |
358 network_quality_estimator_->AddEffectiveConnectionTypeObserver(this); | |
359 } | |
360 | |
361 private: | |
362 // net::EffectiveConnectionTypeObserver implementation: | |
363 void OnEffectiveConnectionTypeChanged( | |
364 net::EffectiveConnectionType type) override { | |
365 DCHECK(thread_checker_.CalledOnValidThread()); | |
366 callback_task_runner_->PostTask(FROM_HERE, base::Bind(callback_, type)); | |
367 } | |
368 | |
369 // Notifies |this| when there is a change in the effective connection type. | |
370 net::NetworkQualityEstimator* network_quality_estimator_; | |
371 | |
372 // |callback_| is ran to notify of changes in the effective connection type. | |
RyanSturm
2016/12/28 21:53:11
s/|callback_| is ran to notify of changes in the e
tbansal1
2017/01/03 17:25:13
Done.
| |
373 base::Callback<void(net::EffectiveConnectionType)> callback_; | |
374 | |
375 // Task runner on which |callback_| is run. | |
376 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; | |
377 | |
378 base::ThreadChecker thread_checker_; | |
379 | |
380 DISALLOW_COPY_AND_ASSIGN(EffectiveConnectionTypeObserver); | |
381 }; | |
382 | |
383 void NetworkMetricsProvider::ProvideNetworkQualityEstimator( | |
384 net::NetworkQualityEstimator* network_quality_estimator, | |
385 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | |
386 DCHECK(thread_checker_.CalledOnValidThread()); | |
387 DCHECK(!effective_connection_type_observer_); | |
388 | |
389 network_quality_task_runner_ = task_runner; | |
390 effective_connection_type_observer_.reset(new EffectiveConnectionTypeObserver( | |
391 base::Bind(&NetworkMetricsProvider::OnEffectiveConnectionTypeChanged, | |
392 base::Unretained(this)), | |
393 base::ThreadTaskRunnerHandle::Get())); | |
394 | |
395 // Initialize |effective_connection_type_observer_| on the same task runner on | |
396 // which |network_quality_estimator| lives. | |
397 network_quality_task_runner_->PostTask( | |
398 FROM_HERE, | |
399 base::Bind(&EffectiveConnectionTypeObserver::InitOnIOThread, | |
400 base::Unretained(effective_connection_type_observer_.get()), | |
401 network_quality_estimator)); | |
402 } | |
403 | |
266 } // namespace metrics | 404 } // namespace metrics |
OLD | NEW |