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

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: Tidy up + tests 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') | base/android/jni_android_unittest.cc » ('J')
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"
joth 2012/10/02 17:47:43 rather than pull this spew into the header, can yo
bulach 2012/10/03 13:09:00 see above: I split this into its own file, let me
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 {
joth 2012/10/02 17:47:43 needs class (function) doc - when to use - how to
bulach 2012/10/03 13:09:00 Done.
+ public:
+ enum MethodType {
+ METHODTYPE_STATIC,
+ METHODTYPE_NORMAL,
+ };
+
+ enum ExceptionCheck {
+ EXCEPTIONCHECK_YES,
+ EXCEPTIONCHECK_NO,
+ };
joth 2012/10/02 17:47:43 enum duplication from the previous patch feels fin
bulach 2012/10/02 18:20:42 sure, as soon as the other patch lands :) fyi, I d
+
+ template<MethodType method_type, ExceptionCheck exception_check>
+ static void Get(JNIEnv* env,
joth 2012/10/02 17:47:43 why is this in a class?
bulach 2012/10/03 13:09:00 we already have a bunch of free functions here, an
+ jclass clazz,
+ const char* method_name,
+ const char* jni_signature,
+ jmethodID* method_id) {
joth 2012/10/02 17:47:43 method_id should be passed in & out as a AtomicWor
bulach 2012/10/02 18:20:42 sorry, not sure I understood.. the caller has "sta
joth 2012/10/02 18:30:39 Neither! :-) Calling code: static AtomicWord g_
bulach 2012/10/03 13:09:00 Done.
+ subtle::AtomicWord* atomic = reinterpret_cast<subtle::AtomicWord*>(
+ method_id);
joth 2012/10/02 17:47:43 nit: could compile assert that sizeof(AtomicWord)
bulach 2012/10/03 13:09:00 Done.
+ subtle::AtomicWord value = base::subtle::Acquire_Load(atomic);
+ if (value)
+ return;
joth 2012/10/02 17:47:43 need to document on the function that the caller i
bulach 2012/10/03 13:09:00 Done.
+ jmethodID id = method_type == METHODTYPE_STATIC ?
joth 2012/10/02 17:47:43 comment that if we get a race in loading the value
bulach 2012/10/03 13:09:00 Done.
+ env->GetStaticMethodID(clazz, method_name, jni_signature) :
joth 2012/10/02 17:47:43 as |env| is only need in uncommon case, we could c
bulach 2012/10/03 13:09:00 so far, the caller seems to always have one.
+ 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();
joth 2012/10/02 17:47:43 can we call an existing GetMethodID blah class her
bulach 2012/10/03 13:09:00 see above.. I'd prefer to move towards this api an
+ }
+ base::subtle::Acquire_Store(
joth 2012/10/02 17:47:43 This needs to be a Release_Store
bulach 2012/10/03 13:09:00 Done.
+ 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') | base/android/jni_android_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698