| 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/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 service_.reset(); | 56 service_.reset(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 private: | 59 private: |
| 60 scoped_ptr<DnsConfigService> service_; | 60 scoped_ptr<DnsConfigService> service_; |
| 61 | 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread); | 62 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread); |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 NetworkChangeNotifierMac::NetworkChangeNotifierMac() | 65 NetworkChangeNotifierMac::NetworkChangeNotifierMac() |
| 66 : connection_type_(CONNECTION_UNKNOWN), | 66 : NetworkChangeNotifier(NetworkChangeCalculatorParamsMac()), |
| 67 connection_type_(CONNECTION_UNKNOWN), |
| 67 connection_type_initialized_(false), | 68 connection_type_initialized_(false), |
| 68 initial_connection_type_cv_(&connection_type_lock_), | 69 initial_connection_type_cv_(&connection_type_lock_), |
| 69 forwarder_(this), | 70 forwarder_(this), |
| 70 dns_config_service_thread_(new DnsConfigServiceThread()) { | 71 dns_config_service_thread_(new DnsConfigServiceThread()) { |
| 71 // Must be initialized after the rest of this object, as it may call back into | 72 // Must be initialized after the rest of this object, as it may call back into |
| 72 // SetInitialConnectionType(). | 73 // SetInitialConnectionType(). |
| 73 config_watcher_.reset(new NetworkConfigWatcherMac(&forwarder_)); | 74 config_watcher_.reset(new NetworkConfigWatcherMac(&forwarder_)); |
| 74 dns_config_service_thread_->StartWithOptions( | 75 dns_config_service_thread_->StartWithOptions( |
| 75 base::Thread::Options(MessageLoop::TYPE_IO, 0)); | 76 base::Thread::Options(MessageLoop::TYPE_IO, 0)); |
| 76 } | 77 } |
| 77 | 78 |
| 78 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() { | 79 NetworkChangeNotifierMac::~NetworkChangeNotifierMac() { |
| 79 // Delete the ConfigWatcher to join the notifier thread, ensuring that | 80 // Delete the ConfigWatcher to join the notifier thread, ensuring that |
| 80 // StartReachabilityNotifications() has an opportunity to run to completion. | 81 // StartReachabilityNotifications() has an opportunity to run to completion. |
| 81 config_watcher_.reset(); | 82 config_watcher_.reset(); |
| 82 | 83 |
| 83 // Now that StartReachabilityNotifications() has either run to completion or | 84 // Now that StartReachabilityNotifications() has either run to completion or |
| 84 // never run at all, unschedule reachability_ if it was previously scheduled. | 85 // never run at all, unschedule reachability_ if it was previously scheduled. |
| 85 if (reachability_.get() && run_loop_.get()) { | 86 if (reachability_.get() && run_loop_.get()) { |
| 86 SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(), | 87 SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(), |
| 87 run_loop_.get(), | 88 run_loop_.get(), |
| 88 kCFRunLoopCommonModes); | 89 kCFRunLoopCommonModes); |
| 89 } | 90 } |
| 90 } | 91 } |
| 91 | 92 |
| 93 // static |
| 94 NetworkChangeNotifier::NetworkChangeCalculatorParams |
| 95 NetworkChangeNotifierMac::NetworkChangeCalculatorParamsMac() { |
| 96 NetworkChangeCalculatorParams params; |
| 97 // Delay values arrived at by simple experimentation and adjusted so as to |
| 98 // produce a single signal when switching between network connections. |
| 99 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(500); |
| 100 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(500); |
| 101 params.connection_type_offline_delay_ = |
| 102 base::TimeDelta::FromMilliseconds(1000); |
| 103 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500); |
| 104 return params; |
| 105 } |
| 106 |
| 92 NetworkChangeNotifier::ConnectionType | 107 NetworkChangeNotifier::ConnectionType |
| 93 NetworkChangeNotifierMac::GetCurrentConnectionType() const { | 108 NetworkChangeNotifierMac::GetCurrentConnectionType() const { |
| 94 base::AutoLock lock(connection_type_lock_); | 109 base::AutoLock lock(connection_type_lock_); |
| 95 // Make sure the initial connection type is set before returning. | 110 // Make sure the initial connection type is set before returning. |
| 96 while (!connection_type_initialized_) { | 111 while (!connection_type_initialized_) { |
| 97 initial_connection_type_cv_.Wait(); | 112 initial_connection_type_cv_.Wait(); |
| 98 } | 113 } |
| 99 return connection_type_; | 114 return connection_type_; |
| 100 } | 115 } |
| 101 | 116 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 | 264 |
| 250 #if defined(OS_IOS) | 265 #if defined(OS_IOS) |
| 251 // On iOS, the SCDynamicStore API does not exist, and we use the reachability | 266 // On iOS, the SCDynamicStore API does not exist, and we use the reachability |
| 252 // API to detect IP address changes instead. | 267 // API to detect IP address changes instead. |
| 253 if (new_type != CONNECTION_NONE) | 268 if (new_type != CONNECTION_NONE) |
| 254 NotifyObserversOfIPAddressChange(); | 269 NotifyObserversOfIPAddressChange(); |
| 255 #endif // defined(OS_IOS) | 270 #endif // defined(OS_IOS) |
| 256 } | 271 } |
| 257 | 272 |
| 258 } // namespace net | 273 } // namespace net |
| OLD | NEW |