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.h"
6
7 #include <limits>
8 #include <string>
9 #include <unordered_set>
10 #include <utility>
11
12 #include "base/memory/ref_counted.h"
13 #include "base/metrics/histogram_functions.h"
14 #include "base/metrics/histogram_macros.h"
15 #include "base/no_destructor.h"
16 #include "base/observer_list.h"
17 #include "base/sequence_checker.h"
18 #include "base/strings/string_util.h"
19 #include "base/synchronization/lock.h"
20 #include "base/threading/thread_checker.h"
21 #include "base/timer/timer.h"
22 #include "build/build_config.h"
23 #include "build/chromeos_buildflags.h"
24 #include "net/base/network_change_notifier_factory.h"
25 #include "net/base/network_interfaces.h"
26 #include "net/base/url_util.h"
27 #include "net/dns/dns_config.h"
28 #include "net/dns/dns_config_service.h"
29 #include "net/dns/system_dns_config_change_notifier.h"
30 #include "net/url_request/url_request.h"
31 #include "third_party/abseil-cpp/absl/types/optional.h"
32 #include "url/gurl.h"
33
34 #if BUILDFLAG(IS_WIN)
35 #include "net/base/network_change_notifier_win.h"
36 #elif BUILDFLAG(IS_LINUX)
37 #include "net/base/network_change_notifier_linux.h"
38 #elif BUILDFLAG(IS_APPLE)
39 #include "net/base/network_change_notifier_mac.h"
40 #elif BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
41 #include "net/base/network_change_notifier_passive.h"
42 #elif BUILDFLAG(IS_FUCHSIA)
43 #include "net/base/network_change_notifier_fuchsia.h"
44 #endif
45
46 namespace net {
47
48 namespace {
49
50 // The process-wide singleton notifier.
51 NetworkChangeNotifier* g_network_change_notifier = nullptr;
52
53 // Class factory singleton.
54 NetworkChangeNotifierFactory* g_network_change_notifier_factory = nullptr;
55
56 // Lock to protect |g_network_change_notifier| during creation time. Since
57 // creation of the process-wide instance can happen on any thread, this lock is
58 // used to guarantee only one instance is created. Once the global instance is
59 // created, the owner is responsible for destroying it on the same thread. All
60 // the other calls to the NetworkChangeNotifier do not require this lock as
61 // the global instance is only destroyed when the process is getting killed.
NetworkChangeNotifierCreationLock()62 base::Lock& NetworkChangeNotifierCreationLock() {
63 static base::NoDestructor<base::Lock> instance;
64 return *instance;
65 }
66
67 class MockNetworkChangeNotifier : public NetworkChangeNotifier {
68 public:
MockNetworkChangeNotifier(std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier)69 explicit MockNetworkChangeNotifier(
70 std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier)
71 : NetworkChangeNotifier(
72 NetworkChangeCalculatorParams(),
73 dns_config_notifier.get(),
74 // Omit adding observers from the constructor as that would prevent
75 // construction when SingleThreadTaskRunner::CurrentDefaultHandle
76 // isn't set.
77 /* omit_observers_in_constructor_for_testing=*/true),
78 dns_config_notifier_(std::move(dns_config_notifier)) {}
79
~MockNetworkChangeNotifier()80 ~MockNetworkChangeNotifier() override { StopSystemDnsConfigNotifier(); }
81
GetCurrentConnectionType() const82 ConnectionType GetCurrentConnectionType() const override {
83 return CONNECTION_UNKNOWN;
84 }
85
86 private:
87 std::unique_ptr<SystemDnsConfigChangeNotifier> dns_config_notifier_;
88 };
89
90 } // namespace
91
92 // static
93 bool NetworkChangeNotifier::test_notifications_only_ = false;
94
95 NetworkChangeNotifier::NetworkChangeCalculatorParams::
96 NetworkChangeCalculatorParams() = default;
97
98 // Calculates NetworkChange signal from IPAddress, ConnectionCost, and
99 // ConnectionType signals.
100 class NetworkChangeNotifier::NetworkChangeCalculator
101 : public ConnectionTypeObserver,
102 public ConnectionCostObserver,
103 public IPAddressObserver {
104 public:
NetworkChangeCalculator(const NetworkChangeCalculatorParams & params)105 explicit NetworkChangeCalculator(const NetworkChangeCalculatorParams& params)
106 : params_(params) {
107 DCHECK(g_network_change_notifier);
108 AddConnectionTypeObserver(this);
109 AddConnectionCostObserver(this);
110 AddIPAddressObserver(this);
111 }
112
113 NetworkChangeCalculator(const NetworkChangeCalculator&) = delete;
114 NetworkChangeCalculator& operator=(const NetworkChangeCalculator&) = delete;
115
~NetworkChangeCalculator()116 ~NetworkChangeCalculator() override {
117 DCHECK(thread_checker_.CalledOnValidThread());
118 RemoveConnectionTypeObserver(this);
119 RemoveConnectionCostObserver(this);
120 RemoveIPAddressObserver(this);
121 }
122
123 // NetworkChangeNotifier::IPAddressObserver implementation.
OnIPAddressChanged()124 void OnIPAddressChanged() override {
125 DCHECK(thread_checker_.CalledOnValidThread());
126 pending_connection_type_ = GetConnectionType();
127 base::TimeDelta delay = last_announced_connection_type_ == CONNECTION_NONE
128 ? params_.ip_address_offline_delay_ : params_.ip_address_online_delay_;
129 // Cancels any previous timer.
130 timer_.Start(FROM_HERE, delay, this, &NetworkChangeCalculator::Notify);
131 }
132
133 // NetworkChangeNotifier::ConnectionTypeObserver implementation.
OnConnectionTypeChanged(ConnectionType type)134 void OnConnectionTypeChanged(ConnectionType type) override {
135 DCHECK(thread_checker_.CalledOnValidThread());
136 pending_connection_type_ = type;
137 base::TimeDelta delay = last_announced_connection_type_ == CONNECTION_NONE
138 ? params_.connection_type_offline_delay_
139 : params_.connection_type_online_delay_;
140 // Cancels any previous timer.
141 timer_.Start(FROM_HERE, delay, this, &NetworkChangeCalculator::Notify);
142 }
143
144 // NetworkChangeNotifier::ConnectionCostObserver implementation.
OnConnectionCostChanged(ConnectionCost cost)145 void OnConnectionCostChanged(ConnectionCost cost) override {
146 base::UmaHistogramEnumeration("Net.NetworkChangeNotifier.NewConnectionCost",
147 cost, CONNECTION_COST_LAST);
148 }
149
150 private:
Notify()151 void Notify() {
152 DCHECK(thread_checker_.CalledOnValidThread());
153 // Don't bother signaling about dead connections.
154 if (have_announced_ &&
155 (last_announced_connection_type_ == CONNECTION_NONE) &&
156 (pending_connection_type_ == CONNECTION_NONE)) {
157 return;
158 }
159
160 UMA_HISTOGRAM_ENUMERATION("Net.NetworkChangeNotifier.NewConnectionType",
161 pending_connection_type_, CONNECTION_LAST + 1);
162
163 have_announced_ = true;
164 last_announced_connection_type_ = pending_connection_type_;
165 // Immediately before sending out an online signal, send out an offline
166 // signal to perform any destructive actions before constructive actions.
167 if (pending_connection_type_ != CONNECTION_NONE)
168 NetworkChangeNotifier::NotifyObserversOfNetworkChange(CONNECTION_NONE);
169 NetworkChangeNotifier::NotifyObserversOfNetworkChange(
170 pending_connection_type_);
171 }
172
173 const NetworkChangeCalculatorParams params_;
174
175 // Indicates if NotifyObserversOfNetworkChange has been called yet.
176 bool have_announced_ = false;
177 // Last value passed to NotifyObserversOfNetworkChange.
178 ConnectionType last_announced_connection_type_ = CONNECTION_NONE;
179 // Value to pass to NotifyObserversOfNetworkChange when Notify is called.
180 ConnectionType pending_connection_type_ = CONNECTION_NONE;
181 // Used to delay notifications so duplicates can be combined.
182 base::OneShotTimer timer_;
183
184 base::ThreadChecker thread_checker_;
185 };
186
187 // Holds the collection of observer lists used by NetworkChangeNotifier.
188 class NetworkChangeNotifier::ObserverList {
189 public:
ObserverList()190 ObserverList()
191 : ip_address_observer_list_(
192 base::MakeRefCounted<base::ObserverListThreadSafe<
193 NetworkChangeNotifier::IPAddressObserver>>(
194 base::ObserverListPolicy::EXISTING_ONLY)),
195 connection_type_observer_list_(
196 base::MakeRefCounted<base::ObserverListThreadSafe<
197 NetworkChangeNotifier::ConnectionTypeObserver>>(
198 base::ObserverListPolicy::EXISTING_ONLY)),
199 resolver_state_observer_list_(
200 base::MakeRefCounted<base::ObserverListThreadSafe<
201 NetworkChangeNotifier::DNSObserver>>(
202 base::ObserverListPolicy::EXISTING_ONLY)),
203 network_change_observer_list_(
204 base::MakeRefCounted<base::ObserverListThreadSafe<
205 NetworkChangeNotifier::NetworkChangeObserver>>(
206 base::ObserverListPolicy::EXISTING_ONLY)),
207 max_bandwidth_observer_list_(
208 base::MakeRefCounted<base::ObserverListThreadSafe<
209 NetworkChangeNotifier::MaxBandwidthObserver>>(
210 base::ObserverListPolicy::EXISTING_ONLY)),
211 network_observer_list_(
212 base::MakeRefCounted<base::ObserverListThreadSafe<
213 NetworkChangeNotifier::NetworkObserver>>(
214 base::ObserverListPolicy::EXISTING_ONLY)),
215 connection_cost_observer_list_(
216 base::MakeRefCounted<base::ObserverListThreadSafe<
217 NetworkChangeNotifier::ConnectionCostObserver>>(
218 base::ObserverListPolicy::EXISTING_ONLY)),
219 default_network_active_observer_list_(
220 base::MakeRefCounted<
221 base::ObserverListThreadSafe<DefaultNetworkActiveObserver>>(
222 base::ObserverListPolicy::EXISTING_ONLY)) {}
223
224 ObserverList(const ObserverList&) = delete;
225 ObserverList& operator=(const ObserverList&) = delete;
226 ~ObserverList() = default;
227
228 const scoped_refptr<
229 base::ObserverListThreadSafe<NetworkChangeNotifier::IPAddressObserver>>
230 ip_address_observer_list_;
231 const scoped_refptr<base::ObserverListThreadSafe<
232 NetworkChangeNotifier::ConnectionTypeObserver>>
233 connection_type_observer_list_;
234 const scoped_refptr<
235 base::ObserverListThreadSafe<NetworkChangeNotifier::DNSObserver>>
236 resolver_state_observer_list_;
237 const scoped_refptr<base::ObserverListThreadSafe<
238 NetworkChangeNotifier::NetworkChangeObserver>>
239 network_change_observer_list_;
240 const scoped_refptr<
241 base::ObserverListThreadSafe<NetworkChangeNotifier::MaxBandwidthObserver>>
242 max_bandwidth_observer_list_;
243 const scoped_refptr<
244 base::ObserverListThreadSafe<NetworkChangeNotifier::NetworkObserver>>
245 network_observer_list_;
246 const scoped_refptr<base::ObserverListThreadSafe<
247 NetworkChangeNotifier::ConnectionCostObserver>>
248 connection_cost_observer_list_;
249 const scoped_refptr<
250 base::ObserverListThreadSafe<DefaultNetworkActiveObserver>>
251 default_network_active_observer_list_;
252
253 // Indicates if connection cost observer was added before
254 // network_change_notifier was initialized, if so ConnectionCostObserverAdded
255 // is invoked from constructor.
256 std::atomic_bool connection_cost_observers_added_ = false;
257 };
258
259 class NetworkChangeNotifier::SystemDnsConfigObserver
260 : public SystemDnsConfigChangeNotifier::Observer {
261 public:
262 virtual ~SystemDnsConfigObserver() = default;
263
OnSystemDnsConfigChanged(absl::optional<DnsConfig> config)264 void OnSystemDnsConfigChanged(absl::optional<DnsConfig> config) override {
265 NotifyObserversOfDNSChange();
266 }
267 };
268
ClearGlobalPointer()269 void NetworkChangeNotifier::ClearGlobalPointer() {
270 if (!cleared_global_pointer_) {
271 cleared_global_pointer_ = true;
272 DCHECK_EQ(this, g_network_change_notifier);
273 g_network_change_notifier = nullptr;
274 }
275 }
276
~NetworkChangeNotifier()277 NetworkChangeNotifier::~NetworkChangeNotifier() {
278 network_change_calculator_.reset();
279 ClearGlobalPointer();
280 StopSystemDnsConfigNotifier();
281 }
282
283 // static
GetFactory()284 NetworkChangeNotifierFactory* NetworkChangeNotifier::GetFactory() {
285 return g_network_change_notifier_factory;
286 }
287
288 // static
SetFactory(NetworkChangeNotifierFactory * factory)289 void NetworkChangeNotifier::SetFactory(
290 NetworkChangeNotifierFactory* factory) {
291 CHECK(!g_network_change_notifier_factory);
292 g_network_change_notifier_factory = factory;
293 }
294
295 // static
CreateIfNeeded(NetworkChangeNotifier::ConnectionType initial_type,NetworkChangeNotifier::ConnectionSubtype initial_subtype)296 std::unique_ptr<NetworkChangeNotifier> NetworkChangeNotifier::CreateIfNeeded(
297 NetworkChangeNotifier::ConnectionType initial_type,
298 NetworkChangeNotifier::ConnectionSubtype initial_subtype) {
299 {
300 base::AutoLock auto_lock(NetworkChangeNotifierCreationLock());
301 if (g_network_change_notifier)
302 return nullptr;
303 }
304
305 if (g_network_change_notifier_factory) {
306 return g_network_change_notifier_factory->CreateInstanceWithInitialTypes(
307 initial_type, initial_subtype);
308 }
309
310 #if BUILDFLAG(IS_WIN)
311 std::unique_ptr<NetworkChangeNotifierWin> network_change_notifier =
312 std::make_unique<NetworkChangeNotifierWin>();
313 network_change_notifier->WatchForAddressChange();
314 return network_change_notifier;
315 #elif BUILDFLAG(IS_ANDROID)
316 // Fallback to use NetworkChangeNotifierPassive if
317 // NetworkChangeNotifierFactory is not set. Currently used for tests and when
318 // running network service in a separate process.
319 return std::make_unique<NetworkChangeNotifierPassive>(initial_type,
320 initial_subtype);
321 #elif BUILDFLAG(IS_CHROMEOS)
322 return std::make_unique<NetworkChangeNotifierPassive>(initial_type,
323 initial_subtype);
324 #elif BUILDFLAG(IS_LINUX)
325 return std::make_unique<NetworkChangeNotifierLinux>(
326 std::unordered_set<std::string>());
327 #elif BUILDFLAG(IS_APPLE)
328 return std::make_unique<NetworkChangeNotifierMac>();
329 #elif BUILDFLAG(IS_FUCHSIA)
330 return std::make_unique<NetworkChangeNotifierFuchsia>(
331 /*require_wlan=*/false);
332 #else
333 NOTIMPLEMENTED();
334 return nullptr;
335 #endif
336 }
337
338 // static
339 NetworkChangeNotifier::ConnectionCost
GetConnectionCost()340 NetworkChangeNotifier::GetConnectionCost() {
341 return g_network_change_notifier
342 ? g_network_change_notifier->GetCurrentConnectionCost()
343 : CONNECTION_COST_UNKNOWN;
344 }
345
346 // static
347 NetworkChangeNotifier::ConnectionType
GetConnectionType()348 NetworkChangeNotifier::GetConnectionType() {
349 return g_network_change_notifier ?
350 g_network_change_notifier->GetCurrentConnectionType() :
351 CONNECTION_UNKNOWN;
352 }
353
354 // static
355 NetworkChangeNotifier::ConnectionSubtype
GetConnectionSubtype()356 NetworkChangeNotifier::GetConnectionSubtype() {
357 return g_network_change_notifier
358 ? g_network_change_notifier->GetCurrentConnectionSubtype()
359 : SUBTYPE_UNKNOWN;
360 }
361
362 // static
GetMaxBandwidthAndConnectionType(double * max_bandwidth_mbps,ConnectionType * connection_type)363 void NetworkChangeNotifier::GetMaxBandwidthAndConnectionType(
364 double* max_bandwidth_mbps,
365 ConnectionType* connection_type) {
366 if (!g_network_change_notifier) {
367 *connection_type = CONNECTION_UNKNOWN;
368 *max_bandwidth_mbps =
369 GetMaxBandwidthMbpsForConnectionSubtype(SUBTYPE_UNKNOWN);
370 return;
371 }
372
373 g_network_change_notifier->GetCurrentMaxBandwidthAndConnectionType(
374 max_bandwidth_mbps, connection_type);
375 }
376
377 // static
GetMaxBandwidthMbpsForConnectionSubtype(ConnectionSubtype subtype)378 double NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype(
379 ConnectionSubtype subtype) {
380 switch (subtype) {
381 case SUBTYPE_GSM:
382 return 0.01;
383 case SUBTYPE_IDEN:
384 return 0.064;
385 case SUBTYPE_CDMA:
386 return 0.115;
387 case SUBTYPE_1XRTT:
388 return 0.153;
389 case SUBTYPE_GPRS:
390 return 0.237;
391 case SUBTYPE_EDGE:
392 return 0.384;
393 case SUBTYPE_UMTS:
394 return 2.0;
395 case SUBTYPE_EVDO_REV_0:
396 return 2.46;
397 case SUBTYPE_EVDO_REV_A:
398 return 3.1;
399 case SUBTYPE_HSPA:
400 return 3.6;
401 case SUBTYPE_EVDO_REV_B:
402 return 14.7;
403 case SUBTYPE_HSDPA:
404 return 14.3;
405 case SUBTYPE_HSUPA:
406 return 14.4;
407 case SUBTYPE_EHRPD:
408 return 21.0;
409 case SUBTYPE_HSPAP:
410 return 42.0;
411 case SUBTYPE_LTE:
412 return 100.0;
413 case SUBTYPE_LTE_ADVANCED:
414 return 100.0;
415 case SUBTYPE_BLUETOOTH_1_2:
416 return 1.0;
417 case SUBTYPE_BLUETOOTH_2_1:
418 return 3.0;
419 case SUBTYPE_BLUETOOTH_3_0:
420 return 24.0;
421 case SUBTYPE_BLUETOOTH_4_0:
422 return 1.0;
423 case SUBTYPE_ETHERNET:
424 return 10.0;
425 case SUBTYPE_FAST_ETHERNET:
426 return 100.0;
427 case SUBTYPE_GIGABIT_ETHERNET:
428 return 1000.0;
429 case SUBTYPE_10_GIGABIT_ETHERNET:
430 return 10000.0;
431 case SUBTYPE_WIFI_B:
432 return 11.0;
433 case SUBTYPE_WIFI_G:
434 return 54.0;
435 case SUBTYPE_WIFI_N:
436 return 600.0;
437 case SUBTYPE_WIFI_AC:
438 return 1300.0;
439 case SUBTYPE_WIFI_AD:
440 return 7000.0;
441 case SUBTYPE_UNKNOWN:
442 return std::numeric_limits<double>::infinity();
443 case SUBTYPE_NONE:
444 return 0.0;
445 case SUBTYPE_OTHER:
446 return std::numeric_limits<double>::infinity();
447 }
448 NOTREACHED();
449 return std::numeric_limits<double>::infinity();
450 }
451
452 // static
AreNetworkHandlesSupported()453 bool NetworkChangeNotifier::AreNetworkHandlesSupported() {
454 if (g_network_change_notifier) {
455 return g_network_change_notifier->AreNetworkHandlesCurrentlySupported();
456 }
457 return false;
458 }
459
460 // static
GetConnectedNetworks(NetworkList * network_list)461 void NetworkChangeNotifier::GetConnectedNetworks(NetworkList* network_list) {
462 DCHECK(AreNetworkHandlesSupported());
463 if (g_network_change_notifier) {
464 g_network_change_notifier->GetCurrentConnectedNetworks(network_list);
465 } else {
466 network_list->clear();
467 }
468 }
469
470 // static
471 NetworkChangeNotifier::ConnectionType
GetNetworkConnectionType(handles::NetworkHandle network)472 NetworkChangeNotifier::GetNetworkConnectionType(
473 handles::NetworkHandle network) {
474 DCHECK(AreNetworkHandlesSupported());
475 return g_network_change_notifier
476 ? g_network_change_notifier->GetCurrentNetworkConnectionType(
477 network)
478 : CONNECTION_UNKNOWN;
479 }
480
481 // static
GetDefaultNetwork()482 handles::NetworkHandle NetworkChangeNotifier::GetDefaultNetwork() {
483 DCHECK(AreNetworkHandlesSupported());
484 return g_network_change_notifier
485 ? g_network_change_notifier->GetCurrentDefaultNetwork()
486 : handles::kInvalidNetworkHandle;
487 }
488
489 // static
490 SystemDnsConfigChangeNotifier*
GetSystemDnsConfigNotifier()491 NetworkChangeNotifier::GetSystemDnsConfigNotifier() {
492 if (g_network_change_notifier)
493 return g_network_change_notifier->GetCurrentSystemDnsConfigNotifier();
494 return nullptr;
495 }
496
497 // static
IsDefaultNetworkActive()498 bool NetworkChangeNotifier::IsDefaultNetworkActive() {
499 if (g_network_change_notifier)
500 return g_network_change_notifier->IsDefaultNetworkActiveInternal();
501 // Assume true as a "default" to avoid batching indefinitely.
502 return true;
503 }
504
505 // static
ConnectionTypeToString(ConnectionType type)506 const char* NetworkChangeNotifier::ConnectionTypeToString(
507 ConnectionType type) {
508 static const char* const kConnectionTypeNames[] = {
509 "CONNECTION_UNKNOWN", "CONNECTION_ETHERNET", "CONNECTION_WIFI",
510 "CONNECTION_2G", "CONNECTION_3G", "CONNECTION_4G",
511 "CONNECTION_NONE", "CONNECTION_BLUETOOTH", "CONNECTION_5G",
512 };
513 static_assert(std::size(kConnectionTypeNames) ==
514 NetworkChangeNotifier::CONNECTION_LAST + 1,
515 "ConnectionType name count should match");
516 if (type < CONNECTION_UNKNOWN || type > CONNECTION_LAST) {
517 NOTREACHED();
518 return "CONNECTION_INVALID";
519 }
520 return kConnectionTypeNames[type];
521 }
522
523 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
524 // static
GetAddressMapOwner()525 AddressMapOwnerLinux* NetworkChangeNotifier::GetAddressMapOwner() {
526 return g_network_change_notifier
527 ? g_network_change_notifier->GetAddressMapOwnerInternal()
528 : nullptr;
529 }
530 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
531
532 #if BUILDFLAG(IS_FUCHSIA)
533 // static
534 const internal::NetworkInterfaceCache*
GetNetworkInterfaceCache()535 NetworkChangeNotifier::GetNetworkInterfaceCache() {
536 return g_network_change_notifier
537 ? g_network_change_notifier->GetNetworkInterfaceCacheInternal()
538 : nullptr;
539 }
540 #endif // BUILDFLAG(IS_FUCHSIA)
541
542 // static
IsOffline()543 bool NetworkChangeNotifier::IsOffline() {
544 return GetConnectionType() == CONNECTION_NONE;
545 }
546
547 // static
IsConnectionCellular(ConnectionType type)548 bool NetworkChangeNotifier::IsConnectionCellular(ConnectionType type) {
549 bool is_cellular = false;
550 switch (type) {
551 case CONNECTION_2G:
552 case CONNECTION_3G:
553 case CONNECTION_4G:
554 case CONNECTION_5G:
555 is_cellular = true;
556 break;
557 case CONNECTION_UNKNOWN:
558 case CONNECTION_ETHERNET:
559 case CONNECTION_WIFI:
560 case CONNECTION_NONE:
561 case CONNECTION_BLUETOOTH:
562 is_cellular = false;
563 break;
564 }
565 return is_cellular;
566 }
567
568 // static
569 NetworkChangeNotifier::ConnectionType
ConnectionTypeFromInterfaces()570 NetworkChangeNotifier::ConnectionTypeFromInterfaces() {
571 NetworkInterfaceList interfaces;
572 if (!GetNetworkList(&interfaces, EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES))
573 return CONNECTION_UNKNOWN;
574 return ConnectionTypeFromInterfaceList(interfaces);
575 }
576
577 // static
578 NetworkChangeNotifier::ConnectionType
ConnectionTypeFromInterfaceList(const NetworkInterfaceList & interfaces)579 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(
580 const NetworkInterfaceList& interfaces) {
581 bool first = true;
582 ConnectionType result = CONNECTION_NONE;
583 for (const auto& network_interface : interfaces) {
584 #if BUILDFLAG(IS_WIN)
585 if (network_interface.friendly_name == "Teredo Tunneling Pseudo-Interface")
586 continue;
587 #endif
588 #if BUILDFLAG(IS_APPLE)
589 // Ignore link-local addresses as they aren't globally routable.
590 // Mac assigns these to disconnected interfaces like tunnel interfaces
591 // ("utun"), airdrop interfaces ("awdl"), and ethernet ports ("en").
592 if (network_interface.address.IsLinkLocal())
593 continue;
594 #endif
595
596 // Remove VMware network interfaces as they're internal and should not be
597 // used to determine the network connection type.
598 if (base::ToLowerASCII(network_interface.friendly_name).find("vmnet") !=
599 std::string::npos) {
600 continue;
601 }
602 if (first) {
603 first = false;
604 result = network_interface.type;
605 } else if (result != network_interface.type) {
606 return CONNECTION_UNKNOWN;
607 }
608 }
609 return result;
610 }
611
612 // static
613 std::unique_ptr<NetworkChangeNotifier>
CreateMockIfNeeded()614 NetworkChangeNotifier::CreateMockIfNeeded() {
615 {
616 base::AutoLock auto_lock(NetworkChangeNotifierCreationLock());
617 if (g_network_change_notifier)
618 return nullptr;
619 }
620 // Use an empty noop SystemDnsConfigChangeNotifier to disable actual system
621 // DNS configuration notifications.
622 return std::make_unique<MockNetworkChangeNotifier>(
623 std::make_unique<SystemDnsConfigChangeNotifier>(
624 nullptr /* task_runner */, nullptr /* dns_config_service */));
625 }
626
627 NetworkChangeNotifier::IPAddressObserver::IPAddressObserver() = default;
628 NetworkChangeNotifier::IPAddressObserver::~IPAddressObserver() = default;
629
630 NetworkChangeNotifier::ConnectionTypeObserver::ConnectionTypeObserver() =
631 default;
632 NetworkChangeNotifier::ConnectionTypeObserver::~ConnectionTypeObserver() =
633 default;
634
635 NetworkChangeNotifier::DNSObserver::DNSObserver() = default;
636 NetworkChangeNotifier::DNSObserver::~DNSObserver() = default;
637
638 NetworkChangeNotifier::NetworkChangeObserver::NetworkChangeObserver() = default;
639 NetworkChangeNotifier::NetworkChangeObserver::~NetworkChangeObserver() =
640 default;
641
642 NetworkChangeNotifier::MaxBandwidthObserver::MaxBandwidthObserver() = default;
643 NetworkChangeNotifier::MaxBandwidthObserver::~MaxBandwidthObserver() = default;
644
645 NetworkChangeNotifier::NetworkObserver::NetworkObserver() = default;
646 NetworkChangeNotifier::NetworkObserver::~NetworkObserver() = default;
647
648 NetworkChangeNotifier::ConnectionCostObserver::ConnectionCostObserver() =
649 default;
650 NetworkChangeNotifier::ConnectionCostObserver::~ConnectionCostObserver() =
651 default;
652
653 NetworkChangeNotifier::DefaultNetworkActiveObserver::
654 DefaultNetworkActiveObserver() = default;
655 NetworkChangeNotifier::DefaultNetworkActiveObserver::
656 ~DefaultNetworkActiveObserver() = default;
657
AddIPAddressObserver(IPAddressObserver * observer)658 void NetworkChangeNotifier::AddIPAddressObserver(IPAddressObserver* observer) {
659 DCHECK(!observer->observer_list_);
660 observer->observer_list_ = GetObserverList().ip_address_observer_list_;
661 observer->observer_list_->AddObserver(observer);
662 }
663
AddConnectionTypeObserver(ConnectionTypeObserver * observer)664 void NetworkChangeNotifier::AddConnectionTypeObserver(
665 ConnectionTypeObserver* observer) {
666 DCHECK(!observer->observer_list_);
667 observer->observer_list_ = GetObserverList().connection_type_observer_list_;
668 observer->observer_list_->AddObserver(observer);
669 }
670
AddDNSObserver(DNSObserver * observer)671 void NetworkChangeNotifier::AddDNSObserver(DNSObserver* observer) {
672 DCHECK(!observer->observer_list_);
673 observer->observer_list_ = GetObserverList().resolver_state_observer_list_;
674 observer->observer_list_->AddObserver(observer);
675 }
676
AddNetworkChangeObserver(NetworkChangeObserver * observer)677 void NetworkChangeNotifier::AddNetworkChangeObserver(
678 NetworkChangeObserver* observer) {
679 DCHECK(!observer->observer_list_);
680 observer->observer_list_ = GetObserverList().network_change_observer_list_;
681 observer->observer_list_->AddObserver(observer);
682 }
683
AddMaxBandwidthObserver(MaxBandwidthObserver * observer)684 void NetworkChangeNotifier::AddMaxBandwidthObserver(
685 MaxBandwidthObserver* observer) {
686 DCHECK(!observer->observer_list_);
687 observer->observer_list_ = GetObserverList().max_bandwidth_observer_list_;
688 observer->observer_list_->AddObserver(observer);
689 }
690
AddNetworkObserver(NetworkObserver * observer)691 void NetworkChangeNotifier::AddNetworkObserver(NetworkObserver* observer) {
692 base::AutoLock auto_lock(NetworkChangeNotifierCreationLock());
693 DCHECK(AreNetworkHandlesSupported());
694 DCHECK(!observer->observer_list_);
695 observer->observer_list_ = GetObserverList().network_observer_list_;
696 observer->observer_list_->AddObserver(observer);
697 }
698
AddConnectionCostObserver(ConnectionCostObserver * observer)699 void NetworkChangeNotifier::AddConnectionCostObserver(
700 ConnectionCostObserver* observer) {
701 DCHECK(!observer->observer_list_);
702 GetObserverList().connection_cost_observers_added_ = true;
703 observer->observer_list_ = GetObserverList().connection_cost_observer_list_;
704 observer->observer_list_->AddObserver(observer);
705 base::AutoLock auto_lock(NetworkChangeNotifierCreationLock());
706 if (g_network_change_notifier) {
707 g_network_change_notifier->ConnectionCostObserverAdded();
708 }
709 }
710
AddDefaultNetworkActiveObserver(DefaultNetworkActiveObserver * observer)711 void NetworkChangeNotifier::AddDefaultNetworkActiveObserver(
712 DefaultNetworkActiveObserver* observer) {
713 DCHECK(!observer->observer_list_);
714 observer->observer_list_ =
715 GetObserverList().default_network_active_observer_list_;
716 observer->observer_list_->AddObserver(observer);
717 base::AutoLock auto_lock(NetworkChangeNotifierCreationLock());
718 // Currently we lose DefaultNetworkActiveObserverAdded notifications for
719 // observers added prior to NCN creation. This should be a non-issue as
720 // currently only Cronet listens to this and its observers are always added
721 // after NCN creation.
722 if (g_network_change_notifier) {
723 g_network_change_notifier->DefaultNetworkActiveObserverAdded();
724 }
725 }
726
RemoveIPAddressObserver(IPAddressObserver * observer)727 void NetworkChangeNotifier::RemoveIPAddressObserver(
728 IPAddressObserver* observer) {
729 if (observer->observer_list_) {
730 observer->observer_list_->RemoveObserver(observer);
731 observer->observer_list_.reset();
732 }
733 }
734
RemoveConnectionTypeObserver(ConnectionTypeObserver * observer)735 void NetworkChangeNotifier::RemoveConnectionTypeObserver(
736 ConnectionTypeObserver* observer) {
737 if (observer->observer_list_) {
738 observer->observer_list_->RemoveObserver(observer);
739 observer->observer_list_.reset();
740 }
741 }
742
RemoveDNSObserver(DNSObserver * observer)743 void NetworkChangeNotifier::RemoveDNSObserver(DNSObserver* observer) {
744 if (observer->observer_list_) {
745 observer->observer_list_->RemoveObserver(observer);
746 observer->observer_list_.reset();
747 }
748 }
749
RemoveNetworkChangeObserver(NetworkChangeObserver * observer)750 void NetworkChangeNotifier::RemoveNetworkChangeObserver(
751 NetworkChangeObserver* observer) {
752 if (observer->observer_list_) {
753 observer->observer_list_->RemoveObserver(observer);
754 observer->observer_list_.reset();
755 }
756 }
757
RemoveMaxBandwidthObserver(MaxBandwidthObserver * observer)758 void NetworkChangeNotifier::RemoveMaxBandwidthObserver(
759 MaxBandwidthObserver* observer) {
760 if (observer->observer_list_) {
761 observer->observer_list_->RemoveObserver(observer);
762 observer->observer_list_.reset();
763 }
764 }
765
RemoveNetworkObserver(NetworkObserver * observer)766 void NetworkChangeNotifier::RemoveNetworkObserver(NetworkObserver* observer) {
767 if (observer->observer_list_) {
768 observer->observer_list_->RemoveObserver(observer);
769 observer->observer_list_.reset();
770 }
771 }
772
RemoveConnectionCostObserver(ConnectionCostObserver * observer)773 void NetworkChangeNotifier::RemoveConnectionCostObserver(
774 ConnectionCostObserver* observer) {
775 if (observer->observer_list_) {
776 observer->observer_list_->RemoveObserver(observer);
777 observer->observer_list_.reset();
778 }
779 }
780
RemoveDefaultNetworkActiveObserver(DefaultNetworkActiveObserver * observer)781 void NetworkChangeNotifier::RemoveDefaultNetworkActiveObserver(
782 DefaultNetworkActiveObserver* observer) {
783 if (observer->observer_list_) {
784 observer->observer_list_->RemoveObserver(observer);
785 observer->observer_list_.reset();
786 g_network_change_notifier->DefaultNetworkActiveObserverRemoved();
787 }
788 }
789
TriggerNonSystemDnsChange()790 void NetworkChangeNotifier::TriggerNonSystemDnsChange() {
791 NetworkChangeNotifier::NotifyObserversOfDNSChange();
792 }
793
794 // static
NotifyObserversOfIPAddressChangeForTests()795 void NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests() {
796 if (g_network_change_notifier)
797 g_network_change_notifier->NotifyObserversOfIPAddressChangeImpl();
798 }
799
800 // static
NotifyObserversOfConnectionTypeChangeForTests(ConnectionType type)801 void NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests(
802 ConnectionType type) {
803 if (g_network_change_notifier)
804 g_network_change_notifier->NotifyObserversOfConnectionTypeChangeImpl(type);
805 }
806
807 // static
NotifyObserversOfDNSChangeForTests()808 void NetworkChangeNotifier::NotifyObserversOfDNSChangeForTests() {
809 if (g_network_change_notifier)
810 g_network_change_notifier->NotifyObserversOfDNSChangeImpl();
811 }
812
813 // static
NotifyObserversOfNetworkChangeForTests(ConnectionType type)814 void NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
815 ConnectionType type) {
816 if (g_network_change_notifier)
817 g_network_change_notifier->NotifyObserversOfNetworkChangeImpl(type);
818 }
819
820 // static
NotifyObserversOfMaxBandwidthChangeForTests(double max_bandwidth_mbps,ConnectionType type)821 void NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChangeForTests(
822 double max_bandwidth_mbps,
823 ConnectionType type) {
824 if (g_network_change_notifier) {
825 g_network_change_notifier->NotifyObserversOfMaxBandwidthChangeImpl(
826 max_bandwidth_mbps, type);
827 }
828 }
829
830 // static
NotifyObserversOfConnectionCostChangeForTests(ConnectionCost cost)831 void NetworkChangeNotifier::NotifyObserversOfConnectionCostChangeForTests(
832 ConnectionCost cost) {
833 if (g_network_change_notifier)
834 g_network_change_notifier->NotifyObserversOfConnectionCostChangeImpl(cost);
835 }
836
837 // static
NotifyObserversOfDefaultNetworkActiveForTests()838 void NetworkChangeNotifier::NotifyObserversOfDefaultNetworkActiveForTests() {
839 if (g_network_change_notifier)
840 g_network_change_notifier->NotifyObserversOfDefaultNetworkActiveImpl();
841 }
842
843 // static
SetTestNotificationsOnly(bool test_only)844 void NetworkChangeNotifier::SetTestNotificationsOnly(bool test_only) {
845 DCHECK(!g_network_change_notifier);
846 NetworkChangeNotifier::test_notifications_only_ = test_only;
847 }
848
NetworkChangeNotifier(const NetworkChangeCalculatorParams & params,SystemDnsConfigChangeNotifier * system_dns_config_notifier,bool omit_observers_in_constructor_for_testing)849 NetworkChangeNotifier::NetworkChangeNotifier(
850 const NetworkChangeCalculatorParams& params
851 /*= NetworkChangeCalculatorParams()*/,
852 SystemDnsConfigChangeNotifier* system_dns_config_notifier /*= nullptr */,
853 bool omit_observers_in_constructor_for_testing /*= false */)
854 : system_dns_config_notifier_(system_dns_config_notifier),
855 system_dns_config_observer_(std::make_unique<SystemDnsConfigObserver>()) {
856 {
857 base::AutoLock auto_lock(NetworkChangeNotifierCreationLock());
858 if (!system_dns_config_notifier_) {
859 static base::NoDestructor<SystemDnsConfigChangeNotifier> singleton{};
860 system_dns_config_notifier_ = singleton.get();
861 }
862
863 DCHECK(!g_network_change_notifier);
864 g_network_change_notifier = this;
865
866 system_dns_config_notifier_->AddObserver(system_dns_config_observer_.get());
867 if (GetObserverList().connection_cost_observers_added_) {
868 g_network_change_notifier->ConnectionCostObserverAdded();
869 }
870 }
871 if (!omit_observers_in_constructor_for_testing) {
872 network_change_calculator_ =
873 std::make_unique<NetworkChangeCalculator>(params);
874 }
875 }
876
877 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
GetAddressMapOwnerInternal()878 AddressMapOwnerLinux* NetworkChangeNotifier::GetAddressMapOwnerInternal() {
879 return nullptr;
880 }
881 #endif
882
883 #if BUILDFLAG(IS_FUCHSIA)
884 const internal::NetworkInterfaceCache*
GetNetworkInterfaceCacheInternal() const885 NetworkChangeNotifier::GetNetworkInterfaceCacheInternal() const {
886 return nullptr;
887 }
888 #endif
889
890 NetworkChangeNotifier::ConnectionCost
GetCurrentConnectionCost()891 NetworkChangeNotifier::GetCurrentConnectionCost() {
892 // This is the default non-platform specific implementation and assumes that
893 // cellular connectivity is metered and non-cellular is not. The function can
894 // be specialized on each platform specific notifier implementation.
895 return IsConnectionCellular(GetCurrentConnectionType())
896 ? CONNECTION_COST_METERED
897 : CONNECTION_COST_UNMETERED;
898 }
899
900 NetworkChangeNotifier::ConnectionSubtype
GetCurrentConnectionSubtype() const901 NetworkChangeNotifier::GetCurrentConnectionSubtype() const {
902 return SUBTYPE_UNKNOWN;
903 }
904
GetCurrentMaxBandwidthAndConnectionType(double * max_bandwidth_mbps,ConnectionType * connection_type) const905 void NetworkChangeNotifier::GetCurrentMaxBandwidthAndConnectionType(
906 double* max_bandwidth_mbps,
907 ConnectionType* connection_type) const {
908 // This default implementation conforms to the NetInfo V3 specification but
909 // should be overridden to provide specific bandwidth data based on the
910 // platform.
911 *connection_type = GetCurrentConnectionType();
912 *max_bandwidth_mbps =
913 *connection_type == CONNECTION_NONE
914 ? GetMaxBandwidthMbpsForConnectionSubtype(SUBTYPE_NONE)
915 : GetMaxBandwidthMbpsForConnectionSubtype(SUBTYPE_UNKNOWN);
916 }
917
AreNetworkHandlesCurrentlySupported() const918 bool NetworkChangeNotifier::AreNetworkHandlesCurrentlySupported() const {
919 return false;
920 }
921
GetCurrentConnectedNetworks(NetworkList * network_list) const922 void NetworkChangeNotifier::GetCurrentConnectedNetworks(
923 NetworkList* network_list) const {
924 network_list->clear();
925 }
926
927 NetworkChangeNotifier::ConnectionType
GetCurrentNetworkConnectionType(handles::NetworkHandle network) const928 NetworkChangeNotifier::GetCurrentNetworkConnectionType(
929 handles::NetworkHandle network) const {
930 return CONNECTION_UNKNOWN;
931 }
932
GetCurrentDefaultNetwork() const933 handles::NetworkHandle NetworkChangeNotifier::GetCurrentDefaultNetwork() const {
934 return handles::kInvalidNetworkHandle;
935 }
936
937 SystemDnsConfigChangeNotifier*
GetCurrentSystemDnsConfigNotifier()938 NetworkChangeNotifier::GetCurrentSystemDnsConfigNotifier() {
939 DCHECK(system_dns_config_notifier_);
940 return system_dns_config_notifier_;
941 }
942
IsDefaultNetworkActiveInternal()943 bool NetworkChangeNotifier::IsDefaultNetworkActiveInternal() {
944 return true;
945 }
946
947 // static
NotifyObserversOfIPAddressChange()948 void NetworkChangeNotifier::NotifyObserversOfIPAddressChange() {
949 if (g_network_change_notifier &&
950 !NetworkChangeNotifier::test_notifications_only_) {
951 g_network_change_notifier->NotifyObserversOfIPAddressChangeImpl();
952 }
953 }
954
955 // static
NotifyObserversOfConnectionTypeChange()956 void NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange() {
957 if (g_network_change_notifier &&
958 !NetworkChangeNotifier::test_notifications_only_) {
959 g_network_change_notifier->NotifyObserversOfConnectionTypeChangeImpl(
960 GetConnectionType());
961 }
962 }
963
964 // static
NotifyObserversOfNetworkChange(ConnectionType type)965 void NetworkChangeNotifier::NotifyObserversOfNetworkChange(
966 ConnectionType type) {
967 if (g_network_change_notifier &&
968 !NetworkChangeNotifier::test_notifications_only_) {
969 g_network_change_notifier->NotifyObserversOfNetworkChangeImpl(type);
970 }
971 }
972
973 // static
NotifyObserversOfMaxBandwidthChange(double max_bandwidth_mbps,ConnectionType type)974 void NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(
975 double max_bandwidth_mbps,
976 ConnectionType type) {
977 if (g_network_change_notifier &&
978 !NetworkChangeNotifier::test_notifications_only_) {
979 g_network_change_notifier->NotifyObserversOfMaxBandwidthChangeImpl(
980 max_bandwidth_mbps, type);
981 }
982 }
983
984 // static
NotifyObserversOfDNSChange()985 void NetworkChangeNotifier::NotifyObserversOfDNSChange() {
986 if (g_network_change_notifier &&
987 !NetworkChangeNotifier::test_notifications_only_) {
988 g_network_change_notifier->NotifyObserversOfDNSChangeImpl();
989 }
990 }
991
992 // static
NotifyObserversOfSpecificNetworkChange(NetworkChangeType type,handles::NetworkHandle network)993 void NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChange(
994 NetworkChangeType type,
995 handles::NetworkHandle network) {
996 if (g_network_change_notifier &&
997 !NetworkChangeNotifier::test_notifications_only_) {
998 g_network_change_notifier->NotifyObserversOfSpecificNetworkChangeImpl(
999 type, network);
1000 }
1001 }
1002
1003 // static
NotifyObserversOfConnectionCostChange()1004 void NetworkChangeNotifier::NotifyObserversOfConnectionCostChange() {
1005 if (g_network_change_notifier &&
1006 !NetworkChangeNotifier::test_notifications_only_) {
1007 g_network_change_notifier->NotifyObserversOfConnectionCostChangeImpl(
1008 GetConnectionCost());
1009 }
1010 }
1011
1012 // static
NotifyObserversOfDefaultNetworkActive()1013 void NetworkChangeNotifier::NotifyObserversOfDefaultNetworkActive() {
1014 if (g_network_change_notifier &&
1015 !NetworkChangeNotifier::test_notifications_only_) {
1016 g_network_change_notifier->NotifyObserversOfDefaultNetworkActiveImpl();
1017 }
1018 }
1019
StopSystemDnsConfigNotifier()1020 void NetworkChangeNotifier::StopSystemDnsConfigNotifier() {
1021 if (!system_dns_config_notifier_)
1022 return;
1023
1024 system_dns_config_notifier_->RemoveObserver(
1025 system_dns_config_observer_.get());
1026 system_dns_config_observer_ = nullptr;
1027 system_dns_config_notifier_ = nullptr;
1028 }
1029
NotifyObserversOfIPAddressChangeImpl()1030 void NetworkChangeNotifier::NotifyObserversOfIPAddressChangeImpl() {
1031 GetObserverList().ip_address_observer_list_->Notify(
1032 FROM_HERE, &IPAddressObserver::OnIPAddressChanged);
1033 }
1034
NotifyObserversOfConnectionTypeChangeImpl(ConnectionType type)1035 void NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeImpl(
1036 ConnectionType type) {
1037 GetObserverList().connection_type_observer_list_->Notify(
1038 FROM_HERE, &ConnectionTypeObserver::OnConnectionTypeChanged, type);
1039 }
1040
NotifyObserversOfNetworkChangeImpl(ConnectionType type)1041 void NetworkChangeNotifier::NotifyObserversOfNetworkChangeImpl(
1042 ConnectionType type) {
1043 GetObserverList().network_change_observer_list_->Notify(
1044 FROM_HERE, &NetworkChangeObserver::OnNetworkChanged, type);
1045 }
1046
NotifyObserversOfDNSChangeImpl()1047 void NetworkChangeNotifier::NotifyObserversOfDNSChangeImpl() {
1048 GetObserverList().resolver_state_observer_list_->Notify(
1049 FROM_HERE, &DNSObserver::OnDNSChanged);
1050 }
1051
NotifyObserversOfMaxBandwidthChangeImpl(double max_bandwidth_mbps,ConnectionType type)1052 void NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChangeImpl(
1053 double max_bandwidth_mbps,
1054 ConnectionType type) {
1055 GetObserverList().max_bandwidth_observer_list_->Notify(
1056 FROM_HERE, &MaxBandwidthObserver::OnMaxBandwidthChanged,
1057 max_bandwidth_mbps, type);
1058 }
1059
NotifyObserversOfSpecificNetworkChangeImpl(NetworkChangeType type,handles::NetworkHandle network)1060 void NetworkChangeNotifier::NotifyObserversOfSpecificNetworkChangeImpl(
1061 NetworkChangeType type,
1062 handles::NetworkHandle network) {
1063 switch (type) {
1064 case NetworkChangeType::kConnected:
1065 GetObserverList().network_observer_list_->Notify(
1066 FROM_HERE, &NetworkObserver::OnNetworkConnected, network);
1067 break;
1068 case NetworkChangeType::kDisconnected:
1069 GetObserverList().network_observer_list_->Notify(
1070 FROM_HERE, &NetworkObserver::OnNetworkDisconnected, network);
1071 break;
1072 case NetworkChangeType::kSoonToDisconnect:
1073 GetObserverList().network_observer_list_->Notify(
1074 FROM_HERE, &NetworkObserver::OnNetworkSoonToDisconnect, network);
1075 break;
1076 case NetworkChangeType::kMadeDefault:
1077 GetObserverList().network_observer_list_->Notify(
1078 FROM_HERE, &NetworkObserver::OnNetworkMadeDefault, network);
1079 break;
1080 }
1081 }
1082
NotifyObserversOfConnectionCostChangeImpl(ConnectionCost cost)1083 void NetworkChangeNotifier::NotifyObserversOfConnectionCostChangeImpl(
1084 ConnectionCost cost) {
1085 GetObserverList().connection_cost_observer_list_->Notify(
1086 FROM_HERE, &ConnectionCostObserver::OnConnectionCostChanged, cost);
1087 }
1088
NotifyObserversOfDefaultNetworkActiveImpl()1089 void NetworkChangeNotifier::NotifyObserversOfDefaultNetworkActiveImpl() {
1090 GetObserverList().default_network_active_observer_list_->Notify(
1091 FROM_HERE, &DefaultNetworkActiveObserver::OnDefaultNetworkActive);
1092 }
1093
DisableForTest()1094 NetworkChangeNotifier::DisableForTest::DisableForTest()
1095 : network_change_notifier_(g_network_change_notifier) {
1096 DCHECK(g_network_change_notifier);
1097 g_network_change_notifier = nullptr;
1098 }
1099
~DisableForTest()1100 NetworkChangeNotifier::DisableForTest::~DisableForTest() {
1101 DCHECK(!g_network_change_notifier);
1102 g_network_change_notifier = network_change_notifier_;
1103 }
1104
1105 // static
GetObserverList()1106 NetworkChangeNotifier::ObserverList& NetworkChangeNotifier::GetObserverList() {
1107 static base::NoDestructor<NetworkChangeNotifier::ObserverList> observers;
1108 return *observers;
1109 }
1110
1111 } // namespace net
1112