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