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