| 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 { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 default: | 25 default: |
| 26 NOTREACHED() << "Unknown connection type received: " << connection_type; | 26 NOTREACHED() << "Unknown connection type received: " << connection_type; |
| 27 return false; | 27 return false; |
| 28 } | 28 } |
| 29 } | 29 } |
| 30 | 30 |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| 33 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid() | 33 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid() |
| 34 : observers_(new ObserverListThreadSafe<Observer>()) { | 34 : observers_(new ObserverListThreadSafe<Observer>()) { |
| 35 SetConnectionType(NetworkChangeNotifier::CONNECTION_UNKNOWN); |
| 35 java_network_change_notifier_.Reset( | 36 java_network_change_notifier_.Reset( |
| 36 Java_NetworkChangeNotifier_createInstance( | 37 Java_NetworkChangeNotifier_createInstance( |
| 37 base::android::AttachCurrentThread(), | 38 base::android::AttachCurrentThread(), |
| 38 base::android::GetApplicationContext(), | 39 base::android::GetApplicationContext(), |
| 39 reinterpret_cast<jint>(this))); | 40 reinterpret_cast<jint>(this))); |
| 40 } | 41 } |
| 41 | 42 |
| 42 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() { | 43 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() { |
| 43 DCHECK(thread_checker_.CalledOnValidThread()); | 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 44 observers_->AssertEmpty(); | 45 observers_->AssertEmpty(); |
| 45 JNIEnv* env = base::android::AttachCurrentThread(); | 46 JNIEnv* env = base::android::AttachCurrentThread(); |
| 46 Java_NetworkChangeNotifier_destroyInstance(env); | 47 Java_NetworkChangeNotifier_destroyInstance(env); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged( | 50 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged( |
| 50 JNIEnv* env, | 51 JNIEnv* env, |
| 51 jobject obj, | 52 jobject obj, |
| 52 jint new_connection_type) { | 53 jint new_connection_type) { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); | 54 DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 connection_type_ = CheckConnectionType(new_connection_type) ? | 55 SetConnectionType( |
| 55 static_cast<ConnectionType>(new_connection_type) : | 56 CheckConnectionType(new_connection_type) ? |
| 56 NetworkChangeNotifier::CONNECTION_UNKNOWN; | 57 static_cast<ConnectionType>(new_connection_type) : |
| 58 NetworkChangeNotifier::CONNECTION_UNKNOWN); |
| 57 observers_->Notify(&Observer::OnConnectionTypeChanged, connection_type_); | 59 observers_->Notify(&Observer::OnConnectionTypeChanged, connection_type_); |
| 58 } | 60 } |
| 59 | 61 |
| 60 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*, | 62 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*, |
| 61 jobject) const { | 63 jobject) const { |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
| 65 return GetCurrentConnectionType(); |
| 66 } |
| 67 |
| 68 NetworkChangeNotifierDelegateAndroid::ConnectionType |
| 69 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const { |
| 70 base::AutoLock auto_lock(connection_type_lock_); |
| 63 return connection_type_; | 71 return connection_type_; |
| 64 } | 72 } |
| 65 | 73 |
| 66 void NetworkChangeNotifierDelegateAndroid::AddObserver( | 74 void NetworkChangeNotifierDelegateAndroid::AddObserver( |
| 67 Observer* observer) { | 75 Observer* observer) { |
| 68 observers_->AddObserver(observer); | 76 observers_->AddObserver(observer); |
| 69 } | 77 } |
| 70 | 78 |
| 71 void NetworkChangeNotifierDelegateAndroid::RemoveObserver( | 79 void NetworkChangeNotifierDelegateAndroid::RemoveObserver( |
| 72 Observer* observer) { | 80 Observer* observer) { |
| 73 observers_->RemoveObserver(observer); | 81 observers_->RemoveObserver(observer); |
| 74 } | 82 } |
| 75 | 83 |
| 76 void NetworkChangeNotifierDelegateAndroid::ForceConnectivityState( | 84 void NetworkChangeNotifierDelegateAndroid::ForceConnectivityState( |
| 77 ConnectivityState state) { | 85 ConnectivityState state) { |
| 78 DCHECK(thread_checker_.CalledOnValidThread()); | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 JNIEnv* env = base::android::AttachCurrentThread(); | 87 JNIEnv* env = base::android::AttachCurrentThread(); |
| 80 Java_NetworkChangeNotifier_forceConnectivityState(env, state == ONLINE); | 88 Java_NetworkChangeNotifier_forceConnectivityState(env, state == ONLINE); |
| 81 } | 89 } |
| 82 | 90 |
| 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::SetConnectionType( |
| 97 ConnectionType new_connection_type) { |
| 98 base::AutoLock auto_lock(connection_type_lock_); |
| 99 connection_type_ = new_connection_type; |
| 100 } |
| 101 |
| 88 } // namespace net | 102 } // namespace net |
| OLD | NEW |