Chromium Code Reviews| 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 #include "base/threading/platform_thread.h" | |
| 14 #include "base/threading/thread_local.h" | |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 using base::android::GetClass; | 17 using base::android::GetClass; |
| 16 using base::android::MethodID; | 18 using base::android::MethodID; |
| 17 using base::android::ScopedJavaLocalRef; | 19 using base::android::ScopedJavaLocalRef; |
| 18 | 20 |
| 19 JavaVM* g_jvm = NULL; | 21 JavaVM* g_jvm = NULL; |
| 20 // Leak the global app context, as it is used from a non-joinable worker thread | 22 // 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. | 23 // that may still be running at shutdown. There is no harm in doing this. |
| 22 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky | 24 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky |
| 23 g_application_context = LAZY_INSTANCE_INITIALIZER; | 25 g_application_context = LAZY_INSTANCE_INITIALIZER; |
| 24 | 26 |
| 27 // Whether or not AttachCurrentThread() has been called before. | |
| 28 base::LazyInstance<base::ThreadLocalBoolean>::Leaky already_attached = | |
| 29 LAZY_INSTANCE_INITIALIZER; | |
| 30 | |
| 25 std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { | 31 std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { |
| 26 ScopedJavaLocalRef<jclass> throwable_clazz = | 32 ScopedJavaLocalRef<jclass> throwable_clazz = |
| 27 GetClass(env, "java/lang/Throwable"); | 33 GetClass(env, "java/lang/Throwable"); |
| 28 jmethodID throwable_printstacktrace = | 34 jmethodID throwable_printstacktrace = |
| 29 MethodID::Get<MethodID::TYPE_INSTANCE>( | 35 MethodID::Get<MethodID::TYPE_INSTANCE>( |
| 30 env, throwable_clazz.obj(), "printStackTrace", | 36 env, throwable_clazz.obj(), "printStackTrace", |
| 31 "(Ljava/io/PrintStream;)V"); | 37 "(Ljava/io/PrintStream;)V"); |
| 32 | 38 |
| 33 // Create an instance of ByteArrayOutputStream. | 39 // Create an instance of ByteArrayOutputStream. |
| 34 ScopedJavaLocalRef<jclass> bytearray_output_stream_clazz = | 40 ScopedJavaLocalRef<jclass> bytearray_output_stream_clazz = |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 } | 75 } |
| 70 | 76 |
| 71 } // namespace | 77 } // namespace |
| 72 | 78 |
| 73 namespace base { | 79 namespace base { |
| 74 namespace android { | 80 namespace android { |
| 75 | 81 |
| 76 JNIEnv* AttachCurrentThread() { | 82 JNIEnv* AttachCurrentThread() { |
| 77 DCHECK(g_jvm); | 83 DCHECK(g_jvm); |
| 78 JNIEnv* env = NULL; | 84 JNIEnv* env = NULL; |
| 79 jint ret = g_jvm->AttachCurrentThread(&env, NULL); | 85 jint ret = JNI_OK; |
| 86 // Passes thread name in args for first call. | |
| 87 if (!already_attached.Get().Get()) { | |
|
bulach
2014/06/17 17:26:55
hmm... AttachCurrentThread is a somewhat hot funct
byungchul
2014/06/17 17:51:05
Do you mean call AttachCurrentThread() in Platform
bulach
2014/06/17 20:45:33
tough choice :)
how many named threads which are n
byungchul
2014/06/17 21:11:11
I am not working on android chrome, but a differen
| |
| 88 already_attached.Get().Set(true); | |
| 89 JavaVMAttachArgs args; | |
| 90 args.version = JNI_VERSION_1_2; | |
| 91 args.name = PlatformThread::GetName(); | |
| 92 args.group = NULL; | |
| 93 ret = g_jvm->AttachCurrentThread(&env, &args); | |
| 94 } else { | |
| 95 ret = g_jvm->AttachCurrentThread(&env, NULL); | |
| 96 } | |
| 80 DCHECK_EQ(JNI_OK, ret); | 97 DCHECK_EQ(JNI_OK, ret); |
| 81 return env; | 98 return env; |
| 82 } | 99 } |
| 83 | 100 |
| 84 void DetachFromVM() { | 101 void DetachFromVM() { |
| 85 // Ignore the return value, if the thread is not attached, DetachCurrentThread | 102 // 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. | 103 // will fail. But it is ok as the native thread may never be attached. |
| 87 if (g_jvm) | 104 if (g_jvm) { |
| 88 g_jvm->DetachCurrentThread(); | 105 g_jvm->DetachCurrentThread(); |
| 106 already_attached.Get().Set(false); | |
| 107 } | |
| 89 } | 108 } |
| 90 | 109 |
| 91 void InitVM(JavaVM* vm) { | 110 void InitVM(JavaVM* vm) { |
| 92 DCHECK(!g_jvm); | 111 DCHECK(!g_jvm); |
| 93 g_jvm = vm; | 112 g_jvm = vm; |
| 94 } | 113 } |
| 95 | 114 |
| 96 bool IsVMInitialized() { | 115 bool IsVMInitialized() { |
| 97 return g_jvm != NULL; | 116 return g_jvm != NULL; |
| 98 } | 117 } |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 // RVO should avoid any extra copies of the exception string. | 218 // RVO should avoid any extra copies of the exception string. |
| 200 base::android::BuildInfo::GetInstance()->set_java_exception_info( | 219 base::android::BuildInfo::GetInstance()->set_java_exception_info( |
| 201 GetJavaExceptionInfo(env, java_throwable)); | 220 GetJavaExceptionInfo(env, java_throwable)); |
| 202 | 221 |
| 203 // Now, feel good about it and die. | 222 // Now, feel good about it and die. |
| 204 CHECK(false); | 223 CHECK(false); |
| 205 } | 224 } |
| 206 | 225 |
| 207 } // namespace android | 226 } // namespace android |
| 208 } // namespace base | 227 } // namespace base |
| OLD | NEW |