| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "net/base/network_change_notifier_linux.h" | |
| 6 | |
| 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" | |
| 12 | |
| 13 namespace net { | |
| 14 | |
| 15 class NetworkChangeNotifierLinux::Thread : public base::Thread { | |
| 16 public: | |
| 17 Thread(); | |
| 18 ~Thread() override; | |
| 19 | |
| 20 // Plumbing for NetworkChangeNotifier::GetCurrentConnectionType. | |
| 21 // Safe to call from any thread. | |
| 22 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() { | |
| 23 return address_tracker_.GetCurrentConnectionType(); | |
| 24 } | |
| 25 | |
| 26 const internal::AddressTrackerLinux* address_tracker() const { | |
| 27 return &address_tracker_; | |
| 28 } | |
| 29 | |
| 30 protected: | |
| 31 // base::Thread | |
| 32 void Init() override; | |
| 33 void CleanUp() override; | |
| 34 | |
| 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_; | |
| 42 | |
| 43 DISALLOW_COPY_AND_ASSIGN(Thread); | |
| 44 }; | |
| 45 | |
| 46 NetworkChangeNotifierLinux::Thread::Thread() | |
| 47 : base::Thread("NetworkChangeNotifier"), | |
| 48 address_tracker_( | |
| 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) { | |
| 55 } | |
| 56 | |
| 57 NetworkChangeNotifierLinux::Thread::~Thread() { | |
| 58 DCHECK(!Thread::IsRunning()); | |
| 59 } | |
| 60 | |
| 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)); | |
| 66 } | |
| 67 | |
| 68 void NetworkChangeNotifierLinux::Thread::CleanUp() { | |
| 69 dns_config_service_.reset(); | |
| 70 } | |
| 71 | |
| 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. | |
| 76 OnLinkChanged(); | |
| 77 } | |
| 78 | |
| 79 void NetworkChangeNotifierLinux::Thread::OnLinkChanged() { | |
| 80 if (last_type_ != GetCurrentConnectionType()) { | |
| 81 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); | |
| 82 last_type_ = GetCurrentConnectionType(); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 NetworkChangeNotifierLinux* NetworkChangeNotifierLinux::Create() { | |
| 87 return new NetworkChangeNotifierLinux(); | |
| 88 } | |
| 89 | |
| 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); | |
| 98 } | |
| 99 | |
| 100 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { | |
| 101 // Stopping from here allows us to sanity- check that the notifier | |
| 102 // thread shut down properly. | |
| 103 notifier_thread_->Stop(); | |
| 104 } | |
| 105 | |
| 106 // static | |
| 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); | |
| 117 return params; | |
| 118 } | |
| 119 | |
| 120 NetworkChangeNotifier::ConnectionType | |
| 121 NetworkChangeNotifierLinux::GetCurrentConnectionType() const { | |
| 122 return notifier_thread_->GetCurrentConnectionType(); | |
| 123 } | |
| 124 | |
| 125 const internal::AddressTrackerLinux* | |
| 126 NetworkChangeNotifierLinux::GetAddressTrackerInternal() const { | |
| 127 return notifier_thread_->address_tracker(); | |
| 128 } | |
| 129 | |
| 130 } // namespace net | |
| OLD | NEW |