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