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.
5 #include "net/base/network_change_notifier_linux.h"
8 #include "base/compiler_specific.h"
9 #include "base/threading/thread.h"
10 #include "net/base/address_tracker_linux.h"
11 #include "net/dns/dns_config_service.h"
15 class NetworkChangeNotifierLinux::Thread
: public base::Thread
{
17 explicit Thread(const base::hash_set
<std::string
>& ignored_interfaces
);
20 // Plumbing for NetworkChangeNotifier::GetCurrentConnectionType.
21 // Safe to call from any thread.
22 NetworkChangeNotifier::ConnectionType
GetCurrentConnectionType() {
23 return address_tracker_
.GetCurrentConnectionType();
26 const internal::AddressTrackerLinux
* address_tracker() const {
27 return &address_tracker_
;
33 void CleanUp() override
;
36 void OnIPAddressChanged();
38 scoped_ptr
<DnsConfigService
> dns_config_service_
;
39 // Used to detect online/offline state and IP address changes.
40 internal::AddressTrackerLinux address_tracker_
;
41 NetworkChangeNotifier::ConnectionType last_type_
;
43 DISALLOW_COPY_AND_ASSIGN(Thread
);
46 NetworkChangeNotifierLinux::Thread::Thread(
47 const base::hash_set
<std::string
>& ignored_interfaces
)
48 : base::Thread("NetworkChangeNotifier"),
50 base::Bind(&NetworkChangeNotifierLinux::Thread::OnIPAddressChanged
,
51 base::Unretained(this)),
52 base::Bind(&NetworkChangeNotifierLinux::Thread::OnLinkChanged
,
53 base::Unretained(this)),
54 base::Bind(base::DoNothing
),
56 last_type_(NetworkChangeNotifier::CONNECTION_NONE
) {
59 NetworkChangeNotifierLinux::Thread::~Thread() {
60 DCHECK(!Thread::IsRunning());
63 void NetworkChangeNotifierLinux::Thread::Init() {
64 address_tracker_
.Init();
65 dns_config_service_
= DnsConfigService::CreateSystemService();
66 dns_config_service_
->WatchConfig(
67 base::Bind(&NetworkChangeNotifier::SetDnsConfig
));
70 void NetworkChangeNotifierLinux::Thread::CleanUp() {
71 dns_config_service_
.reset();
74 void NetworkChangeNotifierLinux::Thread::OnIPAddressChanged() {
75 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
76 // When the IP address of a network interface is added/deleted, the
77 // connection type may have changed.
81 void NetworkChangeNotifierLinux::Thread::OnLinkChanged() {
82 if (last_type_
!= GetCurrentConnectionType()) {
83 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
84 last_type_
= GetCurrentConnectionType();
85 double max_bandwidth_mbps
=
86 NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
87 last_type_
== CONNECTION_NONE
? SUBTYPE_NONE
: SUBTYPE_UNKNOWN
);
88 NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(
89 max_bandwidth_mbps
, last_type_
);
93 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux(
94 const base::hash_set
<std::string
>& ignored_interfaces
)
95 : NetworkChangeNotifier(NetworkChangeCalculatorParamsLinux()),
96 notifier_thread_(new Thread(ignored_interfaces
)) {
97 // We create this notifier thread because the notification implementation
98 // needs a MessageLoopForIO, and there's no guarantee that
99 // MessageLoop::current() meets that criterion.
100 base::Thread::Options
thread_options(base::MessageLoop::TYPE_IO
, 0);
101 notifier_thread_
->StartWithOptions(thread_options
);
104 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() {
105 // Stopping from here allows us to sanity- check that the notifier
106 // thread shut down properly.
107 notifier_thread_
->Stop();
111 NetworkChangeNotifier::NetworkChangeCalculatorParams
112 NetworkChangeNotifierLinux::NetworkChangeCalculatorParamsLinux() {
113 NetworkChangeCalculatorParams params
;
114 // Delay values arrived at by simple experimentation and adjusted so as to
115 // produce a single signal when switching between network connections.
116 params
.ip_address_offline_delay_
= base::TimeDelta::FromMilliseconds(2000);
117 params
.ip_address_online_delay_
= base::TimeDelta::FromMilliseconds(2000);
118 params
.connection_type_offline_delay_
=
119 base::TimeDelta::FromMilliseconds(1500);
120 params
.connection_type_online_delay_
= base::TimeDelta::FromMilliseconds(500);
124 NetworkChangeNotifier::ConnectionType
125 NetworkChangeNotifierLinux::GetCurrentConnectionType() const {
126 return notifier_thread_
->GetCurrentConnectionType();
129 const internal::AddressTrackerLinux
*
130 NetworkChangeNotifierLinux::GetAddressTrackerInternal() const {
131 return notifier_thread_
->address_tracker();