Commit c94943b2 authored by jri's avatar jri Committed by Commit bot

Adding platform support check for NetworkObserver

Review URL: https://codereview.chromium.org/1454313002

Cr-Commit-Position: refs/heads/master@{#363020}
parent aa2a5a49
...@@ -59,6 +59,7 @@ ...@@ -59,6 +59,7 @@
#include "net/android/network_change_notifier_android.h" #include "net/android/network_change_notifier_android.h"
#include "base/android/build_info.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "net/base/address_tracker_linux.h" #include "net/base/address_tracker_linux.h"
#include "net/dns/dns_config_service_posix.h" #include "net/dns/dns_config_service_posix.h"
...@@ -162,6 +163,18 @@ void NetworkChangeNotifierAndroid::GetCurrentMaxBandwidthAndConnectionType( ...@@ -162,6 +163,18 @@ void NetworkChangeNotifierAndroid::GetCurrentMaxBandwidthAndConnectionType(
connection_type); 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( void NetworkChangeNotifierAndroid::GetCurrentConnectedNetworks(
NetworkChangeNotifier::NetworkList* networks) const { NetworkChangeNotifier::NetworkList* networks) const {
delegate_->GetCurrentlyConnectedNetworks(networks); delegate_->GetCurrentlyConnectedNetworks(networks);
...@@ -222,7 +235,8 @@ NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid( ...@@ -222,7 +235,8 @@ NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid(
: NetworkChangeNotifier(NetworkChangeCalculatorParamsAndroid()), : NetworkChangeNotifier(NetworkChangeCalculatorParamsAndroid()),
delegate_(delegate), delegate_(delegate),
dns_config_service_thread_( 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) CHECK_EQ(NetId::INVALID, NetworkChangeNotifier::kInvalidNetworkHandle)
<< "kInvalidNetworkHandle doesn't match NetId::INVALID"; << "kInvalidNetworkHandle doesn't match NetId::INVALID";
delegate_->AddObserver(this); delegate_->AddObserver(this);
......
...@@ -52,6 +52,7 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid ...@@ -52,6 +52,7 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid
void GetCurrentMaxBandwidthAndConnectionType( void GetCurrentMaxBandwidthAndConnectionType(
double* max_bandwidth_mbps, double* max_bandwidth_mbps,
ConnectionType* connection_type) const override; ConnectionType* connection_type) const override;
bool AreNetworkHandlesCurrentlySupported() const override;
void GetCurrentConnectedNetworks(NetworkList* network_list) const override; void GetCurrentConnectedNetworks(NetworkList* network_list) const override;
ConnectionType GetCurrentNetworkConnectionType( ConnectionType GetCurrentNetworkConnectionType(
NetworkHandle network) const override; NetworkHandle network) const override;
...@@ -78,6 +79,9 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid ...@@ -78,6 +79,9 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid
class DnsConfigServiceThread; class DnsConfigServiceThread;
// Enable NetworkHandles support for tests.
void ForceNetworkHandlesSupportedForTesting();
NetworkChangeNotifierAndroid(NetworkChangeNotifierDelegateAndroid* delegate, NetworkChangeNotifierAndroid(NetworkChangeNotifierDelegateAndroid* delegate,
const DnsConfig* dns_config_for_testing); const DnsConfig* dns_config_for_testing);
...@@ -85,6 +89,7 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid ...@@ -85,6 +89,7 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierAndroid
NetworkChangeNotifierDelegateAndroid* const delegate_; NetworkChangeNotifierDelegateAndroid* const delegate_;
scoped_ptr<DnsConfigServiceThread> dns_config_service_thread_; scoped_ptr<DnsConfigServiceThread> dns_config_service_thread_;
bool force_network_handles_supported_for_testing_;
DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierAndroid); DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierAndroid);
}; };
......
...@@ -317,6 +317,10 @@ class NetworkChangeNotifierAndroidTest ...@@ -317,6 +317,10 @@ class NetworkChangeNotifierAndroidTest
&other_connection_type_observer_); &other_connection_type_observer_);
} }
void ForceNetworkHandlesSupportedForTesting() {
notifier_->ForceNetworkHandlesSupportedForTesting();
}
NetworkChangeNotifierObserver connection_type_observer_; NetworkChangeNotifierObserver connection_type_observer_;
NetworkChangeNotifierObserver other_connection_type_observer_; NetworkChangeNotifierObserver other_connection_type_observer_;
NetworkChangeNotifier::DisableForTest disable_for_test_; NetworkChangeNotifier::DisableForTest disable_for_test_;
...@@ -387,6 +391,8 @@ TEST_F(NetworkChangeNotifierAndroidTest, InitialSignal) { ...@@ -387,6 +391,8 @@ TEST_F(NetworkChangeNotifierAndroidTest, InitialSignal) {
} }
TEST_F(NetworkChangeNotifierAndroidTest, NetworkCallbacks) { TEST_F(NetworkChangeNotifierAndroidTest, NetworkCallbacks) {
ForceNetworkHandlesSupportedForTesting();
TestNetworkObserver network_observer; TestNetworkObserver network_observer;
NetworkChangeNotifier::AddNetworkObserver(&network_observer); NetworkChangeNotifier::AddNetworkObserver(&network_observer);
......
...@@ -633,8 +633,17 @@ double NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype( ...@@ -633,8 +633,17 @@ double NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
return std::numeric_limits<double>::infinity(); return std::numeric_limits<double>::infinity();
} }
// static
bool NetworkChangeNotifier::AreNetworkHandlesSupported() {
if (g_network_change_notifier) {
return g_network_change_notifier->AreNetworkHandlesCurrentlySupported();
}
return false;
}
// static // static
void NetworkChangeNotifier::GetConnectedNetworks(NetworkList* network_list) { void NetworkChangeNotifier::GetConnectedNetworks(NetworkList* network_list) {
DCHECK(AreNetworkHandlesSupported());
if (g_network_change_notifier) { if (g_network_change_notifier) {
g_network_change_notifier->GetCurrentConnectedNetworks(network_list); g_network_change_notifier->GetCurrentConnectedNetworks(network_list);
} else { } else {
...@@ -645,6 +654,7 @@ void NetworkChangeNotifier::GetConnectedNetworks(NetworkList* network_list) { ...@@ -645,6 +654,7 @@ void NetworkChangeNotifier::GetConnectedNetworks(NetworkList* network_list) {
// static // static
NetworkChangeNotifier::ConnectionType NetworkChangeNotifier::ConnectionType
NetworkChangeNotifier::GetNetworkConnectionType(NetworkHandle network) { NetworkChangeNotifier::GetNetworkConnectionType(NetworkHandle network) {
DCHECK(AreNetworkHandlesSupported());
return g_network_change_notifier return g_network_change_notifier
? g_network_change_notifier->GetCurrentNetworkConnectionType( ? g_network_change_notifier->GetCurrentNetworkConnectionType(
network) network)
...@@ -654,6 +664,7 @@ NetworkChangeNotifier::GetNetworkConnectionType(NetworkHandle network) { ...@@ -654,6 +664,7 @@ NetworkChangeNotifier::GetNetworkConnectionType(NetworkHandle network) {
// static // static
NetworkChangeNotifier::NetworkHandle NetworkChangeNotifier::NetworkHandle
NetworkChangeNotifier::GetDefaultNetwork() { NetworkChangeNotifier::GetDefaultNetwork() {
DCHECK(AreNetworkHandlesSupported());
return g_network_change_notifier return g_network_change_notifier
? g_network_change_notifier->GetCurrentDefaultNetwork() ? g_network_change_notifier->GetCurrentDefaultNetwork()
: kInvalidNetworkHandle; : kInvalidNetworkHandle;
...@@ -838,6 +849,7 @@ void NetworkChangeNotifier::AddMaxBandwidthObserver( ...@@ -838,6 +849,7 @@ void NetworkChangeNotifier::AddMaxBandwidthObserver(
} }
void NetworkChangeNotifier::AddNetworkObserver(NetworkObserver* observer) { void NetworkChangeNotifier::AddNetworkObserver(NetworkObserver* observer) {
DCHECK(AreNetworkHandlesSupported());
if (g_network_change_notifier) { if (g_network_change_notifier) {
g_network_change_notifier->network_observer_list_->AddObserver(observer); g_network_change_notifier->network_observer_list_->AddObserver(observer);
} }
...@@ -883,6 +895,7 @@ void NetworkChangeNotifier::RemoveMaxBandwidthObserver( ...@@ -883,6 +895,7 @@ void NetworkChangeNotifier::RemoveMaxBandwidthObserver(
} }
void NetworkChangeNotifier::RemoveNetworkObserver(NetworkObserver* observer) { void NetworkChangeNotifier::RemoveNetworkObserver(NetworkObserver* observer) {
DCHECK(AreNetworkHandlesSupported());
if (g_network_change_notifier) { if (g_network_change_notifier) {
g_network_change_notifier->network_observer_list_->RemoveObserver(observer); g_network_change_notifier->network_observer_list_->RemoveObserver(observer);
} }
...@@ -978,6 +991,10 @@ void NetworkChangeNotifier::GetCurrentMaxBandwidthAndConnectionType( ...@@ -978,6 +991,10 @@ void NetworkChangeNotifier::GetCurrentMaxBandwidthAndConnectionType(
: GetMaxBandwidthForConnectionSubtype(SUBTYPE_UNKNOWN); : GetMaxBandwidthForConnectionSubtype(SUBTYPE_UNKNOWN);
} }
bool NetworkChangeNotifier::AreNetworkHandlesCurrentlySupported() const {
return false;
}
void NetworkChangeNotifier::GetCurrentConnectedNetworks( void NetworkChangeNotifier::GetCurrentConnectedNetworks(
NetworkList* network_list) const { NetworkList* network_list) const {
network_list->clear(); network_list->clear();
......
...@@ -291,16 +291,24 @@ class NET_EXPORT NetworkChangeNotifier { ...@@ -291,16 +291,24 @@ class NET_EXPORT NetworkChangeNotifier {
// TODO(jkarlin): Rename to GetMaxBandwidthMbpsForConnectionSubtype. // TODO(jkarlin): Rename to GetMaxBandwidthMbpsForConnectionSubtype.
static double GetMaxBandwidthForConnectionSubtype(ConnectionSubtype subtype); 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. // Sets |network_list| to a list of all networks that are currently connected.
// Only implemented for Android (Lollipop and newer), leaves |network_list| // 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); static void GetConnectedNetworks(NetworkList* network_list);
// Returns the type of connection |network| uses. Note that this may vary // Returns the type of connection |network| uses. Note that this may vary
// slightly over time (e.g. CONNECTION_2G to CONNECTION_3G). If |network| // slightly over time (e.g. CONNECTION_2G to CONNECTION_3G). If |network|
// is no longer connected, it will return CONNECTION_UNKNOWN. // is no longer connected, it will return CONNECTION_UNKNOWN.
// Only implemented for Android (Lollipop and newer), returns // 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); static ConnectionType GetNetworkConnectionType(NetworkHandle network);
// Returns the device's current default network connection. This is the // Returns the device's current default network connection. This is the
...@@ -310,6 +318,7 @@ class NET_EXPORT NetworkChangeNotifier { ...@@ -310,6 +318,7 @@ class NET_EXPORT NetworkChangeNotifier {
// there is no default connected network. // there is no default connected network.
// Only implemented for Android (Lollipop and newer), returns // Only implemented for Android (Lollipop and newer), returns
// |kInvalidNetworkHandle| when unimplemented. // |kInvalidNetworkHandle| when unimplemented.
// Requires NetworkHandles support, see AreNetworkHandlesSupported().
static NetworkHandle GetDefaultNetwork(); static NetworkHandle GetDefaultNetwork();
// Retrieve the last read DnsConfig. This could be expensive if the system has // Retrieve the last read DnsConfig. This could be expensive if the system has
...@@ -481,6 +490,7 @@ class NET_EXPORT NetworkChangeNotifier { ...@@ -481,6 +490,7 @@ class NET_EXPORT NetworkChangeNotifier {
virtual void GetCurrentMaxBandwidthAndConnectionType( virtual void GetCurrentMaxBandwidthAndConnectionType(
double* max_bandwidth_mbps, double* max_bandwidth_mbps,
ConnectionType* connection_type) const; ConnectionType* connection_type) const;
virtual bool AreNetworkHandlesCurrentlySupported() const;
virtual void GetCurrentConnectedNetworks(NetworkList* network_list) const; virtual void GetCurrentConnectedNetworks(NetworkList* network_list) const;
virtual ConnectionType GetCurrentNetworkConnectionType( virtual ConnectionType GetCurrentNetworkConnectionType(
NetworkHandle network) const; NetworkHandle network) const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment