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