blob: 05a956508c44a3545adc1538a64d440f79c743cf [file] [log] [blame]
perkj@webrtc.org96e4db92015-02-13 12:46:51 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
perkj@webrtc.org96e4db92015-02-13 12:46:51 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
perkj@webrtc.org96e4db92015-02-13 12:46:51 +00009 */
10
11// This file contain convenience functions and classes for JNI.
12// Before using any of the methods, InitGlobalJniVariables must be called.
13
Henrik Kjellander15583c12016-02-10 10:53:12 +010014#ifndef WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_
15#define WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000016
17#include <jni.h>
18#include <string>
19
20#include "webrtc/base/checks.h"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000021
22// Abort the process if |jni| has a Java exception pending.
23// This macros uses the comma operator to execute ExceptionDescribe
24// and ExceptionClear ignoring their return values and sending ""
25// to the error stream.
henrikg91d6ede2015-09-17 00:24:34 -070026#define CHECK_EXCEPTION(jni) \
27 RTC_CHECK(!jni->ExceptionCheck()) \
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000028 << (jni->ExceptionDescribe(), jni->ExceptionClear(), "")
29
30// Helper that calls ptr->Release() and aborts the process with a useful
31// message if that didn't actually delete *ptr because of extra refcounts.
32#define CHECK_RELEASE(ptr) \
henrikg91d6ede2015-09-17 00:24:34 -070033 RTC_CHECK_EQ(0, (ptr)->Release()) << "Unexpected refcount."
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000034
35namespace webrtc_jni {
36
37jint InitGlobalJniVariables(JavaVM *jvm);
38
39// Return a |JNIEnv*| usable on this thread or NULL if this thread is detached.
40JNIEnv* GetEnv();
41
42JavaVM *GetJVM();
43
44// Return a |JNIEnv*| usable on this thread. Attaches to |g_jvm| if necessary.
45JNIEnv* AttachCurrentThreadIfNeeded();
46
47// Return a |jlong| that will correctly convert back to |ptr|. This is needed
48// because the alternative (of silently passing a 32-bit pointer to a vararg
49// function expecting a 64-bit param) picks up garbage in the high 32 bits.
50jlong jlongFromPointer(void* ptr);
51
henrikg91d6ede2015-09-17 00:24:34 -070052// JNIEnv-helper methods that RTC_CHECK success: no Java exception thrown and
53// found object/class/method/field is non-null.
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000054jmethodID GetMethodID(
55 JNIEnv* jni, jclass c, const std::string& name, const char* signature);
56
57jmethodID GetStaticMethodID(
58 JNIEnv* jni, jclass c, const char* name, const char* signature);
59
60jfieldID GetFieldID(JNIEnv* jni, jclass c, const char* name,
61 const char* signature);
62
63jclass GetObjectClass(JNIEnv* jni, jobject object);
64
65jobject GetObjectField(JNIEnv* jni, jobject object, jfieldID id);
66
67jstring GetStringField(JNIEnv* jni, jobject object, jfieldID id);
68
69jlong GetLongField(JNIEnv* jni, jobject object, jfieldID id);
70
71jint GetIntField(JNIEnv* jni, jobject object, jfieldID id);
72
73bool GetBooleanField(JNIEnv* jni, jobject object, jfieldID id);
74
75// Java references to "null" can only be distinguished as such in C++ by
76// creating a local reference, so this helper wraps that logic.
77bool IsNull(JNIEnv* jni, jobject obj);
78
79// Given a UTF-8 encoded |native| string return a new (UTF-16) jstring.
80jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native);
81
82// Given a (UTF-16) jstring return a new UTF-8 native string.
83std::string JavaToStdString(JNIEnv* jni, const jstring& j_string);
84
85// Return the (singleton) Java Enum object corresponding to |index|;
86jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class,
87 const std::string& state_class_name, int index);
88
honghaizcec0a082016-01-15 14:49:09 -080089// Returns the name of a Java enum.
90std::string GetJavaEnumName(JNIEnv* jni,
91 const std::string& className,
92 jobject j_enum);
93
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000094jobject NewGlobalRef(JNIEnv* jni, jobject o);
95
96void DeleteGlobalRef(JNIEnv* jni, jobject o);
97
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000098// Scope Java local references to the lifetime of this object. Use in all C++
99// callbacks (i.e. entry points that don't originate in a Java callstack
100// through a "native" method call).
101class ScopedLocalRefFrame {
102 public:
103 explicit ScopedLocalRefFrame(JNIEnv* jni);
104 ~ScopedLocalRefFrame();
105
106 private:
107 JNIEnv* jni_;
108};
109
110// Scoped holder for global Java refs.
111template<class T> // T is jclass, jobject, jintArray, etc.
112class ScopedGlobalRef {
113 public:
114 ScopedGlobalRef(JNIEnv* jni, T obj)
115 : obj_(static_cast<T>(jni->NewGlobalRef(obj))) {}
116 ~ScopedGlobalRef() {
117 DeleteGlobalRef(AttachCurrentThreadIfNeeded(), obj_);
118 }
119 T operator*() const {
120 return obj_;
121 }
122 private:
123 T obj_;
124};
125
126} // namespace webrtc_jni
127
Henrik Kjellander15583c12016-02-10 10:53:12 +0100128#endif // WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_