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 namespace android { | 12 namespace android { |
13 | 13 |
14 NetworkChangeNotifier::NetworkChangeNotifier() { | 14 namespace { |
15 JNIEnv* env = base::android::AttachCurrentThread(); | 15 |
16 CreateJavaObject(env); | 16 // Returns whether the provided connection type is known. |
17 bool CheckConnectionType(int connection_type) { | |
18 switch (connection_type) { | |
19 case NetworkChangeNotifier::CONNECTION_UNKNOWN: | |
20 case NetworkChangeNotifier::CONNECTION_ETHERNET: | |
21 case NetworkChangeNotifier::CONNECTION_WIFI: | |
22 case NetworkChangeNotifier::CONNECTION_2G: | |
23 case NetworkChangeNotifier::CONNECTION_3G: | |
24 case NetworkChangeNotifier::CONNECTION_4G: | |
25 case NetworkChangeNotifier::CONNECTION_NONE: | |
26 return true; | |
27 default: | |
28 NOTREACHED() << "Unknown connection type received: " << connection_type; | |
29 return false; | |
30 } | |
17 } | 31 } |
18 | 32 |
33 } // namespace | |
34 | |
19 NetworkChangeNotifier::~NetworkChangeNotifier() { | 35 NetworkChangeNotifier::~NetworkChangeNotifier() { |
20 JNIEnv* env = base::android::AttachCurrentThread(); | 36 JNIEnv* env = base::android::AttachCurrentThread(); |
21 Java_NetworkChangeNotifier_destroy( | 37 Java_NetworkChangeNotifier_destroy( |
22 env, java_network_change_notifier_.obj()); | 38 env, java_network_change_notifier_.obj()); |
23 } | 39 } |
24 | 40 |
41 void NetworkChangeNotifier::NotifyObservers( | |
42 JNIEnv* env, jobject obj, jint new_connection_type) { | |
43 int connection_type = CheckConnectionType(new_connection_type) ? | |
44 new_connection_type : CONNECTION_UNKNOWN; | |
45 SetConnectionType(connection_type); | |
46 NotifyObserversOfConnectionTypeChange(); | |
47 } | |
48 | |
49 jint NetworkChangeNotifier::GetConnectionType(JNIEnv* env, jobject obj) { | |
50 return GetCurrentConnectionType(); | |
51 } | |
52 | |
53 // static | |
54 bool NetworkChangeNotifier::Register(JNIEnv* env) { | |
55 return RegisterNativesImpl(env); | |
56 } | |
57 | |
58 NetworkChangeNotifier::NetworkChangeNotifier() { | |
59 SetConnectionType(CONNECTION_UNKNOWN); | |
60 JNIEnv* env = base::android::AttachCurrentThread(); | |
61 CreateJavaObject(env); | |
62 } | |
63 | |
64 void NetworkChangeNotifier::SetConnectionType(int connection_type) { | |
szym
2012/09/19 18:46:55
nit: make order of SetConnectionType and CreateJav
Philippe
2012/09/19 20:57:56
Done.
| |
65 base::AutoLock auto_lock(lock_); | |
66 connection_type_ = connection_type; | |
67 } | |
68 | |
25 void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) { | 69 void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) { |
26 java_network_change_notifier_.Reset( | 70 java_network_change_notifier_.Reset( |
27 Java_NetworkChangeNotifier_create( | 71 Java_NetworkChangeNotifier_create( |
28 env, | 72 env, |
29 base::android::GetApplicationContext(), | 73 base::android::GetApplicationContext(), |
30 reinterpret_cast<jint>(this))); | 74 reinterpret_cast<jint>(this))); |
31 } | 75 } |
32 | 76 |
33 void NetworkChangeNotifier::NotifyObservers(JNIEnv* env, jobject obj) { | |
34 NotifyObserversOfConnectionTypeChange(); | |
35 } | |
36 | |
37 net::NetworkChangeNotifier::ConnectionType | 77 net::NetworkChangeNotifier::ConnectionType |
szym
2012/09/19 18:46:55
No need for net:: here either.
Philippe
2012/09/19 20:57:56
Done.
| |
38 NetworkChangeNotifier::GetCurrentConnectionType() const { | 78 NetworkChangeNotifier::GetCurrentConnectionType() const { |
39 JNIEnv* env = base::android::AttachCurrentThread(); | 79 base::AutoLock auto_lock(lock_); |
40 | 80 return static_cast<NetworkChangeNotifier::ConnectionType>(connection_type_); |
41 // Pull the connection type from the Java-side then convert it to a | |
42 // native-side NetworkChangeNotifier::ConnectionType. | |
43 jint connection_type = Java_NetworkChangeNotifier_connectionType( | |
44 env, java_network_change_notifier_.obj()); | |
45 switch (connection_type) { | |
46 case CONNECTION_UNKNOWN: | |
47 return CONNECTION_UNKNOWN; | |
48 case CONNECTION_ETHERNET: | |
49 return CONNECTION_ETHERNET; | |
50 case CONNECTION_WIFI: | |
51 return CONNECTION_WIFI; | |
52 case CONNECTION_2G: | |
53 return CONNECTION_2G; | |
54 case CONNECTION_3G: | |
55 return CONNECTION_3G; | |
56 case CONNECTION_4G: | |
57 return CONNECTION_4G; | |
58 case CONNECTION_NONE: | |
59 return CONNECTION_NONE; | |
60 default: | |
61 NOTREACHED() << "Unknown connection type received: " << connection_type; | |
62 return CONNECTION_NONE; | |
63 } | |
64 } | |
65 | |
66 // static | |
67 bool NetworkChangeNotifier::Register(JNIEnv* env) { | |
68 return RegisterNativesImpl(env); | |
69 } | 81 } |
70 | 82 |
71 } // namespace android | 83 } // namespace android |
72 } // namespace net | 84 } // namespace net |
OLD | NEW |