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