network_change_notifier_posix.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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
  3. // found in the LICENSE file.
  4. #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_POSIX_H_
  5. #define NET_BASE_NETWORK_CHANGE_NOTIFIER_POSIX_H_
  6. #include "base/gtest_prod_util.h"
  7. #include "base/memory/scoped_refptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/sequence_checker.h"
  10. #include "base/synchronization/lock.h"
  11. #include "base/threading/thread.h"
  12. #include "base/threading/thread_checker.h"
  13. #include "net/base/net_export.h"
  14. #include "net/base/network_change_notifier.h"
  15. namespace net {
  16. // A NetworkChangeNotifier that needs to be told about network changes by some
  17. // other object. This class can't directly listen for network changes because on
  18. // ChromeOS and Android only objects running in the browser process can listen
  19. // for network state changes.
  20. class NET_EXPORT NetworkChangeNotifierPosix : public NetworkChangeNotifier {
  21. public:
  22. NetworkChangeNotifierPosix(
  23. NetworkChangeNotifier::ConnectionType initial_connection_type,
  24. NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype);
  25. NetworkChangeNotifierPosix(const NetworkChangeNotifierPosix&) = delete;
  26. NetworkChangeNotifierPosix& operator=(const NetworkChangeNotifierPosix&) =
  27. delete;
  28. ~NetworkChangeNotifierPosix() override;
  29. // These methods are used to notify this object that a network property has
  30. // changed. These must be called from the thread that owns this object.
  31. void OnDNSChanged();
  32. void OnIPAddressChanged();
  33. void OnConnectionChanged(
  34. NetworkChangeNotifier::ConnectionType connection_type);
  35. void OnConnectionSubtypeChanged(
  36. NetworkChangeNotifier::ConnectionType connection_type,
  37. NetworkChangeNotifier::ConnectionSubtype connection_subtype);
  38. protected:
  39. // NetworkChangeNotifier overrides.
  40. NetworkChangeNotifier::ConnectionType GetCurrentConnectionType()
  41. const override;
  42. void GetCurrentMaxBandwidthAndConnectionType(
  43. double* max_bandwidth_mbps,
  44. ConnectionType* connection_type) const override;
  45. private:
  46. friend class NetworkChangeNotifierPosixTest;
  47. // For testing purposes, allows specifying a SystemDnsConfigChangeNotifier.
  48. // If |system_dns_config_notifier| is nullptr, NetworkChangeNotifier create a
  49. // global one.
  50. NetworkChangeNotifierPosix(
  51. NetworkChangeNotifier::ConnectionType initial_connection_type,
  52. NetworkChangeNotifier::ConnectionSubtype initial_connection_subtype,
  53. SystemDnsConfigChangeNotifier* system_dns_config_notifier);
  54. // Calculates parameters used for network change notifier online/offline
  55. // signals.
  56. static NetworkChangeNotifier::NetworkChangeCalculatorParams
  57. NetworkChangeCalculatorParamsPosix();
  58. THREAD_CHECKER(thread_checker_);
  59. mutable base::Lock lock_;
  60. NetworkChangeNotifier::ConnectionType
  61. connection_type_; // Guarded by |lock_|.
  62. double max_bandwidth_mbps_; // Guarded by |lock_|.
  63. };
  64. } // namespace net
  65. #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_POSIX_H_