Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(630)

Side by Side Diff: net/base/network_change_notifier_mac.cc

Issue 7006015: Implement NetworkChangeNotifier::IsCurrentlyOffline() for Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Responded to review comments Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/network_change_notifier_mac.h ('k') | net/base/network_config_watcher_mac.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_),
19 network_reachable_(true) {}
17 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {} 20 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {}
18 21
19 bool NetworkChangeNotifierMac::IsCurrentlyOffline() const { 22 bool NetworkChangeNotifierMac::IsCurrentlyOffline() const {
20 // TODO(eroman): http://crbug.com/53473 23 return !network_reachable_;
21 return false;
22 } 24 }
23 25
24 void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys( 26 void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys(
25 SCDynamicStoreRef store) { 27 SCDynamicStoreRef store) {
26 // Called on notifier thread. 28 // Called on notifier thread.
27 base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys( 29 base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys(
28 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); 30 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
29 base::mac::ScopedCFTypeRef<CFStringRef> key( 31 base::mac::ScopedCFTypeRef<CFStringRef> key(
30 SCDynamicStoreKeyCreateNetworkGlobalEntity( 32 SCDynamicStoreKeyCreateNetworkGlobalEntity(
31 NULL, kSCDynamicStoreDomainState, kSCEntNetInterface)); 33 NULL, kSCDynamicStoreDomainState, kSCEntNetInterface));
32 CFArrayAppendValue(notification_keys.get(), key.get()); 34 CFArrayAppendValue(notification_keys.get(), key.get());
33 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity( 35 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity(
34 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)); 36 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4));
35 CFArrayAppendValue(notification_keys.get(), key.get()); 37 CFArrayAppendValue(notification_keys.get(), key.get());
36 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity( 38 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity(
37 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6)); 39 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6));
38 CFArrayAppendValue(notification_keys.get(), key.get()); 40 CFArrayAppendValue(notification_keys.get(), key.get());
39 41
40 // Set the notification keys. This starts us receiving notifications. 42 // Set the notification keys. This starts us receiving notifications.
41 bool ret = SCDynamicStoreSetNotificationKeys( 43 bool ret = SCDynamicStoreSetNotificationKeys(
42 store, notification_keys.get(), NULL); 44 store, notification_keys.get(), NULL);
43 // TODO(willchan): Figure out a proper way to handle this rather than crash. 45 // TODO(willchan): Figure out a proper way to handle this rather than crash.
44 CHECK(ret); 46 CHECK(ret);
47
48 // Try to reach 0.0.0.0. This is the approach taken by Firefox:
49 //
50 // http://mxr.mozilla.org/mozilla2.0/source/netwerk/system/mac/nsNetworkLinkSe rvice.mm
51 //
52 // From my (adamk) testing on Snow Leopard, 0.0.0.0
53 // seems to be reachable if any network connection is available.
54 struct sockaddr_in addr = {};
55 addr.sin_len = sizeof(addr);
56 addr.sin_family = AF_INET;
57 reachability_.reset(SCNetworkReachabilityCreateWithAddress(
58 kCFAllocatorDefault, reinterpret_cast<struct sockaddr*>(&addr)));
59 SCNetworkReachabilityContext reachability_context = {
60 0, // version
61 this, // user data
62 NULL, // retain
63 NULL, // release
64 NULL // description
65 };
66 // TODO(adamk): Either of these calls could fail, can we do any better
67 // in handling this?
Mark Mentovai 2011/06/01 22:02:45 Is the TODO still relevant? It’s fine to leave it
adamk 2011/06/01 22:11:28 Done.
68 if (!SCNetworkReachabilitySetCallback(
69 reachability_.get(),
70 &NetworkChangeNotifierMac::ReachabilityCallback,
71 &reachability_context)) {
72 LOG(DFATAL) << "Could not set network reachability callback";
73 reachability_.reset();
74 } else if (!SCNetworkReachabilityScheduleWithRunLoop(reachability_.get(),
75 CFRunLoopGetCurrent(),
76 kCFRunLoopCommonModes)) {
77 LOG(DFATAL) << "Could not schedule network reachability on run loop";
78 reachability_.reset();
79 }
45 } 80 }
46 81
47 void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) { 82 void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) {
48 // Called on notifier thread. 83 // Called on notifier thread.
49
50 for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) { 84 for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) {
51 CFStringRef key = static_cast<CFStringRef>( 85 CFStringRef key = static_cast<CFStringRef>(
52 CFArrayGetValueAtIndex(changed_keys, i)); 86 CFArrayGetValueAtIndex(changed_keys, i));
53 if (CFStringHasSuffix(key, kSCEntNetIPv4) || 87 if (CFStringHasSuffix(key, kSCEntNetIPv4) ||
54 CFStringHasSuffix(key, kSCEntNetIPv6)) { 88 CFStringHasSuffix(key, kSCEntNetIPv6)) {
55 NotifyObserversOfIPAddressChange(); 89 NotifyObserversOfIPAddressChange();
56 return; 90 return;
57 } 91 }
58 if (CFStringHasSuffix(key, kSCEntNetInterface)) { 92 if (CFStringHasSuffix(key, kSCEntNetInterface)) {
59 // TODO(willchan): Does not appear to be working. Look into this. 93 // TODO(willchan): Does not appear to be working. Look into this.
60 // Perhaps this isn't needed anyway. 94 // Perhaps this isn't needed anyway.
61 } else { 95 } else {
62 NOTREACHED(); 96 NOTREACHED();
63 } 97 }
64 } 98 }
65 } 99 }
66 100
101 // static
102 void NetworkChangeNotifierMac::ReachabilityCallback(
103 SCNetworkReachabilityRef target,
104 SCNetworkConnectionFlags flags,
105 void* notifier) {
106 // Called on notifier thread.
107 bool reachable = flags & kSCNetworkFlagsReachable;
108 bool connection_required = flags & kSCNetworkFlagsConnectionRequired;
109 NetworkChangeNotifierMac* notifier_mac =
110 static_cast<NetworkChangeNotifierMac*>(notifier);
111 notifier_mac->network_reachable_ = reachable && !connection_required;
112 notifier_mac->NotifyObserversOfOnlineStateChange();
113 }
114
115 void NetworkChangeNotifierMac::CleanUp() {
116 // Called on notifier thread.
117 if (!reachability_.get())
118 return;
119 SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(),
120 CFRunLoopGetCurrent(),
121 kCFRunLoopCommonModes);
122 reachability_.reset();
123 }
124
67 } // namespace net 125 } // namespace net
OLDNEW
« no previous file with comments | « net/base/network_change_notifier_mac.h ('k') | net/base/network_config_watcher_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698