blob: f899263f76adb42a85e7baa4096035a8d2928695 [file] [log] [blame]
Henrik Kjellanderff761fb2015-11-04 08:31:52 +01001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
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.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_
12#define MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010013
14#include <jni.h>
kwiberg84be5112016-04-27 01:19:58 -070015
16#include <memory>
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010017#include <string>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/utility/include/helpers_android.h"
20#include "rtc_base/thread_checker.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010021
22namespace webrtc {
23
24// The JNI interface pointer (JNIEnv) is valid only in the current thread.
25// Should another thread need to access the Java VM, it must first call
26// AttachCurrentThread() to attach itself to the VM and obtain a JNI interface
27// pointer. The native thread remains attached to the VM until it calls
28// DetachCurrentThread() to detach.
29class AttachCurrentThreadIfNeeded {
30 public:
31 AttachCurrentThreadIfNeeded();
32 ~AttachCurrentThreadIfNeeded();
33
34 private:
35 rtc::ThreadChecker thread_checker_;
36 bool attached_;
37};
38
39// This class is created by the NativeRegistration class and is used to wrap
40// the actual Java object handle (jobject) on which we can call methods from
41// C++ in to Java. See example in JVM for more details.
42// TODO(henrika): extend support for type of function calls.
43class GlobalRef {
44 public:
45 GlobalRef(JNIEnv* jni, jobject object);
46 ~GlobalRef();
47
48 jboolean CallBooleanMethod(jmethodID methodID, ...);
49 jint CallIntMethod(jmethodID methodID, ...);
50 void CallVoidMethod(jmethodID methodID, ...);
51
52 private:
53 JNIEnv* const jni_;
54 const jobject j_object_;
55};
56
57// Wraps the jclass object on which we can call GetMethodId() functions to
58// query method IDs.
59class JavaClass {
60 public:
61 JavaClass(JNIEnv* jni, jclass clazz) : jni_(jni), j_class_(clazz) {}
62 ~JavaClass() {}
63
64 jmethodID GetMethodId(const char* name, const char* signature);
65 jmethodID GetStaticMethodId(const char* name, const char* signature);
66 jobject CallStaticObjectMethod(jmethodID methodID, ...);
henrika918b5542016-09-19 15:44:09 +020067 jint CallStaticIntMethod(jmethodID methodID, ...);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010068
69 protected:
70 JNIEnv* const jni_;
71 jclass const j_class_;
72};
73
74// Adds support of the NewObject factory method to the JavaClass class.
75// See example in JVM for more details on how to use it.
76class NativeRegistration : public JavaClass {
77 public:
78 NativeRegistration(JNIEnv* jni, jclass clazz);
79 ~NativeRegistration();
80
kwiberg84be5112016-04-27 01:19:58 -070081 std::unique_ptr<GlobalRef> NewObject(
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010082 const char* name, const char* signature, ...);
83
84 private:
85 JNIEnv* const jni_;
86};
87
88// This class is created by the JVM class and is used to expose methods that
89// needs the JNI interface pointer but its main purpose is to create a
90// NativeRegistration object given name of a Java class and a list of native
91// methods. See example in JVM for more details.
92class JNIEnvironment {
93 public:
94 explicit JNIEnvironment(JNIEnv* jni);
95 ~JNIEnvironment();
96
97 // Registers native methods with the Java class specified by |name|.
98 // Note that the class name must be one of the names in the static
99 // |loaded_classes| array defined in jvm_android.cc.
100 // This method must be called on the construction thread.
kwiberg84be5112016-04-27 01:19:58 -0700101 std::unique_ptr<NativeRegistration> RegisterNatives(
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100102 const char* name, const JNINativeMethod *methods, int num_methods);
103
104 // Converts from Java string to std::string.
105 // This method must be called on the construction thread.
106 std::string JavaToStdString(const jstring& j_string);
107
108 private:
109 rtc::ThreadChecker thread_checker_;
110 JNIEnv* const jni_;
111};
112
113// Main class for working with Java from C++ using JNI in WebRTC.
114//
115// Example usage:
116//
117// // At initialization (e.g. in JNI_OnLoad), call JVM::Initialize.
118// JNIEnv* jni = ::base::android::AttachCurrentThread();
119// JavaVM* jvm = NULL;
120// jni->GetJavaVM(&jvm);
sakald7fdb802017-05-26 01:51:53 -0700121// webrtc::JVM::Initialize(jvm);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100122//
123// // Header (.h) file of example class called User.
kwiberg84be5112016-04-27 01:19:58 -0700124// std::unique_ptr<JNIEnvironment> env;
125// std::unique_ptr<NativeRegistration> reg;
126// std::unique_ptr<GlobalRef> obj;
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100127//
128// // Construction (in .cc file) of User class.
129// User::User() {
130// // Calling thread must be attached to the JVM.
131// env = JVM::GetInstance()->environment();
132// reg = env->RegisterNatives("org/webrtc/WebRtcTest", ,);
133// obj = reg->NewObject("<init>", ,);
134// }
135//
136// // Each User method can now use |reg| and |obj| and call Java functions
137// // in WebRtcTest.java, e.g. boolean init() {}.
138// bool User::Foo() {
139// jmethodID id = reg->GetMethodId("init", "()Z");
140// return obj->CallBooleanMethod(id);
141// }
142//
143// // And finally, e.g. in JNI_OnUnLoad, call JVM::Uninitialize.
144// JVM::Uninitialize();
145class JVM {
146 public:
sakald7fdb802017-05-26 01:51:53 -0700147 // Stores global handles to the Java VM interface.
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100148 // Should be called once on a thread that is attached to the JVM.
sakald7fdb802017-05-26 01:51:53 -0700149 static void Initialize(JavaVM* jvm);
Sami Kalliomäkiab842722017-06-01 13:38:27 +0200150 // Like the method above but also passes the context to the ContextUtils
151 // class. This method should be used by pure-C++ Android users that can't call
152 // ContextUtils.initialize directly.
lliuu548cdce2017-05-24 16:45:57 -0700153 static void Initialize(JavaVM* jvm, jobject context);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100154 // Clears handles stored in Initialize(). Must be called on same thread as
155 // Initialize().
156 static void Uninitialize();
157 // Gives access to the global Java VM interface pointer, which then can be
158 // used to create a valid JNIEnvironment object or to get a JavaClass object.
159 static JVM* GetInstance();
160
161 // Creates a JNIEnvironment object.
162 // This method returns a NULL pointer if AttachCurrentThread() has not been
163 // called successfully. Use the AttachCurrentThreadIfNeeded class if needed.
kwiberg84be5112016-04-27 01:19:58 -0700164 std::unique_ptr<JNIEnvironment> environment();
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100165
166 // Returns a JavaClass object given class |name|.
167 // Note that the class name must be one of the names in the static
168 // |loaded_classes| array defined in jvm_android.cc.
169 // This method must be called on the construction thread.
170 JavaClass GetClass(const char* name);
171
172 // TODO(henrika): can we make these private?
173 JavaVM* jvm() const { return jvm_; }
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100174
175 protected:
sakald7fdb802017-05-26 01:51:53 -0700176 JVM(JavaVM* jvm);
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100177 ~JVM();
178
179 private:
180 JNIEnv* jni() const { return GetEnv(jvm_); }
181
182 rtc::ThreadChecker thread_checker_;
183 JavaVM* const jvm_;
Henrik Kjellanderff761fb2015-11-04 08:31:52 +0100184};
185
186} // namespace webrtc
187
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200188#endif // MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_