| 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 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 |
| 99 } // namespace | 129 } // namespace |
| 100 | 130 |
| 101 namespace base { | 131 namespace base { |
| 102 namespace android { | 132 namespace android { |
| 103 | 133 |
| 104 JNIEnv* AttachCurrentThread() { | 134 JNIEnv* AttachCurrentThread() { |
| 105 if (!g_jvm) | 135 if (!g_jvm) |
| 106 return NULL; | 136 return NULL; |
| 107 JNIEnv* env = NULL; | 137 JNIEnv* env = NULL; |
| 108 jint ret = g_jvm->AttachCurrentThread(&env, NULL); | 138 jint ret = g_jvm->AttachCurrentThread(&env, NULL); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 131 DCHECK(!g_application_context.Get().is_null()); | 161 DCHECK(!g_application_context.Get().is_null()); |
| 132 return g_application_context.Get().obj(); | 162 return g_application_context.Get().obj(); |
| 133 } | 163 } |
| 134 | 164 |
| 135 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name) { | 165 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name) { |
| 136 return ScopedJavaLocalRef<jclass>(env, GetUnscopedClass(env, class_name)); | 166 return ScopedJavaLocalRef<jclass>(env, GetUnscopedClass(env, class_name)); |
| 137 } | 167 } |
| 138 | 168 |
| 139 jclass GetUnscopedClass(JNIEnv* env, const char* class_name) { | 169 jclass GetUnscopedClass(JNIEnv* env, const char* class_name) { |
| 140 jclass clazz = env->FindClass(class_name); | 170 jclass clazz = env->FindClass(class_name); |
| 141 CHECK(clazz && !ClearException(env)) << "Failed to find class " << class_name; | 171 CHECK(!ClearException(env) && clazz) << "Failed to find class " << class_name; |
| 142 return clazz; | 172 return clazz; |
| 143 } | 173 } |
| 144 | 174 |
| 145 bool HasClass(JNIEnv* env, const char* class_name) { | 175 bool HasClass(JNIEnv* env, const char* class_name) { |
| 146 ScopedJavaLocalRef<jclass> clazz(env, env->FindClass(class_name)); | 176 ScopedJavaLocalRef<jclass> clazz(env, env->FindClass(class_name)); |
| 147 if (!clazz.obj()) { | 177 if (!clazz.obj()) { |
| 148 ClearException(env); | 178 ClearException(env); |
| 149 return false; | 179 return false; |
| 150 } | 180 } |
| 151 bool error = ClearException(env); | 181 bool error = ClearException(env); |
| 152 DCHECK(!error); | 182 DCHECK(!error); |
| 153 return true; | 183 return true; |
| 154 } | 184 } |
| 155 | 185 |
| 156 jmethodID GetMethodID(JNIEnv* env, | 186 jmethodID GetMethodID(JNIEnv* env, |
| 157 const JavaRef<jclass>& clazz, | 187 const JavaRef<jclass>& clazz, |
| 158 const char* method_name, | 188 const char* method_name, |
| 159 const char* jni_signature) { | 189 const char* jni_signature) { |
| 160 // clazz.env() can not be used as that may be from a different thread. | 190 // clazz.env() can not be used as that may be from a different thread. |
| 161 return GetMethodID(env, clazz.obj(), method_name, jni_signature); | 191 return GetMethodID(env, clazz.obj(), method_name, jni_signature); |
| 162 } | 192 } |
| 163 | 193 |
| 164 jmethodID GetMethodID(JNIEnv* env, | 194 jmethodID GetMethodID(JNIEnv* env, |
| 165 jclass clazz, | 195 jclass clazz, |
| 166 const char* method_name, | 196 const char* method_name, |
| 167 const char* jni_signature) { | 197 const char* jni_signature) { |
| 168 jmethodID method_id = | 198 return GetMethodIDInternal<METHODTYPE_NORMAL, EXCEPTIONCHECK_YES>( |
| 169 env->GetMethodID(clazz, method_name, jni_signature); | 199 env, clazz, method_name, jni_signature); |
| 170 CHECK(method_id && !ClearException(env)) << "Failed to find method " << | 200 } |
| 171 method_name << " " << jni_signature; | 201 |
| 172 return method_id; | 202 jmethodID GetMethodIDOrNull(JNIEnv* env, |
| 203 jclass clazz, |
| 204 const char* method_name, |
| 205 const char* jni_signature) { |
| 206 return GetMethodIDInternal<METHODTYPE_NORMAL, EXCEPTIONCHECK_NO>( |
| 207 env, clazz, method_name, jni_signature); |
| 173 } | 208 } |
| 174 | 209 |
| 175 jmethodID GetStaticMethodID(JNIEnv* env, | 210 jmethodID GetStaticMethodID(JNIEnv* env, |
| 176 const JavaRef<jclass>& clazz, | 211 const JavaRef<jclass>& clazz, |
| 177 const char* method_name, | 212 const char* method_name, |
| 178 const char* jni_signature) { | 213 const char* jni_signature) { |
| 179 return GetStaticMethodID(env, clazz.obj(), method_name, | 214 return GetStaticMethodID(env, clazz.obj(), method_name, |
| 180 jni_signature); | 215 jni_signature); |
| 181 } | 216 } |
| 182 | 217 |
| 183 jmethodID GetStaticMethodID(JNIEnv* env, | 218 jmethodID GetStaticMethodID(JNIEnv* env, |
| 184 jclass clazz, | 219 jclass clazz, |
| 185 const char* method_name, | 220 const char* method_name, |
| 186 const char* jni_signature) { | 221 const char* jni_signature) { |
| 187 jmethodID method_id = | 222 return GetMethodIDInternal<METHODTYPE_STATIC, EXCEPTIONCHECK_YES>( |
| 188 env->GetStaticMethodID(clazz, method_name, jni_signature); | 223 env, clazz, method_name, jni_signature); |
| 189 CHECK(method_id && !ClearException(env)) << "Failed to find static method " << | 224 } |
| 190 method_name << " " << jni_signature; | 225 |
| 191 return method_id; | 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); |
| 192 } | 232 } |
| 193 | 233 |
| 194 bool HasMethod(JNIEnv* env, | 234 bool HasMethod(JNIEnv* env, |
| 195 const JavaRef<jclass>& clazz, | 235 const JavaRef<jclass>& clazz, |
| 196 const char* method_name, | 236 const char* method_name, |
| 197 const char* jni_signature) { | 237 const char* jni_signature) { |
| 198 jmethodID method_id = | 238 jmethodID method_id = |
| 199 env->GetMethodID(clazz.obj(), method_name, jni_signature); | 239 env->GetMethodID(clazz.obj(), method_name, jni_signature); |
| 200 if (!method_id) { | 240 if (!method_id) { |
| 201 ClearException(env); | 241 ClearException(env); |
| 202 return false; | 242 return false; |
| 203 } | 243 } |
| 204 bool error = ClearException(env); | 244 bool error = ClearException(env); |
| 205 DCHECK(!error); | 245 DCHECK(!error); |
| 206 return true; | 246 return true; |
| 207 } | 247 } |
| 208 | 248 |
| 209 jfieldID GetFieldID(JNIEnv* env, | 249 jfieldID GetFieldID(JNIEnv* env, |
| 210 const JavaRef<jclass>& clazz, | 250 const JavaRef<jclass>& clazz, |
| 211 const char* field_name, | 251 const char* field_name, |
| 212 const char* jni_signature) { | 252 const char* jni_signature) { |
| 213 jfieldID field_id = env->GetFieldID(clazz.obj(), field_name, jni_signature); | 253 jfieldID field_id = env->GetFieldID(clazz.obj(), field_name, jni_signature); |
| 214 CHECK(field_id && !ClearException(env)) << "Failed to find field " << | 254 CHECK(!ClearException(env) && field_id) << "Failed to find field " << |
| 215 field_name << " " << jni_signature; | 255 field_name << " " << jni_signature; |
| 216 bool error = ClearException(env); | |
| 217 DCHECK(!error); | |
| 218 return field_id; | 256 return field_id; |
| 219 } | 257 } |
| 220 | 258 |
| 221 bool HasField(JNIEnv* env, | 259 bool HasField(JNIEnv* env, |
| 222 const JavaRef<jclass>& clazz, | 260 const JavaRef<jclass>& clazz, |
| 223 const char* field_name, | 261 const char* field_name, |
| 224 const char* jni_signature) { | 262 const char* jni_signature) { |
| 225 jfieldID field_id = env->GetFieldID(clazz.obj(), field_name, jni_signature); | 263 jfieldID field_id = env->GetFieldID(clazz.obj(), field_name, jni_signature); |
| 226 if (!field_id) { | 264 if (!field_id) { |
| 227 ClearException(env); | 265 ClearException(env); |
| 228 return false; | 266 return false; |
| 229 } | 267 } |
| 230 bool error = ClearException(env); | 268 bool error = ClearException(env); |
| 231 DCHECK(!error); | 269 DCHECK(!error); |
| 232 return true; | 270 return true; |
| 233 } | 271 } |
| 234 | 272 |
| 235 jfieldID GetStaticFieldID(JNIEnv* env, | 273 jfieldID GetStaticFieldID(JNIEnv* env, |
| 236 const JavaRef<jclass>& clazz, | 274 const JavaRef<jclass>& clazz, |
| 237 const char* field_name, | 275 const char* field_name, |
| 238 const char* jni_signature) { | 276 const char* jni_signature) { |
| 239 jfieldID field_id = | 277 jfieldID field_id = |
| 240 env->GetStaticFieldID(clazz.obj(), field_name, jni_signature); | 278 env->GetStaticFieldID(clazz.obj(), field_name, jni_signature); |
| 241 CHECK(field_id && !ClearException(env)) << "Failed to find static field " << | 279 CHECK(!ClearException(env) && field_id) << "Failed to find static field " << |
| 242 field_name << " " << jni_signature; | 280 field_name << " " << jni_signature; |
| 243 bool error = ClearException(env); | |
| 244 DCHECK(!error); | |
| 245 return field_id; | 281 return field_id; |
| 246 } | 282 } |
| 247 | 283 |
| 248 jmethodID GetMethodIDFromClassName(JNIEnv* env, | 284 jmethodID GetMethodIDFromClassName(JNIEnv* env, |
| 249 const char* class_name, | 285 const char* class_name, |
| 250 const char* method, | 286 const char* method, |
| 251 const char* jni_signature) { | 287 const char* jni_signature) { |
| 252 MethodIdentifier key; | 288 MethodIdentifier key; |
| 253 key.class_name = class_name; | 289 key.class_name = class_name; |
| 254 key.method = method; | 290 key.method = method; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 // RVO should avoid any extra copies of the exception string. | 355 // RVO should avoid any extra copies of the exception string. |
| 320 base::android::BuildInfo::GetInstance()->set_java_exception_info( | 356 base::android::BuildInfo::GetInstance()->set_java_exception_info( |
| 321 GetJavaExceptionInfo(env, java_throwable)); | 357 GetJavaExceptionInfo(env, java_throwable)); |
| 322 | 358 |
| 323 // Now, feel good about it and die. | 359 // Now, feel good about it and die. |
| 324 CHECK(false); | 360 CHECK(false); |
| 325 } | 361 } |
| 326 | 362 |
| 327 } // namespace android | 363 } // namespace android |
| 328 } // namespace base | 364 } // namespace base |
| OLD | NEW |