network_change_notifier_posix.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <string>
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/task/task_traits.h"
  8. #include "build/build_config.h"
  9. #include "build/chromeos_buildflags.h"
  10. #include "net/base/network_change_notifier_posix.h"
  11. #include "net/dns/dns_config_service_posix.h"
  12. #include "net/dns/system_dns_config_change_notifier.h"
  13. #if BUILDFLAG(IS_ANDROID)
  14. #include "net/android/network_change_notifier_android.h"
  15. #endif
  16. namespace net {
  17. NetworkChangeNotifierPosix::NetworkChangeNotifierPosix(
  18. NetworkChangeNotifier::ConnectionType initial_connection_type,
  19. NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype)
  20. : NetworkChangeNotifierPosix(initial_connection_type,
  21. initial_connection_subtype,
  22. /*system_dns_config_notifier=*/nullptr) {}
  23. NetworkChangeNotifierPosix::NetworkChangeNotifierPosix(
  24. NetworkChangeNotifier::ConnectionType initial_connection_type,
  25. NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype,
  26. SystemDnsConfigChangeNotifier* system_dns_config_notifier)
  27. : NetworkChangeNotifier(NetworkChangeCalculatorParamsPosix(),
  28. system_dns_config_notifier),
  29. connection_type_(initial_connection_type),
  30. max_bandwidth_mbps_(
  31. NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype(
  32. initial_connection_subtype)) {}
  33. NetworkChangeNotifierPosix::~NetworkChangeNotifierPosix() {
  34. ClearGlobalPointer();
  35. }
  36. void NetworkChangeNotifierPosix::OnDNSChanged() {
  37. GetCurrentSystemDnsConfigNotifier()->RefreshConfig();
  38. }
  39. void NetworkChangeNotifierPosix::OnIPAddressChanged() {
  40. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  41. NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
  42. }
  43. void NetworkChangeNotifierPosix::OnConnectionChanged(
  44. NetworkChangeNotifier::ConnectionType connection_type) {
  45. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  46. {
  47. base::AutoLock scoped_lock(lock_);
  48. connection_type_ = connection_type;
  49. }
  50. NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
  51. }
  52. void NetworkChangeNotifierPosix::OnConnectionSubtypeChanged(
  53. NetworkChangeNotifier::ConnectionType connection_type,
  54. NetworkChangeNotifier::ConnectionSubtype connection_subtype) {
  55. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  56. double max_bandwidth_mbps =
  57. GetMaxBandwidthMbpsForConnectionSubtype(connection_subtype);
  58. {
  59. base::AutoLock scoped_lock(lock_);
  60. max_bandwidth_mbps_ = max_bandwidth_mbps;
  61. }
  62. NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(max_bandwidth_mbps,
  63. connection_type);
  64. }
  65. NetworkChangeNotifier::ConnectionType
  66. NetworkChangeNotifierPosix::GetCurrentConnectionType() const {
  67. base::AutoLock scoped_lock(lock_);
  68. return connection_type_;
  69. }
  70. void NetworkChangeNotifierPosix::GetCurrentMaxBandwidthAndConnectionType(
  71. double* max_bandwidth_mbps,
  72. ConnectionType* connection_type) const {
  73. base::AutoLock scoped_lock(lock_);
  74. *connection_type = connection_type_;
  75. *max_bandwidth_mbps = max_bandwidth_mbps_;
  76. }
  77. // static
  78. NetworkChangeNotifier::NetworkChangeCalculatorParams
  79. NetworkChangeNotifierPosix::NetworkChangeCalculatorParamsPosix() {
  80. NetworkChangeCalculatorParams params;
  81. #if BUILDFLAG(IS_CHROMEOS)
  82. // Delay values arrived at by simple experimentation and adjusted so as to
  83. // produce a single signal when switching between network connections.
  84. params.ip_address_offline_delay_ = base::Milliseconds(4000);
  85. params.ip_address_online_delay_ = base::Milliseconds(1000);
  86. params.connection_type_offline_delay_ = base::Milliseconds(500);
  87. params.connection_type_online_delay_ = base::Milliseconds(500);
  88. #elif BUILDFLAG(IS_ANDROID)
  89. params =
  90. net::NetworkChangeNotifierAndroid::NetworkChangeCalculatorParamsAndroid();
  91. #else
  92. NOTIMPLEMENTED();
  93. #endif
  94. return params;
  95. }
  96. } // namespace net