Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3681)

Unified Diff: base/android/jni_android.h

Issue 11038015: Android: lazy initialization for method id. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/android/jni_android_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « no previous file | base/android/jni_android_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698