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_mac.h" | 5 #include "net/base/network_change_notifier_mac.h" |
6 | 6 |
7 #include <netinet/in.h> | 7 #include <netinet/in.h> |
| 8 #include <resolv.h> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/threading/thread.h" |
| 12 #include "net/dns/dns_config_watcher.h" |
| 13 |
| 14 #ifndef _PATH_RESCONF // Normally defined in <resolv.h> |
| 15 #define _PATH_RESCONF "/etc/resolv.conf" |
| 16 #endif |
8 | 17 |
9 namespace net { | 18 namespace net { |
10 | 19 |
11 static bool CalculateReachability(SCNetworkConnectionFlags flags) { | 20 static bool CalculateReachability(SCNetworkConnectionFlags flags) { |
12 bool reachable = flags & kSCNetworkFlagsReachable; | 21 bool reachable = flags & kSCNetworkFlagsReachable; |
13 bool connection_required = flags & kSCNetworkFlagsConnectionRequired; | 22 bool connection_required = flags & kSCNetworkFlagsConnectionRequired; |
14 return reachable && !connection_required; | 23 return reachable && !connection_required; |
15 } | 24 } |
16 | 25 |
| 26 class NetworkChangeNotifierMac::DnsWatcherThread : public base::Thread { |
| 27 public: |
| 28 DnsWatcherThread() : base::Thread("NetworkChangeNotifier") {} |
| 29 |
| 30 virtual ~DnsWatcherThread() { |
| 31 Stop(); |
| 32 } |
| 33 |
| 34 virtual void Init() OVERRIDE { |
| 35 watcher_.Init(); |
| 36 } |
| 37 |
| 38 virtual void CleanUp() OVERRIDE { |
| 39 watcher_.CleanUp(); |
| 40 } |
| 41 |
| 42 private: |
| 43 internal::DnsConfigWatcher watcher_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(DnsWatcherThread); |
| 46 }; |
| 47 |
17 NetworkChangeNotifierMac::NetworkChangeNotifierMac() | 48 NetworkChangeNotifierMac::NetworkChangeNotifierMac() |
18 : online_state_(UNINITIALIZED), | 49 : online_state_(UNINITIALIZED), |
19 initial_state_cv_(&online_state_lock_), | 50 initial_state_cv_(&online_state_lock_), |
20 forwarder_(this) { | 51 forwarder_(this), |
| 52 dns_watcher_thread_(new DnsWatcherThread()) { |
21 // Must be initialized after the rest of this object, as it may call back into | 53 // Must be initialized after the rest of this object, as it may call back into |
22 // SetInitialState(). | 54 // SetInitialState(). |
23 config_watcher_.reset(new NetworkConfigWatcherMac(&forwarder_)); | 55 config_watcher_.reset(new NetworkConfigWatcherMac(&forwarder_)); |
| 56 dns_watcher_thread_->StartWithOptions( |
| 57 base::Thread::Options(MessageLoop::TYPE_IO, 0)); |
24 } | 58 } |
25 | 59 |
26 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() { | 60 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() { |
27 // Delete the ConfigWatcher to join the notifier thread, ensuring that | 61 // Delete the ConfigWatcher to join the notifier thread, ensuring that |
28 // StartReachabilityNotifications() has an opportunity to run to completion. | 62 // StartReachabilityNotifications() has an opportunity to run to completion. |
29 config_watcher_.reset(); | 63 config_watcher_.reset(); |
30 | 64 |
31 // Now that StartReachabilityNotifications() has either run to completion or | 65 // Now that StartReachabilityNotifications() has either run to completion or |
32 // never run at all, unschedule reachability_ if it was previously scheduled. | 66 // never run at all, unschedule reachability_ if it was previously scheduled. |
33 if (reachability_.get() && run_loop_.get()) { | 67 if (reachability_.get() && run_loop_.get()) { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 { | 192 { |
159 base::AutoLock lock(notifier_mac->online_state_lock_); | 193 base::AutoLock lock(notifier_mac->online_state_lock_); |
160 old_state = notifier_mac->online_state_; | 194 old_state = notifier_mac->online_state_; |
161 notifier_mac->online_state_ = new_state; | 195 notifier_mac->online_state_ = new_state; |
162 } | 196 } |
163 if (old_state != new_state) | 197 if (old_state != new_state) |
164 NotifyObserversOfOnlineStateChange(); | 198 NotifyObserversOfOnlineStateChange(); |
165 } | 199 } |
166 | 200 |
167 } // namespace net | 201 } // namespace net |
OLD | NEW |