From c94943b28b4876071a8d509f478e4c63ebb75ca2 Mon Sep 17 00:00:00 2001 From: jri Date: Thu, 3 Dec 2015 10:50:22 -0800 Subject: Adding platform support check for NetworkObserver Review URL: https://codereview.chromium.org/1454313002 Cr-Commit-Position: refs/heads/master@{#363020} --- net/android/network_change_notifier_android.cc | 16 +++++++++++++++- net/android/network_change_notifier_android.h | 5 +++++ net/android/network_change_notifier_android_unittest.cc | 6 ++++++ net/base/network_change_notifier.cc | 17 +++++++++++++++++ net/base/network_change_notifier.h | 14 ++++++++++++-- 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/net/android/network_change_notifier_android.cc b/net/android/network_change_notifier_android.cc index eac6589..cafa2cd 100644 --- a/net/android/network_change_notifier_android.cc +++ b/net/android/network_change_notifier_android.cc @@ -59,6 +59,7 @@ #include "net/android/network_change_notifier_android.h" +#include "base/android/build_info.h" #include "base/threading/thread.h" #include "net/base/address_tracker_linux.h" #include "net/dns/dns_config_service_posix.h" @@ -162,6 +163,18 @@ void NetworkChangeNotifierAndroid::GetCurrentMaxBandwidthAndConnectionType( connection_type); } +void NetworkChangeNotifierAndroid::ForceNetworkHandlesSupportedForTesting() { + force_network_handles_supported_for_testing_ = true; +} + +bool NetworkChangeNotifierAndroid::AreNetworkHandlesCurrentlySupported() const { + // Notifications for API using NetworkHandles and querying using + // NetworkHandles only implemented for Android versions >= L. + return force_network_handles_supported_for_testing_ || + (base::android::BuildInfo::GetInstance()->sdk_int() >= + base::android::SDK_VERSION_LOLLIPOP); +} + void NetworkChangeNotifierAndroid::GetCurrentConnectedNetworks( NetworkChangeNotifier::NetworkList* networks) const { delegate_->GetCurrentlyConnectedNetworks(networks); @@ -222,7 +235,8 @@ NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid( : NetworkChangeNotifier(NetworkChangeCalculatorParamsAndroid()), delegate_(delegate), dns_config_service_thread_( - new DnsConfigServiceThread(dns_config_for_testing)) { + new DnsConfigServiceThread(dns_config_for_testing)), + force_network_handles_supported_for_testing_(false) { CHECK_EQ(NetId::INVALID, NetworkChangeNotifier::kInvalidNetworkHandle) << "kInvalidNetworkHandle doesn't match NetId::INVALID"; delegate_->AddObserver(this); diff --git a/net/android/network_change_notifier_android.h b/net/android/network_change_notifier_android.h index b4870bf..89e7f19 100644 --- a/net/android/network_change_notifier_android.h +++ b/net/android/network_change_notifier_android.h @@ -52,6 +52,7 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid void GetCurrentMaxBandwidthAndConnectionType( double* max_bandwidth_mbps, ConnectionType* connection_type) const override; + bool AreNetworkHandlesCurrentlySupported() const override; void GetCurrentConnectedNetworks(NetworkList* network_list) const override; ConnectionType GetCurrentNetworkConnectionType( NetworkHandle network) const override; @@ -78,6 +79,9 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid class DnsConfigServiceThread; + // Enable NetworkHandles support for tests. + void ForceNetworkHandlesSupportedForTesting(); + NetworkChangeNotifierAndroid(NetworkChangeNotifierDelegateAndroid* delegate, const DnsConfig* dns_config_for_testing); @@ -85,6 +89,7 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid NetworkChangeNotifierDelegateAndroid* const delegate_; scoped_ptr dns_config_service_thread_; + bool force_network_handles_supported_for_testing_; DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierAndroid); }; diff --git a/net/android/network_change_notifier_android_unittest.cc b/net/android/network_change_notifier_android_unittest.cc index fdb94ef..bd5df46 100644 --- a/net/android/network_change_notifier_android_unittest.cc +++ b/net/android/network_change_notifier_android_unittest.cc @@ -317,6 +317,10 @@ class NetworkChangeNotifierAndroidTest &other_connection_type_observer_); } + void ForceNetworkHandlesSupportedForTesting() { + notifier_->ForceNetworkHandlesSupportedForTesting(); + } + NetworkChangeNotifierObserver connection_type_observer_; NetworkChangeNotifierObserver other_connection_type_observer_; NetworkChangeNotifier::DisableForTest disable_for_test_; @@ -387,6 +391,8 @@ TEST_F(NetworkChangeNotifierAndroidTest, InitialSignal) { } TEST_F(NetworkChangeNotifierAndroidTest, NetworkCallbacks) { + ForceNetworkHandlesSupportedForTesting(); + TestNetworkObserver network_observer; NetworkChangeNotifier::AddNetworkObserver(&network_observer); diff --git a/net/base/network_change_notifier.cc b/net/base/network_change_notifier.cc index fb480e9..187923c 100644 --- a/net/base/network_change_notifier.cc +++ b/net/base/network_change_notifier.cc @@ -634,7 +634,16 @@ double NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype( } // static +bool NetworkChangeNotifier::AreNetworkHandlesSupported() { + if (g_network_change_notifier) { + return g_network_change_notifier->AreNetworkHandlesCurrentlySupported(); + } + return false; +} + +// static void NetworkChangeNotifier::GetConnectedNetworks(NetworkList* network_list) { + DCHECK(AreNetworkHandlesSupported()); if (g_network_change_notifier) { g_network_change_notifier->GetCurrentConnectedNetworks(network_list); } else { @@ -645,6 +654,7 @@ void NetworkChangeNotifier::GetConnectedNetworks(NetworkList* network_list) { // static NetworkChangeNotifier::ConnectionType NetworkChangeNotifier::GetNetworkConnectionType(NetworkHandle network) { + DCHECK(AreNetworkHandlesSupported()); return g_network_change_notifier ? g_network_change_notifier->GetCurrentNetworkConnectionType( network) @@ -654,6 +664,7 @@ NetworkChangeNotifier::GetNetworkConnectionType(NetworkHandle network) { // static NetworkChangeNotifier::NetworkHandle NetworkChangeNotifier::GetDefaultNetwork() { + DCHECK(AreNetworkHandlesSupported()); return g_network_change_notifier ? g_network_change_notifier->GetCurrentDefaultNetwork() : kInvalidNetworkHandle; @@ -838,6 +849,7 @@ void NetworkChangeNotifier::AddMaxBandwidthObserver( } void NetworkChangeNotifier::AddNetworkObserver(NetworkObserver* observer) { + DCHECK(AreNetworkHandlesSupported()); if (g_network_change_notifier) { g_network_change_notifier->network_observer_list_->AddObserver(observer); } @@ -883,6 +895,7 @@ void NetworkChangeNotifier::RemoveMaxBandwidthObserver( } void NetworkChangeNotifier::RemoveNetworkObserver(NetworkObserver* observer) { + DCHECK(AreNetworkHandlesSupported()); if (g_network_change_notifier) { g_network_change_notifier->network_observer_list_->RemoveObserver(observer); } @@ -978,6 +991,10 @@ void NetworkChangeNotifier::GetCurrentMaxBandwidthAndConnectionType( : GetMaxBandwidthForConnectionSubtype(SUBTYPE_UNKNOWN); } +bool NetworkChangeNotifier::AreNetworkHandlesCurrentlySupported() const { + return false; +} + void NetworkChangeNotifier::GetCurrentConnectedNetworks( NetworkList* network_list) const { network_list->clear(); diff --git a/net/base/network_change_notifier.h b/net/base/network_change_notifier.h index 4290331..7ebff81 100644 --- a/net/base/network_change_notifier.h +++ b/net/base/network_change_notifier.h @@ -291,16 +291,24 @@ class NET_EXPORT NetworkChangeNotifier { // TODO(jkarlin): Rename to GetMaxBandwidthMbpsForConnectionSubtype. static double GetMaxBandwidthForConnectionSubtype(ConnectionSubtype subtype); + // Returns true if the platform supports use of APIs based on NetworkHandles. + // Public methods that use NetworkHandles are GetNetworkConnectionType(), + // GetNetworkConnectionType(), GetDefaultNetwork(), AddNetworkObserver(), + // RemoveNetworkObserver(), and all public NetworkObserver methods. + static bool AreNetworkHandlesSupported(); + // Sets |network_list| to a list of all networks that are currently connected. // Only implemented for Android (Lollipop and newer), leaves |network_list| - // empty when unimplemented. + // empty when unimplemented. Requires NetworkHandles support, see + // AreNetworkHandlesSupported(). static void GetConnectedNetworks(NetworkList* network_list); // Returns the type of connection |network| uses. Note that this may vary // slightly over time (e.g. CONNECTION_2G to CONNECTION_3G). If |network| // is no longer connected, it will return CONNECTION_UNKNOWN. // Only implemented for Android (Lollipop and newer), returns - // CONNECTION_UNKNOWN when unimplemented. + // CONNECTION_UNKNOWN when unimplemented. Requires NetworkHandles support, + // see AreNetworkHandlesSupported(). static ConnectionType GetNetworkConnectionType(NetworkHandle network); // Returns the device's current default network connection. This is the @@ -310,6 +318,7 @@ class NET_EXPORT NetworkChangeNotifier { // there is no default connected network. // Only implemented for Android (Lollipop and newer), returns // |kInvalidNetworkHandle| when unimplemented. + // Requires NetworkHandles support, see AreNetworkHandlesSupported(). static NetworkHandle GetDefaultNetwork(); // Retrieve the last read DnsConfig. This could be expensive if the system has @@ -481,6 +490,7 @@ class NET_EXPORT NetworkChangeNotifier { virtual void GetCurrentMaxBandwidthAndConnectionType( double* max_bandwidth_mbps, ConnectionType* connection_type) const; + virtual bool AreNetworkHandlesCurrentlySupported() const; virtual void GetCurrentConnectedNetworks(NetworkList* network_list) const; virtual ConnectionType GetCurrentNetworkConnectionType( NetworkHandle network) const; -- cgit v1.1