[go: nahoru, domu]

blob: 02901fd8baa62336469c9383fbbbbc6d4bb99ab5 [file] [log] [blame]
adamk@chromium.orgdc090f42012-02-11 00:33:481// Copyright (c) 2012 The Chromium Authors. All rights reserved.
willchan@chromium.org100d5fb92009-12-21 21:08:352// 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.org46018c9d2011-09-06 03:42:347#include "base/bind.h"
willchan@chromium.orgb5e0b762010-10-21 20:45:258#include "base/compiler_specific.h"
Avi Drissman13fc8932015-12-20 04:40:469#include "base/macros.h"
gabf64a25e2017-05-12 19:42:5610#include "base/message_loop/message_loop.h"
brettw@chromium.org34b99632011-01-01 01:01:0611#include "base/threading/thread.h"
szym@chromium.org67172802012-07-16 22:27:2412#include "net/base/address_tracker_linux.h"
szym@chromium.orgbb0e34542012-08-31 19:52:4013#include "net/dns/dns_config_service.h"
thakis@chromium.org46018c9d2011-09-06 03:42:3414
willchan@chromium.org100d5fb92009-12-21 21:08:3515namespace net {
16
szym@chromium.org67172802012-07-16 22:27:2417class NetworkChangeNotifierLinux::Thread : public base::Thread {
willchan@chromium.orgb5e0b762010-10-21 20:45:2518 public:
davidben1e912ea2016-04-20 19:17:0719 explicit Thread(const std::unordered_set<std::string>& ignored_interfaces);
dcheng67be2b1f2014-10-27 21:47:2920 ~Thread() override;
willchan@chromium.orgb5e0b762010-10-21 20:45:2521
droger@google.com8bbc7a792012-05-24 11:30:0522 // Plumbing for NetworkChangeNotifier::GetCurrentConnectionType.
adamk@chromium.orgae66a48d2011-11-16 22:28:0623 // Safe to call from any thread.
droger@google.com8bbc7a792012-05-24 11:30:0524 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() {
allan.jensen143cc3e12016-03-21 13:00:3125 return address_tracker_->GetCurrentConnectionType();
adamk@chromium.orgae66a48d2011-11-16 22:28:0626 }
27
szym@chromium.org67172802012-07-16 22:27:2428 const internal::AddressTrackerLinux* address_tracker() const {
allan.jensen143cc3e12016-03-21 13:00:3129 return address_tracker_.get();
szym@chromium.org67172802012-07-16 22:27:2430 }
31
willchan@chromium.orgb5e0b762010-10-21 20:45:2532 protected:
33 // base::Thread
dcheng67be2b1f2014-10-27 21:47:2934 void Init() override;
35 void CleanUp() override;
willchan@chromium.orgb5e0b762010-10-21 20:45:2536
37 private:
derekjchow5482d5e2015-01-31 01:04:5138 void OnIPAddressChanged();
39 void OnLinkChanged();
danakj7f767e62016-04-16 23:20:2340 std::unique_ptr<DnsConfigService> dns_config_service_;
pauljensen@chromium.orgd3eada52012-11-29 17:11:5541 // Used to detect online/offline state and IP address changes.
danakj7f767e62016-04-16 23:20:2342 std::unique_ptr<internal::AddressTrackerLinux> address_tracker_;
derekjchow5482d5e2015-01-31 01:04:5143 NetworkChangeNotifier::ConnectionType last_type_;
szym@chromium.org05aad32d2012-05-16 18:10:5344
willchan@chromium.orgb5e0b762010-10-21 20:45:2545 DISALLOW_COPY_AND_ASSIGN(Thread);
46};
47
derekjchowea1725c2015-05-08 03:16:0148NetworkChangeNotifierLinux::Thread::Thread(
davidben1e912ea2016-04-20 19:17:0749 const std::unordered_set<std::string>& ignored_interfaces)
willchan@chromium.orgb5e0b762010-10-21 20:45:2550 : base::Thread("NetworkChangeNotifier"),
allan.jensen143cc3e12016-03-21 13:00:3151 address_tracker_(new internal::AddressTrackerLinux(
derekjchow5482d5e2015-01-31 01:04:5152 base::Bind(&NetworkChangeNotifierLinux::Thread::OnIPAddressChanged,
53 base::Unretained(this)),
54 base::Bind(&NetworkChangeNotifierLinux::Thread::OnLinkChanged,
55 base::Unretained(this)),
Peter Kasting341e1fb2018-02-24 00:03:0156 base::DoNothing(),
allan.jensen143cc3e12016-03-21 13:00:3157 ignored_interfaces)),
58 last_type_(NetworkChangeNotifier::CONNECTION_NONE) {}
willchan@chromium.orgb5e0b762010-10-21 20:45:2559
joi@chromium.orgd583c3a2011-11-02 15:31:5660NetworkChangeNotifierLinux::Thread::~Thread() {
61 DCHECK(!Thread::IsRunning());
62}
willchan@chromium.orgb5e0b762010-10-21 20:45:2563
64void NetworkChangeNotifierLinux::Thread::Init() {
allan.jensen143cc3e12016-03-21 13:00:3165 address_tracker_->Init();
szym@chromium.orgbb0e34542012-08-31 19:52:4066 dns_config_service_ = DnsConfigService::CreateSystemService();
67 dns_config_service_->WatchConfig(
68 base::Bind(&NetworkChangeNotifier::SetDnsConfig));
willchan@chromium.org6d603aa2010-02-19 18:44:5869}
70
willchan@chromium.orgb5e0b762010-10-21 20:45:2571void NetworkChangeNotifierLinux::Thread::CleanUp() {
allan.jensen143cc3e12016-03-21 13:00:3172 // Delete AddressTrackerLinux before MessageLoop gets deleted as
73 // AddressTrackerLinux's FileDescriptorWatcher holds a pointer to the
74 // MessageLoop.
75 address_tracker_.reset();
szym@chromium.orgbb0e34542012-08-31 19:52:4076 dns_config_service_.reset();
eroman@chromium.org72de04b72010-04-12 23:54:3477}
78
derekjchow5482d5e2015-01-31 01:04:5179void 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
86void NetworkChangeNotifierLinux::Thread::OnLinkChanged() {
87 if (last_type_ != GetCurrentConnectionType()) {
88 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
89 last_type_ = GetCurrentConnectionType();
jkarlinbce491862015-09-18 15:14:0790 double max_bandwidth_mbps =
zhuoyu.qian4d651402017-10-30 13:03:3391 NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype(
jkarlinbce491862015-09-18 15:14:0792 last_type_ == CONNECTION_NONE ? SUBTYPE_NONE : SUBTYPE_UNKNOWN);
93 NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(
94 max_bandwidth_mbps, last_type_);
derekjchow5482d5e2015-01-31 01:04:5195 }
96}
97
derekjchowea1725c2015-05-08 03:16:0198NetworkChangeNotifierLinux::NetworkChangeNotifierLinux(
davidben1e912ea2016-04-20 19:17:0799 const std::unordered_set<std::string>& ignored_interfaces)
pauljensen@chromium.org03848872012-12-08 02:46:41100 : NetworkChangeNotifier(NetworkChangeCalculatorParamsLinux()),
derekjchowea1725c2015-05-08 03:16:01101 notifier_thread_(new Thread(ignored_interfaces)) {
willchan@chromium.orgb5e0b762010-10-21 20:45:25102 // We create this notifier thread because the notification implementation
103 // needs a MessageLoopForIO, and there's no guarantee that
Gabriel Charette1e5bed442018-04-24 23:25:49104 // MessageLoopCurrent::Get() meets that criterion.
xhwang@chromium.org2da659e2013-05-23 20:51:34105 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0);
willchan@chromium.orgb5e0b762010-10-21 20:45:25106 notifier_thread_->StartWithOptions(thread_options);
107}
108
109NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() {
joi@chromium.orgd583c3a2011-11-02 15:31:56110 // Stopping from here allows us to sanity- check that the notifier
111 // thread shut down properly.
willchan@chromium.orgb5e0b762010-10-21 20:45:25112 notifier_thread_->Stop();
113}
114
pauljensen@chromium.org03848872012-12-08 02:46:41115// static
116NetworkChangeNotifier::NetworkChangeCalculatorParams
117NetworkChangeNotifierLinux::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.com8bbc7a792012-05-24 11:30:05129NetworkChangeNotifier::ConnectionType
130NetworkChangeNotifierLinux::GetCurrentConnectionType() const {
131 return notifier_thread_->GetCurrentConnectionType();
willchan@chromium.orgb5e0b762010-10-21 20:45:25132}
133
szym@chromium.org67172802012-07-16 22:27:24134const internal::AddressTrackerLinux*
135NetworkChangeNotifierLinux::GetAddressTrackerInternal() const {
136 return notifier_thread_->address_tracker();
137}
138
willchan@chromium.org100d5fb92009-12-21 21:08:35139} // namespace net