network_change_notifier_android.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_
  5. #define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_
  6. #include <memory>
  7. #include "base/android/jni_android.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "net/android/network_change_notifier_delegate_android.h"
  12. #include "net/base/net_export.h"
  13. #include "net/base/network_change_notifier.h"
  14. #include "net/base/network_handle.h"
  15. namespace base {
  16. struct OnTaskRunnerDeleter;
  17. } // namespace base
  18. namespace net {
  19. class NetworkChangeNotifierAndroidTest;
  20. class NetworkChangeNotifierFactoryAndroid;
  21. // NetworkChangeNotifierAndroid observes network events from the Android
  22. // notification system and forwards them to observers.
  23. //
  24. // The implementation is complicated by the differing lifetime and thread
  25. // affinity requirements of Android notifications and of NetworkChangeNotifier.
  26. //
  27. // High-level overview:
  28. // NetworkChangeNotifier.java - Receives notifications from Android system, and
  29. // notifies native code via JNI (on the main application thread).
  30. // NetworkChangeNotifierDelegateAndroid ('Delegate') - Listens for notifications
  31. // sent via JNI on the main application thread, and forwards them to observers
  32. // on their threads. Owned by Factory, lives exclusively on main application
  33. // thread.
  34. // NetworkChangeNotifierFactoryAndroid ('Factory') - Creates the Delegate on the
  35. // main thread to receive JNI events, and vends Notifiers. Lives exclusively
  36. // on main application thread, and outlives all other classes.
  37. // NetworkChangeNotifierAndroid ('Notifier') - Receives event notifications from
  38. // the Delegate. Processes and forwards these events to the
  39. // NetworkChangeNotifier observers on their threads. May live on any thread
  40. // and be called by any thread.
  41. //
  42. // For more details, see the implementation file.
  43. //
  44. // Note: Alongside of NetworkChangeNotifier.java there is
  45. // NetworkActiveNotifier.java, which handles notifications for when the system
  46. // default network goes in to a high power state. These are handled separately
  47. // since listening to them is expensive (they are fired often) and currently
  48. // only bidi streams connection status check uses them.
  49. class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid
  50. : public NetworkChangeNotifier,
  51. public NetworkChangeNotifierDelegateAndroid::Observer {
  52. public:
  53. NetworkChangeNotifierAndroid(const NetworkChangeNotifierAndroid&) = delete;
  54. NetworkChangeNotifierAndroid& operator=(const NetworkChangeNotifierAndroid&) =
  55. delete;
  56. ~NetworkChangeNotifierAndroid() override;
  57. // NetworkChangeNotifier:
  58. ConnectionType GetCurrentConnectionType() const override;
  59. ConnectionCost GetCurrentConnectionCost() override;
  60. // Requires ACCESS_WIFI_STATE permission in order to provide precise WiFi link
  61. // speed.
  62. void GetCurrentMaxBandwidthAndConnectionType(
  63. double* max_bandwidth_mbps,
  64. ConnectionType* connection_type) const override;
  65. bool AreNetworkHandlesCurrentlySupported() const override;
  66. void GetCurrentConnectedNetworks(NetworkList* network_list) const override;
  67. ConnectionType GetCurrentNetworkConnectionType(
  68. handles::NetworkHandle network) const override;
  69. NetworkChangeNotifier::ConnectionSubtype GetCurrentConnectionSubtype()
  70. const override;
  71. handles::NetworkHandle GetCurrentDefaultNetwork() const override;
  72. bool IsDefaultNetworkActiveInternal() override;
  73. // NetworkChangeNotifierDelegateAndroid::Observer:
  74. void OnConnectionTypeChanged() override;
  75. void OnConnectionCostChanged() override;
  76. void OnMaxBandwidthChanged(double max_bandwidth_mbps,
  77. ConnectionType type) override;
  78. void OnNetworkConnected(handles::NetworkHandle network) override;
  79. void OnNetworkSoonToDisconnect(handles::NetworkHandle network) override;
  80. void OnNetworkDisconnected(handles::NetworkHandle network) override;
  81. void OnNetworkMadeDefault(handles::NetworkHandle network) override;
  82. void OnDefaultNetworkActive() override;
  83. // Promote GetMaxBandwidthMbpsForConnectionSubtype to public for the Android
  84. // delegate class.
  85. using NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype;
  86. static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsAndroid();
  87. void DefaultNetworkActiveObserverAdded() override;
  88. void DefaultNetworkActiveObserverRemoved() override;
  89. private:
  90. friend class NetworkChangeNotifierAndroidTest;
  91. friend class NetworkChangeNotifierFactoryAndroid;
  92. class BlockingThreadObjects;
  93. // Enable handles::NetworkHandles support for tests.
  94. void ForceNetworkHandlesSupportedForTesting();
  95. explicit NetworkChangeNotifierAndroid(
  96. NetworkChangeNotifierDelegateAndroid* delegate);
  97. const raw_ptr<NetworkChangeNotifierDelegateAndroid> delegate_;
  98. // A collection of objects that must live on blocking sequences. These objects
  99. // listen for notifications and relay the notifications to the registered
  100. // observers without posting back to the thread the object was created on.
  101. // Also used for DnsConfigService which also must live on blocking sequences.
  102. std::unique_ptr<BlockingThreadObjects, base::OnTaskRunnerDeleter>
  103. blocking_thread_objects_;
  104. bool force_network_handles_supported_for_testing_ = false;
  105. };
  106. } // namespace net
  107. #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_