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

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: sync 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 // 77 //
78 // I ran some benchmarks comparing the performance of each on my Windows 7 78 // I ran some benchmarks comparing the performance of each on my Windows 7
79 // workstation. Here is what I found: 79 // workstation. Here is what I found:
80 // * Approach (1) was pretty much zero-cost after the initial call. 80 // * Approach (1) was pretty much zero-cost after the initial call.
81 // * Approach (2) took an average of 3.25 milliseconds to enumerate the 81 // * Approach (2) took an average of 3.25 milliseconds to enumerate the
82 // adapters. 82 // adapters.
83 // * Approach (3) took an average of 0.8 ms to enumerate the providers. 83 // * Approach (3) took an average of 0.8 ms to enumerate the providers.
84 // 84 //
85 // In terms of correctness, all three approaches were comparable for the simple 85 // In terms of correctness, all three approaches were comparable for the simple
86 // experiments I ran... However none of them correctly returned "offline" when 86 // experiments I ran... However none of them correctly returned "offline" when
87 // executing 'ipconfig /release'. 87 // executing 'ipconfig /release'.
wtc 2012/05/11 01:35:05 This whole comment block needs to be updated becau
88 // 88 //
89 bool NetworkChangeNotifierWin::IsCurrentlyOffline() const { 89 NetworkChangeNotifier::ConnectionType
90 NetworkChangeNotifierWin::GetCurrentConnectionType() const {
90 91
91 // TODO(eroman): We could cache this value, and only re-calculate it on 92 // TODO(eroman): We could cache this value, and only re-calculate it on
92 // network changes. For now we recompute it each time asked, 93 // network changes. For now we recompute it each time asked,
93 // since it is relatively fast (sub 1ms) and not called often. 94 // since it is relatively fast (sub 1ms) and not called often.
94 95
95 EnsureWinsockInit(); 96 EnsureWinsockInit();
96 97
97 // The following code was adapted from: 98 // The following code was adapted from:
98 // http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/net/notifier/ base/win/async_network_alive_win32.cc?view=markup&pathrev=47343 99 // http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/net/notifier/ base/win/async_network_alive_win32.cc?view=markup&pathrev=47343
99 // The main difference is we only call WSALookupServiceNext once, whereas 100 // The main difference is we only call WSALookupServiceNext once, whereas
100 // the earlier code would traverse the entire list and pass LUP_FLUSHPREVIOUS 101 // the earlier code would traverse the entire list and pass LUP_FLUSHPREVIOUS
101 // to skip past the large results. 102 // to skip past the large results.
102 103
103 HANDLE ws_handle; 104 HANDLE ws_handle;
104 WSAQUERYSET query_set = {0}; 105 WSAQUERYSET query_set = {0};
105 query_set.dwSize = sizeof(WSAQUERYSET); 106 query_set.dwSize = sizeof(WSAQUERYSET);
106 query_set.dwNameSpace = NS_NLA; 107 query_set.dwNameSpace = NS_NLA;
107 // Initiate a client query to iterate through the 108 // Initiate a client query to iterate through the
108 // currently connected networks. 109 // currently connected networks.
109 if (0 != WSALookupServiceBegin(&query_set, LUP_RETURN_ALL, 110 if (0 != WSALookupServiceBegin(&query_set, LUP_RETURN_ALL,
110 &ws_handle)) { 111 &ws_handle)) {
111 LOG(ERROR) << "WSALookupServiceBegin failed with: " << WSAGetLastError(); 112 LOG(ERROR) << "WSALookupServiceBegin failed with: " << WSAGetLastError();
112 return false; 113 return NetworkChangeNotifier::CONNECTION_NONE;
wtc 2012/05/11 01:35:05 We should return NetworkChangeNotifier::CONNECTION
113 } 114 }
114 115
115 bool found_connection = false; 116 bool found_connection = false;
116 117
117 // Retrieve the first available network. In this function, we only 118 // Retrieve the first available network. In this function, we only
118 // need to know whether or not there is network connection. 119 // need to know whether or not there is network connection.
119 // Allocate 256 bytes for name, it should be enough for most cases. 120 // Allocate 256 bytes for name, it should be enough for most cases.
120 // If the name is longer, it is OK as we will check the code returned and 121 // If the name is longer, it is OK as we will check the code returned and
121 // set correct network status. 122 // set correct network status.
122 char result_buffer[sizeof(WSAQUERYSET) + 256] = {0}; 123 char result_buffer[sizeof(WSAQUERYSET) + 256] = {0};
(...skipping 24 matching lines...) Expand all
147 // There was nothing to iterate over! 148 // There was nothing to iterate over!
148 } else { 149 } else {
149 LOG(WARNING) << "WSALookupServiceNext() failed with:" << result; 150 LOG(WARNING) << "WSALookupServiceNext() failed with:" << result;
150 } 151 }
151 } 152 }
152 153
153 result = WSALookupServiceEnd(ws_handle); 154 result = WSALookupServiceEnd(ws_handle);
154 LOG_IF(ERROR, result != 0) 155 LOG_IF(ERROR, result != 0)
155 << "WSALookupServiceEnd() failed with: " << result; 156 << "WSALookupServiceEnd() failed with: " << result;
156 157
157 return !found_connection; 158 return found_connection ? NetworkChangeNotifier::CONNECTION_ETHERNET :
wtc 2012/05/11 01:35:05 Add a TODO comment here, because you're returning
159 NetworkChangeNotifier::CONNECTION_NONE;
158 } 160 }
159 161
160 void NetworkChangeNotifierWin::OnObjectSignaled(HANDLE object) { 162 void NetworkChangeNotifierWin::OnObjectSignaled(HANDLE object) {
161 DCHECK(CalledOnValidThread()); 163 DCHECK(CalledOnValidThread());
162 DCHECK(is_watching_); 164 DCHECK(is_watching_);
163 is_watching_ = false; 165 is_watching_ = false;
164 166
165 // Start watching for the next address change. 167 // Start watching for the next address change.
166 WatchForAddressChange(); 168 WatchForAddressChange();
167 169
168 NotifyObservers(); 170 NotifyObservers();
169 } 171 }
170 172
171 void NetworkChangeNotifierWin::NotifyObservers() { 173 void NetworkChangeNotifierWin::NotifyObservers() {
172 DCHECK(CalledOnValidThread()); 174 DCHECK(CalledOnValidThread());
173 NotifyObserversOfIPAddressChange(); 175 NotifyObserversOfIPAddressChange();
174 176
175 // Calling IsOffline() at this very moment is likely to give 177 // Calling GetConnectionType() at this very moment is likely to give
176 // the wrong result, so we delay that until a little bit later. 178 // the wrong result, so we delay that until a little bit later.
177 // 179 //
178 // The one second delay chosen here was determined experimentally 180 // The one second delay chosen here was determined experimentally
179 // by adamk on Windows 7. 181 // by adamk on Windows 7.
180 timer_.Stop(); // cancel any already waiting notification 182 timer_.Stop(); // cancel any already waiting notification
181 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this, 183 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this,
182 &NetworkChangeNotifierWin::NotifyParentOfOnlineStateChange); 184 &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange);
183 } 185 }
184 186
185 void NetworkChangeNotifierWin::WatchForAddressChange() { 187 void NetworkChangeNotifierWin::WatchForAddressChange() {
186 DCHECK(CalledOnValidThread()); 188 DCHECK(CalledOnValidThread());
187 DCHECK(!is_watching_); 189 DCHECK(!is_watching_);
188 190
189 // NotifyAddrChange occasionally fails with ERROR_OPEN_FAILED for unknown 191 // NotifyAddrChange occasionally fails with ERROR_OPEN_FAILED for unknown
190 // reasons. More rarely, it's also been observed failing with 192 // reasons. More rarely, it's also been observed failing with
191 // ERROR_NO_SYSTEM_RESOURCES. When either of these happens, we retry later. 193 // ERROR_NO_SYSTEM_RESOURCES. When either of these happens, we retry later.
192 if (!WatchForAddressChangeInternal()) { 194 if (!WatchForAddressChangeInternal()) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 DCHECK(CalledOnValidThread()); 230 DCHECK(CalledOnValidThread());
229 HANDLE handle = NULL; 231 HANDLE handle = NULL;
230 DWORD ret = NotifyAddrChange(&handle, &addr_overlapped_); 232 DWORD ret = NotifyAddrChange(&handle, &addr_overlapped_);
231 if (ret != ERROR_IO_PENDING) 233 if (ret != ERROR_IO_PENDING)
232 return false; 234 return false;
233 235
234 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this); 236 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this);
235 return true; 237 return true;
236 } 238 }
237 239
238 void NetworkChangeNotifierWin::NotifyParentOfOnlineStateChange() { 240 void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() {
239 NotifyObserversOfOnlineStateChange(); 241 NotifyObserversOfConnectionTypeChange();
240 } 242 }
241 243
242 } // namespace net 244 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698