xref: /aosp_15_r20/external/cronet/net/base/network_change_notifier_passive.cc (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 #include "net/base/network_change_notifier_passive.h"
6 
7 #include <string>
8 #include <unordered_set>
9 #include <utility>
10 
11 #include "base/functional/bind.h"
12 #include "base/task/task_traits.h"
13 #include "build/build_config.h"
14 #include "build/chromeos_buildflags.h"
15 #include "net/dns/dns_config_service_posix.h"
16 #include "net/dns/system_dns_config_change_notifier.h"
17 
18 #if BUILDFLAG(IS_ANDROID)
19 #include "net/android/network_change_notifier_android.h"
20 #endif
21 
22 #if BUILDFLAG(IS_LINUX)
23 #include <linux/rtnetlink.h>
24 
25 #include "net/base/network_change_notifier_linux.h"
26 #endif
27 
28 namespace net {
29 
NetworkChangeNotifierPassive(NetworkChangeNotifier::ConnectionType initial_connection_type,NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype)30 NetworkChangeNotifierPassive::NetworkChangeNotifierPassive(
31     NetworkChangeNotifier::ConnectionType initial_connection_type,
32     NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype)
33     : NetworkChangeNotifierPassive(initial_connection_type,
34                                    initial_connection_subtype,
35                                    /*system_dns_config_notifier=*/nullptr) {}
36 
NetworkChangeNotifierPassive(NetworkChangeNotifier::ConnectionType initial_connection_type,NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype,SystemDnsConfigChangeNotifier * system_dns_config_notifier)37 NetworkChangeNotifierPassive::NetworkChangeNotifierPassive(
38     NetworkChangeNotifier::ConnectionType initial_connection_type,
39     NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype,
40     SystemDnsConfigChangeNotifier* system_dns_config_notifier)
41     : NetworkChangeNotifier(NetworkChangeCalculatorParamsPassive(),
42                             system_dns_config_notifier),
43       connection_type_(initial_connection_type),
44       max_bandwidth_mbps_(
45           NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype(
46               initial_connection_subtype)) {}
47 
~NetworkChangeNotifierPassive()48 NetworkChangeNotifierPassive::~NetworkChangeNotifierPassive() {
49   ClearGlobalPointer();
50 }
51 
OnDNSChanged()52 void NetworkChangeNotifierPassive::OnDNSChanged() {
53   GetCurrentSystemDnsConfigNotifier()->RefreshConfig();
54 }
55 
OnIPAddressChanged()56 void NetworkChangeNotifierPassive::OnIPAddressChanged() {
57   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
58   NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
59 }
60 
OnConnectionChanged(NetworkChangeNotifier::ConnectionType connection_type)61 void NetworkChangeNotifierPassive::OnConnectionChanged(
62     NetworkChangeNotifier::ConnectionType connection_type) {
63   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
64   {
65     base::AutoLock scoped_lock(lock_);
66     connection_type_ = connection_type;
67   }
68   NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
69 }
70 
OnConnectionSubtypeChanged(NetworkChangeNotifier::ConnectionType connection_type,NetworkChangeNotifier::ConnectionSubtype connection_subtype)71 void NetworkChangeNotifierPassive::OnConnectionSubtypeChanged(
72     NetworkChangeNotifier::ConnectionType connection_type,
73     NetworkChangeNotifier::ConnectionSubtype connection_subtype) {
74   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
75   double max_bandwidth_mbps =
76       GetMaxBandwidthMbpsForConnectionSubtype(connection_subtype);
77   {
78     base::AutoLock scoped_lock(lock_);
79     max_bandwidth_mbps_ = max_bandwidth_mbps;
80   }
81   NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(max_bandwidth_mbps,
82                                                              connection_type);
83 }
84 
85 NetworkChangeNotifier::ConnectionType
GetCurrentConnectionType() const86 NetworkChangeNotifierPassive::GetCurrentConnectionType() const {
87   base::AutoLock scoped_lock(lock_);
88   return connection_type_;
89 }
90 
GetCurrentMaxBandwidthAndConnectionType(double * max_bandwidth_mbps,ConnectionType * connection_type) const91 void NetworkChangeNotifierPassive::GetCurrentMaxBandwidthAndConnectionType(
92     double* max_bandwidth_mbps,
93     ConnectionType* connection_type) const {
94   base::AutoLock scoped_lock(lock_);
95   *connection_type = connection_type_;
96   *max_bandwidth_mbps = max_bandwidth_mbps_;
97 }
98 
99 #if BUILDFLAG(IS_LINUX)
100 AddressMapOwnerLinux*
GetAddressMapOwnerInternal()101 NetworkChangeNotifierPassive::GetAddressMapOwnerInternal() {
102   return &address_map_cache_;
103 }
104 #endif
105 
106 // static
107 NetworkChangeNotifier::NetworkChangeCalculatorParams
NetworkChangeCalculatorParamsPassive()108 NetworkChangeNotifierPassive::NetworkChangeCalculatorParamsPassive() {
109   NetworkChangeCalculatorParams params;
110 #if BUILDFLAG(IS_CHROMEOS)
111   // Delay values arrived at by simple experimentation and adjusted so as to
112   // produce a single signal when switching between network connections.
113   params.ip_address_offline_delay_ = base::Milliseconds(4000);
114   params.ip_address_online_delay_ = base::Milliseconds(1000);
115   params.connection_type_offline_delay_ = base::Milliseconds(500);
116   params.connection_type_online_delay_ = base::Milliseconds(500);
117 #elif BUILDFLAG(IS_ANDROID)
118   params = NetworkChangeNotifierAndroid::NetworkChangeCalculatorParamsAndroid();
119 #elif BUILDFLAG(IS_LINUX)
120   params = NetworkChangeNotifierLinux::NetworkChangeCalculatorParamsLinux();
121 #else
122   NOTIMPLEMENTED();
123 #endif
124   return params;
125 }
126 
127 }  // namespace net
128