xref: /aosp_15_r20/external/cronet/net/quic/network_connection.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 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 NET_QUIC_NETWORK_CONNECTION_H_
6 #define NET_QUIC_NETWORK_CONNECTION_H_
7 
8 #include "net/base/net_export.h"
9 #include "net/base/network_change_notifier.h"
10 
11 namespace net {
12 
13 // This class stores information about the current network type and
14 // provides a textual description of it.
15 class NET_EXPORT NetworkConnection
16     : public NetworkChangeNotifier::IPAddressObserver,
17       public NetworkChangeNotifier::ConnectionTypeObserver {
18  public:
19   NetworkConnection();
20 
21   NetworkConnection(const NetworkConnection&) = delete;
22   NetworkConnection& operator=(const NetworkConnection&) = delete;
23 
24   ~NetworkConnection() override;
25 
26   // Returns the underlying connection type.
connection_type()27   NetworkChangeNotifier::ConnectionType connection_type() {
28     return connection_type_;
29   }
30 
31   // Return a string equivalent of current connection type. Callers don't need
32   // to make a copy of the returned C-string value. If the connection type is
33   // CONNECTION_WIFI, then we'll tease out some details when we are on WiFi, and
34   // hopefully leave only ethernet (with no WiFi available) in the
35   // CONNECTION_UNKNOWN category.  This *might* err if there is both ethernet,
36   // as well as WiFi, where WiFi was not being used that much. Most platforms
37   // don't distinguish Wifi vs Ethernet, and call everything CONNECTION_UNKNOWN
38   // :-(. For non CONNECTIION_WIFI, this returns the C-string returned by
39   // NetworkChangeNotifier::ConnectionTypeToString.
connection_description()40   const char* connection_description() { return connection_description_; }
41 
42   // NetworkChangeNotifier::IPAddressObserver methods:
43   void OnIPAddressChanged() override;
44 
45   // NetworkChangeNotifier::ConnectionTypeObserver methods:
46   void OnConnectionTypeChanged(
47       NetworkChangeNotifier::ConnectionType type) override;
48 
49  private:
50   // Cache the connection type to avoid calling the potentially expensive
51   // NetworkChangeNotifier::GetConnectionType() function.
52   NetworkChangeNotifier::ConnectionType connection_type_ =
53       NetworkChangeNotifier::CONNECTION_UNKNOWN;
54   // Cache the connection description string to avoid calling the expensive
55   // GetWifiPHYLayerProtocol() function.
56   const char* connection_description_ = nullptr;
57 };
58 
59 }  // namespace net
60 
61 #endif  // NET_QUIC_NETWORK_CONNECTION_H_
62