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. | 68 // Does the actual work to determine the current connection type. |
62 // It is not thread safe, see crbug.com/324913. | 69 // It is not thread safe, see crbug.com/324913. |
63 virtual ConnectionType RecomputeCurrentConnectionType() const; | 70 static ConnectionType RecomputeCurrentConnectionType(); |
pauljensen
2017/05/23 17:57:40
can we leave this virtual since NCNWin outlives dn
jkarlin
2017/05/23 18:02:15
NCNWin outlives dns thread, but there is no guaran
pauljensen
2017/05/23 18:05:47
can it post back with a weakptr?
pauljensen
2017/05/23 18:06:47
I think this is very similar to the example here:
jkarlin
2017/05/26 15:09:55
Back to the original.
| |
64 | 71 |
65 void SetCurrentConnectionType(ConnectionType connection_type); | 72 void SetCurrentConnectionType(ConnectionType connection_type); |
66 | 73 |
67 // Notifies IP address change observers of a change immediately, and notifies | 74 // 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 | 75 // network state change observers on a delay. Must only be called on the |
69 // thread |this| was created on. | 76 // thread |this| was created on. |
70 void NotifyObservers(); | 77 void NotifyObservers(ConnectionType connection_type); |
71 | 78 |
72 // Forwards connection type notifications to parent class. | 79 // Forwards connection type notifications to parent class. |
73 void NotifyParentOfConnectionTypeChange(); | 80 void NotifyParentOfConnectionTypeChange(); |
81 void NotifyParentOfConnectionTypeChangeImpl(ConnectionType connection_type); | |
74 | 82 |
75 // Tries to start listening for a single subsequent address change. Returns | 83 // Tries to start listening for a single subsequent address change. Returns |
76 // false on failure. The caller is responsible for updating |is_watching_|. | 84 // 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 | 85 // Virtual for unit tests. Must only be called on the thread |this| was |
78 // created on. | 86 // created on. |
79 virtual bool WatchForAddressChangeInternal(); | 87 virtual bool WatchForAddressChangeInternal(); |
80 | 88 |
81 static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsWin(); | 89 static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsWin(); |
82 | 90 |
83 // All member variables may only be accessed on the thread |this| was created | 91 // All member variables may only be accessed on the thread |this| was created |
84 // on. | 92 // on. |
85 | 93 |
86 // False when not currently watching for network change events. This only | 94 // False when not currently watching for network change events. This only |
87 // happens on initialization and when WatchForAddressChangeInternal fails and | 95 // happens on initialization and when WatchForAddressChangeInternal fails and |
88 // there is a pending task to try again. Needed for safe cleanup. | 96 // there is a pending task to try again. Needed for safe cleanup. |
89 bool is_watching_; | 97 bool is_watching_; |
90 | 98 |
91 base::win::ObjectWatcher addr_watcher_; | 99 base::win::ObjectWatcher addr_watcher_; |
92 OVERLAPPED addr_overlapped_; | 100 OVERLAPPED addr_overlapped_; |
93 | 101 |
94 base::OneShotTimer timer_; | 102 base::OneShotTimer timer_; |
95 | 103 |
96 // Number of times WatchForAddressChange has failed in a row. | 104 // Number of times WatchForAddressChange has failed in a row. |
97 int sequential_failures_; | 105 int sequential_failures_; |
98 | 106 |
99 // Thread on which we can run DnsConfigService. | 107 // Thread on which we can run DnsConfigService. |
100 std::unique_ptr<DnsConfigServiceThread> dns_config_service_thread_; | 108 std::unique_ptr<DnsConfigServiceThread> dns_config_service_thread_; |
109 scoped_refptr<base::SingleThreadTaskRunner> dns_task_runner_; | |
101 | 110 |
102 mutable base::Lock last_computed_connection_type_lock_; | 111 mutable base::Lock last_computed_connection_type_lock_; |
103 ConnectionType last_computed_connection_type_; | 112 ConnectionType last_computed_connection_type_; |
104 | 113 |
105 // Result of IsOffline() when NotifyObserversOfConnectionTypeChange() | 114 // Result of IsOffline() when NotifyObserversOfConnectionTypeChange() |
106 // was last called. | 115 // was last called. |
107 bool last_announced_offline_; | 116 bool last_announced_offline_; |
108 // Number of times polled to check if still offline. | 117 // Number of times polled to check if still offline. |
109 int offline_polls_; | 118 int offline_polls_; |
110 | 119 |
120 // The callback run to calculate connection type. | |
121 base::Callback<ConnectionType()> calculate_connection_type_; | |
122 | |
111 // Used for calling WatchForAddressChange again on failure. | 123 // Used for calling WatchForAddressChange again on failure. |
112 base::WeakPtrFactory<NetworkChangeNotifierWin> weak_factory_; | 124 base::WeakPtrFactory<NetworkChangeNotifierWin> weak_factory_; |
113 | 125 |
114 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin); | 126 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin); |
115 }; | 127 }; |
116 | 128 |
117 } // namespace net | 129 } // namespace net |
118 | 130 |
119 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ | 131 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ |
OLD | NEW |