summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFeng Qian <feng@chromium.org>2015-01-06 17:25:48 -0800
committerFeng Qian <feng@chromium.org>2015-01-07 01:28:15 +0000
commit1c83b4bd76926a2c1e37a2c5094a7af98196ae71 (patch)
tree6c0ec539ae2b9138000ef226696a924de9731277
parentcada861900be3ec76d1b4098ec010a583a520b4d (diff)
downloadchromium_src-1c83b4bd76926a2c1e37a2c5094a7af98196ae71.zip
chromium_src-1c83b4bd76926a2c1e37a2c5094a7af98196ae71.tar.gz
chromium_src-1c83b4bd76926a2c1e37a2c5094a7af98196ae71.tar.bz2
Fix races resulting in NullPointerException in Android NetworkChangeNotifier.
The old code relied on calls to ConnectivityManager.getActiveNetworkInfo() returning the same result over a short period of time. Review URL: https://codereview.chromium.org/750563003 Cr-Commit-Position: refs/heads/master@{#305651} (cherry picked from commit 844146e0a3b0c614b4c8dabe9a910feb6d482afb) BUG=421685 TBR=pauljensen Review URL: https://codereview.chromium.org/799203003 Cr-Commit-Position: refs/branch-heads/2214@{#392} Cr-Branched-From: 03655fd3f6d72165dc3c9bd2c89807305316fe6c-refs/heads/master@{#303346}
-rw-r--r--net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java53
-rw-r--r--net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java20
2 files changed, 38 insertions, 35 deletions
diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
index 1e72da9..2ae6641 100644
--- a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
+++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
@@ -9,6 +9,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
@@ -25,6 +26,30 @@ import org.chromium.base.ApplicationStatus;
public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
implements ApplicationStatus.ApplicationStateListener {
+ static class NetworkState {
+ private final boolean mConnected;
+ private final int mType;
+ private final int mSubtype;
+
+ public NetworkState(boolean connected, int type, int subtype) {
+ mConnected = connected;
+ mType = type;
+ mSubtype = subtype;
+ }
+
+ public boolean isConnected() {
+ return mConnected;
+ }
+
+ public int getNetworkType() {
+ return mType;
+ }
+
+ public int getNetworkSubType() {
+ return mSubtype;
+ }
+ }
+
/** Queries the ConnectivityManager for information about the current connection. */
static class ConnectivityManagerDelegate {
private final ConnectivityManager mConnectivityManager;
@@ -40,20 +65,12 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
mConnectivityManager = null;
}
- boolean activeNetworkExists() {
- return mConnectivityManager.getActiveNetworkInfo() != null;
- }
-
- boolean isConnected() {
- return mConnectivityManager.getActiveNetworkInfo().isConnected();
- }
-
- int getNetworkType() {
- return mConnectivityManager.getActiveNetworkInfo().getType();
- }
-
- int getNetworkSubtype() {
- return mConnectivityManager.getActiveNetworkInfo().getSubtype();
+ NetworkState getNetworkState() {
+ final NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
+ if (networkInfo == null || !networkInfo.isConnected()) {
+ return new NetworkState(false, -1, -1);
+ }
+ return new NetworkState(true, networkInfo.getType(), networkInfo.getSubtype());
}
}
@@ -161,12 +178,12 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
public int getCurrentConnectionType() {
// Track exactly what type of connection we have.
- if (!mConnectivityManagerDelegate.activeNetworkExists() ||
- !mConnectivityManagerDelegate.isConnected()) {
+ final NetworkState networkState = mConnectivityManagerDelegate.getNetworkState();
+ if (!networkState.isConnected()) {
return NetworkChangeNotifier.CONNECTION_NONE;
}
- switch (mConnectivityManagerDelegate.getNetworkType()) {
+ switch (networkState.getNetworkType()) {
case ConnectivityManager.TYPE_ETHERNET:
return NetworkChangeNotifier.CONNECTION_ETHERNET;
case ConnectivityManager.TYPE_WIFI:
@@ -177,7 +194,7 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
return NetworkChangeNotifier.CONNECTION_BLUETOOTH;
case ConnectivityManager.TYPE_MOBILE:
// Use information from TelephonyManager to classify the connection.
- switch (mConnectivityManagerDelegate.getNetworkSubtype()) {
+ switch (networkState.getNetworkSubType()) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
diff --git a/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java b/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
index 887bc3b..dd15a76 100644
--- a/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
+++ b/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
@@ -14,6 +14,7 @@ import android.test.suitebuilder.annotation.MediumTest;
import org.chromium.base.ApplicationState;
import org.chromium.base.test.util.Feature;
+import org.chromium.net.NetworkChangeNotifierAutoDetect.NetworkState;
/**
* Tests for org.chromium.net.NetworkChangeNotifier.
@@ -50,33 +51,18 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
private int mNetworkSubtype;
@Override
- boolean activeNetworkExists() {
- return mActiveNetworkExists;
- }
-
- @Override
- boolean isConnected() {
- return getNetworkType() != NetworkChangeNotifier.CONNECTION_NONE;
+ NetworkState getNetworkState() {
+ return new NetworkState(mActiveNetworkExists, mNetworkType, mNetworkSubtype);
}
void setActiveNetworkExists(boolean networkExists) {
mActiveNetworkExists = networkExists;
}
- @Override
- int getNetworkType() {
- return mNetworkType;
- }
-
void setNetworkType(int networkType) {
mNetworkType = networkType;
}
- @Override
- int getNetworkSubtype() {
- return mNetworkSubtype;
- }
-
void setNetworkSubtype(int networkSubtype) {
mNetworkSubtype = networkSubtype;
}