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 #include <android/log.h> | 11 #include <android/log.h> |
12 | 12 |
13 #include <memory> | |
14 | |
15 #include "webrtc/modules/utility/include/jvm_android.h" | 13 #include "webrtc/modules/utility/include/jvm_android.h" |
16 | 14 |
17 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
18 | 16 |
19 #define TAG "JVM" | 17 #define TAG "JVM" |
20 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) | 18 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) |
21 #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) | 19 #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) |
22 | 20 |
23 namespace webrtc { | 21 namespace webrtc { |
24 | 22 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 : JavaClass(jni, clazz), jni_(jni) { | 132 : JavaClass(jni, clazz), jni_(jni) { |
135 ALOGD("NativeRegistration::ctor%s", GetThreadInfo().c_str()); | 133 ALOGD("NativeRegistration::ctor%s", GetThreadInfo().c_str()); |
136 } | 134 } |
137 | 135 |
138 NativeRegistration::~NativeRegistration() { | 136 NativeRegistration::~NativeRegistration() { |
139 ALOGD("NativeRegistration::dtor%s", GetThreadInfo().c_str()); | 137 ALOGD("NativeRegistration::dtor%s", GetThreadInfo().c_str()); |
140 jni_->UnregisterNatives(j_class_); | 138 jni_->UnregisterNatives(j_class_); |
141 CHECK_EXCEPTION(jni_) << "Error during UnregisterNatives"; | 139 CHECK_EXCEPTION(jni_) << "Error during UnregisterNatives"; |
142 } | 140 } |
143 | 141 |
144 std::unique_ptr<GlobalRef> NativeRegistration::NewObject( | 142 rtc::scoped_ptr<GlobalRef> NativeRegistration::NewObject( |
145 const char* name, const char* signature, ...) { | 143 const char* name, const char* signature, ...) { |
146 ALOGD("NativeRegistration::NewObject%s", GetThreadInfo().c_str()); | 144 ALOGD("NativeRegistration::NewObject%s", GetThreadInfo().c_str()); |
147 va_list args; | 145 va_list args; |
148 va_start(args, signature); | 146 va_start(args, signature); |
149 jobject obj = jni_->NewObjectV(j_class_, | 147 jobject obj = jni_->NewObjectV(j_class_, |
150 GetMethodID(jni_, j_class_, name, signature), | 148 GetMethodID(jni_, j_class_, name, signature), |
151 args); | 149 args); |
152 CHECK_EXCEPTION(jni_) << "Error during NewObjectV"; | 150 CHECK_EXCEPTION(jni_) << "Error during NewObjectV"; |
153 va_end(args); | 151 va_end(args); |
154 return std::unique_ptr<GlobalRef>(new GlobalRef(jni_, obj)); | 152 return rtc::scoped_ptr<GlobalRef>(new GlobalRef(jni_, obj)); |
155 } | 153 } |
156 | 154 |
157 // JavaClass implementation. | 155 // JavaClass implementation. |
158 jmethodID JavaClass::GetMethodId( | 156 jmethodID JavaClass::GetMethodId( |
159 const char* name, const char* signature) { | 157 const char* name, const char* signature) { |
160 return GetMethodID(jni_, j_class_, name, signature); | 158 return GetMethodID(jni_, j_class_, name, signature); |
161 } | 159 } |
162 | 160 |
163 jmethodID JavaClass::GetStaticMethodId( | 161 jmethodID JavaClass::GetStaticMethodId( |
164 const char* name, const char* signature) { | 162 const char* name, const char* signature) { |
(...skipping 11 matching lines...) Expand all Loading... |
176 // JNIEnvironment implementation. | 174 // JNIEnvironment implementation. |
177 JNIEnvironment::JNIEnvironment(JNIEnv* jni) : jni_(jni) { | 175 JNIEnvironment::JNIEnvironment(JNIEnv* jni) : jni_(jni) { |
178 ALOGD("JNIEnvironment::ctor%s", GetThreadInfo().c_str()); | 176 ALOGD("JNIEnvironment::ctor%s", GetThreadInfo().c_str()); |
179 } | 177 } |
180 | 178 |
181 JNIEnvironment::~JNIEnvironment() { | 179 JNIEnvironment::~JNIEnvironment() { |
182 ALOGD("JNIEnvironment::dtor%s", GetThreadInfo().c_str()); | 180 ALOGD("JNIEnvironment::dtor%s", GetThreadInfo().c_str()); |
183 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 181 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
184 } | 182 } |
185 | 183 |
186 std::unique_ptr<NativeRegistration> JNIEnvironment::RegisterNatives( | 184 rtc::scoped_ptr<NativeRegistration> JNIEnvironment::RegisterNatives( |
187 const char* name, const JNINativeMethod *methods, int num_methods) { | 185 const char* name, const JNINativeMethod *methods, int num_methods) { |
188 ALOGD("JNIEnvironment::RegisterNatives(%s)", name); | 186 ALOGD("JNIEnvironment::RegisterNatives(%s)", name); |
189 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 187 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
190 jclass clazz = LookUpClass(name); | 188 jclass clazz = LookUpClass(name); |
191 jni_->RegisterNatives(clazz, methods, num_methods); | 189 jni_->RegisterNatives(clazz, methods, num_methods); |
192 CHECK_EXCEPTION(jni_) << "Error during RegisterNatives"; | 190 CHECK_EXCEPTION(jni_) << "Error during RegisterNatives"; |
193 return std::unique_ptr<NativeRegistration>( | 191 return rtc::scoped_ptr<NativeRegistration>( |
194 new NativeRegistration(jni_, clazz)); | 192 new NativeRegistration(jni_, clazz)); |
195 } | 193 } |
196 | 194 |
197 std::string JNIEnvironment::JavaToStdString(const jstring& j_string) { | 195 std::string JNIEnvironment::JavaToStdString(const jstring& j_string) { |
198 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 196 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
199 const char* jchars = jni_->GetStringUTFChars(j_string, nullptr); | 197 const char* jchars = jni_->GetStringUTFChars(j_string, nullptr); |
200 CHECK_EXCEPTION(jni_); | 198 CHECK_EXCEPTION(jni_); |
201 const int size = jni_->GetStringUTFLength(j_string); | 199 const int size = jni_->GetStringUTFLength(j_string); |
202 CHECK_EXCEPTION(jni_); | 200 CHECK_EXCEPTION(jni_); |
203 std::string ret(jchars, size); | 201 std::string ret(jchars, size); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 LoadClasses(jni()); | 233 LoadClasses(jni()); |
236 } | 234 } |
237 | 235 |
238 JVM::~JVM() { | 236 JVM::~JVM() { |
239 ALOGD("JVM::~JVM%s", GetThreadInfo().c_str()); | 237 ALOGD("JVM::~JVM%s", GetThreadInfo().c_str()); |
240 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 238 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
241 FreeClassReferences(jni()); | 239 FreeClassReferences(jni()); |
242 DeleteGlobalRef(jni(), context_); | 240 DeleteGlobalRef(jni(), context_); |
243 } | 241 } |
244 | 242 |
245 std::unique_ptr<JNIEnvironment> JVM::environment() { | 243 rtc::scoped_ptr<JNIEnvironment> JVM::environment() { |
246 ALOGD("JVM::environment%s", GetThreadInfo().c_str()); | 244 ALOGD("JVM::environment%s", GetThreadInfo().c_str()); |
247 // The JNIEnv is used for thread-local storage. For this reason, we cannot | 245 // The JNIEnv is used for thread-local storage. For this reason, we cannot |
248 // share a JNIEnv between threads. If a piece of code has no other way to get | 246 // share a JNIEnv between threads. If a piece of code has no other way to get |
249 // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the | 247 // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the |
250 // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread). | 248 // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread). |
251 // See // http://developer.android.com/training/articles/perf-jni.html. | 249 // See // http://developer.android.com/training/articles/perf-jni.html. |
252 JNIEnv* jni = GetEnv(jvm_); | 250 JNIEnv* jni = GetEnv(jvm_); |
253 if (!jni) { | 251 if (!jni) { |
254 ALOGE("AttachCurrentThread() has not been called on this thread."); | 252 ALOGE("AttachCurrentThread() has not been called on this thread."); |
255 return std::unique_ptr<JNIEnvironment>(); | 253 return rtc::scoped_ptr<JNIEnvironment>(); |
256 } | 254 } |
257 return std::unique_ptr<JNIEnvironment>(new JNIEnvironment(jni)); | 255 return rtc::scoped_ptr<JNIEnvironment>(new JNIEnvironment(jni)); |
258 } | 256 } |
259 | 257 |
260 JavaClass JVM::GetClass(const char* name) { | 258 JavaClass JVM::GetClass(const char* name) { |
261 ALOGD("JVM::GetClass(%s)%s", name, GetThreadInfo().c_str()); | 259 ALOGD("JVM::GetClass(%s)%s", name, GetThreadInfo().c_str()); |
262 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 260 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
263 return JavaClass(jni(), LookUpClass(name)); | 261 return JavaClass(jni(), LookUpClass(name)); |
264 } | 262 } |
265 | 263 |
266 } // namespace webrtc | 264 } // namespace webrtc |
OLD | NEW |