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

Unified Diff: net/android/network_change_notifier_android.cc

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/network_change_notifier_android.cc
diff --git a/net/android/network_change_notifier_android.cc b/net/android/network_change_notifier_android.cc
index 66aacaf1a8edc68402fe0ff6aeaa0313415a1989..f055af971db4a41bf54e6d386da5c2dc4a2d873e 100644
--- a/net/android/network_change_notifier_android.cc
+++ b/net/android/network_change_notifier_android.cc
@@ -4,33 +4,72 @@
#include "net/android/network_change_notifier_android.h"
-#include "base/logging.h"
#include "base/android/jni_android.h"
+#include "base/logging.h"
#include "jni/NetworkChangeNotifier_jni.h"
namespace net {
-NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid() {
- JNIEnv* env = base::android::AttachCurrentThread();
- java_network_change_notifier_.Reset(
- Java_NetworkChangeNotifier_createInstance(
- env,
- base::android::GetApplicationContext(),
- reinterpret_cast<jint>(this)));
+namespace {
+
+// Returns whether the provided connection type is known.
+bool CheckConnectionType(int connection_type) {
+ switch (connection_type) {
+ case NetworkChangeNotifier::CONNECTION_UNKNOWN:
+ case NetworkChangeNotifier::CONNECTION_ETHERNET:
+ case NetworkChangeNotifier::CONNECTION_WIFI:
+ case NetworkChangeNotifier::CONNECTION_2G:
+ case NetworkChangeNotifier::CONNECTION_3G:
+ case NetworkChangeNotifier::CONNECTION_4G:
+ case NetworkChangeNotifier::CONNECTION_NONE:
+ return true;
+ default:
+ NOTREACHED() << "Unknown connection type received: " << connection_type;
+ return false;
+ }
}
+} // namespace
+
NetworkChangeNotifierAndroid::~NetworkChangeNotifierAndroid() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_destroyInstance(env);
- java_network_change_notifier_.Reset();
}
void NetworkChangeNotifierAndroid::NotifyObserversOfConnectionTypeChange(
JNIEnv* env,
- jobject obj) {
+ jobject obj,
+ jint new_connection_type) {
+ int connection_type = CheckConnectionType(new_connection_type) ?
+ new_connection_type : CONNECTION_UNKNOWN;
gone 2012/09/20 21:52:01 Just saw this when rebasing my patch; this overloa
szym 2012/09/20 21:54:19 I believe so. CONNECTION_NONE is used to indicate
+ SetConnectionType(connection_type);
NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
}
+jint NetworkChangeNotifierAndroid::GetConnectionType(JNIEnv* env, jobject obj) {
+ return GetCurrentConnectionType();
+}
+
+// static
+bool NetworkChangeNotifierAndroid::Register(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid() {
+ SetConnectionType(CONNECTION_UNKNOWN);
+ JNIEnv* env = base::android::AttachCurrentThread();
+ java_network_change_notifier_.Reset(
+ Java_NetworkChangeNotifier_createInstance(
+ env,
+ base::android::GetApplicationContext(),
+ reinterpret_cast<jint>(this)));
+}
+
+void NetworkChangeNotifierAndroid::SetConnectionType(int connection_type) {
+ base::AutoLock auto_lock(lock_);
+ connection_type_ = connection_type;
+}
+
void NetworkChangeNotifierAndroid::ForceConnectivityState(bool state) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_forceConnectivityState(env, state);
@@ -38,36 +77,8 @@ void NetworkChangeNotifierAndroid::ForceConnectivityState(bool state) {
NetworkChangeNotifier::ConnectionType
NetworkChangeNotifierAndroid::GetCurrentConnectionType() const {
- JNIEnv* env = base::android::AttachCurrentThread();
-
- // Pull the connection type from the Java-side then convert it to a
- // native-side NetworkChangeNotifier::ConnectionType.
- jint connection_type = Java_NetworkChangeNotifier_connectionType(
- env, java_network_change_notifier_.obj());
- switch (connection_type) {
- case CONNECTION_UNKNOWN:
- return CONNECTION_UNKNOWN;
- case CONNECTION_ETHERNET:
- return CONNECTION_ETHERNET;
- case CONNECTION_WIFI:
- return CONNECTION_WIFI;
- case CONNECTION_2G:
- return CONNECTION_2G;
- case CONNECTION_3G:
- return CONNECTION_3G;
- case CONNECTION_4G:
- return CONNECTION_4G;
- case CONNECTION_NONE:
- return CONNECTION_NONE;
- default:
- NOTREACHED() << "Unknown connection type received: " << connection_type;
- return CONNECTION_NONE;
- }
-}
-
-// static
-bool NetworkChangeNotifierAndroid::Register(JNIEnv* env) {
- return RegisterNativesImpl(env);
+ base::AutoLock auto_lock(lock_);
+ return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type_);
}
} // namespace net
« no previous file with comments | « net/android/network_change_notifier_android.h ('k') | net/android/network_change_notifier_factory_android.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698