perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2015 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | // This file contain convenience functions and classes for JNI. |
| 30 | // Before using any of the methods, InitGlobalJniVariables must be called. |
| 31 | |
| 32 | #ifndef TALK_APP_WEBRTC_JAVA_JNI_JNI_HELPERS_H_ |
| 33 | #define TALK_APP_WEBRTC_JAVA_JNI_JNI_HELPERS_H_ |
| 34 | |
| 35 | #include <jni.h> |
| 36 | #include <string> |
| 37 | |
| 38 | #include "webrtc/base/checks.h" |
| 39 | #include "third_party/icu/source/common/unicode/unistr.h" |
| 40 | |
| 41 | // Abort the process if |jni| has a Java exception pending. |
| 42 | // This macros uses the comma operator to execute ExceptionDescribe |
| 43 | // and ExceptionClear ignoring their return values and sending "" |
| 44 | // to the error stream. |
| 45 | #define CHECK_EXCEPTION(jni) \ |
| 46 | CHECK(!jni->ExceptionCheck()) \ |
| 47 | << (jni->ExceptionDescribe(), jni->ExceptionClear(), "") |
| 48 | |
| 49 | // Helper that calls ptr->Release() and aborts the process with a useful |
| 50 | // message if that didn't actually delete *ptr because of extra refcounts. |
| 51 | #define CHECK_RELEASE(ptr) \ |
| 52 | CHECK_EQ(0, (ptr)->Release()) << "Unexpected refcount." |
| 53 | |
| 54 | namespace webrtc_jni { |
| 55 | |
| 56 | jint InitGlobalJniVariables(JavaVM *jvm); |
| 57 | |
| 58 | // Return a |JNIEnv*| usable on this thread or NULL if this thread is detached. |
| 59 | JNIEnv* GetEnv(); |
| 60 | |
| 61 | JavaVM *GetJVM(); |
| 62 | |
| 63 | // Return a |JNIEnv*| usable on this thread. Attaches to |g_jvm| if necessary. |
| 64 | JNIEnv* AttachCurrentThreadIfNeeded(); |
| 65 | |
| 66 | // Return a |jlong| that will correctly convert back to |ptr|. This is needed |
| 67 | // because the alternative (of silently passing a 32-bit pointer to a vararg |
| 68 | // function expecting a 64-bit param) picks up garbage in the high 32 bits. |
| 69 | jlong jlongFromPointer(void* ptr); |
| 70 | |
| 71 | // JNIEnv-helper methods that CHECK success: no Java exception thrown and found |
| 72 | // object/class/method/field is non-null. |
| 73 | jmethodID GetMethodID( |
| 74 | JNIEnv* jni, jclass c, const std::string& name, const char* signature); |
| 75 | |
| 76 | jmethodID GetStaticMethodID( |
| 77 | JNIEnv* jni, jclass c, const char* name, const char* signature); |
| 78 | |
| 79 | jfieldID GetFieldID(JNIEnv* jni, jclass c, const char* name, |
| 80 | const char* signature); |
| 81 | |
| 82 | jclass GetObjectClass(JNIEnv* jni, jobject object); |
| 83 | |
| 84 | jobject GetObjectField(JNIEnv* jni, jobject object, jfieldID id); |
| 85 | |
| 86 | jstring GetStringField(JNIEnv* jni, jobject object, jfieldID id); |
| 87 | |
| 88 | jlong GetLongField(JNIEnv* jni, jobject object, jfieldID id); |
| 89 | |
| 90 | jint GetIntField(JNIEnv* jni, jobject object, jfieldID id); |
| 91 | |
| 92 | bool GetBooleanField(JNIEnv* jni, jobject object, jfieldID id); |
| 93 | |
| 94 | // Java references to "null" can only be distinguished as such in C++ by |
| 95 | // creating a local reference, so this helper wraps that logic. |
| 96 | bool IsNull(JNIEnv* jni, jobject obj); |
| 97 | |
| 98 | // Given a UTF-8 encoded |native| string return a new (UTF-16) jstring. |
| 99 | jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native); |
| 100 | |
| 101 | // Given a (UTF-16) jstring return a new UTF-8 native string. |
| 102 | std::string JavaToStdString(JNIEnv* jni, const jstring& j_string); |
| 103 | |
| 104 | // Return the (singleton) Java Enum object corresponding to |index|; |
| 105 | jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class, |
| 106 | const std::string& state_class_name, int index); |
| 107 | |
| 108 | jobject NewGlobalRef(JNIEnv* jni, jobject o); |
| 109 | |
| 110 | void DeleteGlobalRef(JNIEnv* jni, jobject o); |
| 111 | |
| 112 | // Given a jweak reference, allocate a (strong) local reference scoped to the |
| 113 | // lifetime of this object if the weak reference is still valid, or NULL |
| 114 | // otherwise. |
| 115 | class WeakRef { |
| 116 | public: |
| 117 | WeakRef(JNIEnv* jni, jweak ref); |
| 118 | ~WeakRef(); |
| 119 | jobject obj() { return obj_; } |
| 120 | |
| 121 | private: |
| 122 | JNIEnv* const jni_; |
| 123 | jobject const obj_; |
| 124 | }; |
| 125 | |
| 126 | // Scope Java local references to the lifetime of this object. Use in all C++ |
| 127 | // callbacks (i.e. entry points that don't originate in a Java callstack |
| 128 | // through a "native" method call). |
| 129 | class ScopedLocalRefFrame { |
| 130 | public: |
| 131 | explicit ScopedLocalRefFrame(JNIEnv* jni); |
| 132 | ~ScopedLocalRefFrame(); |
| 133 | |
| 134 | private: |
| 135 | JNIEnv* jni_; |
| 136 | }; |
| 137 | |
| 138 | // Scoped holder for global Java refs. |
| 139 | template<class T> // T is jclass, jobject, jintArray, etc. |
| 140 | class ScopedGlobalRef { |
| 141 | public: |
| 142 | ScopedGlobalRef(JNIEnv* jni, T obj) |
| 143 | : obj_(static_cast<T>(jni->NewGlobalRef(obj))) {} |
| 144 | ~ScopedGlobalRef() { |
| 145 | DeleteGlobalRef(AttachCurrentThreadIfNeeded(), obj_); |
| 146 | } |
| 147 | T operator*() const { |
| 148 | return obj_; |
| 149 | } |
| 150 | private: |
| 151 | T obj_; |
| 152 | }; |
| 153 | |
| 154 | } // namespace webrtc_jni |
| 155 | |
| 156 | #endif // TALK_APP_WEBRTC_JAVA_JNI_JNI_HELPERS_H_ |