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

Side by Side Diff: net/base/network_change_notifier_win.cc

Issue 9147026: API for connection type (Ethernet/WIFI/WWAN ...) in NetworkChangeNotifier. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed a typo Created 8 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 unified diff | Download patch
OLDNEW
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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 } 62 }
63 63
64 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() { 64 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() {
65 if (is_watching_) { 65 if (is_watching_) {
66 CancelIPChangeNotify(&addr_overlapped_); 66 CancelIPChangeNotify(&addr_overlapped_);
67 addr_watcher_.StopWatching(); 67 addr_watcher_.StopWatching();
68 } 68 }
69 WSACloseEvent(addr_overlapped_.hEvent); 69 WSACloseEvent(addr_overlapped_.hEvent);
70 } 70 }
71 71
72 // Conceptually we would like to tell whether the user is "online" verus 72 // This implementation does not return the actual connection type but merely
73 // "offline". This is challenging since the only thing we can test with 73 // determines if the user is "online" (in which case it returns
74 // certainty is whether a *particular* host is reachable. 74 // CONNECTION_UNKNOWN) or "offline" (and then it returns CONNECTION_NONE).
75 // This is challenging since the only thing we can test with certainty is
76 // whether a *particular* host is reachable.
75 // 77 //
76 // While we can't conclusively determine when a user is "online", we can at 78 // While we can't conclusively determine when a user is "online", we can at
77 // least reliably recognize some of the situtations when they are clearly 79 // least reliably recognize some of the situtations when they are clearly
78 // "offline". For example, if the user's laptop is not plugged into an ethernet 80 // "offline". For example, if the user's laptop is not plugged into an ethernet
79 // network and is not connected to any wireless networks, it must be offline. 81 // network and is not connected to any wireless networks, it must be offline.
80 // 82 //
81 // There are a number of different ways to implement this on Windows, each with 83 // There are a number of different ways to implement this on Windows, each with
82 // their pros and cons. Here is a comparison of various techniques considered: 84 // their pros and cons. Here is a comparison of various techniques considered:
83 // 85 //
84 // (1) Use InternetGetConnectedState (wininet.dll). This function is really easy 86 // (1) Use InternetGetConnectedState (wininet.dll). This function is really easy
(...skipping 23 matching lines...) Expand all
108 // workstation. Here is what I found: 110 // workstation. Here is what I found:
109 // * Approach (1) was pretty much zero-cost after the initial call. 111 // * Approach (1) was pretty much zero-cost after the initial call.
110 // * Approach (2) took an average of 3.25 milliseconds to enumerate the 112 // * Approach (2) took an average of 3.25 milliseconds to enumerate the
111 // adapters. 113 // adapters.
112 // * Approach (3) took an average of 0.8 ms to enumerate the providers. 114 // * Approach (3) took an average of 0.8 ms to enumerate the providers.
113 // 115 //
114 // In terms of correctness, all three approaches were comparable for the simple 116 // In terms of correctness, all three approaches were comparable for the simple
115 // experiments I ran... However none of them correctly returned "offline" when 117 // experiments I ran... However none of them correctly returned "offline" when
116 // executing 'ipconfig /release'. 118 // executing 'ipconfig /release'.
117 // 119 //
118 bool NetworkChangeNotifierWin::IsCurrentlyOffline() const { 120 NetworkChangeNotifier::ConnectionType
121 NetworkChangeNotifierWin::GetCurrentConnectionType() const {
119 122
120 // TODO(eroman): We could cache this value, and only re-calculate it on 123 // TODO(eroman): We could cache this value, and only re-calculate it on
121 // network changes. For now we recompute it each time asked, 124 // network changes. For now we recompute it each time asked,
122 // since it is relatively fast (sub 1ms) and not called often. 125 // since it is relatively fast (sub 1ms) and not called often.
123 126
124 EnsureWinsockInit(); 127 EnsureWinsockInit();
125 128
126 // The following code was adapted from: 129 // The following code was adapted from:
127 // http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/net/notifier/ base/win/async_network_alive_win32.cc?view=markup&pathrev=47343 130 // http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/net/notifier/ base/win/async_network_alive_win32.cc?view=markup&pathrev=47343
128 // The main difference is we only call WSALookupServiceNext once, whereas 131 // The main difference is we only call WSALookupServiceNext once, whereas
129 // the earlier code would traverse the entire list and pass LUP_FLUSHPREVIOUS 132 // the earlier code would traverse the entire list and pass LUP_FLUSHPREVIOUS
130 // to skip past the large results. 133 // to skip past the large results.
131 134
132 HANDLE ws_handle; 135 HANDLE ws_handle;
133 WSAQUERYSET query_set = {0}; 136 WSAQUERYSET query_set = {0};
134 query_set.dwSize = sizeof(WSAQUERYSET); 137 query_set.dwSize = sizeof(WSAQUERYSET);
135 query_set.dwNameSpace = NS_NLA; 138 query_set.dwNameSpace = NS_NLA;
136 // Initiate a client query to iterate through the 139 // Initiate a client query to iterate through the
137 // currently connected networks. 140 // currently connected networks.
138 if (0 != WSALookupServiceBegin(&query_set, LUP_RETURN_ALL, 141 if (0 != WSALookupServiceBegin(&query_set, LUP_RETURN_ALL,
139 &ws_handle)) { 142 &ws_handle)) {
140 LOG(ERROR) << "WSALookupServiceBegin failed with: " << WSAGetLastError(); 143 LOG(ERROR) << "WSALookupServiceBegin failed with: " << WSAGetLastError();
141 return false; 144 return NetworkChangeNotifier::CONNECTION_UNKNOWN;
142 } 145 }
143 146
144 bool found_connection = false; 147 bool found_connection = false;
145 148
146 // Retrieve the first available network. In this function, we only 149 // Retrieve the first available network. In this function, we only
147 // need to know whether or not there is network connection. 150 // need to know whether or not there is network connection.
148 // Allocate 256 bytes for name, it should be enough for most cases. 151 // Allocate 256 bytes for name, it should be enough for most cases.
149 // If the name is longer, it is OK as we will check the code returned and 152 // If the name is longer, it is OK as we will check the code returned and
150 // set correct network status. 153 // set correct network status.
151 char result_buffer[sizeof(WSAQUERYSET) + 256] = {0}; 154 char result_buffer[sizeof(WSAQUERYSET) + 256] = {0};
(...skipping 24 matching lines...) Expand all
176 // There was nothing to iterate over! 179 // There was nothing to iterate over!
177 } else { 180 } else {
178 LOG(WARNING) << "WSALookupServiceNext() failed with:" << result; 181 LOG(WARNING) << "WSALookupServiceNext() failed with:" << result;
179 } 182 }
180 } 183 }
181 184
182 result = WSALookupServiceEnd(ws_handle); 185 result = WSALookupServiceEnd(ws_handle);
183 LOG_IF(ERROR, result != 0) 186 LOG_IF(ERROR, result != 0)
184 << "WSALookupServiceEnd() failed with: " << result; 187 << "WSALookupServiceEnd() failed with: " << result;
185 188
186 return !found_connection; 189 // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN.
190 return found_connection ? NetworkChangeNotifier::CONNECTION_UNKNOWN :
191 NetworkChangeNotifier::CONNECTION_NONE;
187 } 192 }
188 193
189 void NetworkChangeNotifierWin::OnObjectSignaled(HANDLE object) { 194 void NetworkChangeNotifierWin::OnObjectSignaled(HANDLE object) {
190 DCHECK(CalledOnValidThread()); 195 DCHECK(CalledOnValidThread());
191 DCHECK(is_watching_); 196 DCHECK(is_watching_);
192 is_watching_ = false; 197 is_watching_ = false;
193 198
194 // Start watching for the next address change. 199 // Start watching for the next address change.
195 WatchForAddressChange(); 200 WatchForAddressChange();
196 201
197 NotifyObservers(); 202 NotifyObservers();
198 } 203 }
199 204
200 void NetworkChangeNotifierWin::NotifyObservers() { 205 void NetworkChangeNotifierWin::NotifyObservers() {
201 DCHECK(CalledOnValidThread()); 206 DCHECK(CalledOnValidThread());
202 NotifyObserversOfIPAddressChange(); 207 NotifyObserversOfIPAddressChange();
203 208
204 // Calling IsOffline() at this very moment is likely to give 209 // Calling GetConnectionType() at this very moment is likely to give
205 // the wrong result, so we delay that until a little bit later. 210 // the wrong result, so we delay that until a little bit later.
206 // 211 //
207 // The one second delay chosen here was determined experimentally 212 // The one second delay chosen here was determined experimentally
208 // by adamk on Windows 7. 213 // by adamk on Windows 7.
209 timer_.Stop(); // cancel any already waiting notification 214 timer_.Stop(); // cancel any already waiting notification
210 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this, 215 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this,
211 &NetworkChangeNotifierWin::NotifyParentOfOnlineStateChange); 216 &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange);
212 } 217 }
213 218
214 void NetworkChangeNotifierWin::WatchForAddressChange() { 219 void NetworkChangeNotifierWin::WatchForAddressChange() {
215 DCHECK(CalledOnValidThread()); 220 DCHECK(CalledOnValidThread());
216 DCHECK(!is_watching_); 221 DCHECK(!is_watching_);
217 222
218 // NotifyAddrChange occasionally fails with ERROR_OPEN_FAILED for unknown 223 // NotifyAddrChange occasionally fails with ERROR_OPEN_FAILED for unknown
219 // reasons. More rarely, it's also been observed failing with 224 // reasons. More rarely, it's also been observed failing with
220 // ERROR_NO_SYSTEM_RESOURCES. When either of these happens, we retry later. 225 // ERROR_NO_SYSTEM_RESOURCES. When either of these happens, we retry later.
221 if (!WatchForAddressChangeInternal()) { 226 if (!WatchForAddressChangeInternal()) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 DCHECK(CalledOnValidThread()); 262 DCHECK(CalledOnValidThread());
258 HANDLE handle = NULL; 263 HANDLE handle = NULL;
259 DWORD ret = NotifyAddrChange(&handle, &addr_overlapped_); 264 DWORD ret = NotifyAddrChange(&handle, &addr_overlapped_);
260 if (ret != ERROR_IO_PENDING) 265 if (ret != ERROR_IO_PENDING)
261 return false; 266 return false;
262 267
263 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this); 268 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this);
264 return true; 269 return true;
265 } 270 }
266 271
267 void NetworkChangeNotifierWin::NotifyParentOfOnlineStateChange() { 272 void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() {
268 NotifyObserversOfOnlineStateChange(); 273 NotifyObserversOfConnectionTypeChange();
269 } 274 }
270 275
271 } // namespace net 276 } // namespace net
OLDNEW
« no previous file with comments | « net/base/network_change_notifier_win.h ('k') | net/base/network_change_notifier_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698