OLD | NEW |
---|---|
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 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ | 5 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ |
6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ | 6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/callback.h" | |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
15 #include "base/threading/non_thread_safe.h" | 17 #include "base/threading/non_thread_safe.h" |
16 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
17 #include "base/win/object_watcher.h" | 19 #include "base/win/object_watcher.h" |
18 #include "net/base/net_export.h" | 20 #include "net/base/net_export.h" |
19 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
20 | 22 |
23 namespace base { | |
24 class SingleThreadTaskRunner; | |
25 } // namespace base | |
26 | |
21 namespace net { | 27 namespace net { |
22 | 28 |
23 // NetworkChangeNotifierWin inherits from NonThreadSafe, as all its internal | 29 // NetworkChangeNotifierWin inherits from NonThreadSafe, as all its internal |
24 // notification code must be called on the thread it is created and destroyed | 30 // notification code must be called on the thread it is created and destroyed |
25 // on. All the NetworkChangeNotifier methods it implements are threadsafe. | 31 // on. All the NetworkChangeNotifier methods it implements are threadsafe. |
26 class NET_EXPORT_PRIVATE NetworkChangeNotifierWin | 32 class NET_EXPORT_PRIVATE NetworkChangeNotifierWin |
27 : public NetworkChangeNotifier, | 33 : public NetworkChangeNotifier, |
28 public base::win::ObjectWatcher::Delegate, | 34 public base::win::ObjectWatcher::Delegate, |
29 NON_EXPORTED_BASE(public base::NonThreadSafe) { | 35 NON_EXPORTED_BASE(public base::NonThreadSafe) { |
30 public: | 36 public: |
(...skipping 12 matching lines...) Expand all Loading... | |
43 ~NetworkChangeNotifierWin() override; | 49 ~NetworkChangeNotifierWin() override; |
44 | 50 |
45 // For unit tests only. | 51 // For unit tests only. |
46 bool is_watching() { return is_watching_; } | 52 bool is_watching() { return is_watching_; } |
47 void set_is_watching(bool is_watching) { is_watching_ = is_watching; } | 53 void set_is_watching(bool is_watching) { is_watching_ = is_watching; } |
48 int sequential_failures() { return sequential_failures_; } | 54 int sequential_failures() { return sequential_failures_; } |
49 | 55 |
50 private: | 56 private: |
51 class DnsConfigServiceThread; | 57 class DnsConfigServiceThread; |
52 friend class NetworkChangeNotifierWinTest; | 58 friend class NetworkChangeNotifierWinTest; |
59 friend class TestNetworkChangeNotifierWin; | |
53 | 60 |
54 // NetworkChangeNotifier methods: | 61 // NetworkChangeNotifier methods: |
55 ConnectionType GetCurrentConnectionType() const override; | 62 ConnectionType GetCurrentConnectionType() const override; |
56 | 63 |
57 // ObjectWatcher::Delegate methods: | 64 // ObjectWatcher::Delegate methods: |
58 // Must only be called on the thread |this| was created on. | 65 // Must only be called on the thread |this| was created on. |
59 void OnObjectSignaled(HANDLE object) override; | 66 void OnObjectSignaled(HANDLE object) override; |
60 | 67 |
61 // Does the actual work to determine the current connection type. | |
62 // It is not thread safe, see crbug.com/324913. | |
63 virtual ConnectionType RecomputeCurrentConnectionType() const; | |
64 | |
65 void SetCurrentConnectionType(ConnectionType connection_type); | 68 void SetCurrentConnectionType(ConnectionType connection_type); |
66 | 69 |
67 // Notifies IP address change observers of a change immediately, and notifies | 70 // Notifies IP address change observers of a change immediately, and notifies |
68 // network state change observers on a delay. Must only be called on the | 71 // network state change observers on a delay. Must only be called on the |
69 // thread |this| was created on. | 72 // thread |this| was created on. |
70 void NotifyObservers(); | 73 void NotifyObservers(ConnectionType connection_type); |
71 | 74 |
72 // Forwards connection type notifications to parent class. | 75 // Forwards connection type notifications to parent class. |
73 void NotifyParentOfConnectionTypeChange(); | 76 void NotifyParentOfConnectionTypeChange(); |
77 void NotifyParentOfConnectionTypeChangeImpl(ConnectionType connection_type); | |
74 | 78 |
75 // Tries to start listening for a single subsequent address change. Returns | 79 // Tries to start listening for a single subsequent address change. Returns |
76 // false on failure. The caller is responsible for updating |is_watching_|. | 80 // false on failure. The caller is responsible for updating |is_watching_|. |
77 // Virtual for unit tests. Must only be called on the thread |this| was | 81 // Virtual for unit tests. Must only be called on the thread |this| was |
78 // created on. | 82 // created on. |
79 virtual bool WatchForAddressChangeInternal(); | 83 virtual bool WatchForAddressChangeInternal(); |
80 | 84 |
81 static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsWin(); | 85 static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsWin(); |
82 | 86 |
83 // All member variables may only be accessed on the thread |this| was created | 87 // All member variables may only be accessed on the thread |this| was created |
84 // on. | 88 // on. |
85 | 89 |
86 // False when not currently watching for network change events. This only | 90 // False when not currently watching for network change events. This only |
87 // happens on initialization and when WatchForAddressChangeInternal fails and | 91 // happens on initialization and when WatchForAddressChangeInternal fails and |
88 // there is a pending task to try again. Needed for safe cleanup. | 92 // there is a pending task to try again. Needed for safe cleanup. |
89 bool is_watching_; | 93 bool is_watching_; |
90 | 94 |
91 base::win::ObjectWatcher addr_watcher_; | 95 base::win::ObjectWatcher addr_watcher_; |
92 OVERLAPPED addr_overlapped_; | 96 OVERLAPPED addr_overlapped_; |
93 | 97 |
94 base::OneShotTimer timer_; | 98 base::OneShotTimer timer_; |
95 | 99 |
96 // Number of times WatchForAddressChange has failed in a row. | 100 // Number of times WatchForAddressChange has failed in a row. |
97 int sequential_failures_; | 101 int sequential_failures_; |
98 | 102 |
99 // Thread on which we can run DnsConfigService. | 103 // Thread on which we can run DnsConfigService. |
100 std::unique_ptr<DnsConfigServiceThread> dns_config_service_thread_; | 104 std::unique_ptr<DnsConfigServiceThread> dns_config_service_thread_; |
105 scoped_refptr<base::SingleThreadTaskRunner> dns_task_runner_; | |
pauljensen
2017/05/22 18:01:51
Hmm, can we avoid dns_task_runner_ and calculate_c
jkarlin
2017/05/23 11:44:38
We could get rid of dns_task_runner_ that way I su
pauljensen
2017/05/23 14:55:50
Why would DnsConfigServiceThread need to be ref co
jkarlin
2017/05/23 15:11:06
Ah, you're right. All tasks on the dns thread will
| |
101 | 106 |
102 mutable base::Lock last_computed_connection_type_lock_; | 107 mutable base::Lock last_computed_connection_type_lock_; |
103 ConnectionType last_computed_connection_type_; | 108 ConnectionType last_computed_connection_type_; |
104 | 109 |
105 // Result of IsOffline() when NotifyObserversOfConnectionTypeChange() | 110 // Result of IsOffline() when NotifyObserversOfConnectionTypeChange() |
106 // was last called. | 111 // was last called. |
107 bool last_announced_offline_; | 112 bool last_announced_offline_; |
108 // Number of times polled to check if still offline. | 113 // Number of times polled to check if still offline. |
109 int offline_polls_; | 114 int offline_polls_; |
110 | 115 |
116 // The callback run to calculate connection type. | |
117 base::Callback<ConnectionType()> calculate_connection_type_; | |
118 | |
111 // Used for calling WatchForAddressChange again on failure. | 119 // Used for calling WatchForAddressChange again on failure. |
112 base::WeakPtrFactory<NetworkChangeNotifierWin> weak_factory_; | 120 base::WeakPtrFactory<NetworkChangeNotifierWin> weak_factory_; |
113 | 121 |
114 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin); | 122 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin); |
115 }; | 123 }; |
116 | 124 |
117 } // namespace net | 125 } // namespace net |
118 | 126 |
119 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ | 127 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ |
OLD | NEW |