| 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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 CHECK_EXCEPTION(jni_); | 210 CHECK_EXCEPTION(jni_); |
| 211 const int size = jni_->GetStringUTFLength(j_string); | 211 const int size = jni_->GetStringUTFLength(j_string); |
| 212 CHECK_EXCEPTION(jni_); | 212 CHECK_EXCEPTION(jni_); |
| 213 std::string ret(jchars, size); | 213 std::string ret(jchars, size); |
| 214 jni_->ReleaseStringUTFChars(j_string, jchars); | 214 jni_->ReleaseStringUTFChars(j_string, jchars); |
| 215 CHECK_EXCEPTION(jni_); | 215 CHECK_EXCEPTION(jni_); |
| 216 return ret; | 216 return ret; |
| 217 } | 217 } |
| 218 | 218 |
| 219 // static | 219 // static |
| 220 void JVM::Initialize(JavaVM* jvm) { | 220 void JVM::Initialize(JavaVM* jvm, jobject context) { |
| 221 ALOGD("JVM::Initialize%s", GetThreadInfo().c_str()); | 221 ALOGD("JVM::Initialize%s", GetThreadInfo().c_str()); |
| 222 RTC_CHECK(!g_jvm); | 222 RTC_CHECK(!g_jvm); |
| 223 g_jvm = new JVM(jvm); | 223 g_jvm = new JVM(jvm, context); |
| 224 } | 224 } |
| 225 | 225 |
| 226 // static | 226 // static |
| 227 void JVM::Uninitialize() { | 227 void JVM::Uninitialize() { |
| 228 ALOGD("JVM::Uninitialize%s", GetThreadInfo().c_str()); | 228 ALOGD("JVM::Uninitialize%s", GetThreadInfo().c_str()); |
| 229 RTC_DCHECK(g_jvm); | 229 RTC_DCHECK(g_jvm); |
| 230 delete g_jvm; | 230 delete g_jvm; |
| 231 g_jvm = nullptr; | 231 g_jvm = nullptr; |
| 232 } | 232 } |
| 233 | 233 |
| 234 // static | 234 // static |
| 235 JVM* JVM::GetInstance() { | 235 JVM* JVM::GetInstance() { |
| 236 RTC_DCHECK(g_jvm); | 236 RTC_DCHECK(g_jvm); |
| 237 return g_jvm; | 237 return g_jvm; |
| 238 } | 238 } |
| 239 | 239 |
| 240 JVM::JVM(JavaVM* jvm) : jvm_(jvm) { | 240 JVM::JVM(JavaVM* jvm, jobject context) |
| 241 : jvm_(jvm) { |
| 241 ALOGD("JVM::JVM%s", GetThreadInfo().c_str()); | 242 ALOGD("JVM::JVM%s", GetThreadInfo().c_str()); |
| 242 RTC_CHECK(jni()) << "AttachCurrentThread() must be called on this thread."; | 243 RTC_CHECK(jni()) << "AttachCurrentThread() must be called on this thread."; |
| 244 context_ = NewGlobalRef(jni(), context); |
| 243 LoadClasses(jni()); | 245 LoadClasses(jni()); |
| 244 } | 246 } |
| 245 | 247 |
| 246 JVM::~JVM() { | 248 JVM::~JVM() { |
| 247 ALOGD("JVM::~JVM%s", GetThreadInfo().c_str()); | 249 ALOGD("JVM::~JVM%s", GetThreadInfo().c_str()); |
| 248 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 250 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 249 FreeClassReferences(jni()); | 251 FreeClassReferences(jni()); |
| 252 DeleteGlobalRef(jni(), context_); |
| 250 } | 253 } |
| 251 | 254 |
| 252 std::unique_ptr<JNIEnvironment> JVM::environment() { | 255 std::unique_ptr<JNIEnvironment> JVM::environment() { |
| 253 ALOGD("JVM::environment%s", GetThreadInfo().c_str()); | 256 ALOGD("JVM::environment%s", GetThreadInfo().c_str()); |
| 254 // The JNIEnv is used for thread-local storage. For this reason, we cannot | 257 // The JNIEnv is used for thread-local storage. For this reason, we cannot |
| 255 // share a JNIEnv between threads. If a piece of code has no other way to get | 258 // share a JNIEnv between threads. If a piece of code has no other way to get |
| 256 // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the | 259 // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the |
| 257 // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread). | 260 // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread). |
| 258 // See // http://developer.android.com/training/articles/perf-jni.html. | 261 // See // http://developer.android.com/training/articles/perf-jni.html. |
| 259 JNIEnv* jni = GetEnv(jvm_); | 262 JNIEnv* jni = GetEnv(jvm_); |
| 260 if (!jni) { | 263 if (!jni) { |
| 261 ALOGE("AttachCurrentThread() has not been called on this thread."); | 264 ALOGE("AttachCurrentThread() has not been called on this thread."); |
| 262 return std::unique_ptr<JNIEnvironment>(); | 265 return std::unique_ptr<JNIEnvironment>(); |
| 263 } | 266 } |
| 264 return std::unique_ptr<JNIEnvironment>(new JNIEnvironment(jni)); | 267 return std::unique_ptr<JNIEnvironment>(new JNIEnvironment(jni)); |
| 265 } | 268 } |
| 266 | 269 |
| 267 JavaClass JVM::GetClass(const char* name) { | 270 JavaClass JVM::GetClass(const char* name) { |
| 268 ALOGD("JVM::GetClass(%s)%s", name, GetThreadInfo().c_str()); | 271 ALOGD("JVM::GetClass(%s)%s", name, GetThreadInfo().c_str()); |
| 269 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 272 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 270 return JavaClass(jni(), LookUpClass(name)); | 273 return JavaClass(jni(), LookUpClass(name)); |
| 271 } | 274 } |
| 272 | 275 |
| 273 } // namespace webrtc | 276 } // namespace webrtc |
| OLD | NEW |