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_H_ | 5 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/observer_list_threadsafe.h" | 10 #include "base/observer_list_threadsafe.h" |
| 11 #include "base/synchronization/lock.h" |
11 #include "net/base/net_export.h" | 12 #include "net/base/net_export.h" |
12 | 13 |
13 namespace net { | 14 namespace net { |
14 | 15 |
15 class NetworkChangeNotifierFactory; | 16 class NetworkChangeNotifierFactory; |
16 | 17 |
| 18 namespace internal { |
| 19 class DnsConfigWatcher; |
| 20 } |
| 21 |
17 // NetworkChangeNotifier monitors the system for network changes, and notifies | 22 // NetworkChangeNotifier monitors the system for network changes, and notifies |
18 // registered observers of those events. Observers may register on any thread, | 23 // registered observers of those events. Observers may register on any thread, |
19 // and will be called back on the thread from which they registered. | 24 // and will be called back on the thread from which they registered. |
20 // NetworkChangeNotifiers are threadsafe, though they must be created and | 25 // NetworkChangeNotifiers are threadsafe, though they must be created and |
21 // destroyed on the same thread. | 26 // destroyed on the same thread. |
22 class NET_EXPORT NetworkChangeNotifier { | 27 class NET_EXPORT NetworkChangeNotifier { |
23 public: | 28 public: |
24 // Flags which are ORed together to form |detail| in OnDNSChanged. | 29 // Flags which are ORed together to form |detail| in OnDNSChanged. |
25 enum { | 30 enum { |
26 // The DNS configuration (name servers, suffix search) has changed. | 31 // The DNS configuration (name servers, suffix search) has changed. |
27 CHANGE_DNS_SETTINGS = 1 << 0, | 32 CHANGE_DNS_SETTINGS = 1 << 0, |
28 // The HOSTS file has changed. | 33 // The HOSTS file has changed. |
29 CHANGE_DNS_HOSTS = 1 << 1, | 34 CHANGE_DNS_HOSTS = 1 << 1, |
30 // Computer name has changed. | 35 // The watcher has started. |
31 CHANGE_DNS_LOCALHOST = 1 << 2, | 36 CHANGE_DNS_WATCH_STARTED = 1 << 2, |
| 37 // The watcher has failed and will not be available until further notice. |
| 38 CHANGE_DNS_WATCH_FAILED = 1 << 3, |
32 }; | 39 }; |
33 | 40 |
34 class NET_EXPORT IPAddressObserver { | 41 class NET_EXPORT IPAddressObserver { |
35 public: | 42 public: |
36 virtual ~IPAddressObserver() {} | 43 virtual ~IPAddressObserver() {} |
37 | 44 |
38 // Will be called when the IP address of the primary interface changes. | 45 // Will be called when the IP address of the primary interface changes. |
39 // This includes when the primary interface itself changes. | 46 // This includes when the primary interface itself changes. |
40 virtual void OnIPAddressChanged() = 0; | 47 virtual void OnIPAddressChanged() = 0; |
41 | 48 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 105 |
99 // Returns true if there is currently no internet connection. | 106 // Returns true if there is currently no internet connection. |
100 // | 107 // |
101 // A return value of |true| is a pretty strong indicator that the user | 108 // A return value of |true| is a pretty strong indicator that the user |
102 // won't be able to connect to remote sites. However, a return value of | 109 // won't be able to connect to remote sites. However, a return value of |
103 // |false| is inconclusive; even if some link is up, it is uncertain | 110 // |false| is inconclusive; even if some link is up, it is uncertain |
104 // whether a particular connection attempt to a particular remote site | 111 // whether a particular connection attempt to a particular remote site |
105 // will be successfully. | 112 // will be successfully. |
106 static bool IsOffline(); | 113 static bool IsOffline(); |
107 | 114 |
| 115 // Returns true if DNS watcher is operational. |
| 116 static bool IsWatchingDNS(); |
| 117 |
108 // Like Create(), but for use in tests. The mock object doesn't monitor any | 118 // Like Create(), but for use in tests. The mock object doesn't monitor any |
109 // events, it merely rebroadcasts notifications when requested. | 119 // events, it merely rebroadcasts notifications when requested. |
110 static NetworkChangeNotifier* CreateMock(); | 120 static NetworkChangeNotifier* CreateMock(); |
111 | 121 |
112 // Registers |observer| to receive notifications of network changes. The | 122 // Registers |observer| to receive notifications of network changes. The |
113 // thread on which this is called is the thread on which |observer| will be | 123 // thread on which this is called is the thread on which |observer| will be |
114 // called back with notifications. This is safe to call if Create() has not | 124 // called back with notifications. This is safe to call if Create() has not |
115 // been called (as long as it doesn't race the Create() call on another | 125 // been called (as long as it doesn't race the Create() call on another |
116 // thread), in which case it will simply do nothing. | 126 // thread), in which case it will simply do nothing. |
117 static void AddIPAddressObserver(IPAddressObserver* observer); | 127 static void AddIPAddressObserver(IPAddressObserver* observer); |
(...skipping 10 matching lines...) Expand all Loading... |
128 static void RemoveIPAddressObserver(IPAddressObserver* observer); | 138 static void RemoveIPAddressObserver(IPAddressObserver* observer); |
129 static void RemoveOnlineStateObserver(OnlineStateObserver* observer); | 139 static void RemoveOnlineStateObserver(OnlineStateObserver* observer); |
130 static void RemoveDNSObserver(DNSObserver* observer); | 140 static void RemoveDNSObserver(DNSObserver* observer); |
131 | 141 |
132 // Allow unit tests to trigger notifications. | 142 // Allow unit tests to trigger notifications. |
133 static void NotifyObserversOfIPAddressChangeForTests() { | 143 static void NotifyObserversOfIPAddressChangeForTests() { |
134 NotifyObserversOfIPAddressChange(); | 144 NotifyObserversOfIPAddressChange(); |
135 } | 145 } |
136 | 146 |
137 protected: | 147 protected: |
| 148 friend class internal::DnsConfigWatcher; |
| 149 |
138 NetworkChangeNotifier(); | 150 NetworkChangeNotifier(); |
139 | 151 |
140 // Broadcasts a notification to all registered observers. Note that this | 152 // Broadcasts a notification to all registered observers. Note that this |
141 // happens asynchronously, even for observers on the current thread, even in | 153 // happens asynchronously, even for observers on the current thread, even in |
142 // tests. | 154 // tests. |
143 static void NotifyObserversOfIPAddressChange(); | 155 static void NotifyObserversOfIPAddressChange(); |
144 static void NotifyObserversOfOnlineStateChange(); | 156 static void NotifyObserversOfOnlineStateChange(); |
145 static void NotifyObserversOfDNSChange(unsigned detail); | 157 static void NotifyObserversOfDNSChange(unsigned detail); |
146 | 158 |
147 private: | 159 private: |
(...skipping 17 matching lines...) Expand all Loading... |
165 NetworkChangeNotifier* network_change_notifier_; | 177 NetworkChangeNotifier* network_change_notifier_; |
166 }; | 178 }; |
167 | 179 |
168 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > | 180 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > |
169 ip_address_observer_list_; | 181 ip_address_observer_list_; |
170 const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > | 182 const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > |
171 online_state_observer_list_; | 183 online_state_observer_list_; |
172 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > | 184 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > |
173 resolver_state_observer_list_; | 185 resolver_state_observer_list_; |
174 | 186 |
| 187 // True iff DNS watchers are operational. |
| 188 // Otherwise, OnDNSChanged might not be issued for future changes. |
| 189 // TODO(szym): This is a temporary interface, consider restarting them. |
| 190 // http://crbug.com/116139 |
| 191 base::Lock watching_dns_lock_; |
| 192 bool watching_dns_; |
| 193 |
175 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 194 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
176 }; | 195 }; |
177 | 196 |
178 } // namespace net | 197 } // namespace net |
179 | 198 |
180 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 199 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
OLD | NEW |