xref: /aosp_15_r20/external/cronet/components/metrics/net/network_metrics_provider.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
~NetworkQualityEstimatorProvider()44     virtual ~NetworkQualityEstimatorProvider() {}
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:
NetworkQualityEstimatorProvider()53     NetworkQualityEstimatorProvider() {}
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   SystemProfileProto::Network::WifiPHYLayerProtocol GetWifiPHYLayerProtocol()
86       const;
87 
88   // Posts a call to net::GetWifiPHYLayerProtocol on the blocking pool.
89   void ProbeWifiPHYLayerProtocol();
90   // Callback from the blocking pool with the result of
91   // net::GetWifiPHYLayerProtocol.
92   void OnWifiPHYLayerProtocolResult(net::WifiPHYLayerProtocol mode);
93 
94   void OnEffectiveConnectionTypeChanged(net::EffectiveConnectionType type);
95 
96   // Used as a callback to be given to NetworkConnectionTracker async getter to
97   // set the |network_connection_tracker_|.
98   void SetNetworkConnectionTracker(
99       network::NetworkConnectionTracker* network_connection_tracker);
100 
101   // Watches for network connection changes.
102   // This |network_connection_tracker_| raw pointer is not owned by this class.
103   // It is obtained from the global |g_network_connection_tracker| pointer in
104   // //content/public/browser/network_service_instance.cc and points to the same
105   // object.
106   raw_ptr<network::NetworkConnectionTracker> network_connection_tracker_;
107 
108   // True if |connection_type_| changed during the lifetime of the log.
109   bool connection_type_is_ambiguous_;
110   // The connection type according to network::NetworkConnectionTracker.
111   network::mojom::ConnectionType connection_type_;
112   // True if the network connection tracker has been initialized.
113   bool network_connection_tracker_initialized_;
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 
121   // Provides the network quality estimator. May be null.
122   std::unique_ptr<NetworkQualityEstimatorProvider>
123       network_quality_estimator_provider_;
124 
125   // Last known effective connection type.
126   net::EffectiveConnectionType effective_connection_type_;
127 
128   // Minimum and maximum effective connection type since the metrics were last
129   // provided.
130   net::EffectiveConnectionType min_effective_connection_type_;
131   net::EffectiveConnectionType max_effective_connection_type_;
132 
133   SEQUENCE_CHECKER(sequence_checker_);
134 
135   base::WeakPtrFactory<NetworkMetricsProvider> weak_ptr_factory_{this};
136 };
137 
138 }  // namespace metrics
139 
140 #endif  // COMPONENTS_METRICS_NET_NETWORK_METRICS_PROVIDER_H_
141