| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/network_change_notifier_linux.h" | 5 #include "net/base/network_change_notifier_linux.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
| 10 #include "net/base/address_tracker_linux.h" | 10 #include "net/base/address_tracker_linux.h" |
| 11 #include "net/dns/dns_config_service.h" | 11 #include "net/dns/dns_config_service.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 namespace { |
| 16 NetworkChangeNotifierLinux* g_instance; |
| 17 } // namespace |
| 18 |
| 15 class NetworkChangeNotifierLinux::Thread : public base::Thread { | 19 class NetworkChangeNotifierLinux::Thread : public base::Thread { |
| 16 public: | 20 public: |
| 17 Thread(); | 21 Thread(); |
| 18 ~Thread() override; | 22 ~Thread() override; |
| 19 | 23 |
| 20 // Plumbing for NetworkChangeNotifier::GetCurrentConnectionType. | 24 // Plumbing for NetworkChangeNotifier::GetCurrentConnectionType. |
| 21 // Safe to call from any thread. | 25 // Safe to call from any thread. |
| 22 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() { | 26 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() { |
| 23 return address_tracker_.GetCurrentConnectionType(); | 27 return address_tracker_.GetCurrentConnectionType(); |
| 24 } | 28 } |
| 25 | 29 |
| 26 const internal::AddressTrackerLinux* address_tracker() const { | 30 const internal::AddressTrackerLinux* address_tracker() const { |
| 27 return &address_tracker_; | 31 return &address_tracker_; |
| 28 } | 32 } |
| 29 | 33 |
| 34 bool AddNetifToIgnore(const std::string& netif_name) { |
| 35 return address_tracker_.AddNetifToIgnore(netif_name); |
| 36 } |
| 37 bool RemoveNetifToIgnore(const std::string& netif_name) { |
| 38 return address_tracker_.RemoveNetifToIgnore(netif_name); |
| 39 } |
| 40 |
| 41 std::string GetPrimaryNetif() const { |
| 42 return address_tracker_.GetPrimaryNetif(); |
| 43 }; |
| 44 |
| 30 protected: | 45 protected: |
| 31 // base::Thread | 46 // base::Thread |
| 32 void Init() override; | 47 void Init() override; |
| 33 void CleanUp() override; | 48 void CleanUp() override; |
| 34 | 49 |
| 35 private: | 50 private: |
| 36 scoped_ptr<DnsConfigService> dns_config_service_; | 51 scoped_ptr<DnsConfigService> dns_config_service_; |
| 37 // Used to detect online/offline state and IP address changes. | 52 // Used to detect online/offline state and IP address changes. |
| 38 internal::AddressTrackerLinux address_tracker_; | 53 internal::AddressTrackerLinux address_tracker_; |
| 39 | 54 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 59 dns_config_service_ = DnsConfigService::CreateSystemService(); | 74 dns_config_service_ = DnsConfigService::CreateSystemService(); |
| 60 dns_config_service_->WatchConfig( | 75 dns_config_service_->WatchConfig( |
| 61 base::Bind(&NetworkChangeNotifier::SetDnsConfig)); | 76 base::Bind(&NetworkChangeNotifier::SetDnsConfig)); |
| 62 } | 77 } |
| 63 | 78 |
| 64 void NetworkChangeNotifierLinux::Thread::CleanUp() { | 79 void NetworkChangeNotifierLinux::Thread::CleanUp() { |
| 65 dns_config_service_.reset(); | 80 dns_config_service_.reset(); |
| 66 } | 81 } |
| 67 | 82 |
| 68 NetworkChangeNotifierLinux* NetworkChangeNotifierLinux::Create() { | 83 NetworkChangeNotifierLinux* NetworkChangeNotifierLinux::Create() { |
| 69 return new NetworkChangeNotifierLinux(); | 84 g_instance = new NetworkChangeNotifierLinux(); |
| 85 return g_instance; |
| 86 } |
| 87 |
| 88 NetworkChangeNotifierLinux* NetworkChangeNotifierLinux::GetInstance() { |
| 89 return g_instance; |
| 70 } | 90 } |
| 71 | 91 |
| 72 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux() | 92 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux() |
| 73 : NetworkChangeNotifier(NetworkChangeCalculatorParamsLinux()), | 93 : NetworkChangeNotifier(NetworkChangeCalculatorParamsLinux()), |
| 74 notifier_thread_(new Thread()) { | 94 notifier_thread_(new Thread()) { |
| 75 // We create this notifier thread because the notification implementation | 95 // We create this notifier thread because the notification implementation |
| 76 // needs a MessageLoopForIO, and there's no guarantee that | 96 // needs a MessageLoopForIO, and there's no guarantee that |
| 77 // MessageLoop::current() meets that criterion. | 97 // MessageLoop::current() meets that criterion. |
| 78 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); | 98 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); |
| 79 notifier_thread_->StartWithOptions(thread_options); | 99 notifier_thread_->StartWithOptions(thread_options); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 92 // Delay values arrived at by simple experimentation and adjusted so as to | 112 // Delay values arrived at by simple experimentation and adjusted so as to |
| 93 // produce a single signal when switching between network connections. | 113 // produce a single signal when switching between network connections. |
| 94 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(2000); | 114 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(2000); |
| 95 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(2000); | 115 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(2000); |
| 96 params.connection_type_offline_delay_ = | 116 params.connection_type_offline_delay_ = |
| 97 base::TimeDelta::FromMilliseconds(1500); | 117 base::TimeDelta::FromMilliseconds(1500); |
| 98 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500); | 118 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500); |
| 99 return params; | 119 return params; |
| 100 } | 120 } |
| 101 | 121 |
| 122 scoped_refptr<base::MessageLoopProxy> |
| 123 NetworkChangeNotifierLinux::GetMessageLoopProxy() const { |
| 124 return notifier_thread_->message_loop_proxy(); |
| 125 } |
| 126 |
| 102 NetworkChangeNotifier::ConnectionType | 127 NetworkChangeNotifier::ConnectionType |
| 103 NetworkChangeNotifierLinux::GetCurrentConnectionType() const { | 128 NetworkChangeNotifierLinux::GetCurrentConnectionType() const { |
| 104 return notifier_thread_->GetCurrentConnectionType(); | 129 return notifier_thread_->GetCurrentConnectionType(); |
| 105 } | 130 } |
| 106 | 131 |
| 132 bool NetworkChangeNotifierLinux::AddNetifToIgnore( |
| 133 const std::string& netif_name) { |
| 134 return notifier_thread_->AddNetifToIgnore(netif_name); |
| 135 } |
| 136 |
| 137 bool NetworkChangeNotifierLinux::RemoveNetifToIgnore( |
| 138 const std::string& netif_name) { |
| 139 return notifier_thread_->RemoveNetifToIgnore(netif_name); |
| 140 } |
| 141 |
| 142 std::string NetworkChangeNotifierLinux::GetPrimaryNetif() const { |
| 143 return notifier_thread_->GetPrimaryNetif(); |
| 144 } |
| 145 |
| 107 const internal::AddressTrackerLinux* | 146 const internal::AddressTrackerLinux* |
| 108 NetworkChangeNotifierLinux::GetAddressTrackerInternal() const { | 147 NetworkChangeNotifierLinux::GetAddressTrackerInternal() const { |
| 109 return notifier_thread_->address_tracker(); | 148 return notifier_thread_->address_tracker(); |
| 110 } | 149 } |
| 111 | 150 |
| 112 } // namespace net | 151 } // namespace net |
| OLD | NEW |