OLD | NEW |
1 | |
2 /* | 1 /* |
3 * libjingle | 2 * libjingle |
4 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
5 * | 4 * |
6 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
8 * | 7 * |
9 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
10 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
11 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 14 matching lines...) Expand all Loading... |
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 * | 26 * |
28 */ | 27 */ |
29 #include "talk/app/webrtc/java/jni/jni_helpers.h" | 28 #include "talk/app/webrtc/java/jni/jni_helpers.h" |
30 | 29 |
31 #include <asm/unistd.h> | 30 #include <asm/unistd.h> |
32 #include <sys/prctl.h> | 31 #include <sys/prctl.h> |
33 #include <sys/syscall.h> | 32 #include <sys/syscall.h> |
34 #include <unistd.h> | 33 #include <unistd.h> |
35 | 34 |
36 #include "unicode/unistr.h" | |
37 | |
38 namespace webrtc_jni { | 35 namespace webrtc_jni { |
39 | 36 |
40 static JavaVM* g_jvm = nullptr; | 37 static JavaVM* g_jvm = nullptr; |
41 | 38 |
42 static pthread_once_t g_jni_ptr_once = PTHREAD_ONCE_INIT; | 39 static pthread_once_t g_jni_ptr_once = PTHREAD_ONCE_INIT; |
43 | 40 |
44 // Key for per-thread JNIEnv* data. Non-NULL in threads attached to |g_jvm| by | 41 // Key for per-thread JNIEnv* data. Non-NULL in threads attached to |g_jvm| by |
45 // AttachCurrentThreadIfNeeded(), NULL in unattached threads and threads that | 42 // AttachCurrentThreadIfNeeded(), NULL in unattached threads and threads that |
46 // were attached by the JVM because of a Java->native call. | 43 // were attached by the JVM because of a Java->native call. |
47 static pthread_key_t g_jni_ptr; | 44 static pthread_key_t g_jni_ptr; |
48 | 45 |
49 using icu::UnicodeString; | |
50 | |
51 JavaVM *GetJVM() { | 46 JavaVM *GetJVM() { |
52 RTC_CHECK(g_jvm) << "JNI_OnLoad failed to run?"; | 47 RTC_CHECK(g_jvm) << "JNI_OnLoad failed to run?"; |
53 return g_jvm; | 48 return g_jvm; |
54 } | 49 } |
55 | 50 |
56 // Return a |JNIEnv*| usable on this thread or NULL if this thread is detached. | 51 // Return a |JNIEnv*| usable on this thread or NULL if this thread is detached. |
57 JNIEnv* GetEnv() { | 52 JNIEnv* GetEnv() { |
58 void* env = NULL; | 53 void* env = NULL; |
59 jint status = g_jvm->GetEnv(&env, JNI_VERSION_1_6); | 54 jint status = g_jvm->GetEnv(&env, JNI_VERSION_1_6); |
60 RTC_CHECK(((env != NULL) && (status == JNI_OK)) || | 55 RTC_CHECK(((env != NULL) && (status == JNI_OK)) || |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 220 |
226 // Java references to "null" can only be distinguished as such in C++ by | 221 // Java references to "null" can only be distinguished as such in C++ by |
227 // creating a local reference, so this helper wraps that logic. | 222 // creating a local reference, so this helper wraps that logic. |
228 bool IsNull(JNIEnv* jni, jobject obj) { | 223 bool IsNull(JNIEnv* jni, jobject obj) { |
229 ScopedLocalRefFrame local_ref_frame(jni); | 224 ScopedLocalRefFrame local_ref_frame(jni); |
230 return jni->NewLocalRef(obj) == NULL; | 225 return jni->NewLocalRef(obj) == NULL; |
231 } | 226 } |
232 | 227 |
233 // Given a UTF-8 encoded |native| string return a new (UTF-16) jstring. | 228 // Given a UTF-8 encoded |native| string return a new (UTF-16) jstring. |
234 jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native) { | 229 jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native) { |
235 UnicodeString ustr(UnicodeString::fromUTF8(native)); | 230 jstring jstr = jni->NewStringUTF(native.c_str()); |
236 jstring jstr = jni->NewString(ustr.getBuffer(), ustr.length()); | 231 CHECK_EXCEPTION(jni) << "error during NewStringUTF"; |
237 CHECK_EXCEPTION(jni) << "error during NewString"; | |
238 return jstr; | 232 return jstr; |
239 } | 233 } |
240 | 234 |
241 // Given a (UTF-16) jstring return a new UTF-8 native string. | 235 // Given a (UTF-16) jstring return a new UTF-8 native string. |
242 std::string JavaToStdString(JNIEnv* jni, const jstring& j_string) { | 236 std::string JavaToStdString(JNIEnv* jni, const jstring& j_string) { |
243 const jchar* jchars = jni->GetStringChars(j_string, NULL); | 237 const char* chars = jni->GetStringUTFChars(j_string, NULL); |
244 CHECK_EXCEPTION(jni) << "Error during GetStringChars"; | 238 CHECK_EXCEPTION(jni) << "Error during GetStringUTFChars"; |
245 UnicodeString ustr(jchars, jni->GetStringLength(j_string)); | 239 std::string str(chars, jni->GetStringUTFLength(j_string)); |
246 CHECK_EXCEPTION(jni) << "Error during GetStringLength"; | 240 CHECK_EXCEPTION(jni) << "Error during GetStringUTFLength"; |
247 jni->ReleaseStringChars(j_string, jchars); | 241 jni->ReleaseStringUTFChars(j_string, chars); |
248 CHECK_EXCEPTION(jni) << "Error during ReleaseStringChars"; | 242 CHECK_EXCEPTION(jni) << "Error during ReleaseStringUTFChars"; |
249 std::string ret; | 243 return str; |
250 return ustr.toUTF8String(ret); | |
251 } | 244 } |
252 | 245 |
253 // Return the (singleton) Java Enum object corresponding to |index|; | 246 // Return the (singleton) Java Enum object corresponding to |index|; |
254 jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class, | 247 jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class, |
255 const std::string& state_class_name, int index) { | 248 const std::string& state_class_name, int index) { |
256 jmethodID state_values_id = GetStaticMethodID( | 249 jmethodID state_values_id = GetStaticMethodID( |
257 jni, state_class, "values", ("()[L" + state_class_name + ";").c_str()); | 250 jni, state_class, "values", ("()[L" + state_class_name + ";").c_str()); |
258 jobjectArray state_values = static_cast<jobjectArray>( | 251 jobjectArray state_values = static_cast<jobjectArray>( |
259 jni->CallStaticObjectMethod(state_class, state_values_id)); | 252 jni->CallStaticObjectMethod(state_class, state_values_id)); |
260 CHECK_EXCEPTION(jni) << "error during CallStaticObjectMethod"; | 253 CHECK_EXCEPTION(jni) << "error during CallStaticObjectMethod"; |
(...skipping 18 matching lines...) Expand all Loading... |
279 // callbacks (i.e. entry points that don't originate in a Java callstack | 272 // callbacks (i.e. entry points that don't originate in a Java callstack |
280 // through a "native" method call). | 273 // through a "native" method call). |
281 ScopedLocalRefFrame::ScopedLocalRefFrame(JNIEnv* jni) : jni_(jni) { | 274 ScopedLocalRefFrame::ScopedLocalRefFrame(JNIEnv* jni) : jni_(jni) { |
282 RTC_CHECK(!jni_->PushLocalFrame(0)) << "Failed to PushLocalFrame"; | 275 RTC_CHECK(!jni_->PushLocalFrame(0)) << "Failed to PushLocalFrame"; |
283 } | 276 } |
284 ScopedLocalRefFrame::~ScopedLocalRefFrame() { | 277 ScopedLocalRefFrame::~ScopedLocalRefFrame() { |
285 jni_->PopLocalFrame(NULL); | 278 jni_->PopLocalFrame(NULL); |
286 } | 279 } |
287 | 280 |
288 } // namespace webrtc_jni | 281 } // namespace webrtc_jni |
OLD | NEW |