OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <SystemConfiguration/SCDynamicStoreKey.h> | 8 #include <SystemConfiguration/SCDynamicStoreKey.h> |
9 #include <SystemConfiguration/SCNetworkReachability.h> | 9 #include <SystemConfiguration/SCNetworkReachability.h> |
10 #include <SystemConfiguration/SCSchemaDefinitions.h> | 10 #include <SystemConfiguration/SCSchemaDefinitions.h> |
11 | 11 |
12 #include "base/mac/scoped_cftyperef.h" | 12 #include "base/mac/scoped_cftyperef.h" |
| 13 #include "base/synchronization/lock.h" |
13 | 14 |
14 namespace net { | 15 namespace net { |
15 | 16 |
16 NetworkChangeNotifierMac::NetworkChangeNotifierMac() | 17 NetworkChangeNotifierMac::NetworkChangeNotifierMac() |
17 : forwarder_(this), | 18 : forwarder_(this), |
18 config_watcher_(&forwarder_), | 19 config_watcher_(&forwarder_), |
19 network_reachable_(true) {} | 20 network_reachable_(true) {} |
20 | 21 |
21 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() { | 22 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() { |
22 if (reachability_.get() && run_loop_.get()) { | 23 if (reachability_.get() && run_loop_.get()) { |
23 SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(), | 24 SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(), |
24 run_loop_.get(), | 25 run_loop_.get(), |
25 kCFRunLoopCommonModes); | 26 kCFRunLoopCommonModes); |
26 } | 27 } |
27 } | 28 } |
28 | 29 |
29 bool NetworkChangeNotifierMac::IsCurrentlyOffline() const { | 30 bool NetworkChangeNotifierMac::IsCurrentlyOffline() const { |
| 31 base::AutoLock lock(network_reachable_lock_); |
30 return !network_reachable_; | 32 return !network_reachable_; |
31 } | 33 } |
32 | 34 |
33 void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys( | 35 void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys( |
34 SCDynamicStoreRef store) { | 36 SCDynamicStoreRef store) { |
35 // Called on notifier thread. | 37 // Called on notifier thread. |
36 run_loop_.reset(CFRunLoopGetCurrent()); | 38 run_loop_.reset(CFRunLoopGetCurrent()); |
37 CFRetain(run_loop_.get()); | 39 CFRetain(run_loop_.get()); |
38 | 40 |
39 base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys( | 41 base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys( |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 SCNetworkConnectionFlags flags, | 115 SCNetworkConnectionFlags flags, |
114 void* notifier) { | 116 void* notifier) { |
115 NetworkChangeNotifierMac* notifier_mac = | 117 NetworkChangeNotifierMac* notifier_mac = |
116 static_cast<NetworkChangeNotifierMac*>(notifier); | 118 static_cast<NetworkChangeNotifierMac*>(notifier); |
117 | 119 |
118 DCHECK_EQ(notifier_mac->run_loop_.get(), CFRunLoopGetCurrent()); | 120 DCHECK_EQ(notifier_mac->run_loop_.get(), CFRunLoopGetCurrent()); |
119 | 121 |
120 bool reachable = flags & kSCNetworkFlagsReachable; | 122 bool reachable = flags & kSCNetworkFlagsReachable; |
121 bool connection_required = flags & kSCNetworkFlagsConnectionRequired; | 123 bool connection_required = flags & kSCNetworkFlagsConnectionRequired; |
122 bool old_reachability = notifier_mac->network_reachable_; | 124 bool old_reachability = notifier_mac->network_reachable_; |
123 notifier_mac->network_reachable_ = reachable && !connection_required; | 125 { |
| 126 base::AutoLock lock(notifier_mac->network_reachable_lock_); |
| 127 notifier_mac->network_reachable_ = reachable && !connection_required; |
| 128 } |
124 if (old_reachability != notifier_mac->network_reachable_) | 129 if (old_reachability != notifier_mac->network_reachable_) |
125 NotifyObserversOfOnlineStateChange(); | 130 NotifyObserversOfOnlineStateChange(); |
126 } | 131 } |
127 | 132 |
128 } // namespace net | 133 } // namespace net |
OLD | NEW |