blob: 2832df14b3be71cb0ec7cd6d363a4ce6d417bd4f [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
deadbeef60631772016-04-04 10:21:02 -070067// Throws an exception if the object field is null.
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000068jobject GetObjectField(JNIEnv* jni, jobject object, jfieldID id);
69
deadbeef60631772016-04-04 10:21:02 -070070jobject GetNullableObjectField(JNIEnv* jni, jobject object, jfieldID id);
71
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000072jstring GetStringField(JNIEnv* jni, jobject object, jfieldID id);
73
74jlong GetLongField(JNIEnv* jni, jobject object, jfieldID id);
75
76jint GetIntField(JNIEnv* jni, jobject object, jfieldID id);
77
78bool GetBooleanField(JNIEnv* jni, jobject object, jfieldID id);
79
Magnus Jedvertffdd41e2016-03-01 10:10:01 +010080// Returns true if |obj| == null in Java.
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000081bool IsNull(JNIEnv* jni, jobject obj);
82
83// Given a UTF-8 encoded |native| string return a new (UTF-16) jstring.
84jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native);
85
86// Given a (UTF-16) jstring return a new UTF-8 native string.
87std::string JavaToStdString(JNIEnv* jni, const jstring& j_string);
88
89// Return the (singleton) Java Enum object corresponding to |index|;
90jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class,
91 const std::string& state_class_name, int index);
92
honghaizcec0a082016-01-15 14:49:09 -080093// Returns the name of a Java enum.
94std::string GetJavaEnumName(JNIEnv* jni,
95 const std::string& className,
96 jobject j_enum);
97
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000098jobject NewGlobalRef(JNIEnv* jni, jobject o);
99
100void DeleteGlobalRef(JNIEnv* jni, jobject o);
101
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000102// Scope Java local references to the lifetime of this object. Use in all C++
103// callbacks (i.e. entry points that don't originate in a Java callstack
104// through a "native" method call).
105class ScopedLocalRefFrame {
106 public:
107 explicit ScopedLocalRefFrame(JNIEnv* jni);
108 ~ScopedLocalRefFrame();
109
110 private:
111 JNIEnv* jni_;
112};
113
114// Scoped holder for global Java refs.
115template<class T> // T is jclass, jobject, jintArray, etc.
116class ScopedGlobalRef {
117 public:
118 ScopedGlobalRef(JNIEnv* jni, T obj)
119 : obj_(static_cast<T>(jni->NewGlobalRef(obj))) {}
120 ~ScopedGlobalRef() {
121 DeleteGlobalRef(AttachCurrentThreadIfNeeded(), obj_);
122 }
123 T operator*() const {
124 return obj_;
125 }
126 private:
127 T obj_;
128};
129
skvlad303b3c22016-03-24 19:36:46 -0700130// Provides a convenient way to iterate over a Java Iterable using the
131// C++ range-for loop.
132// E.g. for (jobject value : Iterable(jni, j_iterable)) { ... }
133// Note: Since Java iterators cannot be duplicated, the iterator class is not
134// copyable to prevent creating multiple C++ iterators that refer to the same
135// Java iterator.
136class Iterable {
137 public:
138 Iterable(JNIEnv* jni, jobject iterable) : jni_(jni), iterable_(iterable) {}
139
140 class Iterator {
141 public:
142 // Creates an iterator representing the end of any collection.
143 Iterator();
144 // Creates an iterator pointing to the beginning of the specified
145 // collection.
146 Iterator(JNIEnv* jni, jobject iterable);
147
148 // Move constructor - necessary to be able to return iterator types from
149 // functions.
150 Iterator(Iterator&& other);
151
152 // Move assignment should not be used.
153 Iterator& operator=(Iterator&&) = delete;
154
155 // Advances the iterator one step.
156 Iterator& operator++();
157
158 // Provides a way to compare the iterator with itself and with the end
159 // iterator.
160 // Note: all other comparison results are undefined, just like for C++ input
161 // iterators.
162 bool operator==(const Iterator& other);
163 bool operator!=(const Iterator& other) { return !(*this == other); }
164 jobject operator*();
165
166 private:
167 bool AtEnd() const;
168
169 JNIEnv* jni_ = nullptr;
170 jobject iterator_ = nullptr;
171 jobject value_ = nullptr;
172 jmethodID has_next_id_ = nullptr;
173 jmethodID next_id_ = nullptr;
174 rtc::ThreadChecker thread_checker_;
175
176 RTC_DISALLOW_COPY_AND_ASSIGN(Iterator);
177 };
178
179 Iterable::Iterator begin() { return Iterable::Iterator(jni_, iterable_); }
180 Iterable::Iterator end() { return Iterable::Iterator(); }
181
182 private:
183 JNIEnv* jni_;
184 jobject iterable_;
185
186 RTC_DISALLOW_COPY_AND_ASSIGN(Iterable);
187};
188
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000189} // namespace webrtc_jni
190
Henrik Kjellander15583c12016-02-10 10:53:12 +0100191#endif // WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_