xref: /aosp_15_r20/external/cronet/net/base/network_change_notifier_passive.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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_BASE_NETWORK_CHANGE_NOTIFIER_PASSIVE_H_
6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_PASSIVE_H_
7 
8 #include "base/gtest_prod_util.h"
9 #include "base/sequence_checker.h"
10 #include "base/synchronization/lock.h"
11 #include "base/threading/thread.h"
12 #include "base/threading/thread_checker.h"
13 #include "net/base/net_export.h"
14 #include "net/base/network_change_notifier.h"
15 
16 #if BUILDFLAG(IS_LINUX)
17 #include "net/base/address_map_cache_linux.h"
18 #endif
19 
20 namespace net {
21 
22 // A NetworkChangeNotifier that needs to be told about network changes by some
23 // other object. This is useful on platforms like ChromeOS, Lacros, and Android
24 // where only objects running in the browser process can listen for network
25 // state changes, but other processes want to add observers for network state.
26 // It's also useful on Linux where listening for network state changes in a
27 // sandboxed process requires loosening the sandbox policy too much.
28 class NET_EXPORT NetworkChangeNotifierPassive : public NetworkChangeNotifier {
29  public:
30   NetworkChangeNotifierPassive(
31       NetworkChangeNotifier::ConnectionType initial_connection_type,
32       NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype);
33   NetworkChangeNotifierPassive(const NetworkChangeNotifierPassive&) = delete;
34   NetworkChangeNotifierPassive& operator=(const NetworkChangeNotifierPassive&) =
35       delete;
36   ~NetworkChangeNotifierPassive() override;
37 
38   // These methods are used to notify this object that a network property has
39   // changed. These must be called from the thread that owns this object.
40   void OnDNSChanged();
41   void OnIPAddressChanged();
42   void OnConnectionChanged(
43       NetworkChangeNotifier::ConnectionType connection_type);
44   void OnConnectionSubtypeChanged(
45       NetworkChangeNotifier::ConnectionType connection_type,
46       NetworkChangeNotifier::ConnectionSubtype connection_subtype);
47 
48  protected:
49   // NetworkChangeNotifier overrides.
50   NetworkChangeNotifier::ConnectionType GetCurrentConnectionType()
51       const override;
52   void GetCurrentMaxBandwidthAndConnectionType(
53       double* max_bandwidth_mbps,
54       ConnectionType* connection_type) const override;
55 #if BUILDFLAG(IS_LINUX)
56   AddressMapOwnerLinux* GetAddressMapOwnerInternal() override;
57 #endif
58 
59  private:
60   friend class NetworkChangeNotifierPassiveTest;
61 
62   // For testing purposes, allows specifying a SystemDnsConfigChangeNotifier.
63   // If |system_dns_config_notifier| is nullptr, NetworkChangeNotifier create a
64   // global one.
65   NetworkChangeNotifierPassive(
66       NetworkChangeNotifier::ConnectionType initial_connection_type,
67       NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype,
68       SystemDnsConfigChangeNotifier* system_dns_config_notifier);
69 
70   // Calculates parameters used for network change notifier online/offline
71   // signals.
72   static NetworkChangeNotifier::NetworkChangeCalculatorParams
73   NetworkChangeCalculatorParamsPassive();
74 
75   THREAD_CHECKER(thread_checker_);
76 
77 #if BUILDFLAG(IS_LINUX)
78   AddressMapCacheLinux address_map_cache_;
79 #endif
80 
81   mutable base::Lock lock_;
82   NetworkChangeNotifier::ConnectionType
83       connection_type_;        // Guarded by |lock_|.
84   double max_bandwidth_mbps_;  // Guarded by |lock_|.
85 };
86 
87 }  // namespace net
88 
89 #endif  // NET_BASE_NETWORK_CHANGE_NOTIFIER_PASSIVE_H_
90