adamk@chromium.org | dc090f4 | 2012-02-11 00:33:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
willchan@chromium.org | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 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_linux.h" |
| 6 | |
thakis@chromium.org | 46018c9d | 2011-09-06 03:42:34 | [diff] [blame] | 7 | #include "base/bind.h" |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 9 | #include "base/macros.h" |
gab | f64a25e | 2017-05-12 19:42:56 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
brettw@chromium.org | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 11 | #include "base/threading/thread.h" |
szym@chromium.org | 6717280 | 2012-07-16 22:27:24 | [diff] [blame] | 12 | #include "net/base/address_tracker_linux.h" |
szym@chromium.org | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 13 | #include "net/dns/dns_config_service.h" |
thakis@chromium.org | 46018c9d | 2011-09-06 03:42:34 | [diff] [blame] | 14 | |
willchan@chromium.org | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 15 | namespace net { |
| 16 | |
szym@chromium.org | 6717280 | 2012-07-16 22:27:24 | [diff] [blame] | 17 | class NetworkChangeNotifierLinux::Thread : public base::Thread { |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 18 | public: |
davidben | 1e912ea | 2016-04-20 19:17:07 | [diff] [blame] | 19 | explicit Thread(const std::unordered_set<std::string>& ignored_interfaces); |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 20 | ~Thread() override; |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 21 | |
droger@google.com | 8bbc7a79 | 2012-05-24 11:30:05 | [diff] [blame] | 22 | // Plumbing for NetworkChangeNotifier::GetCurrentConnectionType. |
adamk@chromium.org | ae66a48d | 2011-11-16 22:28:06 | [diff] [blame] | 23 | // Safe to call from any thread. |
droger@google.com | 8bbc7a79 | 2012-05-24 11:30:05 | [diff] [blame] | 24 | NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() { |
allan.jensen | 143cc3e1 | 2016-03-21 13:00:31 | [diff] [blame] | 25 | return address_tracker_->GetCurrentConnectionType(); |
adamk@chromium.org | ae66a48d | 2011-11-16 22:28:06 | [diff] [blame] | 26 | } |
| 27 | |
szym@chromium.org | 6717280 | 2012-07-16 22:27:24 | [diff] [blame] | 28 | const internal::AddressTrackerLinux* address_tracker() const { |
allan.jensen | 143cc3e1 | 2016-03-21 13:00:31 | [diff] [blame] | 29 | return address_tracker_.get(); |
szym@chromium.org | 6717280 | 2012-07-16 22:27:24 | [diff] [blame] | 30 | } |
| 31 | |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 32 | protected: |
| 33 | // base::Thread |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 34 | void Init() override; |
| 35 | void CleanUp() override; |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 36 | |
| 37 | private: |
derekjchow | 5482d5e | 2015-01-31 01:04:51 | [diff] [blame] | 38 | void OnIPAddressChanged(); |
| 39 | void OnLinkChanged(); |
danakj | 7f767e6 | 2016-04-16 23:20:23 | [diff] [blame] | 40 | std::unique_ptr<DnsConfigService> dns_config_service_; |
pauljensen@chromium.org | d3eada5 | 2012-11-29 17:11:55 | [diff] [blame] | 41 | // Used to detect online/offline state and IP address changes. |
danakj | 7f767e6 | 2016-04-16 23:20:23 | [diff] [blame] | 42 | std::unique_ptr<internal::AddressTrackerLinux> address_tracker_; |
derekjchow | 5482d5e | 2015-01-31 01:04:51 | [diff] [blame] | 43 | NetworkChangeNotifier::ConnectionType last_type_; |
szym@chromium.org | 05aad32d | 2012-05-16 18:10:53 | [diff] [blame] | 44 | |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 45 | DISALLOW_COPY_AND_ASSIGN(Thread); |
| 46 | }; |
| 47 | |
derekjchow | ea1725c | 2015-05-08 03:16:01 | [diff] [blame] | 48 | NetworkChangeNotifierLinux::Thread::Thread( |
davidben | 1e912ea | 2016-04-20 19:17:07 | [diff] [blame] | 49 | const std::unordered_set<std::string>& ignored_interfaces) |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 50 | : base::Thread("NetworkChangeNotifier"), |
allan.jensen | 143cc3e1 | 2016-03-21 13:00:31 | [diff] [blame] | 51 | address_tracker_(new internal::AddressTrackerLinux( |
derekjchow | 5482d5e | 2015-01-31 01:04:51 | [diff] [blame] | 52 | base::Bind(&NetworkChangeNotifierLinux::Thread::OnIPAddressChanged, |
| 53 | base::Unretained(this)), |
| 54 | base::Bind(&NetworkChangeNotifierLinux::Thread::OnLinkChanged, |
| 55 | base::Unretained(this)), |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 56 | base::DoNothing(), |
allan.jensen | 143cc3e1 | 2016-03-21 13:00:31 | [diff] [blame] | 57 | ignored_interfaces)), |
| 58 | last_type_(NetworkChangeNotifier::CONNECTION_NONE) {} |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 59 | |
joi@chromium.org | d583c3a | 2011-11-02 15:31:56 | [diff] [blame] | 60 | NetworkChangeNotifierLinux::Thread::~Thread() { |
| 61 | DCHECK(!Thread::IsRunning()); |
| 62 | } |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 63 | |
| 64 | void NetworkChangeNotifierLinux::Thread::Init() { |
allan.jensen | 143cc3e1 | 2016-03-21 13:00:31 | [diff] [blame] | 65 | address_tracker_->Init(); |
szym@chromium.org | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 66 | dns_config_service_ = DnsConfigService::CreateSystemService(); |
| 67 | dns_config_service_->WatchConfig( |
| 68 | base::Bind(&NetworkChangeNotifier::SetDnsConfig)); |
willchan@chromium.org | 6d603aa | 2010-02-19 18:44:58 | [diff] [blame] | 69 | } |
| 70 | |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 71 | void NetworkChangeNotifierLinux::Thread::CleanUp() { |
allan.jensen | 143cc3e1 | 2016-03-21 13:00:31 | [diff] [blame] | 72 | // Delete AddressTrackerLinux before MessageLoop gets deleted as |
| 73 | // AddressTrackerLinux's FileDescriptorWatcher holds a pointer to the |
| 74 | // MessageLoop. |
| 75 | address_tracker_.reset(); |
szym@chromium.org | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 76 | dns_config_service_.reset(); |
eroman@chromium.org | 72de04b7 | 2010-04-12 23:54:34 | [diff] [blame] | 77 | } |
| 78 | |
derekjchow | 5482d5e | 2015-01-31 01:04:51 | [diff] [blame] | 79 | void NetworkChangeNotifierLinux::Thread::OnIPAddressChanged() { |
| 80 | NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); |
| 81 | // When the IP address of a network interface is added/deleted, the |
| 82 | // connection type may have changed. |
| 83 | OnLinkChanged(); |
| 84 | } |
| 85 | |
| 86 | void NetworkChangeNotifierLinux::Thread::OnLinkChanged() { |
| 87 | if (last_type_ != GetCurrentConnectionType()) { |
| 88 | NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); |
| 89 | last_type_ = GetCurrentConnectionType(); |
jkarlin | bce49186 | 2015-09-18 15:14:07 | [diff] [blame] | 90 | double max_bandwidth_mbps = |
zhuoyu.qian | 4d65140 | 2017-10-30 13:03:33 | [diff] [blame] | 91 | NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype( |
jkarlin | bce49186 | 2015-09-18 15:14:07 | [diff] [blame] | 92 | last_type_ == CONNECTION_NONE ? SUBTYPE_NONE : SUBTYPE_UNKNOWN); |
| 93 | NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange( |
| 94 | max_bandwidth_mbps, last_type_); |
derekjchow | 5482d5e | 2015-01-31 01:04:51 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
derekjchow | ea1725c | 2015-05-08 03:16:01 | [diff] [blame] | 98 | NetworkChangeNotifierLinux::NetworkChangeNotifierLinux( |
davidben | 1e912ea | 2016-04-20 19:17:07 | [diff] [blame] | 99 | const std::unordered_set<std::string>& ignored_interfaces) |
pauljensen@chromium.org | 0384887 | 2012-12-08 02:46:41 | [diff] [blame] | 100 | : NetworkChangeNotifier(NetworkChangeCalculatorParamsLinux()), |
derekjchow | ea1725c | 2015-05-08 03:16:01 | [diff] [blame] | 101 | notifier_thread_(new Thread(ignored_interfaces)) { |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 102 | // We create this notifier thread because the notification implementation |
| 103 | // needs a MessageLoopForIO, and there's no guarantee that |
Gabriel Charette | 1e5bed44 | 2018-04-24 23:25:49 | [diff] [blame] | 104 | // MessageLoopCurrent::Get() meets that criterion. |
xhwang@chromium.org | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 105 | base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 106 | notifier_thread_->StartWithOptions(thread_options); |
| 107 | } |
| 108 | |
| 109 | NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { |
joi@chromium.org | d583c3a | 2011-11-02 15:31:56 | [diff] [blame] | 110 | // Stopping from here allows us to sanity- check that the notifier |
| 111 | // thread shut down properly. |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 112 | notifier_thread_->Stop(); |
| 113 | } |
| 114 | |
pauljensen@chromium.org | 0384887 | 2012-12-08 02:46:41 | [diff] [blame] | 115 | // static |
| 116 | NetworkChangeNotifier::NetworkChangeCalculatorParams |
| 117 | NetworkChangeNotifierLinux::NetworkChangeCalculatorParamsLinux() { |
| 118 | NetworkChangeCalculatorParams params; |
| 119 | // Delay values arrived at by simple experimentation and adjusted so as to |
| 120 | // produce a single signal when switching between network connections. |
| 121 | params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(2000); |
| 122 | params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(2000); |
| 123 | params.connection_type_offline_delay_ = |
| 124 | base::TimeDelta::FromMilliseconds(1500); |
| 125 | params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500); |
| 126 | return params; |
| 127 | } |
| 128 | |
droger@google.com | 8bbc7a79 | 2012-05-24 11:30:05 | [diff] [blame] | 129 | NetworkChangeNotifier::ConnectionType |
| 130 | NetworkChangeNotifierLinux::GetCurrentConnectionType() const { |
| 131 | return notifier_thread_->GetCurrentConnectionType(); |
willchan@chromium.org | b5e0b76 | 2010-10-21 20:45:25 | [diff] [blame] | 132 | } |
| 133 | |
szym@chromium.org | 6717280 | 2012-07-16 22:27:24 | [diff] [blame] | 134 | const internal::AddressTrackerLinux* |
| 135 | NetworkChangeNotifierLinux::GetAddressTrackerInternal() const { |
| 136 | return notifier_thread_->address_tracker(); |
| 137 | } |
| 138 | |
willchan@chromium.org | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 139 | } // namespace net |