| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 #include "webrtc/sdk/android/src/jni/jni_helpers.h" | 10 #include "webrtc/sdk/android/src/jni/jni_helpers.h" |
| 11 | 11 |
| 12 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" | 12 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" |
| 13 | 13 |
| 14 #include <asm/unistd.h> | 14 #include <asm/unistd.h> |
| 15 #include <sys/prctl.h> | 15 #include <sys/prctl.h> |
| 16 #include <sys/syscall.h> | 16 #include <sys/syscall.h> |
| 17 #include <unistd.h> | 17 #include <unistd.h> |
| 18 #include <vector> |
| 18 | 19 |
| 19 namespace webrtc_jni { | 20 namespace webrtc_jni { |
| 20 | 21 |
| 21 static JavaVM* g_jvm = nullptr; | 22 static JavaVM* g_jvm = nullptr; |
| 22 | 23 |
| 23 static pthread_once_t g_jni_ptr_once = PTHREAD_ONCE_INIT; | 24 static pthread_once_t g_jni_ptr_once = PTHREAD_ONCE_INIT; |
| 24 | 25 |
| 25 // Key for per-thread JNIEnv* data. Non-NULL in threads attached to |g_jvm| by | 26 // Key for per-thread JNIEnv* data. Non-NULL in threads attached to |g_jvm| by |
| 26 // AttachCurrentThreadIfNeeded(), NULL in unattached threads and threads that | 27 // AttachCurrentThreadIfNeeded(), NULL in unattached threads and threads that |
| 27 // were attached by the JVM because of a Java->native call. | 28 // were attached by the JVM because of a Java->native call. |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 return jni->IsSameObject(obj, nullptr); | 213 return jni->IsSameObject(obj, nullptr); |
| 213 } | 214 } |
| 214 | 215 |
| 215 // Given a UTF-8 encoded |native| string return a new (UTF-16) jstring. | 216 // Given a UTF-8 encoded |native| string return a new (UTF-16) jstring. |
| 216 jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native) { | 217 jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native) { |
| 217 jstring jstr = jni->NewStringUTF(native.c_str()); | 218 jstring jstr = jni->NewStringUTF(native.c_str()); |
| 218 CHECK_EXCEPTION(jni) << "error during NewStringUTF"; | 219 CHECK_EXCEPTION(jni) << "error during NewStringUTF"; |
| 219 return jstr; | 220 return jstr; |
| 220 } | 221 } |
| 221 | 222 |
| 222 // Given a (UTF-16) jstring return a new UTF-8 native string. | 223 // Given a jstring, reinterprets it to a new native string. |
| 223 std::string JavaToStdString(JNIEnv* jni, const jstring& j_string) { | 224 std::string JavaToStdString(JNIEnv* jni, const jstring& j_string) { |
| 224 const char* chars = jni->GetStringUTFChars(j_string, nullptr); | 225 // Invoke String.getBytes(String charsetName) method to convert |j_string| |
| 225 CHECK_EXCEPTION(jni) << "Error during GetStringUTFChars"; | 226 // to a byte array. |
| 226 std::string str(chars, jni->GetStringUTFLength(j_string)); | 227 const jclass string_class = GetObjectClass(jni, j_string); |
| 227 CHECK_EXCEPTION(jni) << "Error during GetStringUTFLength"; | 228 const jmethodID get_bytes = |
| 228 jni->ReleaseStringUTFChars(j_string, chars); | 229 GetMethodID(jni, string_class, "getBytes", "(Ljava/lang/String;)[B"); |
| 229 CHECK_EXCEPTION(jni) << "Error during ReleaseStringUTFChars"; | 230 const jstring charset_name = jni->NewStringUTF("ISO-8859-1"); |
| 230 return str; | 231 CHECK_EXCEPTION(jni) << "error during NewStringUTF"; |
| 232 const jbyteArray j_byte_array = |
| 233 (jbyteArray)jni->CallObjectMethod(j_string, get_bytes, charset_name); |
| 234 CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; |
| 235 |
| 236 const size_t len = jni->GetArrayLength(j_byte_array); |
| 237 CHECK_EXCEPTION(jni) << "error during GetArrayLength"; |
| 238 std::vector<char> buf(len); |
| 239 jni->GetByteArrayRegion(j_byte_array, 0, len, |
| 240 reinterpret_cast<jbyte*>(&buf[0])); |
| 241 CHECK_EXCEPTION(jni) << "error during GetByteArrayRegion"; |
| 242 |
| 243 return std::string(buf.begin(), buf.end()); |
| 231 } | 244 } |
| 232 | 245 |
| 233 // Return the (singleton) Java Enum object corresponding to |index|; | 246 // Return the (singleton) Java Enum object corresponding to |index|; |
| 234 jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class, | 247 jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class, |
| 235 const std::string& state_class_name, int index) { | 248 const std::string& state_class_name, int index) { |
| 236 jmethodID state_values_id = GetStaticMethodID( | 249 jmethodID state_values_id = GetStaticMethodID( |
| 237 jni, state_class, "values", ("()[L" + state_class_name + ";").c_str()); | 250 jni, state_class, "values", ("()[L" + state_class_name + ";").c_str()); |
| 238 jobjectArray state_values = static_cast<jobjectArray>( | 251 jobjectArray state_values = static_cast<jobjectArray>( |
| 239 jni->CallStaticObjectMethod(state_class, state_values_id)); | 252 jni->CallStaticObjectMethod(state_class, state_values_id)); |
| 240 CHECK_EXCEPTION(jni) << "error during CallStaticObjectMethod"; | 253 CHECK_EXCEPTION(jni) << "error during CallStaticObjectMethod"; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 RTC_CHECK(!AtEnd()); | 354 RTC_CHECK(!AtEnd()); |
| 342 return value_; | 355 return value_; |
| 343 } | 356 } |
| 344 | 357 |
| 345 bool Iterable::Iterator::AtEnd() const { | 358 bool Iterable::Iterator::AtEnd() const { |
| 346 RTC_CHECK(thread_checker_.CalledOnValidThread()); | 359 RTC_CHECK(thread_checker_.CalledOnValidThread()); |
| 347 return jni_ == nullptr || IsNull(jni_, iterator_); | 360 return jni_ == nullptr || IsNull(jni_, iterator_); |
| 348 } | 361 } |
| 349 | 362 |
| 350 } // namespace webrtc_jni | 363 } // namespace webrtc_jni |
| OLD | NEW |