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

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: 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>
Mark Mentovai 2011/06/01 21:05:59 These shouldn’t be in a separate group from the Sy
adamk 2011/06/01 21:42:53 Done.
8 #include <string.h>
9
7 #include <SystemConfiguration/SCDynamicStoreKey.h> 10 #include <SystemConfiguration/SCDynamicStoreKey.h>
11 #include <SystemConfiguration/SCNetworkReachability.h>
8 #include <SystemConfiguration/SCSchemaDefinitions.h> 12 #include <SystemConfiguration/SCSchemaDefinitions.h>
9 13
10 #include "base/mac/scoped_cftyperef.h" 14 #include "base/mac/scoped_cftyperef.h"
11 15
12 namespace net { 16 namespace net {
13 17
14 NetworkChangeNotifierMac::NetworkChangeNotifierMac() 18 NetworkChangeNotifierMac::NetworkChangeNotifierMac()
15 : forwarder_(this), 19 : forwarder_(this),
16 config_watcher_(&forwarder_) {} 20 config_watcher_(&forwarder_),
21 network_reachable_(true) {}
17 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {} 22 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {}
18 23
19 bool NetworkChangeNotifierMac::IsCurrentlyOffline() const { 24 bool NetworkChangeNotifierMac::IsCurrentlyOffline() const {
20 // TODO(eroman): http://crbug.com/53473 25 return !network_reachable_;
21 return false;
22 } 26 }
23 27
24 void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys( 28 void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys(
25 SCDynamicStoreRef store) { 29 SCDynamicStoreRef store) {
26 // Called on notifier thread. 30 // Called on notifier thread.
27 base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys( 31 base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys(
28 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); 32 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
29 base::mac::ScopedCFTypeRef<CFStringRef> key( 33 base::mac::ScopedCFTypeRef<CFStringRef> key(
30 SCDynamicStoreKeyCreateNetworkGlobalEntity( 34 SCDynamicStoreKeyCreateNetworkGlobalEntity(
31 NULL, kSCDynamicStoreDomainState, kSCEntNetInterface)); 35 NULL, kSCDynamicStoreDomainState, kSCEntNetInterface));
32 CFArrayAppendValue(notification_keys.get(), key.get()); 36 CFArrayAppendValue(notification_keys.get(), key.get());
33 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity( 37 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity(
34 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)); 38 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4));
35 CFArrayAppendValue(notification_keys.get(), key.get()); 39 CFArrayAppendValue(notification_keys.get(), key.get());
36 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity( 40 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity(
37 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6)); 41 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6));
38 CFArrayAppendValue(notification_keys.get(), key.get()); 42 CFArrayAppendValue(notification_keys.get(), key.get());
39 43
40 // Set the notification keys. This starts us receiving notifications. 44 // Set the notification keys. This starts us receiving notifications.
41 bool ret = SCDynamicStoreSetNotificationKeys( 45 bool ret = SCDynamicStoreSetNotificationKeys(
42 store, notification_keys.get(), NULL); 46 store, notification_keys.get(), NULL);
43 // TODO(willchan): Figure out a proper way to handle this rather than crash. 47 // TODO(willchan): Figure out a proper way to handle this rather than crash.
44 CHECK(ret); 48 CHECK(ret);
49
50 // Try to reach 0.0.0.0. This is the approach taken by Firefox:
51 //
52 // http://mxr.mozilla.org/mozilla2.0/source/netwerk/system/mac/nsNetworkLinkSe rvice.mm
53 //
54 // From my (adamk) testing on Snow Leopard, 0.0.0.0
55 // seems to be reachable if any network connection is available.
56 struct sockaddr_in addr;
Mark Mentovai 2011/06/01 21:05:59 I would just say |struct sockaddr_in addr = { 0 };
adamk 2011/06/01 21:42:53 I don't think the 0 does anything here. I've rewr
Mark Mentovai 2011/06/01 22:02:45 adamk wrote:
adamk 2011/06/01 22:09:34 Sorry, I meant to say "{ 0 }" wouldn't do anything
Mark Mentovai 2011/06/01 22:10:43 adamk wrote:
57 memset(&addr, 0, sizeof(addr));
58 addr.sin_len = sizeof(addr);
59 addr.sin_family = AF_INET;
60 reachability_.reset(SCNetworkReachabilityCreateWithAddress(
61 kCFAllocatorDefault, reinterpret_cast<struct sockaddr*>(&addr)));
62 SCNetworkReachabilityContext reachability_context = {
63 0, // version
64 this, // user data
65 NULL, // retain
66 NULL, // release
67 NULL // description
68 };
69 // TODO(adamk): Either of these calls could fail, can we do any better
70 // in handling this?
71 ret = SCNetworkReachabilitySetCallback(
72 reachability_.get(),
73 &NetworkChangeNotifierMac::ReachabilityCallback,
74 &reachability_context);
Mark Mentovai 2011/06/01 21:05:59 I hope SC copies what it needs from this structure
adamk 2011/06/01 21:42:53 I have to imagine they copy what they need...
75 DCHECK(ret) << "Could not set network reachability callback";
76 ret = SCNetworkReachabilityScheduleWithRunLoop(reachability_.get(),
Mark Mentovai 2011/06/01 21:05:59 I think you’d want to avoid doing this (and maybe
adamk 2011/06/01 21:42:53 Changed the structure around and added some resets
77 CFRunLoopGetCurrent(),
78 kCFRunLoopCommonModes);
79 DCHECK(ret) << "Could not schedule network reachability on run loop";
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);
Mark Mentovai 2011/06/01 21:05:59 No parentheses on this line, the next, or line 111
adamk 2011/06/01 21:42:53 Done.
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