OLD | NEW |
1 // Copyright (c) 2010 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 <SystemConfiguration/SCDynamicStoreKey.h> | 8 #include <SystemConfiguration/SCDynamicStoreKey.h> |
| 9 #include <SystemConfiguration/SCNetworkReachability.h> |
8 #include <SystemConfiguration/SCSchemaDefinitions.h> | 10 #include <SystemConfiguration/SCSchemaDefinitions.h> |
9 | 11 |
10 #include "base/mac/scoped_cftyperef.h" | 12 #include "base/mac/scoped_cftyperef.h" |
11 | 13 |
12 namespace net { | 14 namespace net { |
13 | 15 |
14 NetworkChangeNotifierMac::NetworkChangeNotifierMac() | 16 NetworkChangeNotifierMac::NetworkChangeNotifierMac() |
15 : forwarder_(this), | 17 : forwarder_(this), |
16 config_watcher_(&forwarder_) {} | 18 config_watcher_(&forwarder_), |
17 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {} | 19 network_reachable_(true) {} |
| 20 |
| 21 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() { |
| 22 if (reachability_.get() && run_loop_.get()) { |
| 23 SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(), |
| 24 run_loop_.get(), |
| 25 kCFRunLoopCommonModes); |
| 26 } |
| 27 } |
18 | 28 |
19 bool NetworkChangeNotifierMac::IsCurrentlyOffline() const { | 29 bool NetworkChangeNotifierMac::IsCurrentlyOffline() const { |
20 // TODO(eroman): http://crbug.com/53473 | 30 return !network_reachable_; |
21 return false; | |
22 } | 31 } |
23 | 32 |
24 void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys( | 33 void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys( |
25 SCDynamicStoreRef store) { | 34 SCDynamicStoreRef store) { |
26 // Called on notifier thread. | 35 // Called on notifier thread. |
| 36 run_loop_.reset(CFRunLoopGetCurrent()); |
| 37 CFRetain(run_loop_.get()); |
| 38 |
27 base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys( | 39 base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys( |
28 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); | 40 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); |
29 base::mac::ScopedCFTypeRef<CFStringRef> key( | 41 base::mac::ScopedCFTypeRef<CFStringRef> key( |
30 SCDynamicStoreKeyCreateNetworkGlobalEntity( | 42 SCDynamicStoreKeyCreateNetworkGlobalEntity( |
31 NULL, kSCDynamicStoreDomainState, kSCEntNetInterface)); | 43 NULL, kSCDynamicStoreDomainState, kSCEntNetInterface)); |
32 CFArrayAppendValue(notification_keys.get(), key.get()); | 44 CFArrayAppendValue(notification_keys.get(), key.get()); |
33 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity( | 45 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity( |
34 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)); | 46 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)); |
35 CFArrayAppendValue(notification_keys.get(), key.get()); | 47 CFArrayAppendValue(notification_keys.get(), key.get()); |
36 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity( | 48 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity( |
37 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6)); | 49 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6)); |
38 CFArrayAppendValue(notification_keys.get(), key.get()); | 50 CFArrayAppendValue(notification_keys.get(), key.get()); |
39 | 51 |
40 // Set the notification keys. This starts us receiving notifications. | 52 // Set the notification keys. This starts us receiving notifications. |
41 bool ret = SCDynamicStoreSetNotificationKeys( | 53 bool ret = SCDynamicStoreSetNotificationKeys( |
42 store, notification_keys.get(), NULL); | 54 store, notification_keys.get(), NULL); |
43 // TODO(willchan): Figure out a proper way to handle this rather than crash. | 55 // TODO(willchan): Figure out a proper way to handle this rather than crash. |
44 CHECK(ret); | 56 CHECK(ret); |
| 57 |
| 58 // Try to reach 0.0.0.0. This is the approach taken by Firefox: |
| 59 // |
| 60 // http://mxr.mozilla.org/mozilla2.0/source/netwerk/system/mac/nsNetworkLinkSe
rvice.mm |
| 61 // |
| 62 // From my (adamk) testing on Snow Leopard, 0.0.0.0 |
| 63 // seems to be reachable if any network connection is available. |
| 64 struct sockaddr_in addr = {0}; |
| 65 addr.sin_len = sizeof(addr); |
| 66 addr.sin_family = AF_INET; |
| 67 reachability_.reset(SCNetworkReachabilityCreateWithAddress( |
| 68 kCFAllocatorDefault, reinterpret_cast<struct sockaddr*>(&addr))); |
| 69 SCNetworkReachabilityContext reachability_context = { |
| 70 0, // version |
| 71 this, // user data |
| 72 NULL, // retain |
| 73 NULL, // release |
| 74 NULL // description |
| 75 }; |
| 76 if (!SCNetworkReachabilitySetCallback( |
| 77 reachability_.get(), |
| 78 &NetworkChangeNotifierMac::ReachabilityCallback, |
| 79 &reachability_context)) { |
| 80 LOG(DFATAL) << "Could not set network reachability callback"; |
| 81 reachability_.reset(); |
| 82 } else if (!SCNetworkReachabilityScheduleWithRunLoop(reachability_.get(), |
| 83 run_loop_, |
| 84 kCFRunLoopCommonModes)) { |
| 85 LOG(DFATAL) << "Could not schedule network reachability on run loop"; |
| 86 reachability_.reset(); |
| 87 } |
45 } | 88 } |
46 | 89 |
47 void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) { | 90 void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) { |
48 // Called on notifier thread. | 91 DCHECK_EQ(run_loop_.get(), CFRunLoopGetCurrent()); |
49 | 92 |
50 for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) { | 93 for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) { |
51 CFStringRef key = static_cast<CFStringRef>( | 94 CFStringRef key = static_cast<CFStringRef>( |
52 CFArrayGetValueAtIndex(changed_keys, i)); | 95 CFArrayGetValueAtIndex(changed_keys, i)); |
53 if (CFStringHasSuffix(key, kSCEntNetIPv4) || | 96 if (CFStringHasSuffix(key, kSCEntNetIPv4) || |
54 CFStringHasSuffix(key, kSCEntNetIPv6)) { | 97 CFStringHasSuffix(key, kSCEntNetIPv6)) { |
55 NotifyObserversOfIPAddressChange(); | 98 NotifyObserversOfIPAddressChange(); |
56 return; | 99 return; |
57 } | 100 } |
58 if (CFStringHasSuffix(key, kSCEntNetInterface)) { | 101 if (CFStringHasSuffix(key, kSCEntNetInterface)) { |
59 // TODO(willchan): Does not appear to be working. Look into this. | 102 // TODO(willchan): Does not appear to be working. Look into this. |
60 // Perhaps this isn't needed anyway. | 103 // Perhaps this isn't needed anyway. |
61 } else { | 104 } else { |
62 NOTREACHED(); | 105 NOTREACHED(); |
63 } | 106 } |
64 } | 107 } |
65 } | 108 } |
66 | 109 |
| 110 // static |
| 111 void NetworkChangeNotifierMac::ReachabilityCallback( |
| 112 SCNetworkReachabilityRef target, |
| 113 SCNetworkConnectionFlags flags, |
| 114 void* notifier) { |
| 115 NetworkChangeNotifierMac* notifier_mac = |
| 116 static_cast<NetworkChangeNotifierMac*>(notifier); |
| 117 |
| 118 DCHECK_EQ(notifier_mac->run_loop_.get(), CFRunLoopGetCurrent()); |
| 119 |
| 120 bool reachable = flags & kSCNetworkFlagsReachable; |
| 121 bool connection_required = flags & kSCNetworkFlagsConnectionRequired; |
| 122 bool old_reachability = notifier_mac->network_reachable_; |
| 123 notifier_mac->network_reachable_ = reachable && !connection_required; |
| 124 if (old_reachability != notifier_mac->network_reachable_) |
| 125 notifier_mac->NotifyObserversOfOnlineStateChange(); |
| 126 } |
| 127 |
67 } // namespace net | 128 } // namespace net |
OLD | NEW |