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/atomicops.h" | 11 #include "base/atomicops.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 using base::android::GetClass; | 17 using base::android::GetClass; |
| 18 using base::android::GetMethodID; | 18 using base::android::GetMethodID; |
| 19 using base::android::ScopedJavaLocalRef; | 19 using base::android::ScopedJavaLocalRef; |
| 20 | 20 |
| 21 JavaVM* g_jvm = NULL; | |
| 22 | |
| 23 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> > | |
| 24 g_application_context = LAZY_INSTANCE_INITIALIZER; | |
| 25 | |
| 26 struct MethodIdentifier { | 21 struct MethodIdentifier { |
| 27 const char* class_name; | 22 const char* class_name; |
| 28 const char* method; | 23 const char* method; |
| 29 const char* jni_signature; | 24 const char* jni_signature; |
| 30 | 25 |
| 31 bool operator<(const MethodIdentifier& other) const { | 26 bool operator<(const MethodIdentifier& other) const { |
| 32 int r = strcmp(class_name, other.class_name); | 27 int r = strcmp(class_name, other.class_name); |
| 33 if (r < 0) { | 28 if (r < 0) { |
| 34 return true; | 29 return true; |
| 35 } else if (r > 0) { | 30 } else if (r > 0) { |
| 36 return false; | 31 return false; |
| 37 } | 32 } |
| 38 | 33 |
| 39 r = strcmp(method, other.method); | 34 r = strcmp(method, other.method); |
| 40 if (r < 0) { | 35 if (r < 0) { |
| 41 return true; | 36 return true; |
| 42 } else if (r > 0) { | 37 } else if (r > 0) { |
| 43 return false; | 38 return false; |
| 44 } | 39 } |
| 45 | 40 |
| 46 return strcmp(jni_signature, other.jni_signature) < 0; | 41 return strcmp(jni_signature, other.jni_signature) < 0; |
| 47 } | 42 } |
| 48 }; | 43 }; |
| 49 | 44 |
| 50 typedef std::map<MethodIdentifier, jmethodID> MethodIDMap; | 45 typedef std::map<MethodIdentifier, jmethodID> MethodIDMap; |
| 51 base::LazyInstance<MethodIDMap>::Leaky | 46 |
| 52 g_method_id_map = LAZY_INSTANCE_INITIALIZER; | |
| 53 const base::subtle::AtomicWord kUnlocked = 0; | 47 const base::subtle::AtomicWord kUnlocked = 0; |
| 54 const base::subtle::AtomicWord kLocked = 1; | 48 const base::subtle::AtomicWord kLocked = 1; |
| 55 base::subtle::AtomicWord g_method_id_map_lock = kUnlocked; | 49 base::subtle::AtomicWord g_method_id_map_lock = kUnlocked; |
| 50 JavaVM* g_jvm = NULL; | |
| 51 // Leak the global app context, as it is used from a non-joinable worker thread | |
| 52 // that may still be running at shutdown. There is no harm in doing this. | |
| 53 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky | |
| 54 g_application_context = LAZY_INSTANCE_INITIALIZER; | |
| 55 base::LazyInstance<MethodIDMap> g_method_id_map = LAZY_INSTANCE_INITIALIZER; | |
|
Ryan Sleevi
2012/09/06 18:23:35
BUG: This was Leaky, but when you moved it, you re
Johnny(Jianning) Ding
2012/09/07 04:33:07
Done.
| |
| 56 | 56 |
| 57 std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { | 57 std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { |
| 58 ScopedJavaLocalRef<jclass> throwable_clazz = | 58 ScopedJavaLocalRef<jclass> throwable_clazz = |
| 59 GetClass(env, "java/lang/Throwable"); | 59 GetClass(env, "java/lang/Throwable"); |
| 60 jmethodID throwable_printstacktrace = | 60 jmethodID throwable_printstacktrace = |
| 61 GetMethodID(env, throwable_clazz, "printStackTrace", | 61 GetMethodID(env, throwable_clazz, "printStackTrace", |
| 62 "(Ljava/io/PrintStream;)V"); | 62 "(Ljava/io/PrintStream;)V"); |
| 63 | 63 |
| 64 // Create an instance of ByteArrayOutputStream. | 64 // Create an instance of ByteArrayOutputStream. |
| 65 ScopedJavaLocalRef<jclass> bytearray_output_stream_clazz = | 65 ScopedJavaLocalRef<jclass> bytearray_output_stream_clazz = |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 } | 149 } |
| 150 bool error = ClearException(env); | 150 bool error = ClearException(env); |
| 151 DCHECK(!error); | 151 DCHECK(!error); |
| 152 return true; | 152 return true; |
| 153 } | 153 } |
| 154 | 154 |
| 155 jmethodID GetMethodID(JNIEnv* env, | 155 jmethodID GetMethodID(JNIEnv* env, |
| 156 const JavaRef<jclass>& clazz, | 156 const JavaRef<jclass>& clazz, |
| 157 const char* method_name, | 157 const char* method_name, |
| 158 const char* jni_signature) { | 158 const char* jni_signature) { |
| 159 // We can't use clazz.env() as that may be from a different thread. | 159 // Method clazz.env() can not be used as that may be from a different thread. |
|
Ryan Sleevi
2012/09/06 18:23:35
nit: s/Method //
Johnny(Jianning) Ding
2012/09/07 04:33:07
Done.
| |
| 160 return GetMethodID(env, clazz.obj(), method_name, jni_signature); | 160 return GetMethodID(env, clazz.obj(), method_name, jni_signature); |
| 161 } | 161 } |
| 162 | 162 |
| 163 jmethodID GetMethodID(JNIEnv* env, | 163 jmethodID GetMethodID(JNIEnv* env, |
| 164 jclass clazz, | 164 jclass clazz, |
| 165 const char* method_name, | 165 const char* method_name, |
| 166 const char* jni_signature) { | 166 const char* jni_signature) { |
| 167 jmethodID method_id = | 167 jmethodID method_id = |
| 168 env->GetMethodID(clazz, method_name, jni_signature); | 168 env->GetMethodID(clazz, method_name, jni_signature); |
| 169 CHECK(method_id && !ClearException(env)) << "Failed to find method " << | 169 CHECK(method_id && !ClearException(env)) << "Failed to find method " << |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 if (!HasException(env)) | 297 if (!HasException(env)) |
| 298 return false; | 298 return false; |
| 299 env->ExceptionDescribe(); | 299 env->ExceptionDescribe(); |
| 300 env->ExceptionClear(); | 300 env->ExceptionClear(); |
| 301 return true; | 301 return true; |
| 302 } | 302 } |
| 303 | 303 |
| 304 void CheckException(JNIEnv* env) { | 304 void CheckException(JNIEnv* env) { |
| 305 if (!HasException(env)) return; | 305 if (!HasException(env)) return; |
| 306 | 306 |
| 307 // Ugh, we are going to die, might as well tell breakpad about it. | 307 // Exception has been found, might as well tell breakpad about it. |
| 308 jthrowable java_throwable = env->ExceptionOccurred(); | 308 jthrowable java_throwable = env->ExceptionOccurred(); |
| 309 if (!java_throwable) { | 309 if (!java_throwable) { |
| 310 // Nothing we can do. | 310 // Do nothing but return false. |
| 311 CHECK(false); | 311 CHECK(false); |
| 312 } | 312 } |
| 313 | 313 |
| 314 // Clear the pending exception, we do have a reference to it. | 314 // Clear the pending exception as it has been referred. |
|
Ryan Sleevi
2012/09/06 18:23:35
nit: Clear the pending exception, since a local re
Johnny(Jianning) Ding
2012/09/07 04:33:07
Done.
| |
| 315 env->ExceptionClear(); | 315 env->ExceptionClear(); |
| 316 | 316 |
| 317 // Set the exception_string in BuildInfo so that breakpad can read it. | 317 // Set the exception_string in BuildInfo so that breakpad can read it. |
| 318 // RVO should avoid any extra copies of the exception string. | 318 // RVO should avoid any extra copies of the exception string. |
| 319 base::android::BuildInfo::GetInstance()->set_java_exception_info( | 319 base::android::BuildInfo::GetInstance()->set_java_exception_info( |
| 320 GetJavaExceptionInfo(env, java_throwable)); | 320 GetJavaExceptionInfo(env, java_throwable)); |
| 321 | 321 |
| 322 // Now, feel good about it and die. | 322 // Now, feel good about it and die. |
| 323 CHECK(false); | 323 CHECK(false); |
| 324 } | 324 } |
| 325 | 325 |
| 326 } // namespace android | 326 } // namespace android |
| 327 } // namespace base | 327 } // namespace base |
| OLD | NEW |