Chromium Code Reviews| 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/time.h" | 10 #include "base/time/time.h" |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 virtual void OnNetworkChanged(ConnectionType type) = 0; | 167 virtual void OnNetworkChanged(ConnectionType type) = 0; |
| 168 | 168 |
| 169 protected: | 169 protected: |
| 170 NetworkChangeObserver() {} | 170 NetworkChangeObserver() {} |
| 171 virtual ~NetworkChangeObserver() {} | 171 virtual ~NetworkChangeObserver() {} |
| 172 | 172 |
| 173 private: | 173 private: |
| 174 DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserver); | 174 DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserver); |
| 175 }; | 175 }; |
| 176 | 176 |
| 177 class NET_EXPORT MaxBandwidthChangeObserver { | |
|
pauljensen
2014/12/11 16:13:57
Can we remove the "Change" from this name...and ev
jkarlin
2014/12/15 19:43:55
I've renamed the observer, but not "NotifyMaxBandw
| |
| 178 public: | |
| 179 // Will be called when a change occurs to the network's maximum bandwidth as | |
| 180 // defined in http://w3c.github.io/netinfo/. Generally this will only be | |
| 181 // called on bandwidth changing network connection/disconnection events. | |
| 182 // Some platforms may call it more frequently, such as when WiFi signal | |
| 183 // strength changes. | |
| 184 // TODO(jkarlin): This is currently only implemented for Android. Implement | |
| 185 // on every platform. | |
| 186 virtual void OnMaxBandwidthChanged(double max_bandwidth_mbps) = 0; | |
| 187 | |
| 188 protected: | |
| 189 MaxBandwidthChangeObserver() {} | |
| 190 virtual ~MaxBandwidthChangeObserver() {} | |
| 191 | |
| 192 private: | |
| 193 DISALLOW_COPY_AND_ASSIGN(MaxBandwidthChangeObserver); | |
| 194 }; | |
| 195 | |
| 177 virtual ~NetworkChangeNotifier(); | 196 virtual ~NetworkChangeNotifier(); |
| 178 | 197 |
| 179 // See the description of NetworkChangeNotifier::GetConnectionType(). | 198 // See the description of NetworkChangeNotifier::GetConnectionType(). |
| 180 // Implementations must be thread-safe. Implementations must also be | 199 // Implementations must be thread-safe. Implementations must also be |
| 181 // cheap as it is called often. | 200 // cheap as it is called often. |
| 182 virtual ConnectionType GetCurrentConnectionType() const = 0; | 201 virtual ConnectionType GetCurrentConnectionType() const = 0; |
| 183 | 202 |
| 184 // Replaces the default class factory instance of NetworkChangeNotifier class. | 203 // Replaces the default class factory instance of NetworkChangeNotifier class. |
| 185 // The method will take over the ownership of |factory| object. | 204 // The method will take over the ownership of |factory| object. |
| 186 static void SetFactory(NetworkChangeNotifierFactory* factory); | 205 static void SetFactory(NetworkChangeNotifierFactory* factory); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 | 264 |
| 246 // Registers |observer| to receive notifications of network changes. The | 265 // Registers |observer| to receive notifications of network changes. The |
| 247 // thread on which this is called is the thread on which |observer| will be | 266 // thread on which this is called is the thread on which |observer| will be |
| 248 // called back with notifications. This is safe to call if Create() has not | 267 // called back with notifications. This is safe to call if Create() has not |
| 249 // been called (as long as it doesn't race the Create() call on another | 268 // been called (as long as it doesn't race the Create() call on another |
| 250 // thread), in which case it will simply do nothing. | 269 // thread), in which case it will simply do nothing. |
| 251 static void AddIPAddressObserver(IPAddressObserver* observer); | 270 static void AddIPAddressObserver(IPAddressObserver* observer); |
| 252 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer); | 271 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer); |
| 253 static void AddDNSObserver(DNSObserver* observer); | 272 static void AddDNSObserver(DNSObserver* observer); |
| 254 static void AddNetworkChangeObserver(NetworkChangeObserver* observer); | 273 static void AddNetworkChangeObserver(NetworkChangeObserver* observer); |
| 274 static void AddMaxBandwidthChangeObserver( | |
| 275 MaxBandwidthChangeObserver* observer); | |
| 255 | 276 |
| 256 // Unregisters |observer| from receiving notifications. This must be called | 277 // Unregisters |observer| from receiving notifications. This must be called |
| 257 // on the same thread on which AddObserver() was called. Like AddObserver(), | 278 // on the same thread on which AddObserver() was called. Like AddObserver(), |
| 258 // this is safe to call if Create() has not been called (as long as it doesn't | 279 // this is safe to call if Create() has not been called (as long as it doesn't |
| 259 // race the Create() call on another thread), in which case it will simply do | 280 // race the Create() call on another thread), in which case it will simply do |
| 260 // nothing. Technically, it's also safe to call after the notifier object has | 281 // nothing. Technically, it's also safe to call after the notifier object has |
| 261 // been destroyed, if the call doesn't race the notifier's destruction, but | 282 // been destroyed, if the call doesn't race the notifier's destruction, but |
| 262 // there's no reason to use the API in this risky way, so don't do it. | 283 // there's no reason to use the API in this risky way, so don't do it. |
| 263 static void RemoveIPAddressObserver(IPAddressObserver* observer); | 284 static void RemoveIPAddressObserver(IPAddressObserver* observer); |
| 264 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); | 285 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); |
| 265 static void RemoveDNSObserver(DNSObserver* observer); | 286 static void RemoveDNSObserver(DNSObserver* observer); |
| 266 static void RemoveNetworkChangeObserver(NetworkChangeObserver* observer); | 287 static void RemoveNetworkChangeObserver(NetworkChangeObserver* observer); |
| 288 static void RemoveMaxBandwidthChangeObserver( | |
| 289 MaxBandwidthChangeObserver* observer); | |
| 267 | 290 |
| 268 // Allow unit tests to trigger notifications. | 291 // Allow unit tests to trigger notifications. |
| 269 static void NotifyObserversOfIPAddressChangeForTests(); | 292 static void NotifyObserversOfIPAddressChangeForTests(); |
| 270 static void NotifyObserversOfConnectionTypeChangeForTests( | 293 static void NotifyObserversOfConnectionTypeChangeForTests( |
| 271 ConnectionType type); | 294 ConnectionType type); |
| 272 static void NotifyObserversOfNetworkChangeForTests(ConnectionType type); | 295 static void NotifyObserversOfNetworkChangeForTests(ConnectionType type); |
| 273 | 296 |
| 274 // Enable or disable notifications from the host. After setting to true, be | 297 // Enable or disable notifications from the host. After setting to true, be |
| 275 // sure to pump the RunLoop until idle to finish any preexisting | 298 // sure to pump the RunLoop until idle to finish any preexisting |
| 276 // notifications. | 299 // notifications. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 359 // the NetInfo spec: http://w3c.github.io/netinfo/. | 382 // the NetInfo spec: http://w3c.github.io/netinfo/. |
| 360 static double GetMaxBandwidthForConnectionSubtype(ConnectionSubtype subtype); | 383 static double GetMaxBandwidthForConnectionSubtype(ConnectionSubtype subtype); |
| 361 | 384 |
| 362 // Broadcasts a notification to all registered observers. Note that this | 385 // Broadcasts a notification to all registered observers. Note that this |
| 363 // happens asynchronously, even for observers on the current thread, even in | 386 // happens asynchronously, even for observers on the current thread, even in |
| 364 // tests. | 387 // tests. |
| 365 static void NotifyObserversOfIPAddressChange(); | 388 static void NotifyObserversOfIPAddressChange(); |
| 366 static void NotifyObserversOfConnectionTypeChange(); | 389 static void NotifyObserversOfConnectionTypeChange(); |
| 367 static void NotifyObserversOfDNSChange(); | 390 static void NotifyObserversOfDNSChange(); |
| 368 static void NotifyObserversOfNetworkChange(ConnectionType type); | 391 static void NotifyObserversOfNetworkChange(ConnectionType type); |
| 392 static void NotifyObserversOfMaxBandwidthChange(double max_bandwidth_mbps); | |
| 369 | 393 |
| 370 // Stores |config| in NetworkState and notifies observers. | 394 // Stores |config| in NetworkState and notifies observers. |
| 371 static void SetDnsConfig(const DnsConfig& config); | 395 static void SetDnsConfig(const DnsConfig& config); |
| 372 | 396 |
| 373 private: | 397 private: |
| 374 friend class HostResolverImplDnsTest; | 398 friend class HostResolverImplDnsTest; |
| 375 friend class NetworkChangeNotifierAndroidTest; | 399 friend class NetworkChangeNotifierAndroidTest; |
| 376 friend class NetworkChangeNotifierLinuxTest; | 400 friend class NetworkChangeNotifierLinuxTest; |
| 377 friend class NetworkChangeNotifierWinTest; | 401 friend class NetworkChangeNotifierWinTest; |
| 378 | 402 |
| 379 class NetworkState; | 403 class NetworkState; |
| 380 class NetworkChangeCalculator; | 404 class NetworkChangeCalculator; |
| 381 | 405 |
| 382 void NotifyObserversOfIPAddressChangeImpl(); | 406 void NotifyObserversOfIPAddressChangeImpl(); |
| 383 void NotifyObserversOfConnectionTypeChangeImpl(ConnectionType type); | 407 void NotifyObserversOfConnectionTypeChangeImpl(ConnectionType type); |
| 384 void NotifyObserversOfDNSChangeImpl(); | 408 void NotifyObserversOfDNSChangeImpl(); |
| 385 void NotifyObserversOfNetworkChangeImpl(ConnectionType type); | 409 void NotifyObserversOfNetworkChangeImpl(ConnectionType type); |
| 410 void NotifyObserversOfMaxBandwidthChangeImpl(double max_bandwidth_mbps); | |
| 386 | 411 |
| 387 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > | 412 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver>> |
| 388 ip_address_observer_list_; | 413 ip_address_observer_list_; |
| 389 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> > | 414 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver>> |
| 390 connection_type_observer_list_; | 415 connection_type_observer_list_; |
| 391 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > | 416 const scoped_refptr<ObserverListThreadSafe<DNSObserver>> |
| 392 resolver_state_observer_list_; | 417 resolver_state_observer_list_; |
| 393 const scoped_refptr<ObserverListThreadSafe<NetworkChangeObserver> > | 418 const scoped_refptr<ObserverListThreadSafe<NetworkChangeObserver>> |
| 394 network_change_observer_list_; | 419 network_change_observer_list_; |
| 420 const scoped_refptr<ObserverListThreadSafe<MaxBandwidthChangeObserver>> | |
| 421 max_bandwidth_change_observer_list_; | |
| 395 | 422 |
| 396 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig. | 423 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig. |
| 397 scoped_ptr<NetworkState> network_state_; | 424 scoped_ptr<NetworkState> network_state_; |
| 398 | 425 |
| 399 // A little-piggy-back observer that simply logs UMA histogram data. | 426 // A little-piggy-back observer that simply logs UMA histogram data. |
| 400 scoped_ptr<HistogramWatcher> histogram_watcher_; | 427 scoped_ptr<HistogramWatcher> histogram_watcher_; |
| 401 | 428 |
| 402 // Computes NetworkChange signal from IPAddress and ConnectionType signals. | 429 // Computes NetworkChange signal from IPAddress and ConnectionType signals. |
| 403 scoped_ptr<NetworkChangeCalculator> network_change_calculator_; | 430 scoped_ptr<NetworkChangeCalculator> network_change_calculator_; |
| 404 | 431 |
| 405 // Set true to disable non-test notifications (to prevent flakes in tests). | 432 // Set true to disable non-test notifications (to prevent flakes in tests). |
| 406 bool test_notifications_only_; | 433 bool test_notifications_only_; |
| 407 | 434 |
| 408 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 435 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
| 409 }; | 436 }; |
| 410 | 437 |
| 411 } // namespace net | 438 } // namespace net |
| 412 | 439 |
| 413 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 440 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
| OLD | NEW |