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 |