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