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

Unified 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, 7 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/network_change_notifier_mac.cc
diff --git a/net/base/network_change_notifier_mac.cc b/net/base/network_change_notifier_mac.cc
index 7521c90bd649117a2d7b4400e2e29424d652564e..963ef5e9cf85c2a02ab97497464ed4db6ce764b6 100644
--- a/net/base/network_change_notifier_mac.cc
+++ b/net/base/network_change_notifier_mac.cc
@@ -4,7 +4,9 @@
#include "net/base/network_change_notifier_mac.h"
+#include <netinet/in.h>
#include <SystemConfiguration/SCDynamicStoreKey.h>
+#include <SystemConfiguration/SCNetworkReachability.h>
#include <SystemConfiguration/SCSchemaDefinitions.h>
#include "base/mac/scoped_cftyperef.h"
@@ -13,12 +15,15 @@ namespace net {
NetworkChangeNotifierMac::NetworkChangeNotifierMac()
: forwarder_(this),
- config_watcher_(&forwarder_) {}
-NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {}
+ config_watcher_(&forwarder_),
+ network_reachable_(true) {}
+
+NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {
+ 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
+}
bool NetworkChangeNotifierMac::IsCurrentlyOffline() const {
- // TODO(eroman): http://crbug.com/53473
- return false;
+ return !network_reachable_;
}
void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys(
@@ -42,11 +47,41 @@ void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys(
store, notification_keys.get(), NULL);
// TODO(willchan): Figure out a proper way to handle this rather than crash.
CHECK(ret);
+
+ // Try to reach 0.0.0.0. This is the approach taken by Firefox:
+ //
+ // http://mxr.mozilla.org/mozilla2.0/source/netwerk/system/mac/nsNetworkLinkService.mm
+ //
+ // From my (adamk) testing on Snow Leopard, 0.0.0.0
+ // seems to be reachable if any network connection is available.
+ struct sockaddr_in addr = {0};
+ addr.sin_len = sizeof(addr);
+ addr.sin_family = AF_INET;
+ reachability_.reset(SCNetworkReachabilityCreateWithAddress(
+ kCFAllocatorDefault, reinterpret_cast<struct sockaddr*>(&addr)));
+ SCNetworkReachabilityContext reachability_context = {
+ 0, // version
+ this, // user data
+ NULL, // retain
+ NULL, // release
+ NULL // description
+ };
+ if (!SCNetworkReachabilitySetCallback(
+ reachability_.get(),
+ &NetworkChangeNotifierMac::ReachabilityCallback,
+ &reachability_context)) {
+ LOG(DFATAL) << "Could not set network reachability callback";
+ reachability_.reset();
+ } else if (!SCNetworkReachabilityScheduleWithRunLoop(reachability_.get(),
+ CFRunLoopGetCurrent(),
+ kCFRunLoopCommonModes)) {
+ LOG(DFATAL) << "Could not schedule network reachability on run loop";
+ reachability_.reset();
+ }
}
void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) {
// Called on notifier thread.
-
for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) {
CFStringRef key = static_cast<CFStringRef>(
CFArrayGetValueAtIndex(changed_keys, i));
@@ -64,4 +99,30 @@ void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) {
}
}
+// static
+void NetworkChangeNotifierMac::ReachabilityCallback(
+ SCNetworkReachabilityRef target,
+ SCNetworkConnectionFlags flags,
+ void* notifier) {
+ // Called on notifier thread.
+ bool reachable = flags & kSCNetworkFlagsReachable;
+ bool connection_required = flags & kSCNetworkFlagsConnectionRequired;
+ NetworkChangeNotifierMac* notifier_mac =
+ static_cast<NetworkChangeNotifierMac*>(notifier);
+ bool old_reachability = notifier_mac->network_reachable_;
+ notifier_mac->network_reachable_ = reachable && !connection_required;
+ if (old_reachability != notifier_mac->network_reachable_)
+ notifier_mac->NotifyObserversOfOnlineStateChange();
+}
+
+void NetworkChangeNotifierMac::CleanUp() {
+ // Called on notifier thread.
+ if (!reachability_.get())
+ return;
+ SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(),
+ CFRunLoopGetCurrent(),
+ kCFRunLoopCommonModes);
+ reachability_.reset();
+}
+
} // namespace net
« 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