summaryrefslogtreecommitdiffstats
path: root/base/android
diff options
context:
space:
mode:
authorsteveblock@chromium.org <steveblock@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-11 17:08:35 +0000
committersteveblock@chromium.org <steveblock@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-11 17:08:35 +0000
commitebed17c7823a01b70934460d2cbe0fec70261a41 (patch)
treefe6a0e5e62f5e6a02a1f8dc07abc9dd6af9fbe79 /base/android
parent3826a8a0942fd634616d009a45f5eafb7ee567c6 (diff)
downloadchromium_src-ebed17c7823a01b70934460d2cbe0fec70261a41.zip
chromium_src-ebed17c7823a01b70934460d2cbe0fec70261a41.tar.gz
chromium_src-ebed17c7823a01b70934460d2cbe0fec70261a41.tar.bz2
Add JavaBoundObject
This represents a Java object for use in the Java bridge. It handles all of the JNI required to interrogate the Java object and call methods on it. Also adds some JNI utility methods to simpify the new code. BUG=96703 Review URL: http://codereview.chromium.org/8509019 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@109645 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/android')
-rw-r--r--base/android/jni_android.cc17
-rw-r--r--base/android/jni_android.h25
2 files changed, 40 insertions, 2 deletions
diff --git a/base/android/jni_android.cc b/base/android/jni_android.cc
index 1dc02af..e6d6e56 100644
--- a/base/android/jni_android.cc
+++ b/base/android/jni_android.cc
@@ -4,6 +4,7 @@
#include "base/android/jni_android.h"
+#include "base/android/scoped_java_ref.h"
#include "base/logging.h"
namespace {
@@ -46,6 +47,12 @@ jobject GetApplicationContext() {
return g_application_context;
}
+MethodID::MethodID(JNIEnv* env, const char* class_name, const char* method,
+ const char* jni_signature) {
+ ScopedJavaLocalRef<jclass> clazz(env, env->FindClass(class_name));
+ id_ = GetMethodID(env, clazz.obj(), method, jni_signature);
+}
+
jmethodID GetMethodID(JNIEnv* env,
jclass clazz,
const char* const method,
@@ -66,6 +73,16 @@ jmethodID GetStaticMethodID(JNIEnv* env,
return id;
}
+jfieldID GetFieldID(JNIEnv* env,
+ jclass clazz,
+ const char* field,
+ const char* jni_signature) {
+ jfieldID id = env->GetFieldID(clazz, field, jni_signature);
+ DCHECK(id) << field;
+ CheckException(env);
+ return id;
+}
+
bool CheckException(JNIEnv* env) {
if (env->ExceptionCheck() == JNI_FALSE)
return false;
diff --git a/base/android/jni_android.h b/base/android/jni_android.h
index 66b3134..462173f 100644
--- a/base/android/jni_android.h
+++ b/base/android/jni_android.h
@@ -30,6 +30,20 @@ void InitApplicationContext(jobject context);
// Returns the application context assigned by InitApplicationContext().
jobject GetApplicationContext();
+// Wraps a method ID.
+class MethodID {
+ public:
+ jmethodID id() { return id_; }
+ protected:
+ // Gets the method ID from the class name. Clears the pending Java exception
+ // and returns NULL if the method is not found. Note that GetMethodID() below
+ // avoids a class lookup, so should be used in preference when possible.
+ MethodID(JNIEnv* env, const char* class_name, const char* method,
+ const char* jni_signature);
+ private:
+ jmethodID id_;
+};
+
// Get the method ID for a method. Will clear the pending Java
// exception and return 0 if the method is not found.
jmethodID GetMethodID(JNIEnv* env,
@@ -44,8 +58,15 @@ jmethodID GetStaticMethodID(JNIEnv* env,
const char* const method,
const char* const jni_signature);
-// Returns true if an exception is pending in the provided JNIEnv*.
-// If an exception is pending, it is printed.
+// Gets the field ID for a class field. Clears the pending Java exception and
+// returns NULL if the field is not found.
+jfieldID GetFieldID(JNIEnv* env,
+ jclass clazz,
+ const char* field,
+ const char* jni_signature);
+
+// Returns true if an exception is pending in the provided JNIEnv*. If an
+// exception is pending, this function prints and then clears it.
bool CheckException(JNIEnv* env);
} // namespace android