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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 const internal::AddressTrackerLinux* address_tracker() const { | 26 const internal::AddressTrackerLinux* address_tracker() const { |
27 return &address_tracker_; | 27 return &address_tracker_; |
28 } | 28 } |
29 | 29 |
30 protected: | 30 protected: |
31 // base::Thread | 31 // base::Thread |
32 void Init() override; | 32 void Init() override; |
33 void CleanUp() override; | 33 void CleanUp() override; |
34 | 34 |
35 private: | 35 private: |
| 36 void OnIPAddressChanged(); |
| 37 void OnLinkChanged(); |
36 scoped_ptr<DnsConfigService> dns_config_service_; | 38 scoped_ptr<DnsConfigService> dns_config_service_; |
37 // Used to detect online/offline state and IP address changes. | 39 // Used to detect online/offline state and IP address changes. |
38 internal::AddressTrackerLinux address_tracker_; | 40 internal::AddressTrackerLinux address_tracker_; |
| 41 NetworkChangeNotifier::ConnectionType last_type_; |
39 | 42 |
40 DISALLOW_COPY_AND_ASSIGN(Thread); | 43 DISALLOW_COPY_AND_ASSIGN(Thread); |
41 }; | 44 }; |
42 | 45 |
43 NetworkChangeNotifierLinux::Thread::Thread() | 46 NetworkChangeNotifierLinux::Thread::Thread() |
44 : base::Thread("NetworkChangeNotifier"), | 47 : base::Thread("NetworkChangeNotifier"), |
45 address_tracker_( | 48 address_tracker_( |
46 base::Bind(&NetworkChangeNotifier:: | 49 base::Bind(&NetworkChangeNotifierLinux::Thread::OnIPAddressChanged, |
47 NotifyObserversOfIPAddressChange), | 50 base::Unretained(this)), |
48 base::Bind(&NetworkChangeNotifier:: | 51 base::Bind(&NetworkChangeNotifierLinux::Thread::OnLinkChanged, |
49 NotifyObserversOfConnectionTypeChange), | 52 base::Unretained(this)), |
50 base::Bind(base::DoNothing)) { | 53 base::Bind(base::DoNothing)), |
| 54 last_type_(NetworkChangeNotifier::CONNECTION_NONE) { |
51 } | 55 } |
52 | 56 |
53 NetworkChangeNotifierLinux::Thread::~Thread() { | 57 NetworkChangeNotifierLinux::Thread::~Thread() { |
54 DCHECK(!Thread::IsRunning()); | 58 DCHECK(!Thread::IsRunning()); |
55 } | 59 } |
56 | 60 |
57 void NetworkChangeNotifierLinux::Thread::Init() { | 61 void NetworkChangeNotifierLinux::Thread::Init() { |
58 address_tracker_.Init(); | 62 address_tracker_.Init(); |
59 dns_config_service_ = DnsConfigService::CreateSystemService(); | 63 dns_config_service_ = DnsConfigService::CreateSystemService(); |
60 dns_config_service_->WatchConfig( | 64 dns_config_service_->WatchConfig( |
61 base::Bind(&NetworkChangeNotifier::SetDnsConfig)); | 65 base::Bind(&NetworkChangeNotifier::SetDnsConfig)); |
62 } | 66 } |
63 | 67 |
64 void NetworkChangeNotifierLinux::Thread::CleanUp() { | 68 void NetworkChangeNotifierLinux::Thread::CleanUp() { |
65 dns_config_service_.reset(); | 69 dns_config_service_.reset(); |
66 } | 70 } |
67 | 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 |
68 NetworkChangeNotifierLinux* NetworkChangeNotifierLinux::Create() { | 86 NetworkChangeNotifierLinux* NetworkChangeNotifierLinux::Create() { |
69 return new NetworkChangeNotifierLinux(); | 87 return new NetworkChangeNotifierLinux(); |
70 } | 88 } |
71 | 89 |
72 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux() | 90 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux() |
73 : NetworkChangeNotifier(NetworkChangeCalculatorParamsLinux()), | 91 : NetworkChangeNotifier(NetworkChangeCalculatorParamsLinux()), |
74 notifier_thread_(new Thread()) { | 92 notifier_thread_(new Thread()) { |
75 // We create this notifier thread because the notification implementation | 93 // We create this notifier thread because the notification implementation |
76 // needs a MessageLoopForIO, and there's no guarantee that | 94 // needs a MessageLoopForIO, and there's no guarantee that |
77 // MessageLoop::current() meets that criterion. | 95 // MessageLoop::current() meets that criterion. |
(...skipping 25 matching lines...) Expand all Loading... |
103 NetworkChangeNotifierLinux::GetCurrentConnectionType() const { | 121 NetworkChangeNotifierLinux::GetCurrentConnectionType() const { |
104 return notifier_thread_->GetCurrentConnectionType(); | 122 return notifier_thread_->GetCurrentConnectionType(); |
105 } | 123 } |
106 | 124 |
107 const internal::AddressTrackerLinux* | 125 const internal::AddressTrackerLinux* |
108 NetworkChangeNotifierLinux::GetAddressTrackerInternal() const { | 126 NetworkChangeNotifierLinux::GetAddressTrackerInternal() const { |
109 return notifier_thread_->address_tracker(); | 127 return notifier_thread_->address_tracker(); |
110 } | 128 } |
111 | 129 |
112 } // namespace net | 130 } // namespace net |
OLD | NEW |