blob: 98dcc38b5348b4ad8157cb6752711ed0e438388a [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
skvlad303b3c22016-03-24 19:36:46 -070020#include "webrtc/base/constructormagic.h"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000021#include "webrtc/base/checks.h"
skvlad303b3c22016-03-24 19:36:46 -070022#include "webrtc/base/thread_checker.h"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000023
24// Abort the process if |jni| has a Java exception pending.
25// This macros uses the comma operator to execute ExceptionDescribe
26// and ExceptionClear ignoring their return values and sending ""
27// to the error stream.
henrikg91d6ede2015-09-17 00:24:34 -070028#define CHECK_EXCEPTION(jni) \
29 RTC_CHECK(!jni->ExceptionCheck()) \
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000030 << (jni->ExceptionDescribe(), jni->ExceptionClear(), "")
31
32// Helper that calls ptr->Release() and aborts the process with a useful
33// message if that didn't actually delete *ptr because of extra refcounts.
34#define CHECK_RELEASE(ptr) \
henrikg91d6ede2015-09-17 00:24:34 -070035 RTC_CHECK_EQ(0, (ptr)->Release()) << "Unexpected refcount."
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000036
37namespace webrtc_jni {
38
39jint InitGlobalJniVariables(JavaVM *jvm);
40
41// Return a |JNIEnv*| usable on this thread or NULL if this thread is detached.
42JNIEnv* GetEnv();
43
44JavaVM *GetJVM();
45
46// Return a |JNIEnv*| usable on this thread. Attaches to |g_jvm| if necessary.
47JNIEnv* AttachCurrentThreadIfNeeded();
48
49// Return a |jlong| that will correctly convert back to |ptr|. This is needed
50// because the alternative (of silently passing a 32-bit pointer to a vararg
51// function expecting a 64-bit param) picks up garbage in the high 32 bits.
52jlong jlongFromPointer(void* ptr);
53
henrikg91d6ede2015-09-17 00:24:34 -070054// JNIEnv-helper methods that RTC_CHECK success: no Java exception thrown and
55// found object/class/method/field is non-null.
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000056jmethodID GetMethodID(
57 JNIEnv* jni, jclass c, const std::string& name, const char* signature);
58
59jmethodID GetStaticMethodID(
60 JNIEnv* jni, jclass c, const char* name, const char* signature);
61
62jfieldID GetFieldID(JNIEnv* jni, jclass c, const char* name,
63 const char* signature);
64
65jclass GetObjectClass(JNIEnv* jni, jobject object);
66
67jobject GetObjectField(JNIEnv* jni, jobject object, jfieldID id);
68
69jstring GetStringField(JNIEnv* jni, jobject object, jfieldID id);
70
71jlong GetLongField(JNIEnv* jni, jobject object, jfieldID id);
72
73jint GetIntField(JNIEnv* jni, jobject object, jfieldID id);
74
75bool GetBooleanField(JNIEnv* jni, jobject object, jfieldID id);
76
Magnus Jedvertffdd41e2016-03-01 10:10:01 +010077// Returns true if |obj| == null in Java.
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000078bool IsNull(JNIEnv* jni, jobject obj);
79
80// Given a UTF-8 encoded |native| string return a new (UTF-16) jstring.
81jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native);
82
83// Given a (UTF-16) jstring return a new UTF-8 native string.
84std::string JavaToStdString(JNIEnv* jni, const jstring& j_string);
85
86// Return the (singleton) Java Enum object corresponding to |index|;
87jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class,
88 const std::string& state_class_name, int index);
89
honghaizcec0a082016-01-15 14:49:09 -080090// Returns the name of a Java enum.
91std::string GetJavaEnumName(JNIEnv* jni,
92 const std::string& className,
93 jobject j_enum);
94
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000095jobject NewGlobalRef(JNIEnv* jni, jobject o);
96
97void DeleteGlobalRef(JNIEnv* jni, jobject o);
98
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000099// Scope Java local references to the lifetime of this object. Use in all C++
100// callbacks (i.e. entry points that don't originate in a Java callstack
101// through a "native" method call).
102class ScopedLocalRefFrame {
103 public:
104 explicit ScopedLocalRefFrame(JNIEnv* jni);
105 ~ScopedLocalRefFrame();
106
107 private:
108 JNIEnv* jni_;
109};
110
111// Scoped holder for global Java refs.
112template<class T> // T is jclass, jobject, jintArray, etc.
113class ScopedGlobalRef {
114 public:
115 ScopedGlobalRef(JNIEnv* jni, T obj)
116 : obj_(static_cast<T>(jni->NewGlobalRef(obj))) {}
117 ~ScopedGlobalRef() {
118 DeleteGlobalRef(AttachCurrentThreadIfNeeded(), obj_);
119 }
120 T operator*() const {
121 return obj_;
122 }
123 private:
124 T obj_;
125};
126
skvlad303b3c22016-03-24 19:36:46 -0700127// Provides a convenient way to iterate over a Java Iterable using the
128// C++ range-for loop.
129// E.g. for (jobject value : Iterable(jni, j_iterable)) { ... }
130// Note: Since Java iterators cannot be duplicated, the iterator class is not
131// copyable to prevent creating multiple C++ iterators that refer to the same
132// Java iterator.
133class Iterable {
134 public:
135 Iterable(JNIEnv* jni, jobject iterable) : jni_(jni), iterable_(iterable) {}
136
137 class Iterator {
138 public:
139 // Creates an iterator representing the end of any collection.
140 Iterator();
141 // Creates an iterator pointing to the beginning of the specified
142 // collection.
143 Iterator(JNIEnv* jni, jobject iterable);
144
145 // Move constructor - necessary to be able to return iterator types from
146 // functions.
147 Iterator(Iterator&& other);
148
149 // Move assignment should not be used.
150 Iterator& operator=(Iterator&&) = delete;
151
152 // Advances the iterator one step.
153 Iterator& operator++();
154
155 // Provides a way to compare the iterator with itself and with the end
156 // iterator.
157 // Note: all other comparison results are undefined, just like for C++ input
158 // iterators.
159 bool operator==(const Iterator& other);
160 bool operator!=(const Iterator& other) { return !(*this == other); }
161 jobject operator*();
162
163 private:
164 bool AtEnd() const;
165
166 JNIEnv* jni_ = nullptr;
167 jobject iterator_ = nullptr;
168 jobject value_ = nullptr;
169 jmethodID has_next_id_ = nullptr;
170 jmethodID next_id_ = nullptr;
171 rtc::ThreadChecker thread_checker_;
172
173 RTC_DISALLOW_COPY_AND_ASSIGN(Iterator);
174 };
175
176 Iterable::Iterator begin() { return Iterable::Iterator(jni_, iterable_); }
177 Iterable::Iterator end() { return Iterable::Iterator(); }
178
179 private:
180 JNIEnv* jni_;
181 jobject iterable_;
182
183 RTC_DISALLOW_COPY_AND_ASSIGN(Iterable);
184};
185
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000186} // namespace webrtc_jni
187
Henrik Kjellander15583c12016-02-10 10:53:12 +0100188#endif // WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_