Chromium Code Reviews| Index: base/android/jni_android.h |
| diff --git a/base/android/jni_android.h b/base/android/jni_android.h |
| index 492f11ff90bbcafbd70a8a8f659287eddd94d23a..69bd36c0ac74c087ce4f1930ff9f03f501103b07 100644 |
| --- a/base/android/jni_android.h |
| +++ b/base/android/jni_android.h |
| @@ -9,7 +9,9 @@ |
| #include <sys/types.h> |
| #include "base/android/scoped_java_ref.h" |
| +#include "base/atomicops.h" |
| #include "base/compiler_specific.h" |
| +#include "base/logging.h" |
| namespace base { |
| namespace android { |
| @@ -136,6 +138,46 @@ bool ClearException(JNIEnv* env); |
| // This function will call CHECK() macro if there's any pending exception. |
| void CheckException(JNIEnv* env); |
| +class LazyMethodID { |
| + public: |
| + enum MethodType { |
| + METHODTYPE_STATIC, |
| + METHODTYPE_NORMAL, |
| + }; |
| + |
| + enum ExceptionCheck { |
| + EXCEPTIONCHECK_YES, |
|
Sami
2012/10/02 15:14:05
Super-nit: NO=0, YES=1 would seem more logical.
bulach
2012/10/02 15:28:30
:) done..
|
| + EXCEPTIONCHECK_NO, |
| + }; |
| + |
| + template<MethodType method_type, ExceptionCheck exception_check> |
| + static void Get(JNIEnv* env, |
| + jclass clazz, |
| + const char* method_name, |
| + const char* jni_signature, |
| + jmethodID* method_id) { |
| + subtle::AtomicWord* atomic = reinterpret_cast<subtle::AtomicWord*>( |
|
Sami
2012/10/02 15:14:05
I'm not sure which is better for this use, but an
bulach
2012/10/02 15:28:30
hmm... we have base::internal::ThreadLocalPointer,
|
| + method_id); |
| + subtle::AtomicWord value = base::subtle::Acquire_Load(atomic); |
| + if (value) |
| + return; |
| + jmethodID id = method_type == METHODTYPE_STATIC ? |
| + env->GetStaticMethodID(clazz, method_name, jni_signature) : |
| + env->GetMethodID(clazz, method_name, jni_signature); |
| + if (exception_check == EXCEPTIONCHECK_YES) { |
| + CHECK(id || base::android::ClearException(env)) << |
| + "Failed to find " << |
| + (method_type == METHODTYPE_STATIC ? "static " : "") << |
| + "method " << method_name << " " << jni_signature; |
| + } else if (base::android::HasException(env)) { |
| + env->ExceptionClear(); |
| + } |
| + base::subtle::Acquire_Store( |
| + atomic, reinterpret_cast<subtle::AtomicWord>(id)); |
| + } |
| +}; |
| + |
| + |
| } // namespace android |
| } // namespace base |