| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <netinet/in.h> |
| 8 #include <resolv.h> | 8 #include <resolv.h> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "net/dns/dns_config_service.h" | 14 #include "net/dns/dns_config_service.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 static bool CalculateReachability(SCNetworkConnectionFlags flags) { | 18 static bool CalculateReachability(SCNetworkConnectionFlags flags) { |
| 19 bool reachable = flags & kSCNetworkFlagsReachable; | 19 bool reachable = flags & kSCNetworkFlagsReachable; |
| 20 bool connection_required = flags & kSCNetworkFlagsConnectionRequired; | 20 bool connection_required = flags & kSCNetworkFlagsConnectionRequired; |
| 21 return reachable && !connection_required; | 21 return reachable && !connection_required; |
| 22 } | 22 } |
| 23 | 23 |
| 24 NetworkChangeNotifier::ConnectionType CalculateConnectionType( | |
| 25 SCNetworkConnectionFlags flags) { | |
| 26 bool reachable = CalculateReachability(flags); | |
| 27 if (reachable) { | |
| 28 #if defined(OS_IOS) | |
| 29 return (flags & kSCNetworkReachabilityFlagsIsWWAN) ? | |
| 30 NetworkChangeNotifier::CONNECTION_3G : | |
| 31 NetworkChangeNotifier::CONNECTION_WIFI; | |
| 32 #else | |
| 33 // TODO(droger): Get something more detailed than CONNECTION_UNKNOWN. | |
| 34 // http://crbug.com/112937 | |
| 35 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
| 36 #endif // defined(OS_IOS) | |
| 37 } else { | |
| 38 return NetworkChangeNotifier::CONNECTION_NONE; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 // Thread on which we can run DnsConfigService, which requires a TYPE_IO | 24 // Thread on which we can run DnsConfigService, which requires a TYPE_IO |
| 43 // message loop. | 25 // message loop. |
| 44 class NetworkChangeNotifierMac::DnsConfigServiceThread : public base::Thread { | 26 class NetworkChangeNotifierMac::DnsConfigServiceThread : public base::Thread { |
| 45 public: | 27 public: |
| 46 DnsConfigServiceThread() : base::Thread("DnsConfigService") {} | 28 DnsConfigServiceThread() : base::Thread("DnsConfigService") {} |
| 47 | 29 |
| 48 ~DnsConfigServiceThread() override { Stop(); } | 30 ~DnsConfigServiceThread() override { Stop(); } |
| 49 | 31 |
| 50 void Init() override { | 32 void Init() override { |
| 51 service_ = DnsConfigService::CreateSystemService(); | 33 service_ = DnsConfigService::CreateSystemService(); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 while (!connection_type_initialized_) { | 92 while (!connection_type_initialized_) { |
| 111 initial_connection_type_cv_.Wait(); | 93 initial_connection_type_cv_.Wait(); |
| 112 } | 94 } |
| 113 return connection_type_; | 95 return connection_type_; |
| 114 } | 96 } |
| 115 | 97 |
| 116 void NetworkChangeNotifierMac::Forwarder::Init() { | 98 void NetworkChangeNotifierMac::Forwarder::Init() { |
| 117 net_config_watcher_->SetInitialConnectionType(); | 99 net_config_watcher_->SetInitialConnectionType(); |
| 118 } | 100 } |
| 119 | 101 |
| 102 // static |
| 103 NetworkChangeNotifier::ConnectionType |
| 104 NetworkChangeNotifierMac::CalculateConnectionType( |
| 105 SCNetworkConnectionFlags flags) { |
| 106 bool reachable = CalculateReachability(flags); |
| 107 if (reachable) { |
| 108 #if defined(OS_IOS) |
| 109 return (flags & kSCNetworkReachabilityFlagsIsWWAN) ? CONNECTION_3G |
| 110 : CONNECTION_WIFI; |
| 111 #else |
| 112 return ConnectionTypeFromInterfaces(); |
| 113 #endif // defined(OS_IOS) |
| 114 } else { |
| 115 return CONNECTION_NONE; |
| 116 } |
| 117 } |
| 118 |
| 120 void NetworkChangeNotifierMac::Forwarder::StartReachabilityNotifications() { | 119 void NetworkChangeNotifierMac::Forwarder::StartReachabilityNotifications() { |
| 121 net_config_watcher_->StartReachabilityNotifications(); | 120 net_config_watcher_->StartReachabilityNotifications(); |
| 122 } | 121 } |
| 123 | 122 |
| 124 void NetworkChangeNotifierMac::Forwarder::SetDynamicStoreNotificationKeys( | 123 void NetworkChangeNotifierMac::Forwarder::SetDynamicStoreNotificationKeys( |
| 125 SCDynamicStoreRef store) { | 124 SCDynamicStoreRef store) { |
| 126 net_config_watcher_->SetDynamicStoreNotificationKeys(store); | 125 net_config_watcher_->SetDynamicStoreNotificationKeys(store); |
| 127 } | 126 } |
| 128 | 127 |
| 129 void NetworkChangeNotifierMac::Forwarder::OnNetworkConfigChange( | 128 void NetworkChangeNotifierMac::Forwarder::OnNetworkConfigChange( |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 } | 266 } |
| 268 | 267 |
| 269 #if defined(OS_IOS) | 268 #if defined(OS_IOS) |
| 270 // On iOS, the SCDynamicStore API does not exist, and we use the reachability | 269 // On iOS, the SCDynamicStore API does not exist, and we use the reachability |
| 271 // API to detect IP address changes instead. | 270 // API to detect IP address changes instead. |
| 272 NotifyObserversOfIPAddressChange(); | 271 NotifyObserversOfIPAddressChange(); |
| 273 #endif // defined(OS_IOS) | 272 #endif // defined(OS_IOS) |
| 274 } | 273 } |
| 275 | 274 |
| 276 } // namespace net | 275 } // namespace net |
| OLD | NEW |