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_android.h" | 5 #include "net/android/network_change_notifier_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/android/jni_android.h" | |
9 #include "jni/NetworkChangeNotifier_jni.h" | 9 #include "jni/NetworkChangeNotifier_jni.h" |
10 | 10 |
11 namespace net { | 11 namespace net { |
12 | 12 |
13 namespace { | |
14 | |
15 // Returns whether the provided connection type is known. | |
16 bool CheckConnectionType(int connection_type) { | |
17 switch (connection_type) { | |
18 case NetworkChangeNotifier::CONNECTION_UNKNOWN: | |
19 case NetworkChangeNotifier::CONNECTION_ETHERNET: | |
20 case NetworkChangeNotifier::CONNECTION_WIFI: | |
21 case NetworkChangeNotifier::CONNECTION_2G: | |
22 case NetworkChangeNotifier::CONNECTION_3G: | |
23 case NetworkChangeNotifier::CONNECTION_4G: | |
24 case NetworkChangeNotifier::CONNECTION_NONE: | |
25 return true; | |
26 default: | |
27 NOTREACHED() << "Unknown connection type received: " << connection_type; | |
28 return false; | |
29 } | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 NetworkChangeNotifierAndroid::~NetworkChangeNotifierAndroid() { | |
35 JNIEnv* env = base::android::AttachCurrentThread(); | |
36 Java_NetworkChangeNotifier_destroyInstance(env); | |
37 } | |
38 | |
39 void NetworkChangeNotifierAndroid::NotifyObserversOfConnectionTypeChange( | |
40 JNIEnv* env, | |
41 jobject obj, | |
42 jint new_connection_type) { | |
43 int connection_type = CheckConnectionType(new_connection_type) ? | |
44 new_connection_type : CONNECTION_UNKNOWN; | |
gone
2012/09/20 21:52:01
Just saw this when rebasing my patch; this overloa
szym
2012/09/20 21:54:19
I believe so. CONNECTION_NONE is used to indicate
| |
45 SetConnectionType(connection_type); | |
46 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); | |
47 } | |
48 | |
49 jint NetworkChangeNotifierAndroid::GetConnectionType(JNIEnv* env, jobject obj) { | |
50 return GetCurrentConnectionType(); | |
51 } | |
52 | |
53 // static | |
54 bool NetworkChangeNotifierAndroid::Register(JNIEnv* env) { | |
55 return RegisterNativesImpl(env); | |
56 } | |
57 | |
13 NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid() { | 58 NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid() { |
59 SetConnectionType(CONNECTION_UNKNOWN); | |
14 JNIEnv* env = base::android::AttachCurrentThread(); | 60 JNIEnv* env = base::android::AttachCurrentThread(); |
15 java_network_change_notifier_.Reset( | 61 java_network_change_notifier_.Reset( |
16 Java_NetworkChangeNotifier_createInstance( | 62 Java_NetworkChangeNotifier_createInstance( |
17 env, | 63 env, |
18 base::android::GetApplicationContext(), | 64 base::android::GetApplicationContext(), |
19 reinterpret_cast<jint>(this))); | 65 reinterpret_cast<jint>(this))); |
20 } | 66 } |
21 | 67 |
22 NetworkChangeNotifierAndroid::~NetworkChangeNotifierAndroid() { | 68 void NetworkChangeNotifierAndroid::SetConnectionType(int connection_type) { |
23 JNIEnv* env = base::android::AttachCurrentThread(); | 69 base::AutoLock auto_lock(lock_); |
24 Java_NetworkChangeNotifier_destroyInstance(env); | 70 connection_type_ = connection_type; |
25 java_network_change_notifier_.Reset(); | |
26 } | |
27 | |
28 void NetworkChangeNotifierAndroid::NotifyObserversOfConnectionTypeChange( | |
29 JNIEnv* env, | |
30 jobject obj) { | |
31 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); | |
32 } | 71 } |
33 | 72 |
34 void NetworkChangeNotifierAndroid::ForceConnectivityState(bool state) { | 73 void NetworkChangeNotifierAndroid::ForceConnectivityState(bool state) { |
35 JNIEnv* env = base::android::AttachCurrentThread(); | 74 JNIEnv* env = base::android::AttachCurrentThread(); |
36 Java_NetworkChangeNotifier_forceConnectivityState(env, state); | 75 Java_NetworkChangeNotifier_forceConnectivityState(env, state); |
37 } | 76 } |
38 | 77 |
39 NetworkChangeNotifier::ConnectionType | 78 NetworkChangeNotifier::ConnectionType |
40 NetworkChangeNotifierAndroid::GetCurrentConnectionType() const { | 79 NetworkChangeNotifierAndroid::GetCurrentConnectionType() const { |
41 JNIEnv* env = base::android::AttachCurrentThread(); | 80 base::AutoLock auto_lock(lock_); |
42 | 81 return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type_); |
43 // Pull the connection type from the Java-side then convert it to a | |
44 // native-side NetworkChangeNotifier::ConnectionType. | |
45 jint connection_type = Java_NetworkChangeNotifier_connectionType( | |
46 env, java_network_change_notifier_.obj()); | |
47 switch (connection_type) { | |
48 case CONNECTION_UNKNOWN: | |
49 return CONNECTION_UNKNOWN; | |
50 case CONNECTION_ETHERNET: | |
51 return CONNECTION_ETHERNET; | |
52 case CONNECTION_WIFI: | |
53 return CONNECTION_WIFI; | |
54 case CONNECTION_2G: | |
55 return CONNECTION_2G; | |
56 case CONNECTION_3G: | |
57 return CONNECTION_3G; | |
58 case CONNECTION_4G: | |
59 return CONNECTION_4G; | |
60 case CONNECTION_NONE: | |
61 return CONNECTION_NONE; | |
62 default: | |
63 NOTREACHED() << "Unknown connection type received: " << connection_type; | |
64 return CONNECTION_NONE; | |
65 } | |
66 } | |
67 | |
68 // static | |
69 bool NetworkChangeNotifierAndroid::Register(JNIEnv* env) { | |
70 return RegisterNativesImpl(env); | |
71 } | 82 } |
72 | 83 |
73 } // namespace net | 84 } // namespace net |
OLD | NEW |