[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / net / base / network_change_notifier_linux.cc
blob7e471726d9aaaa58b0ab9fc0582e320487f783a5
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"
7 #include "base/bind.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"
13 namespace net {
15 class NetworkChangeNotifierLinux::Thread : public base::Thread {
16 public:
17 explicit Thread(const base::hash_set<std::string>& ignored_interfaces);
18 ~Thread() override;
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_;
30 protected:
31 // base::Thread
32 void Init() override;
33 void CleanUp() override;
35 private:
36 void OnIPAddressChanged();
37 void OnLinkChanged();
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"),
49 address_tracker_(
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),
55 ignored_interfaces),
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.
78 OnLinkChanged();
81 void NetworkChangeNotifierLinux::Thread::OnLinkChanged() {
82 if (last_type_ != GetCurrentConnectionType()) {
83 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
84 last_type_ = GetCurrentConnectionType();
88 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux(
89 const base::hash_set<std::string>& ignored_interfaces)
90 : NetworkChangeNotifier(NetworkChangeCalculatorParamsLinux()),
91 notifier_thread_(new Thread(ignored_interfaces)) {
92 // We create this notifier thread because the notification implementation
93 // needs a MessageLoopForIO, and there's no guarantee that
94 // MessageLoop::current() meets that criterion.
95 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0);
96 notifier_thread_->StartWithOptions(thread_options);
99 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() {
100 // Stopping from here allows us to sanity- check that the notifier
101 // thread shut down properly.
102 notifier_thread_->Stop();
105 // static
106 NetworkChangeNotifier::NetworkChangeCalculatorParams
107 NetworkChangeNotifierLinux::NetworkChangeCalculatorParamsLinux() {
108 NetworkChangeCalculatorParams params;
109 // Delay values arrived at by simple experimentation and adjusted so as to
110 // produce a single signal when switching between network connections.
111 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(2000);
112 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(2000);
113 params.connection_type_offline_delay_ =
114 base::TimeDelta::FromMilliseconds(1500);
115 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500);
116 return params;
119 NetworkChangeNotifier::ConnectionType
120 NetworkChangeNotifierLinux::GetCurrentConnectionType() const {
121 return notifier_thread_->GetCurrentConnectionType();
124 const internal::AddressTrackerLinux*
125 NetworkChangeNotifierLinux::GetAddressTrackerInternal() const {
126 return notifier_thread_->address_tracker();
129 } // namespace net