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 | |
| 20 | #include "webrtc/base/checks.h" |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 21 | |
| 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. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 26 | #define CHECK_EXCEPTION(jni) \ |
| 27 | RTC_CHECK(!jni->ExceptionCheck()) \ |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 28 | << (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) \ |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 33 | RTC_CHECK_EQ(0, (ptr)->Release()) << "Unexpected refcount." |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 34 | |
| 35 | namespace webrtc_jni { |
| 36 | |
| 37 | jint InitGlobalJniVariables(JavaVM *jvm); |
| 38 | |
| 39 | // Return a |JNIEnv*| usable on this thread or NULL if this thread is detached. |
| 40 | JNIEnv* GetEnv(); |
| 41 | |
| 42 | JavaVM *GetJVM(); |
| 43 | |
| 44 | // Return a |JNIEnv*| usable on this thread. Attaches to |g_jvm| if necessary. |
| 45 | JNIEnv* 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. |
| 50 | jlong jlongFromPointer(void* ptr); |
| 51 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 52 | // JNIEnv-helper methods that RTC_CHECK success: no Java exception thrown and |
| 53 | // found object/class/method/field is non-null. |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 54 | jmethodID GetMethodID( |
| 55 | JNIEnv* jni, jclass c, const std::string& name, const char* signature); |
| 56 | |
| 57 | jmethodID GetStaticMethodID( |
| 58 | JNIEnv* jni, jclass c, const char* name, const char* signature); |
| 59 | |
| 60 | jfieldID GetFieldID(JNIEnv* jni, jclass c, const char* name, |
| 61 | const char* signature); |
| 62 | |
| 63 | jclass GetObjectClass(JNIEnv* jni, jobject object); |
| 64 | |
| 65 | jobject GetObjectField(JNIEnv* jni, jobject object, jfieldID id); |
| 66 | |
| 67 | jstring GetStringField(JNIEnv* jni, jobject object, jfieldID id); |
| 68 | |
| 69 | jlong GetLongField(JNIEnv* jni, jobject object, jfieldID id); |
| 70 | |
| 71 | jint GetIntField(JNIEnv* jni, jobject object, jfieldID id); |
| 72 | |
| 73 | bool 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. |
| 77 | bool IsNull(JNIEnv* jni, jobject obj); |
| 78 | |
| 79 | // Given a UTF-8 encoded |native| string return a new (UTF-16) jstring. |
| 80 | jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native); |
| 81 | |
| 82 | // Given a (UTF-16) jstring return a new UTF-8 native string. |
| 83 | std::string JavaToStdString(JNIEnv* jni, const jstring& j_string); |
| 84 | |
| 85 | // Return the (singleton) Java Enum object corresponding to |index|; |
| 86 | jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class, |
| 87 | const std::string& state_class_name, int index); |
| 88 | |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 89 | // Returns the name of a Java enum. |
| 90 | std::string GetJavaEnumName(JNIEnv* jni, |
| 91 | const std::string& className, |
| 92 | jobject j_enum); |
| 93 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 94 | jobject NewGlobalRef(JNIEnv* jni, jobject o); |
| 95 | |
| 96 | void DeleteGlobalRef(JNIEnv* jni, jobject o); |
| 97 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 98 | // 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). |
| 101 | class 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. |
| 111 | template<class T> // T is jclass, jobject, jintArray, etc. |
| 112 | class 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 Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 128 | #endif // WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ |