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

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: Use a lock rather than atomicops 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
« no previous file with comments | « net/android/network_change_notifier_android.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 473fa644eca3a15a2904577f4f2df9087c0f5f70..03b7ea5de5ab0c2b747fde9818ec96badc6d7f31 100644
--- a/net/android/network_change_notifier_android.cc
+++ b/net/android/network_change_notifier_android.cc
@@ -4,24 +4,68 @@
#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 {
namespace android {
-NetworkChangeNotifier::NetworkChangeNotifier() {
- JNIEnv* env = base::android::AttachCurrentThread();
- CreateJavaObject(env);
+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
+
NetworkChangeNotifier::~NetworkChangeNotifier() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_NetworkChangeNotifier_destroy(
env, java_network_change_notifier_.obj());
}
+void NetworkChangeNotifier::NotifyObservers(
+ JNIEnv* env, jobject obj, jint new_connection_type) {
+ SetConnectionType(
+ CheckConnectionType(new_connection_type) ?
+ new_connection_type : CONNECTION_UNKNOWN);
Ryan Sleevi 2012/09/19 17:43:27 nit: I feel this might read easier with a temporar
Philippe 2012/09/19 17:57:04 Done.
+ NotifyObserversOfConnectionTypeChange();
+}
+
+jint NetworkChangeNotifier::GetConnectionType(JNIEnv* env, jobject obj) {
+ return GetCurrentConnectionType();
+}
+
+// static
+bool NetworkChangeNotifier::Register(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+NetworkChangeNotifier::NetworkChangeNotifier() {
+ SetConnectionType(CONNECTION_UNKNOWN);
+ JNIEnv* env = base::android::AttachCurrentThread();
+ CreateJavaObject(env);
+}
+
+void NetworkChangeNotifier::SetConnectionType(int connection_type) {
+ base::AutoLock auto_lock(lock_);
+ connection_type_ = connection_type;
+}
+
void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) {
java_network_change_notifier_.Reset(
Java_NetworkChangeNotifier_create(
@@ -30,42 +74,11 @@ void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) {
reinterpret_cast<jint>(this)));
}
-void NetworkChangeNotifier::NotifyObservers(JNIEnv* env, jobject obj) {
- NotifyObserversOfConnectionTypeChange();
-}
-
net::NetworkChangeNotifier::ConnectionType
NetworkChangeNotifier::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 NetworkChangeNotifier::Register(JNIEnv* env) {
- return RegisterNativesImpl(env);
+ base::AutoLock auto_lock(lock_);
+ return static_cast<net::NetworkChangeNotifier::ConnectionType>(
Ryan Sleevi 2012/09/19 17:43:27 No need for net:: on line 77 or here (because of l
Philippe 2012/09/19 17:57:04 Indeed.
+ connection_type_);
}
} // namespace android
« no previous file with comments | « net/android/network_change_notifier_android.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698