| Index: net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
|
| diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
|
| index e164c739c8e1512646572bf668a3003c77ae7ee2..331559e421de7faa6a4920e832ab6330bfdc24ee 100644
|
| --- a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
|
| +++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
|
| @@ -262,12 +262,21 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
|
| mHasWifiPermission = false;
|
| }
|
|
|
| - String getWifiSSID() {
|
| + /**
|
| + * Returns currently connected WiFi SSID.
|
| + * @param wifiInfo if app has ACCESS_WIFI_STATE permission, a recently queried WifiInfo.
|
| + */
|
| + String getWifiSSID(WifiInfo wifiInfo) {
|
| + if (mHasWifiPermission) {
|
| + if (wifiInfo == null) return "";
|
| + return wifiInfo.getSSID();
|
| + }
|
| return AndroidNetworkLibrary.getWifiSSID(mContext);
|
| }
|
|
|
| // Fetches WifiInfo and records UMA for NullPointerExceptions.
|
| - private WifiInfo getWifiInfo() {
|
| + WifiInfo getWifiInfo() {
|
| + if (!mHasWifiPermission) return null;
|
| try {
|
| WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
|
| RecordHistogram.recordBooleanHistogram("NCN.getWifiInfo1stSuccess", true);
|
| @@ -290,10 +299,10 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
|
| /*
|
| * Requires ACCESS_WIFI_STATE permission to get the real link speed, else returns
|
| * UNKNOWN_LINK_SPEED.
|
| + * @param wifiInfo if app has ACCESS_WIFI_STATE permission, a recently queried WifiInfo.
|
| */
|
| - int getLinkSpeedInMbps() {
|
| + int getLinkSpeedInMbps(WifiInfo wifiInfo) {
|
| if (!mHasWifiPermission || mWifiManager == null) return UNKNOWN_LINK_SPEED;
|
| - final WifiInfo wifiInfo = getWifiInfo();
|
| if (wifiInfo == null) return UNKNOWN_LINK_SPEED;
|
|
|
| // wifiInfo.getLinkSpeed returns the current wifi linkspeed, which can change even
|
| @@ -517,6 +526,20 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
|
| private String mWifiSSID;
|
| private double mMaxBandwidthMbps;
|
| private int mMaxBandwidthConnectionType;
|
| + // When a BroadcastReceiver is registered for a sticky broadcast that has been sent out at
|
| + // least once, onReceive() will immediately be called. mIgnoreNextBroadcast is set to true
|
| + // when this class is registered in such a circumstance, and indicates that the next
|
| + // invokation of onReceive() can be ignored as the state hasn't actually changed. Immediately
|
| + // prior to mIgnoreNextBroadcast being set, all internal state is updated to the current device
|
| + // state so were this initial onReceive() call not ignored, no signals would be passed to
|
| + // observers anyhow as the state hasn't changed. This is simply an optimization to avoid
|
| + // useless work.
|
| + private boolean mIgnoreNextBroadcast;
|
| + // mDontSignal is set to true when it's not worth calculating if signals to Observers should
|
| + // be sent out because this class is being constructed and the internal state has just
|
| + // been updated to the current device state, so no signals are necessary. This is simply an
|
| + // optimization to avoid useless work.
|
| + private boolean mDontSignal;
|
|
|
| /**
|
| * Observer interface by which observer is notified of network changes.
|
| @@ -590,13 +613,17 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
|
| }
|
| final NetworkState networkState = mConnectivityManagerDelegate.getNetworkState();
|
| mConnectionType = getCurrentConnectionType(networkState);
|
| - mWifiSSID = getCurrentWifiSSID(networkState);
|
| - mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState);
|
| + WifiInfo wifiInfo = getCurrentWifiInfo(networkState);
|
| + mWifiSSID = getCurrentWifiSSID(networkState, wifiInfo);
|
| + mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState, wifiInfo);
|
| mMaxBandwidthConnectionType = mConnectionType;
|
| mIntentFilter =
|
| new NetworkConnectivityIntentFilter(mWifiManagerDelegate.getHasWifiPermission());
|
| mRegistrationPolicy = policy;
|
| + mIgnoreNextBroadcast = false;
|
| + mDontSignal = true;
|
| mRegistrationPolicy.init(this);
|
| + mDontSignal = false;
|
| }
|
|
|
| /**
|
| @@ -637,27 +664,32 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
|
| public void register() {
|
| if (mRegistered) return;
|
|
|
| - final NetworkState networkState = getCurrentNetworkState();
|
| - connectionTypeChanged(networkState);
|
| - maxBandwidthChanged(networkState);
|
| - mContext.registerReceiver(this, mIntentFilter);
|
| + if (!mDontSignal) {
|
| + final NetworkState networkState = getCurrentNetworkState();
|
| + connectionTypeChanged(networkState);
|
| + maxBandwidthChanged(networkState);
|
| + }
|
| + mIgnoreNextBroadcast = mContext.registerReceiver(this, mIntentFilter) != null;
|
| mRegistered = true;
|
|
|
| if (mNetworkCallback != null) {
|
| mNetworkCallback.initializeVpnInPlace();
|
| mConnectivityManagerDelegate.registerNetworkCallback(mNetworkRequest, mNetworkCallback);
|
| - // registerNetworkCallback() will rematch the NetworkRequest
|
| - // against active networks, so a cached list of active networks
|
| - // will be repopulated immediatly after this. However we need to
|
| - // purge any cached networks as they may have been disconnected
|
| - // while mNetworkCallback was unregistered.
|
| - final Network[] networks = getAllNetworksFiltered(mConnectivityManagerDelegate, null);
|
| - // Convert Networks to NetIDs.
|
| - final long[] netIds = new long[networks.length];
|
| - for (int i = 0; i < networks.length; i++) {
|
| - netIds[i] = networkToNetId(networks[i]);
|
| + if (!mDontSignal) {
|
| + // registerNetworkCallback() will rematch the NetworkRequest
|
| + // against active networks, so a cached list of active networks
|
| + // will be repopulated immediatly after this. However we need to
|
| + // purge any cached networks as they may have been disconnected
|
| + // while mNetworkCallback was unregistered.
|
| + final Network[] networks =
|
| + getAllNetworksFiltered(mConnectivityManagerDelegate, null);
|
| + // Convert Networks to NetIDs.
|
| + final long[] netIds = new long[networks.length];
|
| + for (int i = 0; i < networks.length; i++) {
|
| + netIds[i] = networkToNetId(networks[i]);
|
| + }
|
| + mObserver.purgeActiveNetworkList(netIds);
|
| }
|
| - mObserver.purgeActiveNetworkList(netIds);
|
| }
|
| }
|
|
|
| @@ -857,8 +889,16 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
|
| * that is used instead. For more on NetInfo, see http://w3c.github.io/netinfo/.
|
| */
|
| public double getCurrentMaxBandwidthInMbps(NetworkState networkState) {
|
| + return getCurrentMaxBandwidthInMbps(networkState, getCurrentWifiInfo(networkState));
|
| + }
|
| +
|
| + /**
|
| + * Returns the bandwidth of the current connection in Mbps.
|
| + * @param wifiInfo if app has ACCESS_WIFI_STATE permission, a recently queried WifiInfo.
|
| + */
|
| + public double getCurrentMaxBandwidthInMbps(NetworkState networkState, WifiInfo wifiInfo) {
|
| if (getCurrentConnectionType(networkState) == ConnectionType.CONNECTION_WIFI) {
|
| - final int link_speed = mWifiManagerDelegate.getLinkSpeedInMbps();
|
| + final int link_speed = mWifiManagerDelegate.getLinkSpeedInMbps(wifiInfo);
|
| if (link_speed != UNKNOWN_LINK_SPEED) {
|
| return link_speed;
|
| }
|
| @@ -868,14 +908,33 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
|
| getCurrentConnectionSubtype(networkState));
|
| }
|
|
|
| - private String getCurrentWifiSSID(NetworkState networkState) {
|
| + /**
|
| + * Returns the WifiInfo for the current WiFi connection if current default network connection is
|
| + * WiFi.
|
| + * @param networkState recently queried NetworkState.
|
| + */
|
| + private WifiInfo getCurrentWifiInfo(NetworkState networkState) {
|
| + if (getCurrentConnectionType(networkState) != ConnectionType.CONNECTION_WIFI) return null;
|
| + return mWifiManagerDelegate.getWifiInfo();
|
| + }
|
| +
|
| + /**
|
| + * Returns the currently connected WiFi SSID.
|
| + * @param networkState recently queried NetworkState.
|
| + * @param wifiInfo if app has ACCESS_WIFI_STATE permission, a recently queried WifiInfo.
|
| + */
|
| + private String getCurrentWifiSSID(NetworkState networkState, WifiInfo wifiInfo) {
|
| if (getCurrentConnectionType(networkState) != ConnectionType.CONNECTION_WIFI) return "";
|
| - return mWifiManagerDelegate.getWifiSSID();
|
| + return mWifiManagerDelegate.getWifiSSID(wifiInfo);
|
| }
|
|
|
| // BroadcastReceiver
|
| @Override
|
| public void onReceive(Context context, Intent intent) {
|
| + if (mIgnoreNextBroadcast) {
|
| + mIgnoreNextBroadcast = false;
|
| + return;
|
| + }
|
| final NetworkState networkState = getCurrentNetworkState();
|
| if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
|
| connectionTypeChanged(networkState);
|
| @@ -887,7 +946,7 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver {
|
|
|
| private void connectionTypeChanged(NetworkState networkState) {
|
| int newConnectionType = getCurrentConnectionType(networkState);
|
| - String newWifiSSID = getCurrentWifiSSID(networkState);
|
| + String newWifiSSID = getCurrentWifiSSID(networkState, getCurrentWifiInfo(networkState));
|
| if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID)) return;
|
|
|
| mConnectionType = newConnectionType;
|
|
|