summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjkarlin <jkarlin@chromium.org>2014-12-16 11:59:47 -0800
committerCommit bot <commit-bot@chromium.org>2014-12-16 20:00:23 +0000
commit7b25e6c3b62270e30e92ff19a5fa915d5b6213f9 (patch)
treee2209a52feccf54df6b48e5d46a8de1078fb1c81
parent2c719d3954743e3baa47ca02624dc43391f0bec5 (diff)
downloadchromium_src-7b25e6c3b62270e30e92ff19a5fa915d5b6213f9.zip
chromium_src-7b25e6c3b62270e30e92ff19a5fa915d5b6213f9.tar.gz
chromium_src-7b25e6c3b62270e30e92ff19a5fa915d5b6213f9.tar.bz2
[NetInfo] Add a MaxBandwidthChangeObserver and Android implementation
* Add a MaxBandwidthChangeObserver to NetworkChangeNotifier * Android notifies of max bandwidth changes at two points: 1. On connection type change (if the bandwidth changes) 2. On wifi signal strength change BUG=433370 Review URL: https://codereview.chromium.org/780293003 Cr-Commit-Position: refs/heads/master@{#308647}
-rw-r--r--net/android/java/src/org/chromium/net/NetworkChangeNotifier.java33
-rw-r--r--net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java77
-rw-r--r--net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java67
-rw-r--r--net/android/network_change_notifier_android.cc6
-rw-r--r--net/android/network_change_notifier_android.h1
-rw-r--r--net/android/network_change_notifier_android_unittest.cc46
-rw-r--r--net/android/network_change_notifier_delegate_android.cc17
-rw-r--r--net/android/network_change_notifier_delegate_android.h11
-rw-r--r--net/base/network_change_notifier.cc55
-rw-r--r--net/base/network_change_notifier.h33
10 files changed, 264 insertions, 82 deletions
diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
index 928af78..4db6d47 100644
--- a/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
+++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
@@ -73,7 +73,7 @@ public class NetworkChangeNotifier {
}
@CalledByNative
- public double getCurrentMaxBandwidth() {
+ public double getCurrentMaxBandwidthInMbps() {
return mCurrentMaxBandwidth;
}
@@ -147,14 +147,19 @@ public class NetworkChangeNotifier {
new NetworkChangeNotifierAutoDetect.Observer() {
@Override
public void onConnectionTypeChanged(int newConnectionType) {
- updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthInMbps());
updateCurrentConnectionType(newConnectionType);
}
+ @Override
+ public void onMaxBandwidthChanged(double maxBandwidthMbps) {
+ updateCurrentMaxBandwidth(maxBandwidthMbps);
+ }
},
mContext,
alwaysWatchForChanges);
- updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthInMbps());
- updateCurrentConnectionType(mAutoDetector.getCurrentConnectionType());
+ final NetworkChangeNotifierAutoDetect.NetworkState networkState =
+ mAutoDetector.getCurrentNetworkState();
+ updateCurrentConnectionType(mAutoDetector.getCurrentConnectionType(networkState));
+ updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthInMbps(networkState));
}
} else {
destroyAutoDetector();
@@ -177,9 +182,9 @@ public class NetworkChangeNotifier {
boolean connectionCurrentlyExists =
mCurrentConnectionType != ConnectionType.CONNECTION_NONE;
if (connectionCurrentlyExists != forceOnline) {
- updateCurrentMaxBandwidth(forceOnline ? Double.POSITIVE_INFINITY : 0.0);
updateCurrentConnectionType(forceOnline ? ConnectionType.CONNECTION_UNKNOWN
: ConnectionType.CONNECTION_NONE);
+ updateCurrentMaxBandwidth(forceOnline ? Double.POSITIVE_INFINITY : 0.0);
}
}
@@ -188,8 +193,10 @@ public class NetworkChangeNotifier {
notifyObserversOfConnectionTypeChange(newConnectionType);
}
- private void updateCurrentMaxBandwidth(double maxBandwidth) {
- mCurrentMaxBandwidth = maxBandwidth;
+ private void updateCurrentMaxBandwidth(double maxBandwidthMbps) {
+ if (maxBandwidthMbps == mCurrentMaxBandwidth) return;
+ mCurrentMaxBandwidth = maxBandwidthMbps;
+ notifyObserversOfMaxBandwidthChange(maxBandwidthMbps);
}
/**
@@ -205,6 +212,15 @@ public class NetworkChangeNotifier {
}
/**
+ * Alerts all observers of a bandwidth change.
+ */
+ void notifyObserversOfMaxBandwidthChange(double maxBandwidthMbps) {
+ for (Long nativeChangeNotifier : mNativeChangeNotifiers) {
+ nativeNotifyMaxBandwidthChanged(nativeChangeNotifier, maxBandwidthMbps);
+ }
+ }
+
+ /**
* Adds an observer for any connection type changes.
*/
public static void addConnectionTypeObserver(ConnectionTypeObserver observer) {
@@ -229,6 +245,9 @@ public class NetworkChangeNotifier {
@NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid")
private native void nativeNotifyConnectionTypeChanged(long nativePtr, int newConnectionType);
+ @NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid")
+ private native void nativeNotifyMaxBandwidthChanged(long nativePtr, double maxBandwidthMbps);
+
private static native double nativeGetMaxBandwidthForConnectionSubtype(int subtype);
// For testing only.
diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
index 12ae465..cdd85f6 100644
--- a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
+++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
@@ -130,12 +130,15 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
// though the connection type hasn't changed.
return wifiInfo.getLinkSpeed();
}
+
+ boolean getHasWifiPermission() {
+ return mHasWifiPermission;
+ }
}
private static final String TAG = "NetworkChangeNotifierAutoDetect";
private static final int UNKNOWN_LINK_SPEED = -1;
- private final NetworkConnectivityIntentFilter mIntentFilter =
- new NetworkConnectivityIntentFilter();
+ private final NetworkConnectivityIntentFilter mIntentFilter;
private final Observer mObserver;
@@ -145,12 +148,15 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
private boolean mRegistered;
private int mConnectionType;
private String mWifiSSID;
+ private double mMaxBandwidthMbps;
/**
- * Observer notified on the UI thread whenever a new connection type was detected.
+ * Observer notified on the UI thread whenever a new connection type was detected or max
+ * bandwidth is changed.
*/
public static interface Observer {
public void onConnectionTypeChanged(int newConnectionType);
+ public void onMaxBandwidthChanged(double maxBandwidthMbps);
}
/**
@@ -164,13 +170,17 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
mContext = context.getApplicationContext();
mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context);
mWifiManagerDelegate = new WifiManagerDelegate(context);
- mConnectionType = getCurrentConnectionType();
- mWifiSSID = getCurrentWifiSSID();
+ final NetworkState networkState = mConnectivityManagerDelegate.getNetworkState();
+ mConnectionType = getCurrentConnectionType(networkState);
+ mWifiSSID = getCurrentWifiSSID(networkState);
+ mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState);
if (alwaysWatchForChanges) {
registerReceiver();
} else {
ApplicationStatus.registerApplicationStateListener(this);
}
+ mIntentFilter =
+ new NetworkConnectivityIntentFilter(mWifiManagerDelegate.getHasWifiPermission());
}
/**
@@ -211,9 +221,11 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
}
}
- public int getCurrentConnectionType() {
- // Track exactly what type of connection we have.
- final NetworkState networkState = mConnectivityManagerDelegate.getNetworkState();
+ public NetworkState getCurrentNetworkState() {
+ return mConnectivityManagerDelegate.getNetworkState();
+ }
+
+ public int getCurrentConnectionType(NetworkState networkState) {
if (!networkState.isConnected()) {
return ConnectionType.CONNECTION_NONE;
}
@@ -261,13 +273,9 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
* derived from the NetInfo v3 specification's mapping from network type to
* max link speed. In cases where more information is available, such as wifi,
* that is used instead. For more on NetInfo, see http://w3c.github.io/netinfo/.
- *
- * TODO(jkarlin): Add a notification of bandwidth change to the NetworkChangeNotifier.
- * Without that the MaxBandwidth value will be stale until the network type or address
- * changes again.
*/
- public double getCurrentMaxBandwidthInMbps() {
- if (getCurrentConnectionType() == ConnectionType.CONNECTION_WIFI) {
+ public double getCurrentMaxBandwidthInMbps(NetworkState networkState) {
+ if (getCurrentConnectionType(networkState) == ConnectionType.CONNECTION_WIFI) {
final int link_speed = mWifiManagerDelegate.getLinkSpeedInMbps();
if (link_speed != UNKNOWN_LINK_SPEED) {
return link_speed;
@@ -275,11 +283,10 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
}
return NetworkChangeNotifier.getMaxBandwidthForConnectionSubtype(
- getCurrentConnectionSubtype());
+ getCurrentConnectionSubtype(networkState));
}
- private int getCurrentConnectionSubtype() {
- final NetworkState networkState = mConnectivityManagerDelegate.getNetworkState();
+ private int getCurrentConnectionSubtype(NetworkState networkState) {
if (!networkState.isConnected()) {
return ConnectionSubtype.SUBTYPE_NONE;
}
@@ -331,34 +338,40 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
}
}
- private String getCurrentWifiSSID() {
- if (getCurrentConnectionType() != ConnectionType.CONNECTION_WIFI)
- return "";
+ private String getCurrentWifiSSID(NetworkState networkState) {
+ if (getCurrentConnectionType(networkState) != ConnectionType.CONNECTION_WIFI) return "";
return mWifiManagerDelegate.getWifiSSID();
}
// BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
- connectionTypeChanged();
+ final NetworkState networkState = getCurrentNetworkState();
+ if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
+ connectionTypeChanged(networkState);
+ maxBandwidthChanged(networkState);
+ } else if (WifiManager.RSSI_CHANGED_ACTION.equals(intent.getAction())) {
+ maxBandwidthChanged(networkState);
+ }
}
// ApplicationStatus.ApplicationStateListener
@Override
public void onApplicationStateChange(int newState) {
+ final NetworkState networkState = getCurrentNetworkState();
if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) {
- connectionTypeChanged();
+ connectionTypeChanged(networkState);
+ maxBandwidthChanged(networkState);
registerReceiver();
} else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) {
unregisterReceiver();
}
}
- private void connectionTypeChanged() {
- int newConnectionType = getCurrentConnectionType();
- String newWifiSSID = getCurrentWifiSSID();
- if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID))
- return;
+ private void connectionTypeChanged(NetworkState networkState) {
+ int newConnectionType = getCurrentConnectionType(networkState);
+ String newWifiSSID = getCurrentWifiSSID(networkState);
+ if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID)) return;
mConnectionType = newConnectionType;
mWifiSSID = newWifiSSID;
@@ -366,9 +379,17 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
mObserver.onConnectionTypeChanged(newConnectionType);
}
+ private void maxBandwidthChanged(NetworkState networkState) {
+ double newMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState);
+ if (newMaxBandwidthMbps == mMaxBandwidthMbps) return;
+ mMaxBandwidthMbps = newMaxBandwidthMbps;
+ mObserver.onMaxBandwidthChanged(newMaxBandwidthMbps);
+ }
+
private static class NetworkConnectivityIntentFilter extends IntentFilter {
- NetworkConnectivityIntentFilter() {
+ NetworkConnectivityIntentFilter(boolean monitorRSSI) {
addAction(ConnectivityManager.CONNECTIVITY_ACTION);
+ if (monitorRSSI) addAction(WifiManager.RSSI_CHANGED_ACTION);
}
}
}
diff --git a/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java b/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
index f4eafd9..d2229dc 100644
--- a/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
+++ b/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
@@ -7,6 +7,7 @@ package org.chromium.net;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
+import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.test.InstrumentationTestCase;
import android.test.UiThreadTest;
@@ -125,6 +126,49 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
mWifiDelegate.setWifiSSID("foo");
}
+ private double getCurrentMaxBandwidthInMbps() {
+ final NetworkChangeNotifierAutoDetect.NetworkState networkState =
+ mReceiver.getCurrentNetworkState();
+ return mReceiver.getCurrentMaxBandwidthInMbps(networkState);
+ }
+
+ private int getCurrentConnectionType() {
+ final NetworkChangeNotifierAutoDetect.NetworkState networkState =
+ mReceiver.getCurrentNetworkState();
+ return mReceiver.getCurrentConnectionType(networkState);
+ }
+
+ /**
+ * Tests that changing the RSSI_CHANGED_ACTION intent updates MaxBandwidth.
+ */
+ @UiThreadTest
+ @MediumTest
+ @Feature({"Android-AppBase"})
+ public void testNetworkChangeNotifierRSSIEventUpdatesMaxBandwidthForWiFi()
+ throws InterruptedException {
+ NetworkChangeNotifier notifier = NetworkChangeNotifier.getInstance();
+ mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI);
+ mWifiDelegate.setLinkSpeedInMbps(42);
+ Intent intent = new Intent(WifiManager.RSSI_CHANGED_ACTION);
+ mReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
+
+ assertEquals(42.0, notifier.getCurrentMaxBandwidthInMbps());
+
+ // Changing the link speed has no effect until the intent fires.
+ mWifiDelegate.setLinkSpeedInMbps(80);
+ assertEquals(42.0, notifier.getCurrentMaxBandwidthInMbps());
+
+ // Fire the intent.
+ mReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
+ assertEquals(80.0, notifier.getCurrentMaxBandwidthInMbps());
+
+ // Firing a network type change intent also causes max bandwidth to update.
+ mWifiDelegate.setLinkSpeedInMbps(20);
+ intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
+ mReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
+ assertEquals(20.0, notifier.getCurrentMaxBandwidthInMbps());
+ }
+
/**
* Tests that changing the network type changes the maxBandwidth.
*/
@@ -134,9 +178,8 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
public void testNetworkChangeNotifierMaxBandwidthEthernet() throws InterruptedException {
// Show that for Ethernet the link speed is unknown (+Infinity).
mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_ETHERNET);
- assertEquals(ConnectionType.CONNECTION_ETHERNET,
- mReceiver.getCurrentConnectionType());
- assertEquals(Double.POSITIVE_INFINITY, mReceiver.getCurrentMaxBandwidthInMbps());
+ assertEquals(ConnectionType.CONNECTION_ETHERNET, getCurrentConnectionType());
+ assertEquals(Double.POSITIVE_INFINITY, getCurrentMaxBandwidthInMbps());
}
@UiThreadTest
@@ -146,8 +189,8 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
// Test that for wifi types the link speed is read from the WifiManager.
mWifiDelegate.setLinkSpeedInMbps(42);
mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI);
- assertEquals(ConnectionType.CONNECTION_WIFI, mReceiver.getCurrentConnectionType());
- assertEquals(42.0, mReceiver.getCurrentMaxBandwidthInMbps());
+ assertEquals(ConnectionType.CONNECTION_WIFI, getCurrentConnectionType());
+ assertEquals(42.0, getCurrentMaxBandwidthInMbps());
}
@UiThreadTest
@@ -158,9 +201,8 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
// TODO(jkarlin): Add support for CONNECTION_WIMAX as specified in
// http://w3c.github.io/netinfo/.
mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIMAX);
- assertEquals(ConnectionType.CONNECTION_4G,
- mReceiver.getCurrentConnectionType());
- assertEquals(Double.POSITIVE_INFINITY, mReceiver.getCurrentMaxBandwidthInMbps());
+ assertEquals(ConnectionType.CONNECTION_4G, getCurrentConnectionType());
+ assertEquals(Double.POSITIVE_INFINITY, getCurrentMaxBandwidthInMbps());
}
@UiThreadTest
@@ -169,9 +211,8 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
public void testNetworkChangeNotifierMaxBandwidthBluetooth() throws InterruptedException {
// Show that for bluetooth the link speed is unknown (+Infinity).
mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_BLUETOOTH);
- assertEquals(ConnectionType.CONNECTION_BLUETOOTH,
- mReceiver.getCurrentConnectionType());
- assertEquals(Double.POSITIVE_INFINITY, mReceiver.getCurrentMaxBandwidthInMbps());
+ assertEquals(ConnectionType.CONNECTION_BLUETOOTH, getCurrentConnectionType());
+ assertEquals(Double.POSITIVE_INFINITY, getCurrentMaxBandwidthInMbps());
}
@UiThreadTest
@@ -181,8 +222,8 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
// Test that for mobile types the subtype is used to determine the maxBandwidth.
mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_MOBILE);
mConnectivityDelegate.setNetworkSubtype(TelephonyManager.NETWORK_TYPE_LTE);
- assertEquals(ConnectionType.CONNECTION_4G, mReceiver.getCurrentConnectionType());
- assertEquals(100.0, mReceiver.getCurrentMaxBandwidthInMbps());
+ assertEquals(ConnectionType.CONNECTION_4G, getCurrentConnectionType());
+ assertEquals(100.0, getCurrentMaxBandwidthInMbps());
}
/**
diff --git a/net/android/network_change_notifier_android.cc b/net/android/network_change_notifier_android.cc
index 2063e7b..0a109b8 100644
--- a/net/android/network_change_notifier_android.cc
+++ b/net/android/network_change_notifier_android.cc
@@ -122,6 +122,12 @@ void NetworkChangeNotifierAndroid::OnConnectionTypeChanged() {
DnsConfigServiceThread::NotifyNetworkChangeNotifierObservers();
}
+void NetworkChangeNotifierAndroid::OnMaxBandwidthChanged(
+ double max_bandwidth_mbps) {
+ NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(
+ max_bandwidth_mbps);
+}
+
// static
bool NetworkChangeNotifierAndroid::Register(JNIEnv* env) {
return NetworkChangeNotifierDelegateAndroid::Register(env);
diff --git a/net/android/network_change_notifier_android.h b/net/android/network_change_notifier_android.h
index 7710c26..0d90a56 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
// NetworkChangeNotifierDelegateAndroid::Observer:
virtual void OnConnectionTypeChanged() override;
+ virtual void OnMaxBandwidthChanged(double max_bandwidth_mbps) override;
static bool Register(JNIEnv* env);
diff --git a/net/android/network_change_notifier_android_unittest.cc b/net/android/network_change_notifier_android_unittest.cc
index 0818d5a..54e5583 100644
--- a/net/android/network_change_notifier_android_unittest.cc
+++ b/net/android/network_change_notifier_android_unittest.cc
@@ -21,19 +21,26 @@ namespace {
class NetworkChangeNotifierDelegateAndroidObserver
: public NetworkChangeNotifierDelegateAndroid::Observer {
public:
- NetworkChangeNotifierDelegateAndroidObserver() : notifications_count_(0) {}
+ NetworkChangeNotifierDelegateAndroidObserver()
+ : type_notifications_count_(0), max_bandwidth_notifications_count_(0) {}
// NetworkChangeNotifierDelegateAndroid::Observer:
virtual void OnConnectionTypeChanged() override {
- notifications_count_++;
+ type_notifications_count_++;
}
- int notifications_count() const {
- return notifications_count_;
+ virtual void OnMaxBandwidthChanged(double max_bandwidth_mbps) override {
+ max_bandwidth_notifications_count_++;
+ }
+
+ int type_notifications_count() const { return type_notifications_count_; }
+ int bandwidth_notifications_count() const {
+ return max_bandwidth_notifications_count_;
}
private:
- int notifications_count_;
+ int type_notifications_count_;
+ int max_bandwidth_notifications_count_;
};
class NetworkChangeNotifierObserver
@@ -155,17 +162,16 @@ class NetworkChangeNotifierDelegateAndroidTest
// delegate's observers are instances of NetworkChangeNotifierAndroid.
TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) {
// Test the logic with a single observer.
- RunTest(
- base::Bind(
- &NetworkChangeNotifierDelegateAndroidObserver::notifications_count,
- base::Unretained(&delegate_observer_)),
- base::Bind(
- &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType,
- base::Unretained(&delegate_)));
+ RunTest(base::Bind(&NetworkChangeNotifierDelegateAndroidObserver::
+ type_notifications_count,
+ base::Unretained(&delegate_observer_)),
+ base::Bind(
+ &NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType,
+ base::Unretained(&delegate_)));
// Check that *all* the observers are notified. Both observers should have the
// same state.
- EXPECT_EQ(delegate_observer_.notifications_count(),
- other_delegate_observer_.notifications_count());
+ EXPECT_EQ(delegate_observer_.type_notifications_count(),
+ other_delegate_observer_.type_notifications_count());
}
class NetworkChangeNotifierAndroidTest
@@ -219,11 +225,21 @@ TEST_F(NetworkChangeNotifierAndroidTest, MaxBandwidth) {
notifier_.GetConnectionType());
EXPECT_EQ(std::numeric_limits<double>::infinity(),
notifier_.GetMaxBandwidth());
-
SetOffline();
EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
notifier_.GetConnectionType());
EXPECT_EQ(0.0, notifier_.GetMaxBandwidth());
}
+TEST_F(NetworkChangeNotifierDelegateAndroidTest,
+ MaxBandwidthNotifiedOnConnectionChange) {
+ EXPECT_EQ(0, delegate_observer_.bandwidth_notifications_count());
+ SetOffline();
+ EXPECT_EQ(1, delegate_observer_.bandwidth_notifications_count());
+ SetOnline();
+ EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count());
+ SetOnline();
+ EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count());
+}
+
} // namespace net
diff --git a/net/android/network_change_notifier_delegate_android.cc b/net/android/network_change_notifier_delegate_android.cc
index a6a7132..99a6cc2 100644
--- a/net/android/network_change_notifier_delegate_android.cc
+++ b/net/android/network_change_notifier_delegate_android.cc
@@ -64,8 +64,9 @@ NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid()
ConvertConnectionType(
Java_NetworkChangeNotifier_getCurrentConnectionType(
env, java_network_change_notifier_.obj())));
- SetCurrentMaxBandwidth(Java_NetworkChangeNotifier_getCurrentMaxBandwidth(
- env, java_network_change_notifier_.obj()));
+ SetCurrentMaxBandwidth(
+ Java_NetworkChangeNotifier_getCurrentMaxBandwidthInMbps(
+ env, java_network_change_notifier_.obj()));
}
NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() {
@@ -96,8 +97,6 @@ void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged(
const ConnectionType actual_connection_type = ConvertConnectionType(
new_connection_type);
SetCurrentConnectionType(actual_connection_type);
- SetCurrentMaxBandwidth(Java_NetworkChangeNotifier_getCurrentMaxBandwidth(
- env, java_network_change_notifier_.obj()));
observers_->Notify(&Observer::OnConnectionTypeChanged);
}
@@ -107,6 +106,16 @@ jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*,
return GetCurrentConnectionType();
}
+void NetworkChangeNotifierDelegateAndroid::NotifyMaxBandwidthChanged(
+ JNIEnv* env,
+ jobject obj,
+ jdouble new_max_bandwidth) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(new_max_bandwidth != GetCurrentMaxBandwidth());
+ SetCurrentMaxBandwidth(new_max_bandwidth);
+ observers_->Notify(&Observer::OnMaxBandwidthChanged, new_max_bandwidth);
+}
+
void NetworkChangeNotifierDelegateAndroid::AddObserver(
Observer* observer) {
observers_->AddObserver(observer);
diff --git a/net/android/network_change_notifier_delegate_android.h b/net/android/network_change_notifier_delegate_android.h
index f561888..6668b27 100644
--- a/net/android/network_change_notifier_delegate_android.h
+++ b/net/android/network_change_notifier_delegate_android.h
@@ -33,6 +33,9 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierDelegateAndroid {
// Updates the current connection type.
virtual void OnConnectionTypeChanged() = 0;
+
+ // Updates the current max bandwidth.
+ virtual void OnMaxBandwidthChanged(double max_bandwidth_mbps) = 0;
};
NetworkChangeNotifierDelegateAndroid();
@@ -47,6 +50,14 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierDelegateAndroid {
jint new_connection_type);
jint GetConnectionType(JNIEnv* env, jobject obj) const;
+ // Called from NetworkChangeNotifierAndroid.java on the JNI thread whenever
+ // the maximum bandwidth of the connection changes. This updates the current
+ // max bandwidth seen by this class and forwards the notification to the
+ // observers that subscribed through AddObserver().
+ void NotifyMaxBandwidthChanged(JNIEnv* env,
+ jobject obj,
+ jdouble new_max_bandwidth);
+
// These methods can be called on any thread. Note that the provided observer
// will be notified on the thread AddObserver() is called on.
void AddObserver(Observer* observer);
diff --git a/net/base/network_change_notifier.cc b/net/base/network_change_notifier.cc
index b797864..05c72a1 100644
--- a/net/base/network_change_notifier.cc
+++ b/net/base/network_change_notifier.cc
@@ -688,6 +688,14 @@ void NetworkChangeNotifier::AddNetworkChangeObserver(
}
}
+void NetworkChangeNotifier::AddMaxBandwidthObserver(
+ MaxBandwidthObserver* observer) {
+ if (g_network_change_notifier) {
+ g_network_change_notifier->max_bandwidth_observer_list_->AddObserver(
+ observer);
+ }
+}
+
void NetworkChangeNotifier::RemoveIPAddressObserver(
IPAddressObserver* observer) {
if (g_network_change_notifier) {
@@ -719,6 +727,14 @@ void NetworkChangeNotifier::RemoveNetworkChangeObserver(
}
}
+void NetworkChangeNotifier::RemoveMaxBandwidthObserver(
+ MaxBandwidthObserver* observer) {
+ if (g_network_change_notifier) {
+ g_network_change_notifier->max_bandwidth_observer_list_->RemoveObserver(
+ observer);
+ }
+}
+
// static
void NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests() {
if (g_network_change_notifier)
@@ -747,19 +763,20 @@ void NetworkChangeNotifier::SetTestNotificationsOnly(bool test_only) {
NetworkChangeNotifier::NetworkChangeNotifier(
const NetworkChangeCalculatorParams& params
- /*= NetworkChangeCalculatorParams()*/)
- : ip_address_observer_list_(
- new ObserverListThreadSafe<IPAddressObserver>(
- ObserverListBase<IPAddressObserver>::NOTIFY_EXISTING_ONLY)),
+ /*= NetworkChangeCalculatorParams()*/)
+ : ip_address_observer_list_(new ObserverListThreadSafe<IPAddressObserver>(
+ ObserverListBase<IPAddressObserver>::NOTIFY_EXISTING_ONLY)),
connection_type_observer_list_(
- new ObserverListThreadSafe<ConnectionTypeObserver>(
- ObserverListBase<ConnectionTypeObserver>::NOTIFY_EXISTING_ONLY)),
- resolver_state_observer_list_(
- new ObserverListThreadSafe<DNSObserver>(
- ObserverListBase<DNSObserver>::NOTIFY_EXISTING_ONLY)),
+ new ObserverListThreadSafe<ConnectionTypeObserver>(
+ ObserverListBase<ConnectionTypeObserver>::NOTIFY_EXISTING_ONLY)),
+ resolver_state_observer_list_(new ObserverListThreadSafe<DNSObserver>(
+ ObserverListBase<DNSObserver>::NOTIFY_EXISTING_ONLY)),
network_change_observer_list_(
- new ObserverListThreadSafe<NetworkChangeObserver>(
- ObserverListBase<NetworkChangeObserver>::NOTIFY_EXISTING_ONLY)),
+ new ObserverListThreadSafe<NetworkChangeObserver>(
+ ObserverListBase<NetworkChangeObserver>::NOTIFY_EXISTING_ONLY)),
+ max_bandwidth_observer_list_(
+ new ObserverListThreadSafe<MaxBandwidthObserver>(
+ ObserverListBase<MaxBandwidthObserver>::NOTIFY_EXISTING_ONLY)),
network_state_(new NetworkState()),
network_change_calculator_(new NetworkChangeCalculator(params)),
test_notifications_only_(false) {
@@ -886,6 +903,16 @@ void NetworkChangeNotifier::NotifyObserversOfNetworkChange(
}
// static
+void NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChange(
+ double max_bandwidth_mbps) {
+ if (g_network_change_notifier &&
+ !g_network_change_notifier->test_notifications_only_) {
+ g_network_change_notifier->NotifyObserversOfMaxBandwidthChangeImpl(
+ max_bandwidth_mbps);
+ }
+}
+
+// static
void NetworkChangeNotifier::NotifyObserversOfDNSChange() {
if (g_network_change_notifier &&
!g_network_change_notifier->test_notifications_only_) {
@@ -921,6 +948,12 @@ void NetworkChangeNotifier::NotifyObserversOfDNSChangeImpl() {
resolver_state_observer_list_->Notify(&DNSObserver::OnDNSChanged);
}
+void NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChangeImpl(
+ double max_bandwidth_mbps) {
+ max_bandwidth_observer_list_->Notify(
+ &MaxBandwidthObserver::OnMaxBandwidthChanged, max_bandwidth_mbps);
+}
+
NetworkChangeNotifier::DisableForTest::DisableForTest()
: network_change_notifier_(g_network_change_notifier) {
DCHECK(g_network_change_notifier);
diff --git a/net/base/network_change_notifier.h b/net/base/network_change_notifier.h
index 285c52b..73842d4 100644
--- a/net/base/network_change_notifier.h
+++ b/net/base/network_change_notifier.h
@@ -174,6 +174,25 @@ class NET_EXPORT NetworkChangeNotifier {
DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserver);
};
+ class NET_EXPORT MaxBandwidthObserver {
+ public:
+ // Will be called when a change occurs to the network's maximum bandwidth as
+ // defined in http://w3c.github.io/netinfo/. Generally this will only be
+ // called on bandwidth changing network connection/disconnection events.
+ // Some platforms may call it more frequently, such as when WiFi signal
+ // strength changes.
+ // TODO(jkarlin): This is currently only implemented for Android. Implement
+ // on every platform.
+ virtual void OnMaxBandwidthChanged(double max_bandwidth_mbps) = 0;
+
+ protected:
+ MaxBandwidthObserver() {}
+ virtual ~MaxBandwidthObserver() {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MaxBandwidthObserver);
+ };
+
virtual ~NetworkChangeNotifier();
// See the description of NetworkChangeNotifier::GetConnectionType().
@@ -252,6 +271,7 @@ class NET_EXPORT NetworkChangeNotifier {
static void AddConnectionTypeObserver(ConnectionTypeObserver* observer);
static void AddDNSObserver(DNSObserver* observer);
static void AddNetworkChangeObserver(NetworkChangeObserver* observer);
+ static void AddMaxBandwidthObserver(MaxBandwidthObserver* observer);
// Unregisters |observer| from receiving notifications. This must be called
// on the same thread on which AddObserver() was called. Like AddObserver(),
@@ -264,6 +284,7 @@ class NET_EXPORT NetworkChangeNotifier {
static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer);
static void RemoveDNSObserver(DNSObserver* observer);
static void RemoveNetworkChangeObserver(NetworkChangeObserver* observer);
+ static void RemoveMaxBandwidthObserver(MaxBandwidthObserver* observer);
// Allow unit tests to trigger notifications.
static void NotifyObserversOfIPAddressChangeForTests();
@@ -366,6 +387,7 @@ class NET_EXPORT NetworkChangeNotifier {
static void NotifyObserversOfConnectionTypeChange();
static void NotifyObserversOfDNSChange();
static void NotifyObserversOfNetworkChange(ConnectionType type);
+ static void NotifyObserversOfMaxBandwidthChange(double max_bandwidth_mbps);
// Stores |config| in NetworkState and notifies observers.
static void SetDnsConfig(const DnsConfig& config);
@@ -383,15 +405,18 @@ class NET_EXPORT NetworkChangeNotifier {
void NotifyObserversOfConnectionTypeChangeImpl(ConnectionType type);
void NotifyObserversOfDNSChangeImpl();
void NotifyObserversOfNetworkChangeImpl(ConnectionType type);
+ void NotifyObserversOfMaxBandwidthChangeImpl(double max_bandwidth_mbps);
- const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> >
+ const scoped_refptr<ObserverListThreadSafe<IPAddressObserver>>
ip_address_observer_list_;
- const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> >
+ const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver>>
connection_type_observer_list_;
- const scoped_refptr<ObserverListThreadSafe<DNSObserver> >
+ const scoped_refptr<ObserverListThreadSafe<DNSObserver>>
resolver_state_observer_list_;
- const scoped_refptr<ObserverListThreadSafe<NetworkChangeObserver> >
+ const scoped_refptr<ObserverListThreadSafe<NetworkChangeObserver>>
network_change_observer_list_;
+ const scoped_refptr<ObserverListThreadSafe<MaxBandwidthObserver>>
+ max_bandwidth_observer_list_;
// The current network state. Hosts DnsConfig, exposed via GetDnsConfig.
scoped_ptr<NetworkState> network_state_;