perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | // This file contain convenience functions and classes for JNI. |
| 12 | // Before using any of the methods, InitGlobalJniVariables must be called. |
| 13 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 14 | #ifndef WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ |
| 15 | #define WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 16 | |
| 17 | #include <jni.h> |
| 18 | #include <string> |
| 19 | |
skvlad | 303b3c2 | 2016-03-24 19:36:46 -0700 | [diff] [blame] | 20 | #include "webrtc/base/constructormagic.h" |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 21 | #include "webrtc/base/checks.h" |
skvlad | 303b3c2 | 2016-03-24 19:36:46 -0700 | [diff] [blame] | 22 | #include "webrtc/base/thread_checker.h" |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 23 | |
| 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. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 28 | #define CHECK_EXCEPTION(jni) \ |
| 29 | RTC_CHECK(!jni->ExceptionCheck()) \ |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 30 | << (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) \ |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 35 | RTC_CHECK_EQ(0, (ptr)->Release()) << "Unexpected refcount." |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 36 | |
| 37 | namespace webrtc_jni { |
| 38 | |
| 39 | jint InitGlobalJniVariables(JavaVM *jvm); |
| 40 | |
| 41 | // Return a |JNIEnv*| usable on this thread or NULL if this thread is detached. |
| 42 | JNIEnv* GetEnv(); |
| 43 | |
| 44 | JavaVM *GetJVM(); |
| 45 | |
| 46 | // Return a |JNIEnv*| usable on this thread. Attaches to |g_jvm| if necessary. |
| 47 | JNIEnv* 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. |
| 52 | jlong jlongFromPointer(void* ptr); |
| 53 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 54 | // JNIEnv-helper methods that RTC_CHECK success: no Java exception thrown and |
| 55 | // found object/class/method/field is non-null. |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 56 | jmethodID GetMethodID( |
| 57 | JNIEnv* jni, jclass c, const std::string& name, const char* signature); |
| 58 | |
| 59 | jmethodID GetStaticMethodID( |
| 60 | JNIEnv* jni, jclass c, const char* name, const char* signature); |
| 61 | |
| 62 | jfieldID GetFieldID(JNIEnv* jni, jclass c, const char* name, |
| 63 | const char* signature); |
| 64 | |
| 65 | jclass GetObjectClass(JNIEnv* jni, jobject object); |
| 66 | |
deadbeef | 6063177 | 2016-04-04 10:21:02 -0700 | [diff] [blame^] | 67 | // Throws an exception if the object field is null. |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 68 | jobject GetObjectField(JNIEnv* jni, jobject object, jfieldID id); |
| 69 | |
deadbeef | 6063177 | 2016-04-04 10:21:02 -0700 | [diff] [blame^] | 70 | jobject GetNullableObjectField(JNIEnv* jni, jobject object, jfieldID id); |
| 71 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 72 | jstring GetStringField(JNIEnv* jni, jobject object, jfieldID id); |
| 73 | |
| 74 | jlong GetLongField(JNIEnv* jni, jobject object, jfieldID id); |
| 75 | |
| 76 | jint GetIntField(JNIEnv* jni, jobject object, jfieldID id); |
| 77 | |
| 78 | bool GetBooleanField(JNIEnv* jni, jobject object, jfieldID id); |
| 79 | |
Magnus Jedvert | ffdd41e | 2016-03-01 10:10:01 +0100 | [diff] [blame] | 80 | // Returns true if |obj| == null in Java. |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 81 | bool IsNull(JNIEnv* jni, jobject obj); |
| 82 | |
| 83 | // Given a UTF-8 encoded |native| string return a new (UTF-16) jstring. |
| 84 | jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native); |
| 85 | |
| 86 | // Given a (UTF-16) jstring return a new UTF-8 native string. |
| 87 | std::string JavaToStdString(JNIEnv* jni, const jstring& j_string); |
| 88 | |
| 89 | // Return the (singleton) Java Enum object corresponding to |index|; |
| 90 | jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class, |
| 91 | const std::string& state_class_name, int index); |
| 92 | |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 93 | // Returns the name of a Java enum. |
| 94 | std::string GetJavaEnumName(JNIEnv* jni, |
| 95 | const std::string& className, |
| 96 | jobject j_enum); |
| 97 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 98 | jobject NewGlobalRef(JNIEnv* jni, jobject o); |
| 99 | |
| 100 | void DeleteGlobalRef(JNIEnv* jni, jobject o); |
| 101 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 102 | // 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). |
| 105 | class 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. |
| 115 | template<class T> // T is jclass, jobject, jintArray, etc. |
| 116 | class 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 | |
skvlad | 303b3c2 | 2016-03-24 19:36:46 -0700 | [diff] [blame] | 130 | // 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. |
| 136 | class 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.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 189 | } // namespace webrtc_jni |
| 190 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 191 | #endif // WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ |