Roll src/third_party/WebKit 8121bc6:918aba1 (svn 188871:188878)
[chromium-blink-merge.git] / net / base / network_change_notifier_win.h
blob9de1789f3d3ae550edaf2c729d39303b80b44695
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_
8 #include <windows.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"
20 namespace net {
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) {
29 public:
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();
41 protected:
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_; }
49 private:
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 // Does the actual work to determine the current connection type.
61 // It is not thread safe, see crbug.com/324913.
62 virtual ConnectionType RecomputeCurrentConnectionType() const;
64 void SetCurrentConnectionType(ConnectionType connection_type);
66 // Notifies IP address change observers of a change immediately, and notifies
67 // network state change observers on a delay. Must only be called on the
68 // thread |this| was created on.
69 void NotifyObservers();
71 // Forwards connection type notifications to parent class.
72 void NotifyParentOfConnectionTypeChange();
74 // Tries to start listening for a single subsequent address change. Returns
75 // false on failure. The caller is responsible for updating |is_watching_|.
76 // Virtual for unit tests. Must only be called on the thread |this| was
77 // created on.
78 virtual bool WatchForAddressChangeInternal();
80 static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsWin();
82 // All member variables may only be accessed on the thread |this| was created
83 // on.
85 // False when not currently watching for network change events. This only
86 // happens on initialization and when WatchForAddressChangeInternal fails and
87 // there is a pending task to try again. Needed for safe cleanup.
88 bool is_watching_;
90 base::win::ObjectWatcher addr_watcher_;
91 OVERLAPPED addr_overlapped_;
93 base::OneShotTimer<NetworkChangeNotifierWin> timer_;
95 // Number of times WatchForAddressChange has failed in a row.
96 int sequential_failures_;
98 // Thread on which we can run DnsConfigService.
99 scoped_ptr<DnsConfigServiceThread> dns_config_service_thread_;
101 mutable base::Lock last_computed_connection_type_lock_;
102 ConnectionType last_computed_connection_type_;
104 // Result of IsOffline() when NotifyObserversOfConnectionTypeChange()
105 // was last called.
106 bool last_announced_offline_;
107 // Number of times polled to check if still offline.
108 int offline_polls_;
110 // Used for calling WatchForAddressChange again on failure.
111 base::WeakPtrFactory<NetworkChangeNotifierWin> weak_factory_;
113 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin);
116 } // namespace net
118 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_