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

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

Powered by Google App Engine
This is Rietveld 408576698