Chromecast Android buildfix: rework CommandLine initialization logic.
[chromium-blink-merge.git] / net / base / network_change_notifier_win.cc
blob4580fa8a1dc57523b5d894dab102fb28b02f42ce
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
3 // found in the LICENSE file.
5 #include "net/base/network_change_notifier_win.h"
7 #include <iphlpapi.h>
8 #include <winsock2.h>
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/profiler/scoped_profile.h"
14 #include "base/threading/thread.h"
15 #include "base/time/time.h"
16 #include "net/base/winsock_init.h"
17 #include "net/dns/dns_config_service.h"
19 #pragma comment(lib, "iphlpapi.lib")
21 namespace net {
23 namespace {
25 // Time between NotifyAddrChange retries, on failure.
26 const int kWatchForAddressChangeRetryIntervalMs = 500;
28 } // namespace
30 // Thread on which we can run DnsConfigService, which requires AssertIOAllowed
31 // to open registry keys and to handle FilePathWatcher updates.
32 class NetworkChangeNotifierWin::DnsConfigServiceThread : public base::Thread {
33 public:
34 DnsConfigServiceThread() : base::Thread("DnsConfigService") {}
36 virtual ~DnsConfigServiceThread() {
37 Stop();
40 virtual void Init() OVERRIDE {
41 service_ = DnsConfigService::CreateSystemService();
42 service_->WatchConfig(base::Bind(&NetworkChangeNotifier::SetDnsConfig));
45 virtual void CleanUp() OVERRIDE {
46 service_.reset();
49 private:
50 scoped_ptr<DnsConfigService> service_;
52 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread);
55 NetworkChangeNotifierWin::NetworkChangeNotifierWin()
56 : NetworkChangeNotifier(NetworkChangeCalculatorParamsWin()),
57 is_watching_(false),
58 sequential_failures_(0),
59 weak_factory_(this),
60 dns_config_service_thread_(new DnsConfigServiceThread()),
61 last_computed_connection_type_(RecomputeCurrentConnectionType()),
62 last_announced_offline_(
63 last_computed_connection_type_ == CONNECTION_NONE) {
64 memset(&addr_overlapped_, 0, sizeof addr_overlapped_);
65 addr_overlapped_.hEvent = WSACreateEvent();
68 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() {
69 if (is_watching_) {
70 CancelIPChangeNotify(&addr_overlapped_);
71 addr_watcher_.StopWatching();
73 WSACloseEvent(addr_overlapped_.hEvent);
76 // static
77 NetworkChangeNotifier::NetworkChangeCalculatorParams
78 NetworkChangeNotifierWin::NetworkChangeCalculatorParamsWin() {
79 NetworkChangeCalculatorParams params;
80 // Delay values arrived at by simple experimentation and adjusted so as to
81 // produce a single signal when switching between network connections.
82 params.ip_address_offline_delay_ = base::TimeDelta::FromMilliseconds(1500);
83 params.ip_address_online_delay_ = base::TimeDelta::FromMilliseconds(1500);
84 params.connection_type_offline_delay_ =
85 base::TimeDelta::FromMilliseconds(1500);
86 params.connection_type_online_delay_ = base::TimeDelta::FromMilliseconds(500);
87 return params;
90 // This implementation does not return the actual connection type but merely
91 // determines if the user is "online" (in which case it returns
92 // CONNECTION_UNKNOWN) or "offline" (and then it returns CONNECTION_NONE).
93 // This is challenging since the only thing we can test with certainty is
94 // whether a *particular* host is reachable.
96 // While we can't conclusively determine when a user is "online", we can at
97 // least reliably recognize some of the situtations when they are clearly
98 // "offline". For example, if the user's laptop is not plugged into an ethernet
99 // network and is not connected to any wireless networks, it must be offline.
101 // There are a number of different ways to implement this on Windows, each with
102 // their pros and cons. Here is a comparison of various techniques considered:
104 // (1) Use InternetGetConnectedState (wininet.dll). This function is really easy
105 // to use (literally a one-liner), and runs quickly. The drawback is it adds a
106 // dependency on the wininet DLL.
108 // (2) Enumerate all of the network interfaces using GetAdaptersAddresses
109 // (iphlpapi.dll), and assume we are "online" if there is at least one interface
110 // that is connected, and that interface is not a loopback or tunnel.
112 // Safari on Windows has a fairly simple implementation that does this:
113 // http://trac.webkit.org/browser/trunk/WebCore/platform/network/win/NetworkStateNotifierWin.cpp.
115 // Mozilla similarly uses this approach:
116 // http://mxr.mozilla.org/mozilla1.9.2/source/netwerk/system/win32/nsNotifyAddrListener.cpp
118 // The biggest drawback to this approach is it is quite complicated.
119 // WebKit's implementation for example doesn't seem to test for ICS gateways
120 // (internet connection sharing), whereas Mozilla's implementation has extra
121 // code to guess that.
123 // (3) The method used in this file comes from google talk, and is similar to
124 // method (2). The main difference is it enumerates the winsock namespace
125 // providers rather than the actual adapters.
127 // I ran some benchmarks comparing the performance of each on my Windows 7
128 // workstation. Here is what I found:
129 // * Approach (1) was pretty much zero-cost after the initial call.
130 // * Approach (2) took an average of 3.25 milliseconds to enumerate the
131 // adapters.
132 // * Approach (3) took an average of 0.8 ms to enumerate the providers.
134 // In terms of correctness, all three approaches were comparable for the simple
135 // experiments I ran... However none of them correctly returned "offline" when
136 // executing 'ipconfig /release'.
138 NetworkChangeNotifier::ConnectionType
139 NetworkChangeNotifierWin::RecomputeCurrentConnectionType() const {
140 DCHECK(CalledOnValidThread());
142 EnsureWinsockInit();
144 // The following code was adapted from:
145 // http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/net/notifier/base/win/async_network_alive_win32.cc?view=markup&pathrev=47343
146 // The main difference is we only call WSALookupServiceNext once, whereas
147 // the earlier code would traverse the entire list and pass LUP_FLUSHPREVIOUS
148 // to skip past the large results.
150 HANDLE ws_handle;
151 WSAQUERYSET query_set = {0};
152 query_set.dwSize = sizeof(WSAQUERYSET);
153 query_set.dwNameSpace = NS_NLA;
154 // Initiate a client query to iterate through the
155 // currently connected networks.
156 if (0 != WSALookupServiceBegin(&query_set, LUP_RETURN_ALL,
157 &ws_handle)) {
158 LOG(ERROR) << "WSALookupServiceBegin failed with: " << WSAGetLastError();
159 return NetworkChangeNotifier::CONNECTION_UNKNOWN;
162 bool found_connection = false;
164 // Retrieve the first available network. In this function, we only
165 // need to know whether or not there is network connection.
166 // 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
168 // set correct network status.
169 char result_buffer[sizeof(WSAQUERYSET) + 256] = {0};
170 DWORD length = sizeof(result_buffer);
171 reinterpret_cast<WSAQUERYSET*>(&result_buffer[0])->dwSize =
172 sizeof(WSAQUERYSET);
173 int result = WSALookupServiceNext(
174 ws_handle,
175 LUP_RETURN_NAME,
176 &length,
177 reinterpret_cast<WSAQUERYSET*>(&result_buffer[0]));
179 if (result == 0) {
180 // Found a connection!
181 found_connection = true;
182 } else {
183 DCHECK_EQ(SOCKET_ERROR, result);
184 result = WSAGetLastError();
186 // Error code WSAEFAULT means there is a network connection but the
187 // result_buffer size is too small to contain the results. The
188 // variable "length" returned from WSALookupServiceNext is the minimum
189 // number of bytes required. We do not need to retrieve detail info,
190 // it is enough knowing there was a connection.
191 if (result == WSAEFAULT) {
192 found_connection = true;
193 } else if (result == WSA_E_NO_MORE || result == WSAENOMORE) {
194 // There was nothing to iterate over!
195 } else {
196 LOG(WARNING) << "WSALookupServiceNext() failed with:" << result;
200 result = WSALookupServiceEnd(ws_handle);
201 LOG_IF(ERROR, result != 0)
202 << "WSALookupServiceEnd() failed with: " << result;
204 // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN.
205 return found_connection ? NetworkChangeNotifier::CONNECTION_UNKNOWN :
206 NetworkChangeNotifier::CONNECTION_NONE;
209 NetworkChangeNotifier::ConnectionType
210 NetworkChangeNotifierWin::GetCurrentConnectionType() const {
211 base::AutoLock auto_lock(last_computed_connection_type_lock_);
212 return last_computed_connection_type_;
215 void NetworkChangeNotifierWin::SetCurrentConnectionType(
216 ConnectionType connection_type) {
217 base::AutoLock auto_lock(last_computed_connection_type_lock_);
218 last_computed_connection_type_ = connection_type;
221 void NetworkChangeNotifierWin::OnObjectSignaled(HANDLE object) {
222 // TODO(vadimt): Remove ScopedProfile below once crbug.com/418183 is fixed.
223 tracked_objects::ScopedProfile tracking_profile(
224 FROM_HERE_WITH_EXPLICIT_FUNCTION(
225 "NetworkChangeNotifierWin_OnObjectSignaled"));
227 DCHECK(CalledOnValidThread());
228 DCHECK(is_watching_);
229 is_watching_ = false;
231 // Start watching for the next address change.
232 WatchForAddressChange();
234 NotifyObservers();
237 void NetworkChangeNotifierWin::NotifyObservers() {
238 DCHECK(CalledOnValidThread());
239 SetCurrentConnectionType(RecomputeCurrentConnectionType());
240 NotifyObserversOfIPAddressChange();
242 // Calling GetConnectionType() at this very moment is likely to give
243 // the wrong result, so we delay that until a little bit later.
245 // The one second delay chosen here was determined experimentally
246 // by adamk on Windows 7.
247 // If after one second we determine we are still offline, we will
248 // delay again.
249 offline_polls_ = 0;
250 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this,
251 &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange);
254 void NetworkChangeNotifierWin::WatchForAddressChange() {
255 DCHECK(CalledOnValidThread());
256 DCHECK(!is_watching_);
258 // NotifyAddrChange occasionally fails with ERROR_OPEN_FAILED for unknown
259 // reasons. More rarely, it's also been observed failing with
260 // ERROR_NO_SYSTEM_RESOURCES. When either of these happens, we retry later.
261 if (!WatchForAddressChangeInternal()) {
262 ++sequential_failures_;
264 // TODO(mmenke): If the UMA histograms indicate that this fixes
265 // http://crbug.com/69198, remove this histogram and consider reducing the
266 // retry interval.
267 if (sequential_failures_ == 2000) {
268 UMA_HISTOGRAM_COUNTS_10000("Net.NotifyAddrChangeFailures",
269 sequential_failures_);
272 base::MessageLoop::current()->PostDelayedTask(
273 FROM_HERE,
274 base::Bind(&NetworkChangeNotifierWin::WatchForAddressChange,
275 weak_factory_.GetWeakPtr()),
276 base::TimeDelta::FromMilliseconds(
277 kWatchForAddressChangeRetryIntervalMs));
278 return;
281 // Treat the transition from NotifyAddrChange failing to succeeding as a
282 // network change event, since network changes were not being observed in
283 // that interval.
284 if (sequential_failures_ > 0)
285 NotifyObservers();
287 if (sequential_failures_ < 2000) {
288 UMA_HISTOGRAM_COUNTS_10000("Net.NotifyAddrChangeFailures",
289 sequential_failures_);
292 is_watching_ = true;
293 sequential_failures_ = 0;
296 bool NetworkChangeNotifierWin::WatchForAddressChangeInternal() {
297 DCHECK(CalledOnValidThread());
299 if (!dns_config_service_thread_->IsRunning()) {
300 dns_config_service_thread_->StartWithOptions(
301 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
304 HANDLE handle = NULL;
305 DWORD ret = NotifyAddrChange(&handle, &addr_overlapped_);
306 if (ret != ERROR_IO_PENDING)
307 return false;
309 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this);
310 return true;
313 void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() {
314 SetCurrentConnectionType(RecomputeCurrentConnectionType());
315 bool current_offline = IsOffline();
316 offline_polls_++;
317 // 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
319 // we may not detect the transition to online state after 1 second but within
320 // 20 seconds we generally do.
321 if (last_announced_offline_ && current_offline && offline_polls_ <= 20) {
322 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this,
323 &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange);
324 return;
326 if (last_announced_offline_)
327 UMA_HISTOGRAM_CUSTOM_COUNTS("NCN.OfflinePolls", offline_polls_, 1, 50, 50);
328 last_announced_offline_ = current_offline;
330 NotifyObserversOfConnectionTypeChange();
333 } // namespace net