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 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_
6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/timer/timer.h"
16 #include "base/win/object_watcher.h"
17 #include "net/base/net_export.h"
18 #include "net/base/network_change_notifier.h"
22 // NetworkChangeNotifierWin inherits from NonThreadSafe, as all its internal
23 // notification code must be called on the thread it is created and destroyed
24 // on. All the NetworkChangeNotifier methods it implements are threadsafe.
25 class NET_EXPORT_PRIVATE NetworkChangeNotifierWin
26 : public NetworkChangeNotifier
,
27 public base::win::ObjectWatcher::Delegate
,
28 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
30 NetworkChangeNotifierWin();
32 // Begins listening for a single subsequent address change. If it fails to
33 // start watching, it retries on a timer. Must be called only once, on the
34 // thread |this| was created on. This cannot be called in the constructor, as
35 // WatchForAddressChangeInternal is mocked out in unit tests.
36 // TODO(mmenke): Consider making this function a part of the
37 // NetworkChangeNotifier interface, so other subclasses can be
38 // unit tested in similar fashion, as needed.
39 void WatchForAddressChange();
42 virtual ~NetworkChangeNotifierWin();
44 // For unit tests only.
45 bool is_watching() { return is_watching_
; }
46 void set_is_watching(bool is_watching
) { is_watching_
= is_watching
; }
47 int sequential_failures() { return sequential_failures_
; }
50 class DnsConfigServiceThread
;
51 friend class NetworkChangeNotifierWinTest
;
53 // NetworkChangeNotifier methods:
54 virtual ConnectionType
GetCurrentConnectionType() const OVERRIDE
;
56 // ObjectWatcher::Delegate methods:
57 // Must only be called on the thread |this| was created on.
58 virtual void OnObjectSignaled(HANDLE object
) OVERRIDE
;
60 // Notifies IP address change observers of a change immediately, and notifies
61 // network state change observers on a delay. Must only be called on the
62 // thread |this| was created on.
63 void NotifyObservers();
65 // Forwards connection type notifications to parent class.
66 void NotifyParentOfConnectionTypeChange();
68 // Tries to start listening for a single subsequent address change. Returns
69 // false on failure. The caller is responsible for updating |is_watching_|.
70 // Virtual for unit tests. Must only be called on the thread |this| was
72 virtual bool WatchForAddressChangeInternal();
74 static NetworkChangeCalculatorParams
NetworkChangeCalculatorParamsWin();
76 // All member variables may only be accessed on the thread |this| was created
79 // False when not currently watching for network change events. This only
80 // happens on initialization and when WatchForAddressChangeInternal fails and
81 // there is a pending task to try again. Needed for safe cleanup.
84 base::win::ObjectWatcher addr_watcher_
;
85 OVERLAPPED addr_overlapped_
;
87 base::OneShotTimer
<NetworkChangeNotifierWin
> timer_
;
89 // Number of times WatchForAddressChange has failed in a row.
90 int sequential_failures_
;
92 // Used for calling WatchForAddressChange again on failure.
93 base::WeakPtrFactory
<NetworkChangeNotifierWin
> weak_factory_
;
95 // Thread on which we can run DnsConfigService.
96 scoped_ptr
<DnsConfigServiceThread
> dns_config_service_thread_
;
98 // Result of IsOffline() when NotifyObserversOfConnectionTypeChange()
100 bool last_announced_offline_
;
101 // Number of times polled to check if still offline.
104 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin
);
109 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_