Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1050)

Unified Diff: net/android/java/src/org/chromium/net/NetworkChangeNotifier.java

Issue 10905264: Fix race condition in NetworkChangeNotifier on Android. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
index 7f1bdc32ed0305e744b6e0af65b0e5aebf6e7297..d540bc38e057ba3fed71cc63a6c4d35c5bbd4b15 100644
--- a/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
+++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
@@ -31,7 +31,6 @@ public class NetworkChangeNotifier {
private final Context mContext;
private int mNativeChangeNotifier;
- private int mConnectionType;
private NetworkChangeNotifierAutoDetect mAutoDetector;
private static NetworkChangeNotifier sInstance;
@@ -41,7 +40,6 @@ public class NetworkChangeNotifier {
private NetworkChangeNotifier(Context context, int nativeChangeNotifier) {
mContext = context;
mNativeChangeNotifier = nativeChangeNotifier;
- mConnectionType = CONNECTION_UNKNOWN;
}
private void destroy() {
@@ -94,16 +92,27 @@ public class NetworkChangeNotifier {
sInstance.setAutoDetectConnectivityStateInternal(shouldAutoDetect);
}
+ private void destroyAutoDetector() {
+ if (mAutoDetector != null) {
+ mAutoDetector.destroy();
+ mAutoDetector = null;
+ }
+ }
+
private void setAutoDetectConnectivityStateInternal(boolean shouldAutoDetect) {
if (shouldAutoDetect) {
if (mAutoDetector == null) {
- mAutoDetector = new NetworkChangeNotifierAutoDetect(this, mContext);
+ mAutoDetector = new NetworkChangeNotifierAutoDetect(
+ new NetworkChangeNotifierAutoDetect.Observer() {
+ @Override
+ public void onConnectionTypeChanged(int newConnectionType) {
+ notifyObserversOfConnectionTypeChange(newConnectionType);
+ }
+ },
+ mContext);
}
} else {
- if (mAutoDetector != null) {
- mAutoDetector.destroy();
- mAutoDetector = null;
- }
+ destroyAutoDetector();
}
}
@@ -122,29 +131,29 @@ public class NetworkChangeNotifier {
}
private void forceConnectivityStateInternal(boolean forceOnline) {
- boolean connectionCurrentlyExists = mConnectionType != CONNECTION_NONE;
+ if (mNativeChangeNotifier == 0) {
+ return;
+ }
+ boolean connectionCurrentlyExists =
+ nativeGetConnectionType(mNativeChangeNotifier) != CONNECTION_NONE;
if (connectionCurrentlyExists != forceOnline) {
- mConnectionType = forceOnline ? CONNECTION_UNKNOWN : CONNECTION_NONE;
- notifyObserversOfConnectionTypeChange();
+ notifyObserversOfConnectionTypeChange(
+ forceOnline ? CONNECTION_UNKNOWN : CONNECTION_NONE);
}
}
- void notifyObserversOfConnectionTypeChange() {
+ void notifyObserversOfConnectionTypeChange(int newConnectionType) {
if (mNativeChangeNotifier != 0) {
- nativeNotifyObserversOfConnectionTypeChange(mNativeChangeNotifier);
+ nativeNotifyObserversOfConnectionTypeChange(mNativeChangeNotifier, newConnectionType);
}
}
- @CalledByNative
- private int connectionType() {
- if (mAutoDetector != null) {
- return mAutoDetector.connectionType();
- }
- return mConnectionType;
- }
+ @NativeClassQualifiedName("NetworkChangeNotifierAndroid")
+ private native void nativeNotifyObserversOfConnectionTypeChange(
+ int nativePtr, int newConnectionType);
@NativeClassQualifiedName("NetworkChangeNotifierAndroid")
- private native void nativeNotifyObserversOfConnectionTypeChange(int nativePtr);
+ private native int nativeGetConnectionType(int nativePtr);
// For testing only.
public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() {

Powered by Google App Engine
This is Rietveld 408576698