| 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/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| 11 #include "net/base/address_tracker_linux.h" | 11 #include "net/base/address_tracker_linux.h" |
| 12 #include "net/dns/dns_config_service.h" | 12 #include "net/dns/dns_config_service.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 class NetworkChangeNotifierLinux::Thread : public base::Thread { | 16 class NetworkChangeNotifierLinux::Thread : public base::Thread { |
| 17 public: | 17 public: |
| 18 explicit Thread(const base::hash_set<std::string>& ignored_interfaces); | 18 explicit Thread(const base::hash_set<std::string>& ignored_interfaces); |
| 19 ~Thread() override; | 19 ~Thread() override; |
| 20 | 20 |
| 21 // Plumbing for NetworkChangeNotifier::GetCurrentConnectionType. | 21 // Plumbing for NetworkChangeNotifier::GetCurrentConnectionType. |
| 22 // Safe to call from any thread. | 22 // Safe to call from any thread. |
| 23 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() { | 23 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() { |
| 24 return address_tracker_.GetCurrentConnectionType(); | 24 return address_tracker_->GetCurrentConnectionType(); |
| 25 } | 25 } |
| 26 | 26 |
| 27 const internal::AddressTrackerLinux* address_tracker() const { | 27 const internal::AddressTrackerLinux* address_tracker() const { |
| 28 return &address_tracker_; | 28 return address_tracker_.get(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 protected: | 31 protected: |
| 32 // base::Thread | 32 // base::Thread |
| 33 void Init() override; | 33 void Init() override; |
| 34 void CleanUp() override; | 34 void CleanUp() override; |
| 35 | 35 |
| 36 private: | 36 private: |
| 37 void OnIPAddressChanged(); | 37 void OnIPAddressChanged(); |
| 38 void OnLinkChanged(); | 38 void OnLinkChanged(); |
| 39 scoped_ptr<DnsConfigService> dns_config_service_; | 39 scoped_ptr<DnsConfigService> dns_config_service_; |
| 40 // Used to detect online/offline state and IP address changes. | 40 // Used to detect online/offline state and IP address changes. |
| 41 internal::AddressTrackerLinux address_tracker_; | 41 scoped_ptr<internal::AddressTrackerLinux> address_tracker_; |
| 42 NetworkChangeNotifier::ConnectionType last_type_; | 42 NetworkChangeNotifier::ConnectionType last_type_; |
| 43 | 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(Thread); | 44 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 NetworkChangeNotifierLinux::Thread::Thread( | 47 NetworkChangeNotifierLinux::Thread::Thread( |
| 48 const base::hash_set<std::string>& ignored_interfaces) | 48 const base::hash_set<std::string>& ignored_interfaces) |
| 49 : base::Thread("NetworkChangeNotifier"), | 49 : base::Thread("NetworkChangeNotifier"), |
| 50 address_tracker_( | 50 address_tracker_(new internal::AddressTrackerLinux( |
| 51 base::Bind(&NetworkChangeNotifierLinux::Thread::OnIPAddressChanged, | 51 base::Bind(&NetworkChangeNotifierLinux::Thread::OnIPAddressChanged, |
| 52 base::Unretained(this)), | 52 base::Unretained(this)), |
| 53 base::Bind(&NetworkChangeNotifierLinux::Thread::OnLinkChanged, | 53 base::Bind(&NetworkChangeNotifierLinux::Thread::OnLinkChanged, |
| 54 base::Unretained(this)), | 54 base::Unretained(this)), |
| 55 base::Bind(base::DoNothing), | 55 base::Bind(base::DoNothing), |
| 56 ignored_interfaces), | 56 ignored_interfaces)), |
| 57 last_type_(NetworkChangeNotifier::CONNECTION_NONE) { | 57 last_type_(NetworkChangeNotifier::CONNECTION_NONE) {} |
| 58 } | |
| 59 | 58 |
| 60 NetworkChangeNotifierLinux::Thread::~Thread() { | 59 NetworkChangeNotifierLinux::Thread::~Thread() { |
| 61 DCHECK(!Thread::IsRunning()); | 60 DCHECK(!Thread::IsRunning()); |
| 62 } | 61 } |
| 63 | 62 |
| 64 void NetworkChangeNotifierLinux::Thread::Init() { | 63 void NetworkChangeNotifierLinux::Thread::Init() { |
| 65 address_tracker_.Init(); | 64 address_tracker_->Init(); |
| 66 dns_config_service_ = DnsConfigService::CreateSystemService(); | 65 dns_config_service_ = DnsConfigService::CreateSystemService(); |
| 67 dns_config_service_->WatchConfig( | 66 dns_config_service_->WatchConfig( |
| 68 base::Bind(&NetworkChangeNotifier::SetDnsConfig)); | 67 base::Bind(&NetworkChangeNotifier::SetDnsConfig)); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void NetworkChangeNotifierLinux::Thread::CleanUp() { | 70 void NetworkChangeNotifierLinux::Thread::CleanUp() { |
| 71 // Delete AddressTrackerLinux before MessageLoop gets deleted as |
| 72 // AddressTrackerLinux's FileDescriptorWatcher holds a pointer to the |
| 73 // MessageLoop. |
| 74 address_tracker_.reset(); |
| 72 dns_config_service_.reset(); | 75 dns_config_service_.reset(); |
| 73 } | 76 } |
| 74 | 77 |
| 75 void NetworkChangeNotifierLinux::Thread::OnIPAddressChanged() { | 78 void NetworkChangeNotifierLinux::Thread::OnIPAddressChanged() { |
| 76 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); | 79 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); |
| 77 // When the IP address of a network interface is added/deleted, the | 80 // When the IP address of a network interface is added/deleted, the |
| 78 // connection type may have changed. | 81 // connection type may have changed. |
| 79 OnLinkChanged(); | 82 OnLinkChanged(); |
| 80 } | 83 } |
| 81 | 84 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 NetworkChangeNotifierLinux::GetCurrentConnectionType() const { | 129 NetworkChangeNotifierLinux::GetCurrentConnectionType() const { |
| 127 return notifier_thread_->GetCurrentConnectionType(); | 130 return notifier_thread_->GetCurrentConnectionType(); |
| 128 } | 131 } |
| 129 | 132 |
| 130 const internal::AddressTrackerLinux* | 133 const internal::AddressTrackerLinux* |
| 131 NetworkChangeNotifierLinux::GetAddressTrackerInternal() const { | 134 NetworkChangeNotifierLinux::GetAddressTrackerInternal() const { |
| 132 return notifier_thread_->address_tracker(); | 135 return notifier_thread_->address_tracker(); |
| 133 } | 136 } |
| 134 | 137 |
| 135 } // namespace net | 138 } // namespace net |
| OLD | NEW |