| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ | 11 #ifndef WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ |
| 12 #define WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ | 12 #define WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ |
| 13 | 13 |
| 14 #include <jni.h> | 14 #include <jni.h> |
| 15 |
| 16 #include <memory> |
| 15 #include <string> | 17 #include <string> |
| 16 | 18 |
| 17 #include "webrtc/base/scoped_ptr.h" | 19 #include "webrtc/base/scoped_ptr.h" |
| 18 #include "webrtc/base/thread_checker.h" | 20 #include "webrtc/base/thread_checker.h" |
| 19 #include "webrtc/modules/utility/include/helpers_android.h" | 21 #include "webrtc/modules/utility/include/helpers_android.h" |
| 20 | 22 |
| 21 namespace webrtc { | 23 namespace webrtc { |
| 22 | 24 |
| 23 // The JNI interface pointer (JNIEnv) is valid only in the current thread. | 25 // The JNI interface pointer (JNIEnv) is valid only in the current thread. |
| 24 // Should another thread need to access the Java VM, it must first call | 26 // Should another thread need to access the Java VM, it must first call |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 jclass const j_class_; | 71 jclass const j_class_; |
| 70 }; | 72 }; |
| 71 | 73 |
| 72 // Adds support of the NewObject factory method to the JavaClass class. | 74 // Adds support of the NewObject factory method to the JavaClass class. |
| 73 // See example in JVM for more details on how to use it. | 75 // See example in JVM for more details on how to use it. |
| 74 class NativeRegistration : public JavaClass { | 76 class NativeRegistration : public JavaClass { |
| 75 public: | 77 public: |
| 76 NativeRegistration(JNIEnv* jni, jclass clazz); | 78 NativeRegistration(JNIEnv* jni, jclass clazz); |
| 77 ~NativeRegistration(); | 79 ~NativeRegistration(); |
| 78 | 80 |
| 79 rtc::scoped_ptr<GlobalRef> NewObject( | 81 std::unique_ptr<GlobalRef> NewObject( |
| 80 const char* name, const char* signature, ...); | 82 const char* name, const char* signature, ...); |
| 81 | 83 |
| 82 private: | 84 private: |
| 83 JNIEnv* const jni_; | 85 JNIEnv* const jni_; |
| 84 }; | 86 }; |
| 85 | 87 |
| 86 // This class is created by the JVM class and is used to expose methods that | 88 // This class is created by the JVM class and is used to expose methods that |
| 87 // needs the JNI interface pointer but its main purpose is to create a | 89 // needs the JNI interface pointer but its main purpose is to create a |
| 88 // NativeRegistration object given name of a Java class and a list of native | 90 // NativeRegistration object given name of a Java class and a list of native |
| 89 // methods. See example in JVM for more details. | 91 // methods. See example in JVM for more details. |
| 90 class JNIEnvironment { | 92 class JNIEnvironment { |
| 91 public: | 93 public: |
| 92 explicit JNIEnvironment(JNIEnv* jni); | 94 explicit JNIEnvironment(JNIEnv* jni); |
| 93 ~JNIEnvironment(); | 95 ~JNIEnvironment(); |
| 94 | 96 |
| 95 // Registers native methods with the Java class specified by |name|. | 97 // Registers native methods with the Java class specified by |name|. |
| 96 // Note that the class name must be one of the names in the static | 98 // Note that the class name must be one of the names in the static |
| 97 // |loaded_classes| array defined in jvm_android.cc. | 99 // |loaded_classes| array defined in jvm_android.cc. |
| 98 // This method must be called on the construction thread. | 100 // This method must be called on the construction thread. |
| 99 rtc::scoped_ptr<NativeRegistration> RegisterNatives( | 101 std::unique_ptr<NativeRegistration> RegisterNatives( |
| 100 const char* name, const JNINativeMethod *methods, int num_methods); | 102 const char* name, const JNINativeMethod *methods, int num_methods); |
| 101 | 103 |
| 102 // Converts from Java string to std::string. | 104 // Converts from Java string to std::string. |
| 103 // This method must be called on the construction thread. | 105 // This method must be called on the construction thread. |
| 104 std::string JavaToStdString(const jstring& j_string); | 106 std::string JavaToStdString(const jstring& j_string); |
| 105 | 107 |
| 106 private: | 108 private: |
| 107 rtc::ThreadChecker thread_checker_; | 109 rtc::ThreadChecker thread_checker_; |
| 108 JNIEnv* const jni_; | 110 JNIEnv* const jni_; |
| 109 }; | 111 }; |
| 110 | 112 |
| 111 // Main class for working with Java from C++ using JNI in WebRTC. | 113 // Main class for working with Java from C++ using JNI in WebRTC. |
| 112 // | 114 // |
| 113 // Example usage: | 115 // Example usage: |
| 114 // | 116 // |
| 115 // // At initialization (e.g. in JNI_OnLoad), call JVM::Initialize. | 117 // // At initialization (e.g. in JNI_OnLoad), call JVM::Initialize. |
| 116 // JNIEnv* jni = ::base::android::AttachCurrentThread(); | 118 // JNIEnv* jni = ::base::android::AttachCurrentThread(); |
| 117 // JavaVM* jvm = NULL; | 119 // JavaVM* jvm = NULL; |
| 118 // jni->GetJavaVM(&jvm); | 120 // jni->GetJavaVM(&jvm); |
| 119 // jobject context = ::base::android::GetApplicationContext(); | 121 // jobject context = ::base::android::GetApplicationContext(); |
| 120 // webrtc::JVM::Initialize(jvm, context); | 122 // webrtc::JVM::Initialize(jvm, context); |
| 121 // | 123 // |
| 122 // // Header (.h) file of example class called User. | 124 // // Header (.h) file of example class called User. |
| 123 // rtc::scoped_ptr<JNIEnvironment> env; | 125 // std::unique_ptr<JNIEnvironment> env; |
| 124 // rtc::scoped_ptr<NativeRegistration> reg; | 126 // std::unique_ptr<NativeRegistration> reg; |
| 125 // rtc::scoped_ptr<GlobalRef> obj; | 127 // std::unique_ptr<GlobalRef> obj; |
| 126 // | 128 // |
| 127 // // Construction (in .cc file) of User class. | 129 // // Construction (in .cc file) of User class. |
| 128 // User::User() { | 130 // User::User() { |
| 129 // // Calling thread must be attached to the JVM. | 131 // // Calling thread must be attached to the JVM. |
| 130 // env = JVM::GetInstance()->environment(); | 132 // env = JVM::GetInstance()->environment(); |
| 131 // reg = env->RegisterNatives("org/webrtc/WebRtcTest", ,); | 133 // reg = env->RegisterNatives("org/webrtc/WebRtcTest", ,); |
| 132 // obj = reg->NewObject("<init>", ,); | 134 // obj = reg->NewObject("<init>", ,); |
| 133 // } | 135 // } |
| 134 // | 136 // |
| 135 // // Each User method can now use |reg| and |obj| and call Java functions | 137 // // Each User method can now use |reg| and |obj| and call Java functions |
| (...skipping 13 matching lines...) Expand all Loading... |
| 149 // Clears handles stored in Initialize(). Must be called on same thread as | 151 // Clears handles stored in Initialize(). Must be called on same thread as |
| 150 // Initialize(). | 152 // Initialize(). |
| 151 static void Uninitialize(); | 153 static void Uninitialize(); |
| 152 // Gives access to the global Java VM interface pointer, which then can be | 154 // Gives access to the global Java VM interface pointer, which then can be |
| 153 // used to create a valid JNIEnvironment object or to get a JavaClass object. | 155 // used to create a valid JNIEnvironment object or to get a JavaClass object. |
| 154 static JVM* GetInstance(); | 156 static JVM* GetInstance(); |
| 155 | 157 |
| 156 // Creates a JNIEnvironment object. | 158 // Creates a JNIEnvironment object. |
| 157 // This method returns a NULL pointer if AttachCurrentThread() has not been | 159 // This method returns a NULL pointer if AttachCurrentThread() has not been |
| 158 // called successfully. Use the AttachCurrentThreadIfNeeded class if needed. | 160 // called successfully. Use the AttachCurrentThreadIfNeeded class if needed. |
| 159 rtc::scoped_ptr<JNIEnvironment> environment(); | 161 std::unique_ptr<JNIEnvironment> environment(); |
| 160 | 162 |
| 161 // Returns a JavaClass object given class |name|. | 163 // Returns a JavaClass object given class |name|. |
| 162 // Note that the class name must be one of the names in the static | 164 // Note that the class name must be one of the names in the static |
| 163 // |loaded_classes| array defined in jvm_android.cc. | 165 // |loaded_classes| array defined in jvm_android.cc. |
| 164 // This method must be called on the construction thread. | 166 // This method must be called on the construction thread. |
| 165 JavaClass GetClass(const char* name); | 167 JavaClass GetClass(const char* name); |
| 166 | 168 |
| 167 // TODO(henrika): can we make these private? | 169 // TODO(henrika): can we make these private? |
| 168 JavaVM* jvm() const { return jvm_; } | 170 JavaVM* jvm() const { return jvm_; } |
| 169 jobject context() const { return context_; } | 171 jobject context() const { return context_; } |
| 170 | 172 |
| 171 protected: | 173 protected: |
| 172 JVM(JavaVM* jvm, jobject context); | 174 JVM(JavaVM* jvm, jobject context); |
| 173 ~JVM(); | 175 ~JVM(); |
| 174 | 176 |
| 175 private: | 177 private: |
| 176 JNIEnv* jni() const { return GetEnv(jvm_); } | 178 JNIEnv* jni() const { return GetEnv(jvm_); } |
| 177 | 179 |
| 178 rtc::ThreadChecker thread_checker_; | 180 rtc::ThreadChecker thread_checker_; |
| 179 JavaVM* const jvm_; | 181 JavaVM* const jvm_; |
| 180 jobject context_; | 182 jobject context_; |
| 181 }; | 183 }; |
| 182 | 184 |
| 183 } // namespace webrtc | 185 } // namespace webrtc |
| 184 | 186 |
| 185 #endif // WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ | 187 #endif // WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ |
| OLD | NEW |