Chromium Code Reviews| 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 #ifndef BASE_ANDROID_JNI_ANDROID_H_ | 5 #ifndef BASE_ANDROID_JNI_ANDROID_H_ |
| 6 #define BASE_ANDROID_JNI_ANDROID_H_ | 6 #define BASE_ANDROID_JNI_ANDROID_H_ |
| 7 | 7 |
| 8 #include <jni.h> | 8 #include <jni.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 | 10 |
| 11 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/atomicops.h" | |
| 12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #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
| |
| 13 | 15 |
| 14 namespace base { | 16 namespace base { |
| 15 namespace android { | 17 namespace android { |
| 16 | 18 |
| 17 // Used to mark symbols to be exported in a shared library's symbol table. | 19 // Used to mark symbols to be exported in a shared library's symbol table. |
| 18 #define JNI_EXPORT __attribute__ ((visibility("default"))) | 20 #define JNI_EXPORT __attribute__ ((visibility("default"))) |
| 19 | 21 |
| 20 // Contains the registration method information for initializing JNI bindings. | 22 // Contains the registration method information for initializing JNI bindings. |
| 21 struct RegistrationMethod { | 23 struct RegistrationMethod { |
| 22 const char* name; | 24 const char* name; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 // Returns true if an exception is pending in the provided JNIEnv*. | 131 // Returns true if an exception is pending in the provided JNIEnv*. |
| 130 bool HasException(JNIEnv* env); | 132 bool HasException(JNIEnv* env); |
| 131 | 133 |
| 132 // If an exception is pending in the provided JNIEnv*, this function clears it | 134 // If an exception is pending in the provided JNIEnv*, this function clears it |
| 133 // and returns true. | 135 // and returns true. |
| 134 bool ClearException(JNIEnv* env); | 136 bool ClearException(JNIEnv* env); |
| 135 | 137 |
| 136 // This function will call CHECK() macro if there's any pending exception. | 138 // This function will call CHECK() macro if there's any pending exception. |
| 137 void CheckException(JNIEnv* env); | 139 void CheckException(JNIEnv* env); |
| 138 | 140 |
| 141 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.
| |
| 142 public: | |
| 143 enum MethodType { | |
| 144 METHODTYPE_STATIC, | |
| 145 METHODTYPE_NORMAL, | |
| 146 }; | |
| 147 | |
| 148 enum ExceptionCheck { | |
| 149 EXCEPTIONCHECK_YES, | |
| 150 EXCEPTIONCHECK_NO, | |
| 151 }; | |
|
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
| |
| 152 | |
| 153 template<MethodType method_type, ExceptionCheck exception_check> | |
| 154 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
| |
| 155 jclass clazz, | |
| 156 const char* method_name, | |
| 157 const char* jni_signature, | |
| 158 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.
| |
| 159 subtle::AtomicWord* atomic = reinterpret_cast<subtle::AtomicWord*>( | |
| 160 method_id); | |
|
joth
2012/10/02 17:47:43
nit: could compile assert that sizeof(AtomicWord)
bulach
2012/10/03 13:09:00
Done.
| |
| 161 subtle::AtomicWord value = base::subtle::Acquire_Load(atomic); | |
| 162 if (value) | |
| 163 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.
| |
| 164 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.
| |
| 165 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.
| |
| 166 env->GetMethodID(clazz, method_name, jni_signature); | |
| 167 if (exception_check == EXCEPTIONCHECK_YES) { | |
| 168 CHECK(id || base::android::ClearException(env)) << | |
| 169 "Failed to find " << | |
| 170 (method_type == METHODTYPE_STATIC ? "static " : "") << | |
| 171 "method " << method_name << " " << jni_signature; | |
| 172 } else if (base::android::HasException(env)) { | |
| 173 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
| |
| 174 } | |
| 175 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.
| |
| 176 atomic, reinterpret_cast<subtle::AtomicWord>(id)); | |
| 177 } | |
| 178 }; | |
| 179 | |
| 180 | |
| 139 } // namespace android | 181 } // namespace android |
| 140 } // namespace base | 182 } // namespace base |
| 141 | 183 |
| 142 #endif // BASE_ANDROID_JNI_ANDROID_H_ | 184 #endif // BASE_ANDROID_JNI_ANDROID_H_ |
| OLD | NEW |