Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_UTILITY_INCLUDE_HELPERS_ANDROID_H_ |
| 12 | #define MODULES_UTILITY_INCLUDE_HELPERS_ANDROID_H_ |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 13 | |
| 14 | #include <jni.h> |
| 15 | #include <string> |
| 16 | |
Niels Möller | a12c42a | 2018-07-25 16:05:48 +0200 | [diff] [blame^] | 17 | #include "rtc_base/system/arch.h" |
| 18 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 19 | // Abort the process if |jni| has a Java exception pending. |
| 20 | // TODO(henrika): merge with CHECK_JNI_EXCEPTION() in jni_helpers.h. |
| 21 | #define CHECK_EXCEPTION(jni) \ |
| 22 | RTC_CHECK(!jni->ExceptionCheck()) \ |
| 23 | << (jni->ExceptionDescribe(), jni->ExceptionClear(), "") |
| 24 | |
Yura Yaroshevich | 278d03a | 2018-03-23 11:47:19 +0300 | [diff] [blame] | 25 | #if defined(WEBRTC_ARCH_X86) |
| 26 | // Dalvik JIT generated code doesn't guarantee 16-byte stack alignment on |
| 27 | // x86 - use force_align_arg_pointer to realign the stack at the JNI |
| 28 | // boundary. bugs.webrtc.org/9050 |
| 29 | #define JNI_FUNCTION_ALIGN __attribute__((force_align_arg_pointer)) |
| 30 | #else |
| 31 | #define JNI_FUNCTION_ALIGN |
| 32 | #endif |
| 33 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 34 | namespace webrtc { |
| 35 | |
| 36 | // Return a |JNIEnv*| usable on this thread or NULL if this thread is detached. |
| 37 | JNIEnv* GetEnv(JavaVM* jvm); |
| 38 | |
| 39 | // Return a |jlong| that will correctly convert back to |ptr|. This is needed |
| 40 | // because the alternative (of silently passing a 32-bit pointer to a vararg |
| 41 | // function expecting a 64-bit param) picks up garbage in the high 32 bits. |
| 42 | jlong PointerTojlong(void* ptr); |
| 43 | |
| 44 | // JNIEnv-helper methods that wraps the API which uses the JNI interface |
| 45 | // pointer (JNIEnv*). It allows us to RTC_CHECK success and that no Java |
| 46 | // exception is thrown while calling the method. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 47 | jmethodID GetMethodID(JNIEnv* jni, |
| 48 | jclass c, |
| 49 | const char* name, |
| 50 | const char* signature); |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 51 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | jmethodID GetStaticMethodID(JNIEnv* jni, |
| 53 | jclass c, |
| 54 | const char* name, |
| 55 | const char* signature); |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 56 | |
| 57 | jclass FindClass(JNIEnv* jni, const char* name); |
| 58 | |
| 59 | jobject NewGlobalRef(JNIEnv* jni, jobject o); |
| 60 | |
| 61 | void DeleteGlobalRef(JNIEnv* jni, jobject o); |
| 62 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 63 | // Attach thread to JVM if necessary and detach at scope end if originally |
| 64 | // attached. |
| 65 | class AttachThreadScoped { |
| 66 | public: |
| 67 | explicit AttachThreadScoped(JavaVM* jvm); |
| 68 | ~AttachThreadScoped(); |
| 69 | JNIEnv* env(); |
| 70 | |
| 71 | private: |
| 72 | bool attached_; |
| 73 | JavaVM* jvm_; |
| 74 | JNIEnv* env_; |
| 75 | }; |
| 76 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 77 | } // namespace webrtc |
| 78 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 79 | #endif // MODULES_UTILITY_INCLUDE_HELPERS_ANDROID_H_ |