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