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(method_id || base::android::ClearException(env)) << | |
joth
2012/10/02 17:12:56
every where else in this file we have
CHECK(some
bulach
2012/10/02 17:25:40
sounds reasonable. done.
| |
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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
319 // RVO should avoid any extra copies of the exception string. | 359 // RVO should avoid any extra copies of the exception string. |
320 base::android::BuildInfo::GetInstance()->set_java_exception_info( | 360 base::android::BuildInfo::GetInstance()->set_java_exception_info( |
321 GetJavaExceptionInfo(env, java_throwable)); | 361 GetJavaExceptionInfo(env, java_throwable)); |
322 | 362 |
323 // Now, feel good about it and die. | 363 // Now, feel good about it and die. |
324 CHECK(false); | 364 CHECK(false); |
325 } | 365 } |
326 | 366 |
327 } // namespace android | 367 } // namespace android |
328 } // namespace base | 368 } // namespace base |
OLD | NEW |