Chromium Code Reviews| 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..609162236a0ed61daf1d441c6c41c8b225d121ed 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) { |
| + int connection_type = CheckConnectionType(new_connection_type) ? |
| + new_connection_type : CONNECTION_UNKNOWN; |
| + SetConnectionType(connection_type); |
| + 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) { |
|
szym
2012/09/19 18:46:55
nit: make order of SetConnectionType and CreateJav
Philippe
2012/09/19 20:57:56
Done.
|
| + 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,10 @@ void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) { |
| reinterpret_cast<jint>(this))); |
| } |
| -void NetworkChangeNotifier::NotifyObservers(JNIEnv* env, jobject obj) { |
| - NotifyObserversOfConnectionTypeChange(); |
| -} |
| - |
| net::NetworkChangeNotifier::ConnectionType |
|
szym
2012/09/19 18:46:55
No need for net:: here either.
Philippe
2012/09/19 20:57:56
Done.
|
| 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<NetworkChangeNotifier::ConnectionType>(connection_type_); |
| } |
| } // namespace android |