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 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/observer_list_threadsafe.h" | 9 #include "base/observer_list_threadsafe.h" |
| 10 #include "base/time.h" |
10 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
11 | 12 |
12 class GURL; | 13 class GURL; |
13 | 14 |
14 namespace net { | 15 namespace net { |
15 | 16 |
16 struct DnsConfig; | 17 struct DnsConfig; |
17 class HistogramWatcher; | 18 class HistogramWatcher; |
18 class NetworkChangeNotifierFactory; | 19 class NetworkChangeNotifierFactory; |
19 | 20 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 virtual void OnDNSChanged() = 0; | 80 virtual void OnDNSChanged() = 0; |
80 | 81 |
81 protected: | 82 protected: |
82 DNSObserver() {} | 83 DNSObserver() {} |
83 virtual ~DNSObserver() {} | 84 virtual ~DNSObserver() {} |
84 | 85 |
85 private: | 86 private: |
86 DISALLOW_COPY_AND_ASSIGN(DNSObserver); | 87 DISALLOW_COPY_AND_ASSIGN(DNSObserver); |
87 }; | 88 }; |
88 | 89 |
| 90 class NET_EXPORT NetworkChangeObserver { |
| 91 public: |
| 92 // OnNetworkChanged will be called when a change occurs to the host |
| 93 // computer's hardware or software that affects the route network packets |
| 94 // take to any network server. Some examples: |
| 95 // 1. A network connection becoming available or going away. For example |
| 96 // plugging or unplugging an Ethernet cable, WiFi or cellular modem |
| 97 // connecting or disconnecting from a network, or a VPN tunnel being |
| 98 // established or taken down. |
| 99 // 2. An active network connection's IP address changes. |
| 100 // 3. A change to the local IP routing tables. |
| 101 // The signal shall only be produced when the change is complete. For |
| 102 // example if a new network connection has become available, only give the |
| 103 // signal once we think the O/S has finished establishing the connection |
| 104 // (i.e. DHCP is done) to the point where the new connection is usable. |
| 105 // The signal shall not be produced spuriously as it will be triggering some |
| 106 // expensive operations, like socket pools closing all connections and |
| 107 // sockets and then re-establishing them. |
| 108 // |type| indicates the type of the active primary network connection after |
| 109 // the change. Observers performing "constructive" activities like trying |
| 110 // to establish a connection to a server should only do so when |
| 111 // |type != CONNECTION_NONE|. Observers performing "destructive" activities |
| 112 // like resetting already established server connections should only do so |
| 113 // when |type == CONNECTION_NONE|. OnNetworkChanged will always be called |
| 114 // with CONNECTION_NONE immediately prior to being called with an online |
| 115 // state; this is done to make sure that destructive actions take place |
| 116 // prior to constructive actions. |
| 117 virtual void OnNetworkChanged(ConnectionType type) = 0; |
| 118 |
| 119 protected: |
| 120 NetworkChangeObserver() {} |
| 121 virtual ~NetworkChangeObserver() {} |
| 122 |
| 123 private: |
| 124 DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserver); |
| 125 }; |
| 126 |
89 virtual ~NetworkChangeNotifier(); | 127 virtual ~NetworkChangeNotifier(); |
90 | 128 |
91 // See the description of NetworkChangeNotifier::GetConnectionType(). | 129 // See the description of NetworkChangeNotifier::GetConnectionType(). |
92 // Implementations must be thread-safe. Implementations must also be | 130 // Implementations must be thread-safe. Implementations must also be |
93 // cheap as this could be called (repeatedly) from the network thread. | 131 // cheap as this could be called (repeatedly) from the network thread. |
94 virtual ConnectionType GetCurrentConnectionType() const = 0; | 132 virtual ConnectionType GetCurrentConnectionType() const = 0; |
95 | 133 |
96 // Replaces the default class factory instance of NetworkChangeNotifier class. | 134 // Replaces the default class factory instance of NetworkChangeNotifier class. |
97 // The method will take over the ownership of |factory| object. | 135 // The method will take over the ownership of |factory| object. |
98 static void SetFactory(NetworkChangeNotifierFactory* factory); | 136 static void SetFactory(NetworkChangeNotifierFactory* factory); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 static NetworkChangeNotifier* CreateMock(); | 186 static NetworkChangeNotifier* CreateMock(); |
149 | 187 |
150 // Registers |observer| to receive notifications of network changes. The | 188 // Registers |observer| to receive notifications of network changes. The |
151 // thread on which this is called is the thread on which |observer| will be | 189 // thread on which this is called is the thread on which |observer| will be |
152 // called back with notifications. This is safe to call if Create() has not | 190 // called back with notifications. This is safe to call if Create() has not |
153 // been called (as long as it doesn't race the Create() call on another | 191 // been called (as long as it doesn't race the Create() call on another |
154 // thread), in which case it will simply do nothing. | 192 // thread), in which case it will simply do nothing. |
155 static void AddIPAddressObserver(IPAddressObserver* observer); | 193 static void AddIPAddressObserver(IPAddressObserver* observer); |
156 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer); | 194 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer); |
157 static void AddDNSObserver(DNSObserver* observer); | 195 static void AddDNSObserver(DNSObserver* observer); |
| 196 static void AddNetworkChangeObserver(NetworkChangeObserver* observer); |
158 | 197 |
159 // Unregisters |observer| from receiving notifications. This must be called | 198 // Unregisters |observer| from receiving notifications. This must be called |
160 // on the same thread on which AddObserver() was called. Like AddObserver(), | 199 // on the same thread on which AddObserver() was called. Like AddObserver(), |
161 // this is safe to call if Create() has not been called (as long as it doesn't | 200 // this is safe to call if Create() has not been called (as long as it doesn't |
162 // race the Create() call on another thread), in which case it will simply do | 201 // race the Create() call on another thread), in which case it will simply do |
163 // nothing. Technically, it's also safe to call after the notifier object has | 202 // nothing. Technically, it's also safe to call after the notifier object has |
164 // been destroyed, if the call doesn't race the notifier's destruction, but | 203 // been destroyed, if the call doesn't race the notifier's destruction, but |
165 // there's no reason to use the API in this risky way, so don't do it. | 204 // there's no reason to use the API in this risky way, so don't do it. |
166 static void RemoveIPAddressObserver(IPAddressObserver* observer); | 205 static void RemoveIPAddressObserver(IPAddressObserver* observer); |
167 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); | 206 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); |
168 static void RemoveDNSObserver(DNSObserver* observer); | 207 static void RemoveDNSObserver(DNSObserver* observer); |
| 208 static void RemoveNetworkChangeObserver(NetworkChangeObserver* observer); |
169 | 209 |
170 // Allow unit tests to trigger notifications. | 210 // Allow unit tests to trigger notifications. |
171 static void NotifyObserversOfIPAddressChangeForTests() { | 211 static void NotifyObserversOfIPAddressChangeForTests() { |
172 NotifyObserversOfIPAddressChange(); | 212 NotifyObserversOfIPAddressChange(); |
173 } | 213 } |
174 | 214 |
175 // Return a string equivalent to |type|. | 215 // Return a string equivalent to |type|. |
176 static const char* ConnectionTypeToString(ConnectionType type); | 216 static const char* ConnectionTypeToString(ConnectionType type); |
177 | 217 |
178 // Let the NetworkChangeNotifier know we received some data. | 218 // Let the NetworkChangeNotifier know we received some data. |
179 // This is used strictly for producing histogram data about the accuracy of | 219 // This is used strictly for producing histogram data about the accuracy of |
180 // the NetworkChangenotifier's online detection. | 220 // the NetworkChangenotifier's online detection. |
181 static void NotifyDataReceived(const GURL& source); | 221 static void NotifyDataReceived(const GURL& source); |
182 | 222 |
183 // Register the Observer callbacks for producing histogram data. This | 223 // Register the Observer callbacks for producing histogram data. This |
184 // should be called from the network thread to avoid race conditions. | 224 // should be called from the network thread to avoid race conditions. |
185 static void InitHistogramWatcher(); | 225 static void InitHistogramWatcher(); |
186 | 226 |
187 protected: | 227 protected: |
188 NetworkChangeNotifier(); | 228 // NetworkChanged signal is calculated from the IPAddressChanged and |
| 229 // ConnectionTypeChanged signals. Delay parameters control how long to delay |
| 230 // producing NetworkChanged signal after particular input signals so as to |
| 231 // combine duplicates. In other words if an input signal is repeated within |
| 232 // the corresponding delay period, only one resulting NetworkChange signal is |
| 233 // produced. |
| 234 struct NET_EXPORT NetworkChangeCalculatorParams { |
| 235 NetworkChangeCalculatorParams(); |
| 236 // Controls delay after OnIPAddressChanged when transitioning from an |
| 237 // offline state. |
| 238 base::TimeDelta ip_address_offline_delay_; |
| 239 // Controls delay after OnIPAddressChanged when transitioning from an |
| 240 // online state. |
| 241 base::TimeDelta ip_address_online_delay_; |
| 242 // Controls delay after OnConnectionTypeChanged when transitioning from an |
| 243 // offline state. |
| 244 base::TimeDelta connection_type_offline_delay_; |
| 245 // Controls delay after OnConnectionTypeChanged when transitioning from an |
| 246 // online state. |
| 247 base::TimeDelta connection_type_online_delay_; |
| 248 }; |
| 249 |
| 250 explicit NetworkChangeNotifier( |
| 251 const NetworkChangeCalculatorParams& params = |
| 252 NetworkChangeCalculatorParams()); |
189 | 253 |
190 #if defined(OS_LINUX) | 254 #if defined(OS_LINUX) |
191 // Returns the AddressTrackerLinux if present. | 255 // Returns the AddressTrackerLinux if present. |
192 // TODO(szym): Retrieve AddressMap from NetworkState. http://crbug.com/144212 | 256 // TODO(szym): Retrieve AddressMap from NetworkState. http://crbug.com/144212 |
193 virtual const internal::AddressTrackerLinux* | 257 virtual const internal::AddressTrackerLinux* |
194 GetAddressTrackerInternal() const; | 258 GetAddressTrackerInternal() const; |
195 #endif | 259 #endif |
196 | 260 |
197 // Broadcasts a notification to all registered observers. Note that this | 261 // Broadcasts a notification to all registered observers. Note that this |
198 // happens asynchronously, even for observers on the current thread, even in | 262 // happens asynchronously, even for observers on the current thread, even in |
199 // tests. | 263 // tests. |
200 static void NotifyObserversOfIPAddressChange(); | 264 static void NotifyObserversOfIPAddressChange(); |
201 static void NotifyObserversOfConnectionTypeChange(); | 265 static void NotifyObserversOfConnectionTypeChange(); |
202 static void NotifyObserversOfDNSChange(); | 266 static void NotifyObserversOfDNSChange(); |
| 267 static void NotifyObserversOfNetworkChange(ConnectionType type); |
203 | 268 |
204 // Stores |config| in NetworkState and notifies observers. | 269 // Stores |config| in NetworkState and notifies observers. |
205 static void SetDnsConfig(const DnsConfig& config); | 270 static void SetDnsConfig(const DnsConfig& config); |
206 | 271 |
207 private: | 272 private: |
208 friend class HostResolverImplDnsTest; | 273 friend class HostResolverImplDnsTest; |
209 friend class NetworkChangeNotifierAndroidTest; | 274 friend class NetworkChangeNotifierAndroidTest; |
210 friend class NetworkChangeNotifierLinuxTest; | 275 friend class NetworkChangeNotifierLinuxTest; |
211 friend class NetworkChangeNotifierWinTest; | 276 friend class NetworkChangeNotifierWinTest; |
212 | 277 |
213 class NetworkState; | 278 class NetworkState; |
| 279 class NetworkChangeCalculator; |
214 | 280 |
215 // Allows a second NetworkChangeNotifier to be created for unit testing, so | 281 // Allows a second NetworkChangeNotifier to be created for unit testing, so |
216 // the test suite can create a MockNetworkChangeNotifier, but platform | 282 // the test suite can create a MockNetworkChangeNotifier, but platform |
217 // specific NetworkChangeNotifiers can also be created for testing. To use, | 283 // specific NetworkChangeNotifiers can also be created for testing. To use, |
218 // create an DisableForTest object, and then create the new | 284 // create an DisableForTest object, and then create the new |
219 // NetworkChangeNotifier object. The NetworkChangeNotifier must be | 285 // NetworkChangeNotifier object. The NetworkChangeNotifier must be |
220 // destroyed before the DisableForTest object, as its destruction will restore | 286 // destroyed before the DisableForTest object, as its destruction will restore |
221 // the original NetworkChangeNotifier. | 287 // the original NetworkChangeNotifier. |
222 class NET_EXPORT_PRIVATE DisableForTest { | 288 class NET_EXPORT_PRIVATE DisableForTest { |
223 public: | 289 public: |
224 DisableForTest(); | 290 DisableForTest(); |
225 ~DisableForTest(); | 291 ~DisableForTest(); |
226 | 292 |
227 private: | 293 private: |
228 // The original NetworkChangeNotifier to be restored on destruction. | 294 // The original NetworkChangeNotifier to be restored on destruction. |
229 NetworkChangeNotifier* network_change_notifier_; | 295 NetworkChangeNotifier* network_change_notifier_; |
230 }; | 296 }; |
231 | 297 |
232 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > | 298 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > |
233 ip_address_observer_list_; | 299 ip_address_observer_list_; |
234 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> > | 300 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> > |
235 connection_type_observer_list_; | 301 connection_type_observer_list_; |
236 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > | 302 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > |
237 resolver_state_observer_list_; | 303 resolver_state_observer_list_; |
| 304 const scoped_refptr<ObserverListThreadSafe<NetworkChangeObserver> > |
| 305 network_change_observer_list_; |
238 | 306 |
239 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig. | 307 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig. |
240 scoped_ptr<NetworkState> network_state_; | 308 scoped_ptr<NetworkState> network_state_; |
241 | 309 |
242 // A little-piggy-back observer that simply logs UMA histogram data. | 310 // A little-piggy-back observer that simply logs UMA histogram data. |
243 scoped_ptr<HistogramWatcher> histogram_watcher_; | 311 scoped_ptr<HistogramWatcher> histogram_watcher_; |
244 | 312 |
| 313 // Computes NetworkChange signal from IPAddress and ConnectionType signals. |
| 314 scoped_ptr<NetworkChangeCalculator> network_change_calculator_; |
| 315 |
245 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 316 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
246 }; | 317 }; |
247 | 318 |
248 } // namespace net | 319 } // namespace net |
249 | 320 |
250 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 321 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
OLD | NEW |