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
{
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 : base::Thread("NetworkChangeNotifier"),
49 base::Bind(&NetworkChangeNotifierLinux::Thread::OnIPAddressChanged
,
50 base::Unretained(this)),
51 base::Bind(&NetworkChangeNotifierLinux::Thread::OnLinkChanged
,
52 base::Unretained(this)),
53 base::Bind(base::DoNothing
)),
54 last_type_(NetworkChangeNotifier::CONNECTION_NONE
) {
57 NetworkChangeNotifierLinux::Thread::~Thread() {
58 DCHECK(!Thread::IsRunning());
61 void NetworkChangeNotifierLinux::Thread::Init() {
62 address_tracker_
.Init();
63 dns_config_service_
= DnsConfigService::CreateSystemService();
64 dns_config_service_
->WatchConfig(
65 base::Bind(&NetworkChangeNotifier::SetDnsConfig
));
68 void NetworkChangeNotifierLinux::Thread::CleanUp() {
69 dns_config_service_
.reset();
72 void NetworkChangeNotifierLinux::Thread::OnIPAddressChanged() {
73 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
74 // When the IP address of a network interface is added/deleted, the
75 // connection type may have changed.
79 void NetworkChangeNotifierLinux::Thread::OnLinkChanged() {
80 if (last_type_
!= GetCurrentConnectionType()) {
81 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
82 last_type_
= GetCurrentConnectionType();
86 NetworkChangeNotifierLinux
* NetworkChangeNotifierLinux::Create() {
87 return new NetworkChangeNotifierLinux();
90 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux()
91 : NetworkChangeNotifier(NetworkChangeCalculatorParamsLinux()),
92 notifier_thread_(new Thread()) {
93 // We create this notifier thread because the notification implementation
94 // needs a MessageLoopForIO, and there's no guarantee that
95 // MessageLoop::current() meets that criterion.
96 base::Thread::Options
thread_options(base::MessageLoop::TYPE_IO
, 0);
97 notifier_thread_
->StartWithOptions(thread_options
);
100 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() {
101 // Stopping from here allows us to sanity- check that the notifier
102 // thread shut down properly.
103 notifier_thread_
->Stop();
107 NetworkChangeNotifier::NetworkChangeCalculatorParams
108 NetworkChangeNotifierLinux::NetworkChangeCalculatorParamsLinux() {
109 NetworkChangeCalculatorParams params
;
110 // Delay values arrived at by simple experimentation and adjusted so as to
111 // produce a single signal when switching between network connections.
112 params
.ip_address_offline_delay_
= base::TimeDelta::FromMilliseconds(2000);
113 params
.ip_address_online_delay_
= base::TimeDelta::FromMilliseconds(2000);
114 params
.connection_type_offline_delay_
=
115 base::TimeDelta::FromMilliseconds(1500);
116 params
.connection_type_online_delay_
= base::TimeDelta::FromMilliseconds(500);
120 NetworkChangeNotifier::ConnectionType
121 NetworkChangeNotifierLinux::GetCurrentConnectionType() const {
122 return notifier_thread_
->GetCurrentConnectionType();
125 const internal::AddressTrackerLinux
*
126 NetworkChangeNotifierLinux::GetAddressTrackerInternal() const {
127 return notifier_thread_
->address_tracker();