helpers_android.h (2539B)
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 11 #ifndef MODULES_UTILITY_INCLUDE_HELPERS_ANDROID_H_ 12 #define MODULES_UTILITY_INCLUDE_HELPERS_ANDROID_H_ 13 14 #include <jni.h> 15 16 // Abort the process if `jni` has a Java exception pending. 17 // TODO(henrika): merge with CHECK_JNI_EXCEPTION() in jni_helpers.h. 18 #define CHECK_EXCEPTION(jni) \ 19 RTC_CHECK(!jni->ExceptionCheck()) \ 20 << (jni->ExceptionDescribe(), jni->ExceptionClear(), "") 21 22 #if defined(WEBRTC_ARCH_X86) 23 // Dalvik JIT generated code doesn't guarantee 16-byte stack alignment on 24 // x86 - use force_align_arg_pointer to realign the stack at the JNI 25 // boundary. bugs.webrtc.org/9050 26 #define JNI_FUNCTION_ALIGN __attribute__((force_align_arg_pointer)) 27 #else 28 #define JNI_FUNCTION_ALIGN 29 #endif 30 31 namespace webrtc { 32 33 // Return a |JNIEnv*| usable on this thread or NULL if this thread is detached. 34 JNIEnv* GetEnv(JavaVM* jvm); 35 36 // Return a `jlong` that will correctly convert back to `ptr`. This is needed 37 // because the alternative (of silently passing a 32-bit pointer to a vararg 38 // function expecting a 64-bit param) picks up garbage in the high 32 bits. 39 jlong PointerTojlong(void* ptr); 40 41 // JNIEnv-helper methods that wraps the API which uses the JNI interface 42 // pointer (JNIEnv*). It allows us to RTC_CHECK success and that no Java 43 // exception is thrown while calling the method. 44 jmethodID GetMethodID(JNIEnv* jni, 45 jclass c, 46 const char* name, 47 const char* signature); 48 49 jmethodID GetStaticMethodID(JNIEnv* jni, 50 jclass c, 51 const char* name, 52 const char* signature); 53 54 jclass FindClass(JNIEnv* jni, const char* name); 55 56 jobject NewGlobalRef(JNIEnv* jni, jobject o); 57 58 void DeleteGlobalRef(JNIEnv* jni, jobject o); 59 60 // Attach thread to JVM if necessary and detach at scope end if originally 61 // attached. 62 class AttachThreadScoped { 63 public: 64 explicit AttachThreadScoped(JavaVM* jvm); 65 ~AttachThreadScoped(); 66 JNIEnv* env(); 67 68 private: 69 bool attached_; 70 JavaVM* jvm_; 71 JNIEnv* env_; 72 }; 73 74 } // namespace webrtc 75 76 #endif // MODULES_UTILITY_INCLUDE_HELPERS_ANDROID_H_