| 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_win.h" | 5 #include "net/base/network_change_notifier_win.h" |
| 6 | 6 |
| 7 #include <iphlpapi.h> | 7 #include <iphlpapi.h> |
| 8 #include <winsock2.h> | 8 #include <winsock2.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 const int kWatchForAddressChangeRetryIntervalMs = 500; | 25 const int kWatchForAddressChangeRetryIntervalMs = 500; |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 // Thread on which we can run DnsConfigService, which requires AssertIOAllowed | 29 // Thread on which we can run DnsConfigService, which requires AssertIOAllowed |
| 30 // to open registry keys and to handle FilePathWatcher updates. | 30 // to open registry keys and to handle FilePathWatcher updates. |
| 31 class NetworkChangeNotifierWin::DnsConfigServiceThread : public base::Thread { | 31 class NetworkChangeNotifierWin::DnsConfigServiceThread : public base::Thread { |
| 32 public: | 32 public: |
| 33 DnsConfigServiceThread() : base::Thread("DnsConfigService") {} | 33 DnsConfigServiceThread() : base::Thread("DnsConfigService") {} |
| 34 | 34 |
| 35 virtual ~DnsConfigServiceThread() { | 35 virtual ~DnsConfigServiceThread() { Stop(); } |
| 36 Stop(); | |
| 37 } | |
| 38 | 36 |
| 39 virtual void Init() OVERRIDE { | 37 virtual void Init() OVERRIDE { |
| 40 service_ = DnsConfigService::CreateSystemService(); | 38 service_ = DnsConfigService::CreateSystemService(); |
| 41 service_->WatchConfig(base::Bind(&NetworkChangeNotifier::SetDnsConfig)); | 39 service_->WatchConfig(base::Bind(&NetworkChangeNotifier::SetDnsConfig)); |
| 42 } | 40 } |
| 43 | 41 |
| 44 virtual void CleanUp() OVERRIDE { | 42 virtual void CleanUp() OVERRIDE { service_.reset(); } |
| 45 service_.reset(); | |
| 46 } | |
| 47 | 43 |
| 48 private: | 44 private: |
| 49 scoped_ptr<DnsConfigService> service_; | 45 scoped_ptr<DnsConfigService> service_; |
| 50 | 46 |
| 51 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread); | 47 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread); |
| 52 }; | 48 }; |
| 53 | 49 |
| 54 NetworkChangeNotifierWin::NetworkChangeNotifierWin() | 50 NetworkChangeNotifierWin::NetworkChangeNotifierWin() |
| 55 : NetworkChangeNotifier(NetworkChangeCalculatorParamsWin()), | 51 : NetworkChangeNotifier(NetworkChangeCalculatorParamsWin()), |
| 56 is_watching_(false), | 52 is_watching_(false), |
| 57 sequential_failures_(0), | 53 sequential_failures_(0), |
| 58 weak_factory_(this), | 54 weak_factory_(this), |
| 59 dns_config_service_thread_(new DnsConfigServiceThread()), | 55 dns_config_service_thread_(new DnsConfigServiceThread()), |
| 60 last_computed_connection_type_(RecomputeCurrentConnectionType()), | 56 last_computed_connection_type_(RecomputeCurrentConnectionType()), |
| 61 last_announced_offline_( | 57 last_announced_offline_(last_computed_connection_type_ == |
| 62 last_computed_connection_type_ == CONNECTION_NONE) { | 58 CONNECTION_NONE) { |
| 63 memset(&addr_overlapped_, 0, sizeof addr_overlapped_); | 59 memset(&addr_overlapped_, 0, sizeof addr_overlapped_); |
| 64 addr_overlapped_.hEvent = WSACreateEvent(); | 60 addr_overlapped_.hEvent = WSACreateEvent(); |
| 65 } | 61 } |
| 66 | 62 |
| 67 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() { | 63 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() { |
| 68 if (is_watching_) { | 64 if (is_watching_) { |
| 69 CancelIPChangeNotify(&addr_overlapped_); | 65 CancelIPChangeNotify(&addr_overlapped_); |
| 70 addr_watcher_.StopWatching(); | 66 addr_watcher_.StopWatching(); |
| 71 } | 67 } |
| 72 WSACloseEvent(addr_overlapped_.hEvent); | 68 WSACloseEvent(addr_overlapped_.hEvent); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // The main difference is we only call WSALookupServiceNext once, whereas | 141 // The main difference is we only call WSALookupServiceNext once, whereas |
| 146 // the earlier code would traverse the entire list and pass LUP_FLUSHPREVIOUS | 142 // the earlier code would traverse the entire list and pass LUP_FLUSHPREVIOUS |
| 147 // to skip past the large results. | 143 // to skip past the large results. |
| 148 | 144 |
| 149 HANDLE ws_handle; | 145 HANDLE ws_handle; |
| 150 WSAQUERYSET query_set = {0}; | 146 WSAQUERYSET query_set = {0}; |
| 151 query_set.dwSize = sizeof(WSAQUERYSET); | 147 query_set.dwSize = sizeof(WSAQUERYSET); |
| 152 query_set.dwNameSpace = NS_NLA; | 148 query_set.dwNameSpace = NS_NLA; |
| 153 // Initiate a client query to iterate through the | 149 // Initiate a client query to iterate through the |
| 154 // currently connected networks. | 150 // currently connected networks. |
| 155 if (0 != WSALookupServiceBegin(&query_set, LUP_RETURN_ALL, | 151 if (0 != WSALookupServiceBegin(&query_set, LUP_RETURN_ALL, &ws_handle)) { |
| 156 &ws_handle)) { | |
| 157 LOG(ERROR) << "WSALookupServiceBegin failed with: " << WSAGetLastError(); | 152 LOG(ERROR) << "WSALookupServiceBegin failed with: " << WSAGetLastError(); |
| 158 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | 153 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 159 } | 154 } |
| 160 | 155 |
| 161 bool found_connection = false; | 156 bool found_connection = false; |
| 162 | 157 |
| 163 // Retrieve the first available network. In this function, we only | 158 // Retrieve the first available network. In this function, we only |
| 164 // need to know whether or not there is network connection. | 159 // need to know whether or not there is network connection. |
| 165 // Allocate 256 bytes for name, it should be enough for most cases. | 160 // Allocate 256 bytes for name, it should be enough for most cases. |
| 166 // If the name is longer, it is OK as we will check the code returned and | 161 // If the name is longer, it is OK as we will check the code returned and |
| 167 // set correct network status. | 162 // set correct network status. |
| 168 char result_buffer[sizeof(WSAQUERYSET) + 256] = {0}; | 163 char result_buffer[sizeof(WSAQUERYSET) + 256] = {0}; |
| 169 DWORD length = sizeof(result_buffer); | 164 DWORD length = sizeof(result_buffer); |
| 170 reinterpret_cast<WSAQUERYSET*>(&result_buffer[0])->dwSize = | 165 reinterpret_cast<WSAQUERYSET*>(&result_buffer[0])->dwSize = |
| 171 sizeof(WSAQUERYSET); | 166 sizeof(WSAQUERYSET); |
| 172 int result = WSALookupServiceNext( | 167 int result = |
| 173 ws_handle, | 168 WSALookupServiceNext(ws_handle, |
| 174 LUP_RETURN_NAME, | 169 LUP_RETURN_NAME, |
| 175 &length, | 170 &length, |
| 176 reinterpret_cast<WSAQUERYSET*>(&result_buffer[0])); | 171 reinterpret_cast<WSAQUERYSET*>(&result_buffer[0])); |
| 177 | 172 |
| 178 if (result == 0) { | 173 if (result == 0) { |
| 179 // Found a connection! | 174 // Found a connection! |
| 180 found_connection = true; | 175 found_connection = true; |
| 181 } else { | 176 } else { |
| 182 DCHECK_EQ(SOCKET_ERROR, result); | 177 DCHECK_EQ(SOCKET_ERROR, result); |
| 183 result = WSAGetLastError(); | 178 result = WSAGetLastError(); |
| 184 | 179 |
| 185 // Error code WSAEFAULT means there is a network connection but the | 180 // Error code WSAEFAULT means there is a network connection but the |
| 186 // result_buffer size is too small to contain the results. The | 181 // result_buffer size is too small to contain the results. The |
| 187 // variable "length" returned from WSALookupServiceNext is the minimum | 182 // variable "length" returned from WSALookupServiceNext is the minimum |
| 188 // number of bytes required. We do not need to retrieve detail info, | 183 // number of bytes required. We do not need to retrieve detail info, |
| 189 // it is enough knowing there was a connection. | 184 // it is enough knowing there was a connection. |
| 190 if (result == WSAEFAULT) { | 185 if (result == WSAEFAULT) { |
| 191 found_connection = true; | 186 found_connection = true; |
| 192 } else if (result == WSA_E_NO_MORE || result == WSAENOMORE) { | 187 } else if (result == WSA_E_NO_MORE || result == WSAENOMORE) { |
| 193 // There was nothing to iterate over! | 188 // There was nothing to iterate over! |
| 194 } else { | 189 } else { |
| 195 LOG(WARNING) << "WSALookupServiceNext() failed with:" << result; | 190 LOG(WARNING) << "WSALookupServiceNext() failed with:" << result; |
| 196 } | 191 } |
| 197 } | 192 } |
| 198 | 193 |
| 199 result = WSALookupServiceEnd(ws_handle); | 194 result = WSALookupServiceEnd(ws_handle); |
| 200 LOG_IF(ERROR, result != 0) | 195 LOG_IF(ERROR, result != 0) << "WSALookupServiceEnd() failed with: " << result; |
| 201 << "WSALookupServiceEnd() failed with: " << result; | |
| 202 | 196 |
| 203 // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN. | 197 // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN. |
| 204 return found_connection ? NetworkChangeNotifier::CONNECTION_UNKNOWN : | 198 return found_connection ? NetworkChangeNotifier::CONNECTION_UNKNOWN |
| 205 NetworkChangeNotifier::CONNECTION_NONE; | 199 : NetworkChangeNotifier::CONNECTION_NONE; |
| 206 } | 200 } |
| 207 | 201 |
| 208 NetworkChangeNotifier::ConnectionType | 202 NetworkChangeNotifier::ConnectionType |
| 209 NetworkChangeNotifierWin::GetCurrentConnectionType() const { | 203 NetworkChangeNotifierWin::GetCurrentConnectionType() const { |
| 210 base::AutoLock auto_lock(last_computed_connection_type_lock_); | 204 base::AutoLock auto_lock(last_computed_connection_type_lock_); |
| 211 return last_computed_connection_type_; | 205 return last_computed_connection_type_; |
| 212 } | 206 } |
| 213 | 207 |
| 214 void NetworkChangeNotifierWin::SetCurrentConnectionType( | 208 void NetworkChangeNotifierWin::SetCurrentConnectionType( |
| 215 ConnectionType connection_type) { | 209 ConnectionType connection_type) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 234 NotifyObserversOfIPAddressChange(); | 228 NotifyObserversOfIPAddressChange(); |
| 235 | 229 |
| 236 // Calling GetConnectionType() at this very moment is likely to give | 230 // Calling GetConnectionType() at this very moment is likely to give |
| 237 // the wrong result, so we delay that until a little bit later. | 231 // the wrong result, so we delay that until a little bit later. |
| 238 // | 232 // |
| 239 // The one second delay chosen here was determined experimentally | 233 // The one second delay chosen here was determined experimentally |
| 240 // by adamk on Windows 7. | 234 // by adamk on Windows 7. |
| 241 // If after one second we determine we are still offline, we will | 235 // If after one second we determine we are still offline, we will |
| 242 // delay again. | 236 // delay again. |
| 243 offline_polls_ = 0; | 237 offline_polls_ = 0; |
| 244 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this, | 238 timer_.Start(FROM_HERE, |
| 239 base::TimeDelta::FromSeconds(1), |
| 240 this, |
| 245 &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange); | 241 &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange); |
| 246 } | 242 } |
| 247 | 243 |
| 248 void NetworkChangeNotifierWin::WatchForAddressChange() { | 244 void NetworkChangeNotifierWin::WatchForAddressChange() { |
| 249 DCHECK(CalledOnValidThread()); | 245 DCHECK(CalledOnValidThread()); |
| 250 DCHECK(!is_watching_); | 246 DCHECK(!is_watching_); |
| 251 | 247 |
| 252 // NotifyAddrChange occasionally fails with ERROR_OPEN_FAILED for unknown | 248 // NotifyAddrChange occasionally fails with ERROR_OPEN_FAILED for unknown |
| 253 // reasons. More rarely, it's also been observed failing with | 249 // reasons. More rarely, it's also been observed failing with |
| 254 // ERROR_NO_SYSTEM_RESOURCES. When either of these happens, we retry later. | 250 // ERROR_NO_SYSTEM_RESOURCES. When either of these happens, we retry later. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 285 | 281 |
| 286 is_watching_ = true; | 282 is_watching_ = true; |
| 287 sequential_failures_ = 0; | 283 sequential_failures_ = 0; |
| 288 } | 284 } |
| 289 | 285 |
| 290 bool NetworkChangeNotifierWin::WatchForAddressChangeInternal() { | 286 bool NetworkChangeNotifierWin::WatchForAddressChangeInternal() { |
| 291 DCHECK(CalledOnValidThread()); | 287 DCHECK(CalledOnValidThread()); |
| 292 | 288 |
| 293 if (!dns_config_service_thread_->IsRunning()) { | 289 if (!dns_config_service_thread_->IsRunning()) { |
| 294 dns_config_service_thread_->StartWithOptions( | 290 dns_config_service_thread_->StartWithOptions( |
| 295 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 291 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 296 } | 292 } |
| 297 | 293 |
| 298 HANDLE handle = NULL; | 294 HANDLE handle = NULL; |
| 299 DWORD ret = NotifyAddrChange(&handle, &addr_overlapped_); | 295 DWORD ret = NotifyAddrChange(&handle, &addr_overlapped_); |
| 300 if (ret != ERROR_IO_PENDING) | 296 if (ret != ERROR_IO_PENDING) |
| 301 return false; | 297 return false; |
| 302 | 298 |
| 303 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this); | 299 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this); |
| 304 return true; | 300 return true; |
| 305 } | 301 } |
| 306 | 302 |
| 307 void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() { | 303 void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() { |
| 308 SetCurrentConnectionType(RecomputeCurrentConnectionType()); | 304 SetCurrentConnectionType(RecomputeCurrentConnectionType()); |
| 309 bool current_offline = IsOffline(); | 305 bool current_offline = IsOffline(); |
| 310 offline_polls_++; | 306 offline_polls_++; |
| 311 // If we continue to appear offline, delay sending out the notification in | 307 // If we continue to appear offline, delay sending out the notification in |
| 312 // case we appear to go online within 20 seconds. UMA histogram data shows | 308 // case we appear to go online within 20 seconds. UMA histogram data shows |
| 313 // we may not detect the transition to online state after 1 second but within | 309 // we may not detect the transition to online state after 1 second but within |
| 314 // 20 seconds we generally do. | 310 // 20 seconds we generally do. |
| 315 if (last_announced_offline_ && current_offline && offline_polls_ <= 20) { | 311 if (last_announced_offline_ && current_offline && offline_polls_ <= 20) { |
| 316 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this, | 312 timer_.Start(FROM_HERE, |
| 313 base::TimeDelta::FromSeconds(1), |
| 314 this, |
| 317 &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange); | 315 &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange); |
| 318 return; | 316 return; |
| 319 } | 317 } |
| 320 if (last_announced_offline_) | 318 if (last_announced_offline_) |
| 321 UMA_HISTOGRAM_CUSTOM_COUNTS("NCN.OfflinePolls", offline_polls_, 1, 50, 50); | 319 UMA_HISTOGRAM_CUSTOM_COUNTS("NCN.OfflinePolls", offline_polls_, 1, 50, 50); |
| 322 last_announced_offline_ = current_offline; | 320 last_announced_offline_ = current_offline; |
| 323 | 321 |
| 324 NotifyObserversOfConnectionTypeChange(); | 322 NotifyObserversOfConnectionTypeChange(); |
| 325 } | 323 } |
| 326 | 324 |
| 327 } // namespace net | 325 } // namespace net |
| OLD | NEW |