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