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" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 printstream.obj()); | 89 printstream.obj()); |
| 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 | |
| 100 enum MethodType { | |
| 101 METHODTYPE_STATIC, | |
| 102 METHODTYPE_NORMAL, | |
| 103 }; | |
| 104 | |
| 105 enum ExceptionCheck { | |
| 106 EXCEPTIIONCHECK_YES, | |
| 107 EXCEPTIIONCHECK_NO, | |
| 108 }; | |
| 109 | |
| 110 jmethodID GetMethodIDInternal(JNIEnv* env, | |
| 111 jclass clazz, | |
| 112 const char* method_name, | |
| 113 const char* jni_signature, | |
| 114 MethodType method_type, | |
| 115 ExceptionCheck exception_check) { | |
|
joth
2012/10/01 16:15:27
for bonus points these enums could be template arg
bulach
2012/10/01 17:25:00
would that make any of this, or the caller code, c
joth
2012/10/01 17:57:19
Possibly. It clearly communicates which params we
| |
| 116 jmethodID method_id = method_type == METHODTYPE_STATIC ? | |
| 117 env->GetMethodID(clazz, method_name, jni_signature) : | |
| 118 env->GetStaticMethodID(clazz, method_name, jni_signature); | |
| 119 if (exception_check == EXCEPTIIONCHECK_YES) { | |
| 120 CHECK(method_id && base::android::ClearException(env)) << | |
| 121 "Failed to find " << | |
| 122 (method_type == METHODTYPE_STATIC ? " static " : " ") << | |
|
joth
2012/10/01 16:15:27
uber-nit: you don't need the space before 'static'
bulach
2012/10/01 17:25:00
Done.
| |
| 123 "method " << method_name << " " << jni_signature; | |
| 124 } else if (base::android::HasException(env)) { | |
| 125 env->ExceptionClear(); | |
| 126 } | |
| 127 return method_id; | |
| 128 } | |
| 129 | |
| 99 } // namespace | 130 } // namespace |
| 100 | 131 |
| 101 namespace base { | 132 namespace base { |
| 102 namespace android { | 133 namespace android { |
| 103 | 134 |
| 104 JNIEnv* AttachCurrentThread() { | 135 JNIEnv* AttachCurrentThread() { |
| 105 if (!g_jvm) | 136 if (!g_jvm) |
| 106 return NULL; | 137 return NULL; |
| 107 JNIEnv* env = NULL; | 138 JNIEnv* env = NULL; |
| 108 jint ret = g_jvm->AttachCurrentThread(&env, NULL); | 139 jint ret = g_jvm->AttachCurrentThread(&env, NULL); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 const char* method_name, | 189 const char* method_name, |
| 159 const char* jni_signature) { | 190 const char* jni_signature) { |
| 160 // clazz.env() can not be used as that may be from a different thread. | 191 // clazz.env() can not be used as that may be from a different thread. |
| 161 return GetMethodID(env, clazz.obj(), method_name, jni_signature); | 192 return GetMethodID(env, clazz.obj(), method_name, jni_signature); |
| 162 } | 193 } |
| 163 | 194 |
| 164 jmethodID GetMethodID(JNIEnv* env, | 195 jmethodID GetMethodID(JNIEnv* env, |
| 165 jclass clazz, | 196 jclass clazz, |
| 166 const char* method_name, | 197 const char* method_name, |
| 167 const char* jni_signature) { | 198 const char* jni_signature) { |
| 168 jmethodID method_id = | 199 return GetMethodIDInternal( |
| 169 env->GetMethodID(clazz, method_name, jni_signature); | 200 env, clazz, method_name, jni_signature, |
| 170 CHECK(method_id && !ClearException(env)) << "Failed to find method " << | 201 METHODTYPE_NORMAL, EXCEPTIIONCHECK_YES); |
| 171 method_name << " " << jni_signature; | 202 } |
| 172 return method_id; | 203 |
| 204 jmethodID GetMethodIDOrNull(JNIEnv* env, | |
| 205 jclass clazz, | |
| 206 const char* method_name, | |
| 207 const char* jni_signature) { | |
| 208 return GetMethodIDInternal( | |
| 209 env, clazz, method_name, jni_signature, | |
| 210 METHODTYPE_NORMAL, EXCEPTIIONCHECK_NO); | |
| 173 } | 211 } |
| 174 | 212 |
| 175 jmethodID GetStaticMethodID(JNIEnv* env, | 213 jmethodID GetStaticMethodID(JNIEnv* env, |
| 176 const JavaRef<jclass>& clazz, | 214 const JavaRef<jclass>& clazz, |
| 177 const char* method_name, | 215 const char* method_name, |
| 178 const char* jni_signature) { | 216 const char* jni_signature) { |
| 179 return GetStaticMethodID(env, clazz.obj(), method_name, | 217 return GetStaticMethodID(env, clazz.obj(), method_name, |
| 180 jni_signature); | 218 jni_signature); |
| 181 } | 219 } |
| 182 | 220 |
| 183 jmethodID GetStaticMethodID(JNIEnv* env, | 221 jmethodID GetStaticMethodID(JNIEnv* env, |
| 184 jclass clazz, | 222 jclass clazz, |
| 185 const char* method_name, | 223 const char* method_name, |
| 186 const char* jni_signature) { | 224 const char* jni_signature) { |
| 187 jmethodID method_id = | 225 return GetMethodIDInternal( |
| 188 env->GetStaticMethodID(clazz, method_name, jni_signature); | 226 env, clazz, method_name, jni_signature, |
| 189 CHECK(method_id && !ClearException(env)) << "Failed to find static method " << | 227 METHODTYPE_STATIC, EXCEPTIIONCHECK_YES); |
| 190 method_name << " " << jni_signature; | 228 } |
| 191 return method_id; | 229 |
| 230 jmethodID GetStaticMethodIDOrNull(JNIEnv* env, | |
| 231 jclass clazz, | |
| 232 const char* method_name, | |
| 233 const char* jni_signature) { | |
| 234 return GetMethodIDInternal( | |
| 235 env, clazz, method_name, jni_signature, | |
| 236 METHODTYPE_STATIC, EXCEPTIIONCHECK_NO); | |
| 192 } | 237 } |
| 193 | 238 |
| 194 bool HasMethod(JNIEnv* env, | 239 bool HasMethod(JNIEnv* env, |
| 195 const JavaRef<jclass>& clazz, | 240 const JavaRef<jclass>& clazz, |
| 196 const char* method_name, | 241 const char* method_name, |
| 197 const char* jni_signature) { | 242 const char* jni_signature) { |
| 198 jmethodID method_id = | 243 jmethodID method_id = |
| 199 env->GetMethodID(clazz.obj(), method_name, jni_signature); | 244 env->GetMethodID(clazz.obj(), method_name, jni_signature); |
| 200 if (!method_id) { | 245 if (!method_id) { |
| 201 ClearException(env); | 246 ClearException(env); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 // RVO should avoid any extra copies of the exception string. | 364 // RVO should avoid any extra copies of the exception string. |
| 320 base::android::BuildInfo::GetInstance()->set_java_exception_info( | 365 base::android::BuildInfo::GetInstance()->set_java_exception_info( |
| 321 GetJavaExceptionInfo(env, java_throwable)); | 366 GetJavaExceptionInfo(env, java_throwable)); |
| 322 | 367 |
| 323 // Now, feel good about it and die. | 368 // Now, feel good about it and die. |
| 324 CHECK(false); | 369 CHECK(false); |
| 325 } | 370 } |
| 326 | 371 |
| 327 } // namespace android | 372 } // namespace android |
| 328 } // namespace base | 373 } // namespace base |
| OLD | NEW |