[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
gunsch | 043f360 | 2014-09-11 21:05:03 | [diff] [blame] | 5 | #ifndef COMPONENTS_METRICS_NET_NETWORK_METRICS_PROVIDER_H_ |
| 6 | #define COMPONENTS_METRICS_NET_NETWORK_METRICS_PROVIDER_H_ |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 7 | |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 11 | #include "base/memory/weak_ptr.h" |
csharrison | 305ab90 | 2016-03-17 19:51:27 | [diff] [blame] | 12 | #include "base/metrics/histogram_base.h" |
tbansal | 9b3dd2bc | 2017-01-11 06:12:59 | [diff] [blame] | 13 | #include "base/threading/thread_checker.h" |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 14 | #include "components/metrics/metrics_provider.h" |
gunsch | 043f360 | 2014-09-11 21:05:03 | [diff] [blame] | 15 | #include "components/metrics/net/wifi_access_point_info_provider.h" |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 16 | #include "components/metrics/proto/system_profile.pb.h" |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 17 | #include "net/base/network_change_notifier.h" |
olli.raula | 80a44be | 2015-09-01 07:41:53 | [diff] [blame] | 18 | #include "net/base/network_interfaces.h" |
tbansal | 9b3dd2bc | 2017-01-11 06:12:59 | [diff] [blame] | 19 | #include "net/nqe/effective_connection_type.h" |
| 20 | |
| 21 | namespace net { |
| 22 | class NetworkQualityEstimator; |
| 23 | } |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 24 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 25 | namespace metrics { |
| 26 | |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 27 | // Registers as observer with net::NetworkChangeNotifier and keeps track of |
| 28 | // the network environment. |
| 29 | class NetworkMetricsProvider |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 30 | : public MetricsProvider, |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 31 | public net::NetworkChangeNotifier::ConnectionTypeObserver { |
| 32 | public: |
tbansal | 9b3dd2bc | 2017-01-11 06:12:59 | [diff] [blame] | 33 | // Class that provides |this| with the network quality estimator. |
| 34 | class NetworkQualityEstimatorProvider { |
| 35 | public: |
| 36 | virtual ~NetworkQualityEstimatorProvider() {} |
| 37 | |
| 38 | // Returns the network quality estimator. May be nullptr. |
| 39 | virtual net::NetworkQualityEstimator* GetNetworkQualityEstimator() = 0; |
| 40 | |
| 41 | // Returns the task runner on which |this| should be used and destroyed. |
| 42 | virtual scoped_refptr<base::SequencedTaskRunner> GetTaskRunner() = 0; |
| 43 | |
| 44 | protected: |
| 45 | NetworkQualityEstimatorProvider() {} |
| 46 | |
| 47 | private: |
| 48 | DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimatorProvider); |
| 49 | }; |
| 50 | |
gunsch | 043f360 | 2014-09-11 21:05:03 | [diff] [blame] | 51 | // Creates a NetworkMetricsProvider, where |io_task_runner| is used to post |
| 52 | // network info collection tasks. |
| 53 | explicit NetworkMetricsProvider(base::TaskRunner* io_task_runner); |
tbansal | 9b3dd2bc | 2017-01-11 06:12:59 | [diff] [blame] | 54 | |
| 55 | // Creates a NetworkMetricsProvider, where |io_task_runner| is used to post |
| 56 | // network info collection tasks. |network_quality_estimator_provider| |
| 57 | // should be set if it is useful to attach the quality of the network to the |
| 58 | // metrics report. |
| 59 | NetworkMetricsProvider(std::unique_ptr<NetworkQualityEstimatorProvider> |
| 60 | network_quality_estimator_provider, |
| 61 | base::TaskRunner* io_task_runner); |
| 62 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 63 | ~NetworkMetricsProvider() override; |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 64 | |
asvitkine | bbde62b | 2014-09-03 16:33:21 | [diff] [blame] | 65 | private: |
tbansal | 9b3dd2bc | 2017-01-11 06:12:59 | [diff] [blame] | 66 | FRIEND_TEST_ALL_PREFIXES(NetworkMetricsProviderTest, EffectiveConnectionType); |
tbansal | e931fa7d | 2017-01-29 07:20:39 | [diff] [blame^] | 67 | FRIEND_TEST_ALL_PREFIXES(NetworkMetricsProviderTest, |
| 68 | ECTAmbiguousOnConnectionTypeChange); |
tbansal | 9b3dd2bc | 2017-01-11 06:12:59 | [diff] [blame] | 69 | |
| 70 | // Listens to the changes in the effective conection type. |
| 71 | class EffectiveConnectionTypeObserver; |
| 72 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 73 | // MetricsProvider: |
bmcquade | 3f55de79 | 2016-02-26 22:10:37 | [diff] [blame] | 74 | void ProvideGeneralMetrics(ChromeUserMetricsExtension* uma_proto) override; |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 75 | void ProvideSystemProfileMetrics(SystemProfileProto* system_profile) override; |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 76 | |
| 77 | // ConnectionTypeObserver: |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 78 | void OnConnectionTypeChanged( |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 79 | net::NetworkChangeNotifier::ConnectionType type) override; |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 80 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 81 | SystemProfileProto::Network::ConnectionType GetConnectionType() const; |
| 82 | SystemProfileProto::Network::WifiPHYLayerProtocol GetWifiPHYLayerProtocol() |
| 83 | const; |
tbansal | 9b3dd2bc | 2017-01-11 06:12:59 | [diff] [blame] | 84 | SystemProfileProto::Network::EffectiveConnectionType |
| 85 | GetEffectiveConnectionType() const; |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 86 | |
| 87 | // Posts a call to net::GetWifiPHYLayerProtocol on the blocking pool. |
| 88 | void ProbeWifiPHYLayerProtocol(); |
| 89 | // Callback from the blocking pool with the result of |
| 90 | // net::GetWifiPHYLayerProtocol. |
| 91 | void OnWifiPHYLayerProtocolResult(net::WifiPHYLayerProtocol mode); |
| 92 | |
[email protected] | 2434789 | 2014-08-18 20:12:07 | [diff] [blame] | 93 | // Writes info about the wireless access points that this system is |
| 94 | // connected to. |
| 95 | void WriteWifiAccessPointProto( |
| 96 | const WifiAccessPointInfoProvider::WifiAccessPointInfo& info, |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 97 | SystemProfileProto::Network* network_proto); |
[email protected] | 2434789 | 2014-08-18 20:12:07 | [diff] [blame] | 98 | |
csharrison | 305ab90 | 2016-03-17 19:51:27 | [diff] [blame] | 99 | // Logs metrics that are functions of other metrics being uploaded. |
| 100 | void LogAggregatedMetrics(); |
| 101 | |
tbansal | 9b3dd2bc | 2017-01-11 06:12:59 | [diff] [blame] | 102 | // Notifies |this| that the effective connection type of the current network |
| 103 | // has changed to |type|. |
| 104 | void OnEffectiveConnectionTypeChanged(net::EffectiveConnectionType type); |
| 105 | |
gunsch | 043f360 | 2014-09-11 21:05:03 | [diff] [blame] | 106 | // Task runner used for blocking file I/O. |
| 107 | base::TaskRunner* io_task_runner_; |
| 108 | |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 109 | // True if |connection_type_| changed during the lifetime of the log. |
| 110 | bool connection_type_is_ambiguous_; |
| 111 | // The connection type according to net::NetworkChangeNotifier. |
| 112 | net::NetworkChangeNotifier::ConnectionType connection_type_; |
| 113 | |
| 114 | // True if |wifi_phy_layer_protocol_| changed during the lifetime of the log. |
| 115 | bool wifi_phy_layer_protocol_is_ambiguous_; |
| 116 | // The PHY mode of the currently associated access point obtained via |
| 117 | // net::GetWifiPHYLayerProtocol. |
| 118 | net::WifiPHYLayerProtocol wifi_phy_layer_protocol_; |
| 119 | |
[email protected] | 2434789 | 2014-08-18 20:12:07 | [diff] [blame] | 120 | // Helper object for retrieving connected wifi access point information. |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 121 | std::unique_ptr<WifiAccessPointInfoProvider> wifi_access_point_info_provider_; |
[email protected] | 2434789 | 2014-08-18 20:12:07 | [diff] [blame] | 122 | |
csharrison | 305ab90 | 2016-03-17 19:51:27 | [diff] [blame] | 123 | // These metrics track histogram totals for the Net.ErrorCodesForMainFrame3 |
| 124 | // histogram. They are used to compute deltas at upload time. |
| 125 | base::HistogramBase::Count total_aborts_; |
| 126 | base::HistogramBase::Count total_codes_; |
| 127 | |
tbansal | 9b3dd2bc | 2017-01-11 06:12:59 | [diff] [blame] | 128 | // Provides the network quality estimator. May be null. |
| 129 | std::unique_ptr<NetworkQualityEstimatorProvider> |
| 130 | network_quality_estimator_provider_; |
| 131 | |
| 132 | // Listens to the changes in the effective connection type. Initialized and |
| 133 | // destroyed using |network_quality_task_runner_|. May be null. |
| 134 | std::unique_ptr<EffectiveConnectionTypeObserver> |
| 135 | effective_connection_type_observer_; |
| 136 | |
| 137 | // Task runner using which |effective_connection_type_observer_| is |
| 138 | // initialized and destroyed. May be null. |
| 139 | scoped_refptr<base::SequencedTaskRunner> network_quality_task_runner_; |
| 140 | |
| 141 | // Last known effective connection type. |
| 142 | net::EffectiveConnectionType effective_connection_type_; |
| 143 | |
| 144 | // True if |effective_connection_type_| changed during the lifetime of the |
| 145 | // log. |
| 146 | bool effective_connection_type_is_ambiguous_; |
| 147 | |
| 148 | base::ThreadChecker thread_checker_; |
| 149 | |
anujk.sharma | 59b96827 | 2014-09-16 16:52:38 | [diff] [blame] | 150 | base::WeakPtrFactory<NetworkMetricsProvider> weak_ptr_factory_; |
| 151 | |
[email protected] | 536324e | 2014-05-22 18:50:19 | [diff] [blame] | 152 | DISALLOW_COPY_AND_ASSIGN(NetworkMetricsProvider); |
| 153 | }; |
| 154 | |
asvitkine | a63d19e | 2014-10-24 16:19:39 | [diff] [blame] | 155 | } // namespace metrics |
| 156 | |
gunsch | 043f360 | 2014-09-11 21:05:03 | [diff] [blame] | 157 | #endif // COMPONENTS_METRICS_NET_NETWORK_METRICS_PROVIDER_H_ |