blob: dde713728bdf665753e6e360de6f221e12c57635 [file] [log] [blame]
perkj@webrtc.org96e4db92015-02-13 12:46:51 +00001/*
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"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000039
40// Abort the process if |jni| has a Java exception pending.
41// This macros uses the comma operator to execute ExceptionDescribe
42// and ExceptionClear ignoring their return values and sending ""
43// to the error stream.
44#define CHECK_EXCEPTION(jni) \
45 CHECK(!jni->ExceptionCheck()) \
46 << (jni->ExceptionDescribe(), jni->ExceptionClear(), "")
47
48// Helper that calls ptr->Release() and aborts the process with a useful
49// message if that didn't actually delete *ptr because of extra refcounts.
50#define CHECK_RELEASE(ptr) \
51 CHECK_EQ(0, (ptr)->Release()) << "Unexpected refcount."
52
53namespace webrtc_jni {
54
55jint InitGlobalJniVariables(JavaVM *jvm);
56
57// Return a |JNIEnv*| usable on this thread or NULL if this thread is detached.
58JNIEnv* GetEnv();
59
60JavaVM *GetJVM();
61
62// Return a |JNIEnv*| usable on this thread. Attaches to |g_jvm| if necessary.
63JNIEnv* AttachCurrentThreadIfNeeded();
64
65// Return a |jlong| that will correctly convert back to |ptr|. This is needed
66// because the alternative (of silently passing a 32-bit pointer to a vararg
67// function expecting a 64-bit param) picks up garbage in the high 32 bits.
68jlong jlongFromPointer(void* ptr);
69
70// JNIEnv-helper methods that CHECK success: no Java exception thrown and found
71// object/class/method/field is non-null.
72jmethodID GetMethodID(
73 JNIEnv* jni, jclass c, const std::string& name, const char* signature);
74
75jmethodID GetStaticMethodID(
76 JNIEnv* jni, jclass c, const char* name, const char* signature);
77
78jfieldID GetFieldID(JNIEnv* jni, jclass c, const char* name,
79 const char* signature);
80
81jclass GetObjectClass(JNIEnv* jni, jobject object);
82
83jobject GetObjectField(JNIEnv* jni, jobject object, jfieldID id);
84
85jstring GetStringField(JNIEnv* jni, jobject object, jfieldID id);
86
87jlong GetLongField(JNIEnv* jni, jobject object, jfieldID id);
88
89jint GetIntField(JNIEnv* jni, jobject object, jfieldID id);
90
91bool GetBooleanField(JNIEnv* jni, jobject object, jfieldID id);
92
93// Java references to "null" can only be distinguished as such in C++ by
94// creating a local reference, so this helper wraps that logic.
95bool IsNull(JNIEnv* jni, jobject obj);
96
97// Given a UTF-8 encoded |native| string return a new (UTF-16) jstring.
98jstring JavaStringFromStdString(JNIEnv* jni, const std::string& native);
99
100// Given a (UTF-16) jstring return a new UTF-8 native string.
101std::string JavaToStdString(JNIEnv* jni, const jstring& j_string);
102
103// Return the (singleton) Java Enum object corresponding to |index|;
104jobject JavaEnumFromIndex(JNIEnv* jni, jclass state_class,
105 const std::string& state_class_name, int index);
106
107jobject NewGlobalRef(JNIEnv* jni, jobject o);
108
109void DeleteGlobalRef(JNIEnv* jni, jobject o);
110
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000111// Scope Java local references to the lifetime of this object. Use in all C++
112// callbacks (i.e. entry points that don't originate in a Java callstack
113// through a "native" method call).
114class ScopedLocalRefFrame {
115 public:
116 explicit ScopedLocalRefFrame(JNIEnv* jni);
117 ~ScopedLocalRefFrame();
118
119 private:
120 JNIEnv* jni_;
121};
122
123// Scoped holder for global Java refs.
124template<class T> // T is jclass, jobject, jintArray, etc.
125class ScopedGlobalRef {
126 public:
127 ScopedGlobalRef(JNIEnv* jni, T obj)
128 : obj_(static_cast<T>(jni->NewGlobalRef(obj))) {}
129 ~ScopedGlobalRef() {
130 DeleteGlobalRef(AttachCurrentThreadIfNeeded(), obj_);
131 }
132 T operator*() const {
133 return obj_;
134 }
135 private:
136 T obj_;
137};
138
139} // namespace webrtc_jni
140
141#endif // TALK_APP_WEBRTC_JAVA_JNI_JNI_HELPERS_H_