| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/android/network_change_notifier_delegate_android.h" | 5 #include "net/android/network_change_notifier_delegate_android.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "jni/NetworkChangeNotifier_jni.h" | 8 #include "jni/NetworkChangeNotifier_jni.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 // Returns whether the provided connection type is known. | 14 // Converts a Java side connection type (integer) to |
| 15 bool CheckConnectionType(int connection_type) { | 15 // the native side NetworkChangeNotifier::ConnectionType. |
| 16 NetworkChangeNotifier::ConnectionType ConvertConnectionType( |
| 17 jint connection_type) { |
| 16 switch (connection_type) { | 18 switch (connection_type) { |
| 17 case NetworkChangeNotifier::CONNECTION_UNKNOWN: | 19 case NetworkChangeNotifier::CONNECTION_UNKNOWN: |
| 18 case NetworkChangeNotifier::CONNECTION_ETHERNET: | 20 case NetworkChangeNotifier::CONNECTION_ETHERNET: |
| 19 case NetworkChangeNotifier::CONNECTION_WIFI: | 21 case NetworkChangeNotifier::CONNECTION_WIFI: |
| 20 case NetworkChangeNotifier::CONNECTION_2G: | 22 case NetworkChangeNotifier::CONNECTION_2G: |
| 21 case NetworkChangeNotifier::CONNECTION_3G: | 23 case NetworkChangeNotifier::CONNECTION_3G: |
| 22 case NetworkChangeNotifier::CONNECTION_4G: | 24 case NetworkChangeNotifier::CONNECTION_4G: |
| 23 case NetworkChangeNotifier::CONNECTION_NONE: | 25 case NetworkChangeNotifier::CONNECTION_NONE: |
| 24 return true; | 26 break; |
| 25 default: | 27 default: |
| 26 NOTREACHED() << "Unknown connection type received: " << connection_type; | 28 NOTREACHED() << "Unknown connection type received: " << connection_type; |
| 27 return false; | 29 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 28 } | 30 } |
| 31 return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type); |
| 29 } | 32 } |
| 30 | 33 |
| 31 } // namespace | 34 } // namespace |
| 32 | 35 |
| 33 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid() | 36 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid() |
| 34 : observers_(new ObserverListThreadSafe<Observer>()) { | 37 : observers_(new ObserverListThreadSafe<Observer>()) { |
| 38 JNIEnv* env = base::android::AttachCurrentThread(); |
| 35 java_network_change_notifier_.Reset( | 39 java_network_change_notifier_.Reset( |
| 36 Java_NetworkChangeNotifier_createInstance( | 40 Java_NetworkChangeNotifier_init( |
| 37 base::android::AttachCurrentThread(), | 41 env, base::android::GetApplicationContext())); |
| 38 base::android::GetApplicationContext(), | 42 Java_NetworkChangeNotifier_addNativeObserver( |
| 39 reinterpret_cast<jint>(this))); | 43 env, java_network_change_notifier_.obj(), reinterpret_cast<jint>(this)); |
| 44 SetCurrentConnectionType( |
| 45 ConvertConnectionType( |
| 46 Java_NetworkChangeNotifier_getCurrentConnectionType( |
| 47 env, java_network_change_notifier_.obj()))); |
| 40 } | 48 } |
| 41 | 49 |
| 42 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() { | 50 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() { |
| 43 DCHECK(thread_checker_.CalledOnValidThread()); | 51 DCHECK(thread_checker_.CalledOnValidThread()); |
| 44 observers_->AssertEmpty(); | 52 observers_->AssertEmpty(); |
| 45 JNIEnv* env = base::android::AttachCurrentThread(); | 53 JNIEnv* env = base::android::AttachCurrentThread(); |
| 46 Java_NetworkChangeNotifier_destroyInstance(env); | 54 Java_NetworkChangeNotifier_removeNativeObserver( |
| 55 env, java_network_change_notifier_.obj(), reinterpret_cast<jint>(this)); |
| 56 } |
| 57 |
| 58 NetworkChangeNotifier::ConnectionType |
| 59 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const { |
| 60 base::AutoLock auto_lock(connection_type_lock_); |
| 61 return connection_type_; |
| 47 } | 62 } |
| 48 | 63 |
| 49 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged( | 64 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged( |
| 50 JNIEnv* env, | 65 JNIEnv* env, |
| 51 jobject obj, | 66 jobject obj, |
| 52 jint new_connection_type) { | 67 jint new_connection_type) { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); | 68 DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 connection_type_ = CheckConnectionType(new_connection_type) ? | 69 const ConnectionType actual_connection_type = ConvertConnectionType( |
| 55 static_cast<ConnectionType>(new_connection_type) : | 70 new_connection_type); |
| 56 NetworkChangeNotifier::CONNECTION_UNKNOWN; | 71 SetCurrentConnectionType(actual_connection_type); |
| 57 observers_->Notify(&Observer::OnConnectionTypeChanged, connection_type_); | 72 observers_->Notify(&Observer::OnConnectionTypeChanged); |
| 58 } | 73 } |
| 59 | 74 |
| 60 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*, | 75 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*, |
| 61 jobject) const { | 76 jobject) const { |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); | 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 63 return connection_type_; | 78 return GetCurrentConnectionType(); |
| 64 } | 79 } |
| 65 | 80 |
| 66 void NetworkChangeNotifierDelegateAndroid::AddObserver( | 81 void NetworkChangeNotifierDelegateAndroid::AddObserver( |
| 67 Observer* observer) { | 82 Observer* observer) { |
| 68 observers_->AddObserver(observer); | 83 observers_->AddObserver(observer); |
| 69 } | 84 } |
| 70 | 85 |
| 71 void NetworkChangeNotifierDelegateAndroid::RemoveObserver( | 86 void NetworkChangeNotifierDelegateAndroid::RemoveObserver( |
| 72 Observer* observer) { | 87 Observer* observer) { |
| 73 observers_->RemoveObserver(observer); | 88 observers_->RemoveObserver(observer); |
| 74 } | 89 } |
| 75 | 90 |
| 76 void NetworkChangeNotifierDelegateAndroid::ForceConnectivityState( | |
| 77 ConnectivityState state) { | |
| 78 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 79 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 80 Java_NetworkChangeNotifier_forceConnectivityState(env, state == ONLINE); | |
| 81 } | |
| 82 | |
| 83 // static | 91 // static |
| 84 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) { | 92 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) { |
| 85 return RegisterNativesImpl(env); | 93 return RegisterNativesImpl(env); |
| 86 } | 94 } |
| 87 | 95 |
| 96 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType( |
| 97 ConnectionType new_connection_type) { |
| 98 base::AutoLock auto_lock(connection_type_lock_); |
| 99 connection_type_ = new_connection_type; |
| 100 } |
| 101 |
| 102 void NetworkChangeNotifierDelegateAndroid::SetOnline() { |
| 103 JNIEnv* env = base::android::AttachCurrentThread(); |
| 104 Java_NetworkChangeNotifier_forceConnectivityState(env, true); |
| 105 } |
| 106 |
| 107 void NetworkChangeNotifierDelegateAndroid::SetOffline() { |
| 108 JNIEnv* env = base::android::AttachCurrentThread(); |
| 109 Java_NetworkChangeNotifier_forceConnectivityState(env, false); |
| 110 } |
| 111 |
| 88 } // namespace net | 112 } // namespace net |
| OLD | NEW |