1 // Copyright 2014 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_METRICS_NET_NETWORK_METRICS_PROVIDER_H_ 6 #define COMPONENTS_METRICS_NET_NETWORK_METRICS_PROVIDER_H_ 7 8 #include <memory> 9 10 #include "base/functional/callback.h" 11 #include "base/gtest_prod_util.h" 12 #include "base/memory/raw_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/metrics/histogram_base.h" 15 #include "base/sequence_checker.h" 16 #include "base/task/sequenced_task_runner.h" 17 #include "base/task/single_thread_task_runner.h" 18 #include "components/metrics/metrics_provider.h" 19 #include "net/base/network_interfaces.h" 20 #include "net/nqe/effective_connection_type.h" 21 #include "services/network/public/cpp/network_connection_tracker.h" 22 #include "third_party/metrics_proto/system_profile.pb.h" 23 24 namespace metrics { 25 26 SystemProfileProto::Network::EffectiveConnectionType 27 ConvertEffectiveConnectionType( 28 net::EffectiveConnectionType effective_connection_type); 29 30 // Registers as observer with network::NetworkConnectionTracker and keeps track 31 // of the network environment. 32 class NetworkMetricsProvider 33 : public MetricsProvider, 34 public network::NetworkConnectionTracker::NetworkConnectionObserver { 35 public: 36 // Class that provides |this| with the network quality estimator. 37 class NetworkQualityEstimatorProvider { 38 public: 39 NetworkQualityEstimatorProvider(const NetworkQualityEstimatorProvider&) = 40 delete; 41 NetworkQualityEstimatorProvider& operator=( 42 const NetworkQualityEstimatorProvider&) = delete; 43 44 virtual ~NetworkQualityEstimatorProvider() = default; 45 46 // Provides |this| with |callback| that would be invoked by |this| every 47 // time there is a change in the network quality estimates. 48 virtual void PostReplyOnNetworkQualityChanged( 49 base::RepeatingCallback<void(net::EffectiveConnectionType)> 50 callback) = 0; 51 52 protected: 53 NetworkQualityEstimatorProvider() = default; 54 }; 55 56 // Creates a NetworkMetricsProvider, where 57 // |network_quality_estimator_provider| should be set if it is useful to 58 // attach the quality of the network to the metrics report. 59 NetworkMetricsProvider(network::NetworkConnectionTrackerAsyncGetter 60 network_connection_tracker_async_getter, 61 std::unique_ptr<NetworkQualityEstimatorProvider> 62 network_quality_estimator_provider = nullptr); 63 64 NetworkMetricsProvider(const NetworkMetricsProvider&) = delete; 65 NetworkMetricsProvider& operator=(const NetworkMetricsProvider&) = delete; 66 67 ~NetworkMetricsProvider() override; 68 69 private: 70 FRIEND_TEST_ALL_PREFIXES(NetworkMetricsProviderTest, EffectiveConnectionType); 71 FRIEND_TEST_ALL_PREFIXES(NetworkMetricsProviderTest, 72 ECTAmbiguousOnConnectionTypeChange); 73 FRIEND_TEST_ALL_PREFIXES(NetworkMetricsProviderTest, 74 ECTNotAmbiguousOnUnknownOrOffline); 75 FRIEND_TEST_ALL_PREFIXES(NetworkMetricsProviderTest, 76 ConnectionTypeIsAmbiguous); 77 78 // MetricsProvider: 79 void ProvideSystemProfileMetrics(SystemProfileProto* system_profile) override; 80 81 // NetworkConnectionObserver: 82 void OnConnectionChanged(network::mojom::ConnectionType type) override; 83 84 SystemProfileProto::Network::ConnectionType GetConnectionType() const; 85 86 void OnEffectiveConnectionTypeChanged(net::EffectiveConnectionType type); 87 88 // Used as a callback to be given to NetworkConnectionTracker async getter to 89 // set the |network_connection_tracker_|. 90 void SetNetworkConnectionTracker( 91 network::NetworkConnectionTracker* network_connection_tracker); 92 93 // Watches for network connection changes. 94 // This |network_connection_tracker_| raw pointer is not owned by this class. 95 // It is obtained from the global |g_network_connection_tracker| pointer in 96 // //content/public/browser/network_service_instance.cc and points to the same 97 // object. 98 raw_ptr<network::NetworkConnectionTracker> network_connection_tracker_; 99 100 // True if |connection_type_| changed during the lifetime of the log. 101 bool connection_type_is_ambiguous_; 102 // The connection type according to network::NetworkConnectionTracker. 103 network::mojom::ConnectionType connection_type_; 104 // True if the network connection tracker has been initialized. 105 bool network_connection_tracker_initialized_; 106 107 // Provides the network quality estimator. May be null. 108 std::unique_ptr<NetworkQualityEstimatorProvider> 109 network_quality_estimator_provider_; 110 111 // Last known effective connection type. 112 net::EffectiveConnectionType effective_connection_type_; 113 114 // Minimum and maximum effective connection type since the metrics were last 115 // provided. 116 net::EffectiveConnectionType min_effective_connection_type_; 117 net::EffectiveConnectionType max_effective_connection_type_; 118 119 SEQUENCE_CHECKER(sequence_checker_); 120 121 base::WeakPtrFactory<NetworkMetricsProvider> weak_ptr_factory_{this}; 122 }; 123 124 } // namespace metrics 125 126 #endif // COMPONENTS_METRICS_NET_NETWORK_METRICS_PROVIDER_H_ 127