| 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_method_id.h" |
| 10 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.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 struct MethodIdentifier { | 21 struct MethodIdentifier { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 90 |
| 91 // Call ByteArrayOutputStream.toString() | 91 // Call ByteArrayOutputStream.toString() |
| 92 ScopedJavaLocalRef<jstring> exception_string( | 92 ScopedJavaLocalRef<jstring> exception_string( |
| 93 env, static_cast<jstring>( | 93 env, static_cast<jstring>( |
| 94 env->CallObjectMethod(bytearray_output_stream.obj(), | 94 env->CallObjectMethod(bytearray_output_stream.obj(), |
| 95 bytearray_output_stream_tostring))); | 95 bytearray_output_stream_tostring))); |
| 96 | 96 |
| 97 return ConvertJavaStringToUTF8(exception_string); | 97 return ConvertJavaStringToUTF8(exception_string); |
| 98 } | 98 } |
| 99 | 99 |
| 100 enum MethodType { | |
| 101 METHODTYPE_STATIC, | |
| 102 METHODTYPE_NORMAL, | |
| 103 }; | |
| 104 | |
| 105 enum ExceptionCheck { | |
| 106 EXCEPTIONCHECK_YES, | |
| 107 EXCEPTIONCHECK_NO, | |
| 108 }; | |
| 109 | |
| 110 template<MethodType method_type, ExceptionCheck exception_check> | |
| 111 jmethodID GetMethodIDInternal(JNIEnv* env, | |
| 112 jclass clazz, | |
| 113 const char* method_name, | |
| 114 const char* jni_signature) { | |
| 115 jmethodID method_id = method_type == METHODTYPE_STATIC ? | |
| 116 env->GetStaticMethodID(clazz, method_name, jni_signature) : | |
| 117 env->GetMethodID(clazz, method_name, jni_signature); | |
| 118 if (exception_check == EXCEPTIONCHECK_YES) { | |
| 119 CHECK(!base::android::ClearException(env) && method_id) << | |
| 120 "Failed to find " << | |
| 121 (method_type == METHODTYPE_STATIC ? "static " : "") << | |
| 122 "method " << method_name << " " << jni_signature; | |
| 123 } else if (base::android::HasException(env)) { | |
| 124 env->ExceptionClear(); | |
| 125 } | |
| 126 return method_id; | |
| 127 } | |
| 128 | |
| 129 } // namespace | 100 } // namespace |
| 130 | 101 |
| 131 namespace base { | 102 namespace base { |
| 132 namespace android { | 103 namespace android { |
| 133 | 104 |
| 134 JNIEnv* AttachCurrentThread() { | 105 JNIEnv* AttachCurrentThread() { |
| 135 if (!g_jvm) | 106 if (!g_jvm) |
| 136 return NULL; | 107 return NULL; |
| 137 JNIEnv* env = NULL; | 108 JNIEnv* env = NULL; |
| 138 jint ret = g_jvm->AttachCurrentThread(&env, NULL); | 109 jint ret = g_jvm->AttachCurrentThread(&env, NULL); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 const char* method_name, | 159 const char* method_name, |
| 189 const char* jni_signature) { | 160 const char* jni_signature) { |
| 190 // clazz.env() can not be used as that may be from a different thread. | 161 // clazz.env() can not be used as that may be from a different thread. |
| 191 return GetMethodID(env, clazz.obj(), method_name, jni_signature); | 162 return GetMethodID(env, clazz.obj(), method_name, jni_signature); |
| 192 } | 163 } |
| 193 | 164 |
| 194 jmethodID GetMethodID(JNIEnv* env, | 165 jmethodID GetMethodID(JNIEnv* env, |
| 195 jclass clazz, | 166 jclass clazz, |
| 196 const char* method_name, | 167 const char* method_name, |
| 197 const char* jni_signature) { | 168 const char* jni_signature) { |
| 198 return GetMethodIDInternal<METHODTYPE_NORMAL, EXCEPTIONCHECK_YES>( | 169 return MethodID::Get<MethodID::METHODTYPE_NORMAL, |
| 170 MethodID::EXCEPTIONCHECK_YES>( |
| 199 env, clazz, method_name, jni_signature); | 171 env, clazz, method_name, jni_signature); |
| 200 } | 172 } |
| 201 | 173 |
| 202 jmethodID GetMethodIDOrNull(JNIEnv* env, | 174 jmethodID GetMethodIDOrNull(JNIEnv* env, |
| 203 jclass clazz, | 175 jclass clazz, |
| 204 const char* method_name, | 176 const char* method_name, |
| 205 const char* jni_signature) { | 177 const char* jni_signature) { |
| 206 return GetMethodIDInternal<METHODTYPE_NORMAL, EXCEPTIONCHECK_NO>( | 178 return MethodID::Get<MethodID::METHODTYPE_NORMAL, |
| 179 MethodID::EXCEPTIONCHECK_NO>( |
| 207 env, clazz, method_name, jni_signature); | 180 env, clazz, method_name, jni_signature); |
| 208 } | 181 } |
| 209 | 182 |
| 210 jmethodID GetStaticMethodID(JNIEnv* env, | 183 jmethodID GetStaticMethodID(JNIEnv* env, |
| 211 const JavaRef<jclass>& clazz, | 184 const JavaRef<jclass>& clazz, |
| 212 const char* method_name, | 185 const char* method_name, |
| 213 const char* jni_signature) { | 186 const char* jni_signature) { |
| 214 return GetStaticMethodID(env, clazz.obj(), method_name, | 187 return GetStaticMethodID(env, clazz.obj(), method_name, |
| 215 jni_signature); | 188 jni_signature); |
| 216 } | 189 } |
| 217 | 190 |
| 218 jmethodID GetStaticMethodID(JNIEnv* env, | 191 jmethodID GetStaticMethodID(JNIEnv* env, |
| 219 jclass clazz, | 192 jclass clazz, |
| 220 const char* method_name, | 193 const char* method_name, |
| 221 const char* jni_signature) { | 194 const char* jni_signature) { |
| 222 return GetMethodIDInternal<METHODTYPE_STATIC, EXCEPTIONCHECK_YES>( | 195 return MethodID::Get<MethodID::METHODTYPE_STATIC, |
| 196 MethodID::EXCEPTIONCHECK_YES>( |
| 223 env, clazz, method_name, jni_signature); | 197 env, clazz, method_name, jni_signature); |
| 224 } | 198 } |
| 225 | 199 |
| 226 jmethodID GetStaticMethodIDOrNull(JNIEnv* env, | |
| 227 jclass clazz, | |
| 228 const char* method_name, | |
| 229 const char* jni_signature) { | |
| 230 return GetMethodIDInternal<METHODTYPE_STATIC, EXCEPTIONCHECK_NO>( | |
| 231 env, clazz, method_name, jni_signature); | |
| 232 } | |
| 233 | |
| 234 bool HasMethod(JNIEnv* env, | 200 bool HasMethod(JNIEnv* env, |
| 235 const JavaRef<jclass>& clazz, | 201 const JavaRef<jclass>& clazz, |
| 236 const char* method_name, | 202 const char* method_name, |
| 237 const char* jni_signature) { | 203 const char* jni_signature) { |
| 238 jmethodID method_id = | 204 jmethodID method_id = |
| 239 env->GetMethodID(clazz.obj(), method_name, jni_signature); | 205 env->GetMethodID(clazz.obj(), method_name, jni_signature); |
| 240 if (!method_id) { | 206 if (!method_id) { |
| 241 ClearException(env); | 207 ClearException(env); |
| 242 return false; | 208 return false; |
| 243 } | 209 } |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 // RVO should avoid any extra copies of the exception string. | 321 // RVO should avoid any extra copies of the exception string. |
| 356 base::android::BuildInfo::GetInstance()->set_java_exception_info( | 322 base::android::BuildInfo::GetInstance()->set_java_exception_info( |
| 357 GetJavaExceptionInfo(env, java_throwable)); | 323 GetJavaExceptionInfo(env, java_throwable)); |
| 358 | 324 |
| 359 // Now, feel good about it and die. | 325 // Now, feel good about it and die. |
| 360 CHECK(false); | 326 CHECK(false); |
| 361 } | 327 } |
| 362 | 328 |
| 363 } // namespace android | 329 } // namespace android |
| 364 } // namespace base | 330 } // namespace base |
| OLD | NEW |