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 18 matching lines...) Expand all Loading... |
29 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | 29 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
30 } | 30 } |
31 return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type); | 31 return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type); |
32 } | 32 } |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid() | 36 NetworkChangeNotifierDelegateAndroid::NetworkChangeNotifierDelegateAndroid() |
37 : observers_(new ObserverListThreadSafe<Observer>()) { | 37 : observers_(new ObserverListThreadSafe<Observer>()) { |
38 JNIEnv* env = base::android::AttachCurrentThread(); | 38 JNIEnv* env = base::android::AttachCurrentThread(); |
39 java_network_change_notifier_.Reset( | 39 java_network_change_notifier_.Reset(Java_NetworkChangeNotifier_init( |
40 Java_NetworkChangeNotifier_init( | 40 env, base::android::GetApplicationContext())); |
41 env, base::android::GetApplicationContext())); | |
42 Java_NetworkChangeNotifier_addNativeObserver( | 41 Java_NetworkChangeNotifier_addNativeObserver( |
43 env, java_network_change_notifier_.obj(), | 42 env, |
| 43 java_network_change_notifier_.obj(), |
44 reinterpret_cast<intptr_t>(this)); | 44 reinterpret_cast<intptr_t>(this)); |
45 SetCurrentConnectionType( | 45 SetCurrentConnectionType( |
46 ConvertConnectionType( | 46 ConvertConnectionType(Java_NetworkChangeNotifier_getCurrentConnectionType( |
47 Java_NetworkChangeNotifier_getCurrentConnectionType( | 47 env, java_network_change_notifier_.obj()))); |
48 env, java_network_change_notifier_.obj()))); | |
49 } | 48 } |
50 | 49 |
51 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() { | 50 NetworkChangeNotifierDelegateAndroid::~NetworkChangeNotifierDelegateAndroid() { |
52 DCHECK(thread_checker_.CalledOnValidThread()); | 51 DCHECK(thread_checker_.CalledOnValidThread()); |
53 observers_->AssertEmpty(); | 52 observers_->AssertEmpty(); |
54 JNIEnv* env = base::android::AttachCurrentThread(); | 53 JNIEnv* env = base::android::AttachCurrentThread(); |
55 Java_NetworkChangeNotifier_removeNativeObserver( | 54 Java_NetworkChangeNotifier_removeNativeObserver( |
56 env, java_network_change_notifier_.obj(), | 55 env, |
| 56 java_network_change_notifier_.obj(), |
57 reinterpret_cast<intptr_t>(this)); | 57 reinterpret_cast<intptr_t>(this)); |
58 } | 58 } |
59 | 59 |
60 NetworkChangeNotifier::ConnectionType | 60 NetworkChangeNotifier::ConnectionType |
61 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const { | 61 NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType() const { |
62 base::AutoLock auto_lock(connection_type_lock_); | 62 base::AutoLock auto_lock(connection_type_lock_); |
63 return connection_type_; | 63 return connection_type_; |
64 } | 64 } |
65 | 65 |
66 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged( | 66 void NetworkChangeNotifierDelegateAndroid::NotifyConnectionTypeChanged( |
67 JNIEnv* env, | 67 JNIEnv* env, |
68 jobject obj, | 68 jobject obj, |
69 jint new_connection_type) { | 69 jint new_connection_type) { |
70 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
71 const ConnectionType actual_connection_type = ConvertConnectionType( | 71 const ConnectionType actual_connection_type = |
72 new_connection_type); | 72 ConvertConnectionType(new_connection_type); |
73 SetCurrentConnectionType(actual_connection_type); | 73 SetCurrentConnectionType(actual_connection_type); |
74 observers_->Notify(&Observer::OnConnectionTypeChanged); | 74 observers_->Notify(&Observer::OnConnectionTypeChanged); |
75 } | 75 } |
76 | 76 |
77 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*, | 77 jint NetworkChangeNotifierDelegateAndroid::GetConnectionType(JNIEnv*, |
78 jobject) const { | 78 jobject) const { |
79 DCHECK(thread_checker_.CalledOnValidThread()); | 79 DCHECK(thread_checker_.CalledOnValidThread()); |
80 return GetCurrentConnectionType(); | 80 return GetCurrentConnectionType(); |
81 } | 81 } |
82 | 82 |
83 void NetworkChangeNotifierDelegateAndroid::AddObserver( | 83 void NetworkChangeNotifierDelegateAndroid::AddObserver(Observer* observer) { |
84 Observer* observer) { | |
85 observers_->AddObserver(observer); | 84 observers_->AddObserver(observer); |
86 } | 85 } |
87 | 86 |
88 void NetworkChangeNotifierDelegateAndroid::RemoveObserver( | 87 void NetworkChangeNotifierDelegateAndroid::RemoveObserver(Observer* observer) { |
89 Observer* observer) { | |
90 observers_->RemoveObserver(observer); | 88 observers_->RemoveObserver(observer); |
91 } | 89 } |
92 | 90 |
93 // static | 91 // static |
94 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) { | 92 bool NetworkChangeNotifierDelegateAndroid::Register(JNIEnv* env) { |
95 return RegisterNativesImpl(env); | 93 return RegisterNativesImpl(env); |
96 } | 94 } |
97 | 95 |
98 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType( | 96 void NetworkChangeNotifierDelegateAndroid::SetCurrentConnectionType( |
99 ConnectionType new_connection_type) { | 97 ConnectionType new_connection_type) { |
100 base::AutoLock auto_lock(connection_type_lock_); | 98 base::AutoLock auto_lock(connection_type_lock_); |
101 connection_type_ = new_connection_type; | 99 connection_type_ = new_connection_type; |
102 } | 100 } |
103 | 101 |
104 void NetworkChangeNotifierDelegateAndroid::SetOnline() { | 102 void NetworkChangeNotifierDelegateAndroid::SetOnline() { |
105 JNIEnv* env = base::android::AttachCurrentThread(); | 103 JNIEnv* env = base::android::AttachCurrentThread(); |
106 Java_NetworkChangeNotifier_forceConnectivityState(env, true); | 104 Java_NetworkChangeNotifier_forceConnectivityState(env, true); |
107 } | 105 } |
108 | 106 |
109 void NetworkChangeNotifierDelegateAndroid::SetOffline() { | 107 void NetworkChangeNotifierDelegateAndroid::SetOffline() { |
110 JNIEnv* env = base::android::AttachCurrentThread(); | 108 JNIEnv* env = base::android::AttachCurrentThread(); |
111 Java_NetworkChangeNotifier_forceConnectivityState(env, false); | 109 Java_NetworkChangeNotifier_forceConnectivityState(env, false); |
112 } | 110 } |
113 | 111 |
114 } // namespace net | 112 } // namespace net |
OLD | NEW |