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 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 }; | 111 }; |
112 | 112 |
113 // 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. |
114 // | 114 // |
115 // Example usage: | 115 // Example usage: |
116 // | 116 // |
117 // // At initialization (e.g. in JNI_OnLoad), call JVM::Initialize. | 117 // // At initialization (e.g. in JNI_OnLoad), call JVM::Initialize. |
118 // JNIEnv* jni = ::base::android::AttachCurrentThread(); | 118 // JNIEnv* jni = ::base::android::AttachCurrentThread(); |
119 // JavaVM* jvm = NULL; | 119 // JavaVM* jvm = NULL; |
120 // jni->GetJavaVM(&jvm); | 120 // jni->GetJavaVM(&jvm); |
121 // jobject context = ::base::android::GetApplicationContext(); | 121 // webrtc::JVM::Initialize(jvm); |
122 // webrtc::JVM::Initialize(jvm, context); | |
123 // | 122 // |
124 // // Header (.h) file of example class called User. | 123 // // Header (.h) file of example class called User. |
125 // std::unique_ptr<JNIEnvironment> env; | 124 // std::unique_ptr<JNIEnvironment> env; |
126 // std::unique_ptr<NativeRegistration> reg; | 125 // std::unique_ptr<NativeRegistration> reg; |
127 // std::unique_ptr<GlobalRef> obj; | 126 // std::unique_ptr<GlobalRef> obj; |
128 // | 127 // |
129 // // Construction (in .cc file) of User class. | 128 // // Construction (in .cc file) of User class. |
130 // User::User() { | 129 // User::User() { |
131 // // Calling thread must be attached to the JVM. | 130 // // Calling thread must be attached to the JVM. |
132 // env = JVM::GetInstance()->environment(); | 131 // env = JVM::GetInstance()->environment(); |
133 // reg = env->RegisterNatives("org/webrtc/WebRtcTest", ,); | 132 // reg = env->RegisterNatives("org/webrtc/WebRtcTest", ,); |
134 // obj = reg->NewObject("<init>", ,); | 133 // obj = reg->NewObject("<init>", ,); |
135 // } | 134 // } |
136 // | 135 // |
137 // // Each User method can now use |reg| and |obj| and call Java functions | 136 // // Each User method can now use |reg| and |obj| and call Java functions |
138 // // in WebRtcTest.java, e.g. boolean init() {}. | 137 // // in WebRtcTest.java, e.g. boolean init() {}. |
139 // bool User::Foo() { | 138 // bool User::Foo() { |
140 // jmethodID id = reg->GetMethodId("init", "()Z"); | 139 // jmethodID id = reg->GetMethodId("init", "()Z"); |
141 // return obj->CallBooleanMethod(id); | 140 // return obj->CallBooleanMethod(id); |
142 // } | 141 // } |
143 // | 142 // |
144 // // And finally, e.g. in JNI_OnUnLoad, call JVM::Uninitialize. | 143 // // And finally, e.g. in JNI_OnUnLoad, call JVM::Uninitialize. |
145 // JVM::Uninitialize(); | 144 // JVM::Uninitialize(); |
146 class JVM { | 145 class JVM { |
147 public: | 146 public: |
148 // Stores global handles to the Java VM interface and the application context. | 147 // Stores global handles to the Java VM interface. |
149 // Should be called once on a thread that is attached to the JVM. | 148 // Should be called once on a thread that is attached to the JVM. |
149 static void Initialize(JavaVM* jvm); | |
150 // TODO(sakal): Remove once downstream dependencies have been updated. | |
151 // Deprecated old signature with Android context. | |
henrika_webrtc
2017/05/26 08:34:56
Nit, deprecate.
sakal
2017/05/26 08:49:33
Done.
| |
150 static void Initialize(JavaVM* jvm, jobject context); | 152 static void Initialize(JavaVM* jvm, jobject context); |
151 // Clears handles stored in Initialize(). Must be called on same thread as | 153 // Clears handles stored in Initialize(). Must be called on same thread as |
152 // Initialize(). | 154 // Initialize(). |
153 static void Uninitialize(); | 155 static void Uninitialize(); |
154 // Gives access to the global Java VM interface pointer, which then can be | 156 // Gives access to the global Java VM interface pointer, which then can be |
155 // used to create a valid JNIEnvironment object or to get a JavaClass object. | 157 // used to create a valid JNIEnvironment object or to get a JavaClass object. |
156 static JVM* GetInstance(); | 158 static JVM* GetInstance(); |
157 | 159 |
158 // Creates a JNIEnvironment object. | 160 // Creates a JNIEnvironment object. |
159 // This method returns a NULL pointer if AttachCurrentThread() has not been | 161 // This method returns a NULL pointer if AttachCurrentThread() has not been |
160 // called successfully. Use the AttachCurrentThreadIfNeeded class if needed. | 162 // called successfully. Use the AttachCurrentThreadIfNeeded class if needed. |
161 std::unique_ptr<JNIEnvironment> environment(); | 163 std::unique_ptr<JNIEnvironment> environment(); |
162 | 164 |
163 // Returns a JavaClass object given class |name|. | 165 // Returns a JavaClass object given class |name|. |
164 // Note that the class name must be one of the names in the static | 166 // Note that the class name must be one of the names in the static |
165 // |loaded_classes| array defined in jvm_android.cc. | 167 // |loaded_classes| array defined in jvm_android.cc. |
166 // This method must be called on the construction thread. | 168 // This method must be called on the construction thread. |
167 JavaClass GetClass(const char* name); | 169 JavaClass GetClass(const char* name); |
168 | 170 |
169 // TODO(henrika): can we make these private? | 171 // TODO(henrika): can we make these private? |
170 JavaVM* jvm() const { return jvm_; } | 172 JavaVM* jvm() const { return jvm_; } |
171 jobject context() const { return context_; } | |
172 | 173 |
173 protected: | 174 protected: |
174 JVM(JavaVM* jvm, jobject context); | 175 JVM(JavaVM* jvm); |
175 ~JVM(); | 176 ~JVM(); |
176 | 177 |
177 private: | 178 private: |
178 JNIEnv* jni() const { return GetEnv(jvm_); } | 179 JNIEnv* jni() const { return GetEnv(jvm_); } |
179 | 180 |
180 rtc::ThreadChecker thread_checker_; | 181 rtc::ThreadChecker thread_checker_; |
181 JavaVM* const jvm_; | 182 JavaVM* const jvm_; |
182 jobject context_; | |
183 }; | 183 }; |
184 | 184 |
185 } // namespace webrtc | 185 } // namespace webrtc |
186 | 186 |
187 #endif // WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ | 187 #endif // WEBRTC_MODULES_UTILITY_INCLUDE_JVM_ANDROID_H_ |
OLD | NEW |