| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/android/jni_android.h" | 5 #include "base/android/jni_android.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/android/build_info.h" | 9 #include "base/android/build_info.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 using base::android::GetClass; | 15 using base::android::GetClass; |
| 16 using base::android::MethodID; | 16 using base::android::MethodID; |
| 17 using base::android::ScopedJavaLocalRef; | 17 using base::android::ScopedJavaLocalRef; |
| 18 | 18 |
| 19 JavaVM* g_jvm = NULL; | 19 JavaVM* g_jvm = NULL; |
| 20 |
| 21 // NOTE: This variable is only used for debugging http://crbug.com/322200 |
| 22 const JNIInvokeInterface* g_jvm_functions = NULL; |
| 23 |
| 20 // Leak the global app context, as it is used from a non-joinable worker thread | 24 // Leak the global app context, as it is used from a non-joinable worker thread |
| 21 // that may still be running at shutdown. There is no harm in doing this. | 25 // that may still be running at shutdown. There is no harm in doing this. |
| 22 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky | 26 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky |
| 23 g_application_context = LAZY_INSTANCE_INITIALIZER; | 27 g_application_context = LAZY_INSTANCE_INITIALIZER; |
| 24 | 28 |
| 25 std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { | 29 std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { |
| 26 ScopedJavaLocalRef<jclass> throwable_clazz = | 30 ScopedJavaLocalRef<jclass> throwable_clazz = |
| 27 GetClass(env, "java/lang/Throwable"); | 31 GetClass(env, "java/lang/Throwable"); |
| 28 jmethodID throwable_printstacktrace = | 32 jmethodID throwable_printstacktrace = |
| 29 MethodID::Get<MethodID::TYPE_INSTANCE>( | 33 MethodID::Get<MethodID::TYPE_INSTANCE>( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 } | 73 } |
| 70 | 74 |
| 71 } // namespace | 75 } // namespace |
| 72 | 76 |
| 73 namespace base { | 77 namespace base { |
| 74 namespace android { | 78 namespace android { |
| 75 | 79 |
| 76 JNIEnv* AttachCurrentThread() { | 80 JNIEnv* AttachCurrentThread() { |
| 77 DCHECK(g_jvm); | 81 DCHECK(g_jvm); |
| 78 JNIEnv* env = NULL; | 82 JNIEnv* env = NULL; |
| 79 jint ret = g_jvm->AttachCurrentThread(&env, NULL); | 83 // See http://crbug.com/322200 for the reasons for these CHECKs. |
| 84 CHECK(g_jvm); |
| 85 CHECK_EQ(g_jvm_functions, g_jvm->functions); |
| 86 jint ret = g_jvm_functions->AttachCurrentThread(g_jvm, &env, NULL); |
| 80 DCHECK_EQ(JNI_OK, ret); | 87 DCHECK_EQ(JNI_OK, ret); |
| 81 return env; | 88 return env; |
| 82 } | 89 } |
| 83 | 90 |
| 84 void DetachFromVM() { | 91 void DetachFromVM() { |
| 85 // Ignore the return value, if the thread is not attached, DetachCurrentThread | 92 // Ignore the return value, if the thread is not attached, DetachCurrentThread |
| 86 // will fail. But it is ok as the native thread may never be attached. | 93 // will fail. But it is ok as the native thread may never be attached. |
| 87 if (g_jvm) | 94 if (g_jvm) { |
| 95 // See http://crbug.com/322200 for the reasons for these CHECKs. |
| 96 CHECK(g_jvm); |
| 97 CHECK_EQ(g_jvm_functions, g_jvm->functions); |
| 88 g_jvm->DetachCurrentThread(); | 98 g_jvm->DetachCurrentThread(); |
| 99 } |
| 89 } | 100 } |
| 90 | 101 |
| 91 void InitVM(JavaVM* vm) { | 102 void InitVM(JavaVM* vm) { |
| 92 DCHECK(!g_jvm); | 103 DCHECK(!g_jvm); |
| 93 g_jvm = vm; | 104 g_jvm = vm; |
| 105 g_jvm_functions = vm->functions; |
| 94 } | 106 } |
| 95 | 107 |
| 96 bool IsVMInitialized() { | 108 bool IsVMInitialized() { |
| 97 return g_jvm != NULL; | 109 return g_jvm != NULL; |
| 98 } | 110 } |
| 99 | 111 |
| 100 void InitApplicationContext(JNIEnv* env, const JavaRef<jobject>& context) { | 112 void InitApplicationContext(JNIEnv* env, const JavaRef<jobject>& context) { |
| 101 if (env->IsSameObject(g_application_context.Get().obj(), context.obj())) { | 113 if (env->IsSameObject(g_application_context.Get().obj(), context.obj())) { |
| 102 // It's safe to set the context more than once if it's the same context. | 114 // It's safe to set the context more than once if it's the same context. |
| 103 return; | 115 return; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 // RVO should avoid any extra copies of the exception string. | 211 // RVO should avoid any extra copies of the exception string. |
| 200 base::android::BuildInfo::GetInstance()->set_java_exception_info( | 212 base::android::BuildInfo::GetInstance()->set_java_exception_info( |
| 201 GetJavaExceptionInfo(env, java_throwable)); | 213 GetJavaExceptionInfo(env, java_throwable)); |
| 202 | 214 |
| 203 // Now, feel good about it and die. | 215 // Now, feel good about it and die. |
| 204 CHECK(false); | 216 CHECK(false); |
| 205 } | 217 } |
| 206 | 218 |
| 207 } // namespace android | 219 } // namespace android |
| 208 } // namespace base | 220 } // namespace base |
| OLD | NEW |