| 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 #include "net/android/network_change_notifier_android.h" |
| 9 | 10 |
| 10 namespace net { | 11 namespace net { |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 // Converts a Java side connection type (integer) to | 15 // Converts a Java side connection type (integer) to |
| 15 // the native side NetworkChangeNotifier::ConnectionType. | 16 // the native side NetworkChangeNotifier::ConnectionType. |
| 16 NetworkChangeNotifier::ConnectionType ConvertConnectionType( | 17 NetworkChangeNotifier::ConnectionType ConvertConnectionType( |
| 17 jint connection_type) { | 18 jint connection_type) { |
| 18 switch (connection_type) { | 19 switch (connection_type) { |
| 19 case NetworkChangeNotifier::CONNECTION_UNKNOWN: | 20 case NetworkChangeNotifier::CONNECTION_UNKNOWN: |
| 20 case NetworkChangeNotifier::CONNECTION_ETHERNET: | 21 case NetworkChangeNotifier::CONNECTION_ETHERNET: |
| 21 case NetworkChangeNotifier::CONNECTION_WIFI: | 22 case NetworkChangeNotifier::CONNECTION_WIFI: |
| 22 case NetworkChangeNotifier::CONNECTION_2G: | 23 case NetworkChangeNotifier::CONNECTION_2G: |
| 23 case NetworkChangeNotifier::CONNECTION_3G: | 24 case NetworkChangeNotifier::CONNECTION_3G: |
| 24 case NetworkChangeNotifier::CONNECTION_4G: | 25 case NetworkChangeNotifier::CONNECTION_4G: |
| 25 case NetworkChangeNotifier::CONNECTION_NONE: | 26 case NetworkChangeNotifier::CONNECTION_NONE: |
| 26 case NetworkChangeNotifier::CONNECTION_BLUETOOTH: | 27 case NetworkChangeNotifier::CONNECTION_BLUETOOTH: |
| 27 break; | 28 break; |
| 28 default: | 29 default: |
| 29 NOTREACHED() << "Unknown connection type received: " << connection_type; | 30 NOTREACHED() << "Unknown connection type received: " << connection_type; |
| 30 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | 31 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 31 } | 32 } |
| 32 return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type); | 33 return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type); |
| 33 } | 34 } |
| 34 | 35 |
| 36 // Converts a Java side connection type (integer) to |
| 37 // the native side NetworkChangeNotifier::ConnectionType. |
| 38 NetworkChangeNotifier::ConnectionSubtype ConvertConnectionSubtype( |
| 39 jint subtype) { |
| 40 DCHECK(subtype >= 0 && subtype <= NetworkChangeNotifier::SUBTYPE_LAST); |
| 41 |
| 42 return static_cast<NetworkChangeNotifier::ConnectionSubtype>(subtype); |
| 43 } |
| 44 |
| 35 } // namespace | 45 } // namespace |
| 36 | 46 |
| 47 jdouble GetMaxBandwidthForConnectionSubtype(JNIEnv* env, |
| 48 jclass caller, |
| 49 jint subtype) { |
| 50 return NetworkChangeNotifierAndroid::GetMaxBandwidthForConnectionSubtype( |
| 51 ConvertConnectionSubtype(subtype)); |
| 52 } |
| 53 |
| 37 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid() | 54 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid() |
| 38 : observers_(new ObserverListThreadSafe<Observer>()) { | 55 : observers_(new ObserverListThreadSafe<Observer>()) { |
| 39 JNIEnv* env = base::android::AttachCurrentThread(); | 56 JNIEnv* env = base::android::AttachCurrentThread(); |
| 40 java_network_change_notifier_.Reset( | 57 java_network_change_notifier_.Reset( |
| 41 Java_NetworkChangeNotifier_init( | 58 Java_NetworkChangeNotifier_init( |
| 42 env, base::android::GetApplicationContext())); | 59 env, base::android::GetApplicationContext())); |
| 43 Java_NetworkChangeNotifier_addNativeObserver( | 60 Java_NetworkChangeNotifier_addNativeObserver( |
| 44 env, java_network_change_notifier_.obj(), | 61 env, java_network_change_notifier_.obj(), |
| 45 reinterpret_cast<intptr_t>(this)); | 62 reinterpret_cast<intptr_t>(this)); |
| 46 SetCurrentConnectionType( | 63 SetCurrentConnectionType( |
| 47 ConvertConnectionType( | 64 ConvertConnectionType( |
| 48 Java_NetworkChangeNotifier_getCurrentConnectionType( | 65 Java_NetworkChangeNotifier_getCurrentConnectionType( |
| 49 env, java_network_change_notifier_.obj()))); | 66 env, java_network_change_notifier_.obj()))); |
| 67 SetCurrentMaxBandwidth(Java_NetworkChangeNotifier_getCurrentMaxBandwidth( |
| 68 env, java_network_change_notifier_.obj())); |
| 50 } | 69 } |
| 51 | 70 |
| 52 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() { | 71 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); | 72 DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 observers_->AssertEmpty(); | 73 observers_->AssertEmpty(); |
| 55 JNIEnv* env = base::android::AttachCurrentThread(); | 74 JNIEnv* env = base::android::AttachCurrentThread(); |
| 56 Java_NetworkChangeNotifier_removeNativeObserver( | 75 Java_NetworkChangeNotifier_removeNativeObserver( |
| 57 env, java_network_change_notifier_.obj(), | 76 env, java_network_change_notifier_.obj(), |
| 58 reinterpret_cast<intptr_t>(this)); | 77 reinterpret_cast<intptr_t>(this)); |
| 59 } | 78 } |
| 60 | 79 |
| 61 NetworkChangeNotifier::ConnectionType | 80 NetworkChangeNotifier::ConnectionType |
| 62 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const { | 81 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const { |
| 63 base::AutoLock auto_lock(connection_type_lock_); | 82 base::AutoLock auto_lock(connection_lock_); |
| 64 return connection_type_; | 83 return connection_type_; |
| 65 } | 84 } |
| 66 | 85 |
| 86 double NetworkChangeNotifierDelegateAndroid::GetCurrentMaxBandwidth() const { |
| 87 base::AutoLock auto_lock(connection_lock_); |
| 88 return connection_max_bandwidth_; |
| 89 } |
| 90 |
| 67 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged( | 91 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged( |
| 68 JNIEnv* env, | 92 JNIEnv* env, |
| 69 jobject obj, | 93 jobject obj, |
| 70 jint new_connection_type) { | 94 jint new_connection_type) { |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 const ConnectionType actual_connection_type = ConvertConnectionType( | 96 const ConnectionType actual_connection_type = ConvertConnectionType( |
| 73 new_connection_type); | 97 new_connection_type); |
| 74 SetCurrentConnectionType(actual_connection_type); | 98 SetCurrentConnectionType(actual_connection_type); |
| 99 SetCurrentMaxBandwidth(Java_NetworkChangeNotifier_getCurrentMaxBandwidth( |
| 100 env, java_network_change_notifier_.obj())); |
| 75 observers_->Notify(&Observer::OnConnectionTypeChanged); | 101 observers_->Notify(&Observer::OnConnectionTypeChanged); |
| 76 } | 102 } |
| 77 | 103 |
| 78 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*, | 104 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*, |
| 79 jobject) const { | 105 jobject) const { |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
| 81 return GetCurrentConnectionType(); | 107 return GetCurrentConnectionType(); |
| 82 } | 108 } |
| 83 | 109 |
| 84 void NetworkChangeNotifierDelegateAndroid::AddObserver( | 110 void NetworkChangeNotifierDelegateAndroid::AddObserver( |
| 85 Observer* observer) { | 111 Observer* observer) { |
| 86 observers_->AddObserver(observer); | 112 observers_->AddObserver(observer); |
| 87 } | 113 } |
| 88 | 114 |
| 89 void NetworkChangeNotifierDelegateAndroid::RemoveObserver( | 115 void NetworkChangeNotifierDelegateAndroid::RemoveObserver( |
| 90 Observer* observer) { | 116 Observer* observer) { |
| 91 observers_->RemoveObserver(observer); | 117 observers_->RemoveObserver(observer); |
| 92 } | 118 } |
| 93 | 119 |
| 94 // static | 120 // static |
| 95 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) { | 121 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) { |
| 96 return RegisterNativesImpl(env); | 122 return RegisterNativesImpl(env); |
| 97 } | 123 } |
| 98 | 124 |
| 99 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType( | 125 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType( |
| 100 ConnectionType new_connection_type) { | 126 ConnectionType new_connection_type) { |
| 101 base::AutoLock auto_lock(connection_type_lock_); | 127 base::AutoLock auto_lock(connection_lock_); |
| 102 connection_type_ = new_connection_type; | 128 connection_type_ = new_connection_type; |
| 103 } | 129 } |
| 104 | 130 |
| 131 void NetworkChangeNotifierDelegateAndroid::SetCurrentMaxBandwidth( |
| 132 double max_bandwidth) { |
| 133 base::AutoLock auto_lock(connection_lock_); |
| 134 connection_max_bandwidth_ = max_bandwidth; |
| 135 } |
| 136 |
| 105 void NetworkChangeNotifierDelegateAndroid::SetOnline() { | 137 void NetworkChangeNotifierDelegateAndroid::SetOnline() { |
| 106 JNIEnv* env = base::android::AttachCurrentThread(); | 138 JNIEnv* env = base::android::AttachCurrentThread(); |
| 107 Java_NetworkChangeNotifier_forceConnectivityState(env, true); | 139 Java_NetworkChangeNotifier_forceConnectivityState(env, true); |
| 108 } | 140 } |
| 109 | 141 |
| 110 void NetworkChangeNotifierDelegateAndroid::SetOffline() { | 142 void NetworkChangeNotifierDelegateAndroid::SetOffline() { |
| 111 JNIEnv* env = base::android::AttachCurrentThread(); | 143 JNIEnv* env = base::android::AttachCurrentThread(); |
| 112 Java_NetworkChangeNotifier_forceConnectivityState(env, false); | 144 Java_NetworkChangeNotifier_forceConnectivityState(env, false); |
| 113 } | 145 } |
| 114 | 146 |
| 115 } // namespace net | 147 } // namespace net |
| OLD | NEW |