network_change_notifier_delegate_android.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_DELEGATE_ANDROID_H_
  5. #define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_ANDROID_H_
  6. #include <atomic>
  7. #include <map>
  8. #include "base/android/jni_android.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/observer_list_threadsafe.h"
  12. #include "base/synchronization/lock.h"
  13. #include "base/threading/thread_checker.h"
  14. #include "net/base/net_export.h"
  15. #include "net/base/network_change_notifier.h"
  16. #include "net/base/network_handle.h"
  17. namespace net {
  18. // Delegate used to thread-safely notify NetworkChangeNotifierAndroid whenever a
  19. // network connection change notification is signaled by the Java side (on the
  20. // JNI thread).
  21. // All the methods exposed below must be called exclusively on the JNI thread
  22. // unless otherwise stated (e.g. RegisterObserver()/UnregisterObserver()).
  23. class NET_EXPORT_PRIVATE NetworkChangeNotifierDelegateAndroid {
  24. public:
  25. typedef NetworkChangeNotifier::ConnectionCost ConnectionCost;
  26. typedef NetworkChangeNotifier::ConnectionType ConnectionType;
  27. typedef NetworkChangeNotifier::ConnectionSubtype ConnectionSubtype;
  28. typedef NetworkChangeNotifier::NetworkList NetworkList;
  29. // Observer interface implemented by NetworkChangeNotifierAndroid which
  30. // subscribes to network change notifications fired by the delegate (and
  31. // initiated by the Java side).
  32. class Observer : public NetworkChangeNotifier::NetworkObserver {
  33. public:
  34. ~Observer() override = default;
  35. // Updates the current connection type.
  36. virtual void OnConnectionTypeChanged() = 0;
  37. // Updates the current connection cost.
  38. virtual void OnConnectionCostChanged() = 0;
  39. // Updates the current max bandwidth.
  40. virtual void OnMaxBandwidthChanged(double max_bandwidth_mbps,
  41. ConnectionType connection_type) = 0;
  42. // Notifies that the default network has gone into a high power mode.
  43. virtual void OnDefaultNetworkActive() = 0;
  44. };
  45. // Initializes native (C++) side of NetworkChangeNotifierAndroid that
  46. // communicates with Java NetworkChangeNotifier class. The Java
  47. // NetworkChangeNotifier must have been previously initialized with calls
  48. // like this:
  49. // // Creates global singleton Java NetworkChangeNotifier class instance.
  50. // NetworkChangeNotifier.init();
  51. // // Creates Java NetworkChangeNotifierAutoDetect class instance.
  52. // NetworkChangeNotifier.registerToReceiveNotificationsAlways();
  53. NetworkChangeNotifierDelegateAndroid();
  54. NetworkChangeNotifierDelegateAndroid(
  55. const NetworkChangeNotifierDelegateAndroid&) = delete;
  56. NetworkChangeNotifierDelegateAndroid& operator=(
  57. const NetworkChangeNotifierDelegateAndroid&) = delete;
  58. ~NetworkChangeNotifierDelegateAndroid();
  59. // Called from NetworkChangeNotifier.java on the JNI thread whenever
  60. // the connection type changes. This updates the current connection type seen
  61. // by this class and forwards the notification to the observers that
  62. // subscribed through RegisterObserver().
  63. void NotifyConnectionTypeChanged(
  64. JNIEnv* env,
  65. const base::android::JavaParamRef<jobject>& obj,
  66. jint new_connection_type,
  67. jlong default_netid);
  68. jint GetConnectionType(JNIEnv* env, jobject obj) const;
  69. // Called from NetworkChangeNotifier.java on the JNI thread whenever
  70. // the connection cost changes. This updates the current connection cost seen
  71. // by this class and forwards the notification to the observers that
  72. // subscribed through RegisterObserver().
  73. void NotifyConnectionCostChanged(
  74. JNIEnv* env,
  75. const base::android::JavaParamRef<jobject>& obj,
  76. jint new_connection_cost);
  77. jint GetConnectionCost(JNIEnv* env, jobject obj);
  78. // Called from NetworkChangeNotifier.java on the JNI thread whenever
  79. // the maximum bandwidth of the connection changes. This updates the current
  80. // max bandwidth seen by this class and forwards the notification to the
  81. // observers that subscribed through RegisterObserver().
  82. void NotifyMaxBandwidthChanged(
  83. JNIEnv* env,
  84. const base::android::JavaParamRef<jobject>& obj,
  85. jint subtype);
  86. // Called from NetworkChangeNotifier.java on the JNI thread to push
  87. // down notifications of network connectivity events. These functions in
  88. // turn:
  89. // 1) Update |network_map_| and |default_network_|.
  90. // 2) Push notifications to NetworkChangeNotifier which in turn pushes
  91. // notifications to its NetworkObservers. Note that these functions
  92. // perform valuable transformations on the signals like deduplicating.
  93. // For descriptions of what individual calls mean, see
  94. // NetworkChangeNotifierAutoDetect.Observer functions of the same names.
  95. void NotifyOfNetworkConnect(JNIEnv* env,
  96. const base::android::JavaParamRef<jobject>& obj,
  97. jlong net_id,
  98. jint connection_type);
  99. void NotifyOfNetworkSoonToDisconnect(
  100. JNIEnv* env,
  101. const base::android::JavaParamRef<jobject>& obj,
  102. jlong net_id);
  103. void NotifyOfNetworkDisconnect(
  104. JNIEnv* env,
  105. const base::android::JavaParamRef<jobject>& obj,
  106. jlong net_id);
  107. void NotifyPurgeActiveNetworkList(
  108. JNIEnv* env,
  109. const base::android::JavaParamRef<jobject>& obj,
  110. const base::android::JavaParamRef<jlongArray>& active_networks);
  111. // Called from NetworkActiveNotifier.java on the JNI thread to push down
  112. // notifications of default network going in to high power mode.
  113. void NotifyOfDefaultNetworkActive(JNIEnv* env);
  114. // Registers/unregisters the observer which receives notifications from this
  115. // delegate. Notifications may be dispatched to the observer from any thread.
  116. // |observer| must not invoke (Register|Unregister)Observer() when receiving a
  117. // notification, because it would cause a reentrant lock acquisition.
  118. // |observer| must unregister itself before
  119. // ~NetworkChangeNotifierDelegateAndroid().
  120. void RegisterObserver(Observer* observer);
  121. void UnregisterObserver(Observer* observer);
  122. // Called by NetworkChangeNotifierAndroid to report when a
  123. // DefaultNetworkActiveObserver has been added (or removed) so that the
  124. // delegate can act on that (possibly enabling or disabling default network
  125. // active notifications).
  126. void DefaultNetworkActiveObserverRemoved();
  127. void DefaultNetworkActiveObserverAdded();
  128. // These methods are simply implementations of NetworkChangeNotifier APIs of
  129. // the same name. They can be called from any thread.
  130. ConnectionCost GetCurrentConnectionCost();
  131. ConnectionType GetCurrentConnectionType() const;
  132. void GetCurrentMaxBandwidthAndConnectionType(
  133. double* max_bandwidth_mbps,
  134. ConnectionType* connection_type) const;
  135. ConnectionType GetNetworkConnectionType(handles::NetworkHandle network) const;
  136. handles::NetworkHandle GetCurrentDefaultNetwork() const;
  137. void GetCurrentlyConnectedNetworks(NetworkList* network_list) const;
  138. bool IsDefaultNetworkActive();
  139. // Can only be called from the main (Java) thread.
  140. NetworkChangeNotifier::ConnectionSubtype GetCurrentConnectionSubtype() const;
  141. // Returns true if NetworkCallback failed to register, indicating that
  142. // network-specific callbacks will not be issued.
  143. bool RegisterNetworkCallbackFailed() const {
  144. return register_network_callback_failed_;
  145. }
  146. static void EnableNetworkChangeNotifierAutoDetectForTest();
  147. private:
  148. friend class BaseNetworkChangeNotifierAndroidTest;
  149. // Map of active connected networks and their connection type.
  150. typedef std::map<handles::NetworkHandle, ConnectionType> NetworkMap;
  151. // Converts a Java long[] into a NetworkMap. Expects long[] to contain
  152. // repeated instances of: handles::NetworkHandle, ConnectionType
  153. static void JavaLongArrayToNetworkMap(
  154. JNIEnv* env,
  155. const base::android::JavaRef<jlongArray>& long_array,
  156. NetworkMap* network_map);
  157. // These can be selectively enabled/disabled as they might be expensive to
  158. // listen to since they could be fired often.
  159. void EnableDefaultNetworkActiveNotifications();
  160. void DisableDefaultNetworkActiveNotifications();
  161. // Setters that grab appropriate lock.
  162. void SetCurrentConnectionCost(ConnectionCost connection_cost);
  163. void SetCurrentConnectionType(ConnectionType connection_type);
  164. void SetCurrentMaxBandwidth(double max_bandwidth);
  165. void SetCurrentDefaultNetwork(handles::NetworkHandle default_network);
  166. void SetCurrentNetworksAndTypes(NetworkMap network_map);
  167. // Methods calling the Java side exposed for testing.
  168. void SetOnline();
  169. void SetOffline();
  170. void FakeNetworkConnected(handles::NetworkHandle network,
  171. ConnectionType type);
  172. void FakeNetworkSoonToBeDisconnected(handles::NetworkHandle network);
  173. void FakeNetworkDisconnected(handles::NetworkHandle network);
  174. void FakePurgeActiveNetworkList(NetworkList networks);
  175. void FakeDefaultNetwork(handles::NetworkHandle network, ConnectionType type);
  176. void FakeConnectionCostChanged(ConnectionCost cost);
  177. void FakeConnectionSubtypeChanged(ConnectionSubtype subtype);
  178. void FakeDefaultNetworkActive();
  179. THREAD_CHECKER(thread_checker_);
  180. base::Lock observer_lock_;
  181. raw_ptr<Observer> observer_ GUARDED_BY(observer_lock_) = nullptr;
  182. const base::android::ScopedJavaGlobalRef<jobject>
  183. java_network_change_notifier_;
  184. // True if NetworkCallback failed to register, indicating that
  185. // network-specific callbacks will not be issued.
  186. const bool register_network_callback_failed_;
  187. base::android::ScopedJavaGlobalRef<jobject> java_network_active_notifier_;
  188. // True if DefaultNetworkActive type of info are not supported, indicating
  189. // that we shouldn't try to enable its callback or query its status.
  190. const bool is_default_network_active_api_supported_;
  191. mutable base::Lock connection_lock_; // Protects the state below.
  192. ConnectionType connection_type_;
  193. ConnectionCost connection_cost_;
  194. double connection_max_bandwidth_;
  195. handles::NetworkHandle default_network_;
  196. NetworkMap network_map_;
  197. // Used to enable/disable default network active notifications on the Java
  198. // side.
  199. std::atomic_int default_network_active_observers_ = 0;
  200. };
  201. } // namespace net
  202. #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_DELEGATE_ANDROID_H_