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

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: Call cleanup from destructor 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_),
17 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {} 19 network_reachable_(true) {}
20
21 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {
22 CleanUp();
Mark Mentovai 2011/06/02 16:26:57 Which thread does this happen on?
adamk 2011/06/02 16:43:50 Ugh, good point, this is why I put it in the deleg
23 }
18 24
19 bool NetworkChangeNotifierMac::IsCurrentlyOffline() const { 25 bool NetworkChangeNotifierMac::IsCurrentlyOffline() const {
20 // TODO(eroman): http://crbug.com/53473 26 return !network_reachable_;
21 return false;
22 } 27 }
23 28
24 void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys( 29 void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys(
25 SCDynamicStoreRef store) { 30 SCDynamicStoreRef store) {
26 // Called on notifier thread. 31 // Called on notifier thread.
Mark Mentovai 2011/06/02 16:26:57 Every time you have this comment: Is this somethi
adamk 2011/06/02 16:43:50 Not without some additional machinery. Possibly t
Mark Mentovai 2011/06/02 16:47:20 adamk wrote:
27 base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys( 32 base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys(
28 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)); 33 CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
29 base::mac::ScopedCFTypeRef<CFStringRef> key( 34 base::mac::ScopedCFTypeRef<CFStringRef> key(
30 SCDynamicStoreKeyCreateNetworkGlobalEntity( 35 SCDynamicStoreKeyCreateNetworkGlobalEntity(
31 NULL, kSCDynamicStoreDomainState, kSCEntNetInterface)); 36 NULL, kSCDynamicStoreDomainState, kSCEntNetInterface));
32 CFArrayAppendValue(notification_keys.get(), key.get()); 37 CFArrayAppendValue(notification_keys.get(), key.get());
33 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity( 38 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity(
34 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)); 39 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4));
35 CFArrayAppendValue(notification_keys.get(), key.get()); 40 CFArrayAppendValue(notification_keys.get(), key.get());
36 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity( 41 key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity(
37 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6)); 42 NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6));
38 CFArrayAppendValue(notification_keys.get(), key.get()); 43 CFArrayAppendValue(notification_keys.get(), key.get());
39 44
40 // Set the notification keys. This starts us receiving notifications. 45 // Set the notification keys. This starts us receiving notifications.
41 bool ret = SCDynamicStoreSetNotificationKeys( 46 bool ret = SCDynamicStoreSetNotificationKeys(
42 store, notification_keys.get(), NULL); 47 store, notification_keys.get(), NULL);
43 // TODO(willchan): Figure out a proper way to handle this rather than crash. 48 // TODO(willchan): Figure out a proper way to handle this rather than crash.
44 CHECK(ret); 49 CHECK(ret);
50
51 // Try to reach 0.0.0.0. This is the approach taken by Firefox:
52 //
53 // http://mxr.mozilla.org/mozilla2.0/source/netwerk/system/mac/nsNetworkLinkSe rvice.mm
54 //
55 // From my (adamk) testing on Snow Leopard, 0.0.0.0
56 // seems to be reachable if any network connection is available.
57 struct sockaddr_in addr = {0};
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 if (!SCNetworkReachabilitySetCallback(
70 reachability_.get(),
71 &NetworkChangeNotifierMac::ReachabilityCallback,
72 &reachability_context)) {
73 LOG(DFATAL) << "Could not set network reachability callback";
74 reachability_.reset();
75 } else if (!SCNetworkReachabilityScheduleWithRunLoop(reachability_.get(),
76 CFRunLoopGetCurrent(),
77 kCFRunLoopCommonModes)) {
78 LOG(DFATAL) << "Could not schedule network reachability on run loop";
79 reachability_.reset();
80 }
45 } 81 }
46 82
47 void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) { 83 void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) {
48 // Called on notifier thread. 84 // Called on notifier thread.
49
50 for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) { 85 for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) {
51 CFStringRef key = static_cast<CFStringRef>( 86 CFStringRef key = static_cast<CFStringRef>(
52 CFArrayGetValueAtIndex(changed_keys, i)); 87 CFArrayGetValueAtIndex(changed_keys, i));
53 if (CFStringHasSuffix(key, kSCEntNetIPv4) || 88 if (CFStringHasSuffix(key, kSCEntNetIPv4) ||
54 CFStringHasSuffix(key, kSCEntNetIPv6)) { 89 CFStringHasSuffix(key, kSCEntNetIPv6)) {
55 NotifyObserversOfIPAddressChange(); 90 NotifyObserversOfIPAddressChange();
56 return; 91 return;
57 } 92 }
58 if (CFStringHasSuffix(key, kSCEntNetInterface)) { 93 if (CFStringHasSuffix(key, kSCEntNetInterface)) {
59 // TODO(willchan): Does not appear to be working. Look into this. 94 // TODO(willchan): Does not appear to be working. Look into this.
60 // Perhaps this isn't needed anyway. 95 // Perhaps this isn't needed anyway.
61 } else { 96 } else {
62 NOTREACHED(); 97 NOTREACHED();
63 } 98 }
64 } 99 }
65 } 100 }
66 101
102 // static
103 void NetworkChangeNotifierMac::ReachabilityCallback(
104 SCNetworkReachabilityRef target,
105 SCNetworkConnectionFlags flags,
106 void* notifier) {
107 // Called on notifier thread.
108 bool reachable = flags & kSCNetworkFlagsReachable;
109 bool connection_required = flags & kSCNetworkFlagsConnectionRequired;
110 NetworkChangeNotifierMac* notifier_mac =
111 static_cast<NetworkChangeNotifierMac*>(notifier);
112 bool old_reachability = notifier_mac->network_reachable_;
113 notifier_mac->network_reachable_ = reachable && !connection_required;
114 if (old_reachability != notifier_mac->network_reachable_)
115 notifier_mac->NotifyObserversOfOnlineStateChange();
116 }
117
118 void NetworkChangeNotifierMac::CleanUp() {
119 // Called on notifier thread.
120 if (!reachability_.get())
121 return;
122 SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(),
123 CFRunLoopGetCurrent(),
124 kCFRunLoopCommonModes);
125 reachability_.reset();
126 }
127
67 } // namespace net 128 } // 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