Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: jni/Utils.h

Issue 6606493159784448: New JNI bindings (Closed)
Patch Set: Reuploaded full diff Created March 28, 2014, 3:56 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: jni/Utils.h
diff --git a/jni/Utils.h b/jni/Utils.h
index 92231d78b0d137afb1e4f671679cb2cdc4e349db..81714fb4a03fd7654186d1f14756bd9d84fc3c98 100644
--- a/jni/Utils.h
+++ b/jni/Utils.h
@@ -21,6 +21,161 @@
#include <string>
#include <algorithm>
#include <jni.h>
+#include <stdexcept>
+
+#include <AdblockPlus.h>
+#include <AdblockPlus/tr1_memory.h>
+
+#include "v8/v8stdint.h"
+
+#define PKG(x) "org/adblockplus/android/api/" x
Felix Dahlke 2014/03/28 17:28:41 PKG is fine, but I'd personally vote for "PACKAGE"
René Jeschke 2014/03/31 09:10:38 I prefer PKG because it's only 3 chars. As there a
Felix Dahlke 2014/03/31 10:43:23 OK, fine with having no prefix. Also fine with PKG
René Jeschke 2014/04/11 12:25:53 Done.
+#define TYP(x) "L" PKG(x) ";"
Felix Dahlke 2014/03/28 17:28:41 Correct me if I'm wrong, but it seems the proper n
René Jeschke 2014/03/31 09:10:38 That's not quite correct. The name of the class co
Felix Dahlke 2014/03/31 10:43:23 Na it's not, we should both be able to understand
René Jeschke 2014/04/11 12:25:53 Done.
+
+#define ABP_JNI_VERSION JNI_VERSION_1_6
+
+void JniThrowException(JNIEnv* env, const std::string& message);
+
+void JniThrowException(JNIEnv* env, const std::exception& e);
+
+void JniThrowException(JNIEnv* env);
+
+class JNIEnvAcquire
+{
+public:
+ JNIEnvAcquire(JavaVM* javaVM);
+ ~JNIEnvAcquire();
+
+ JNIEnv* operator*()
+ {
+ return jniEnv;
+ }
+
+ JNIEnv* operator->()
+ {
+ return jniEnv;
+ }
+
+private:
+ JavaVM* javaVM;
+ JNIEnv* jniEnv;
+ int attachmentStatus;
+};
+
+template<typename T>
+class JniGlobalReference
+{
+public:
+ JniGlobalReference(JNIEnv* env, T reference)
+ {
+ env->GetJavaVM(&javaVM);
+ reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(reference)));
+ }
+
+ ~JniGlobalReference()
+ {
+ JNIEnvAcquire env(javaVM);
+
+ env->DeleteGlobalRef(static_cast<jobject>(reference));
+ }
+
+ JniGlobalReference(const JniGlobalReference& other);
+ JniGlobalReference& operator=(const JniGlobalReference& other);
+
+ T get()
Felix Dahlke 2014/03/28 17:28:41 All functions should start with upper case letters
René Jeschke 2014/04/11 12:25:53 Done.
+ {
+ return reference;
+ }
+
+ typedef std::tr1::shared_ptr<JniGlobalReference<T> > Ptr;
+
+private:
+ T reference;
+ JavaVM* javaVM;
+};
+
+inline void* JniLongToPtr(jlong value)
+{
+ return reinterpret_cast<void*>((size_t)value);
+}
+
+inline jlong JniPtrToLong(void* ptr)
+{
+ return (jlong)reinterpret_cast<size_t>(ptr);
+}
+
+// TODO: It's feeling a bit dirty casting to shared_ptr<T: JsValue> directly, so maybe
+// implement a special cast functions that first reinterpret_casts to shared_ptr<JsValue> and then
+// dynamic_casts to shared_ptr<T: JsValue> ... also as the same inheritance is mirrored on the Java
+// side (and Java will throw a class cast exception on error) this shouldn't be an issue (TM)
+template<typename T>
+inline T* JniLongToTypePtr(jlong value)
+{
+ return reinterpret_cast<T*>((size_t)value);
+}
+
+jobject NewJniArrayList(JNIEnv* env);
+
+std::string JniJavaToStdString(JNIEnv* env, jstring str);
+
+void JniAddObjectToList(JNIEnv* env, jobject list, jobject value);
+
+inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, const char* name)
+{
+ return JniJavaToStdString(env, reinterpret_cast<jstring>(env->GetObjectField(jObj, env->GetFieldID(clazz, name, "Ljava/lang/String;"))));
+}
+
+inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const char* name)
+{
+ return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TRUE;
+}
+
+inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const char* name)
+{
+ return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I"));
+}
+
+inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const char* name)
+{
+ return (int64_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "J"));
+}
+
+inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter)
+{
+ jclass clazz = env->FindClass(PKG("Filter"));
+ jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
+ return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(filter)));
+}
+
+inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPtr& subscription)
+{
+ jclass clazz = env->FindClass(PKG("Subscription"));
+ jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
+ return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::SubscriptionPtr(subscription)));
+}
+
+#define TRY try
Felix Dahlke 2014/03/28 17:28:41 I believe we can live without this one :D
René Jeschke 2014/03/31 09:10:38 It is just for symmetry reasons as: try { // ...
Felix Dahlke 2014/03/31 10:43:23 Using "TRY" suggests that some magic is going on h
René Jeschke 2014/04/11 12:25:53 Done.
+
+#define CATCH_AND_THROW(jEnv) \
Felix Dahlke 2014/03/28 17:28:41 How about "CATCH_AND_RETHROW"?
René Jeschke 2014/03/31 09:10:38 Rethrow would imply that I would catch the C++ exc
Felix Dahlke 2014/03/31 10:43:23 Fair enough.
René Jeschke 2014/04/11 12:25:53 Done.
+ catch (const std::exception& except) \
+ { \
+ JniThrowException(jEnv, except); \
+ } \
+ catch (...) \
+ { \
+ JniThrowException(jEnv); \
+ }
+
+#define CATCH_THROW_AND_RETURN(jEnv, retVal) \
+ catch (const std::exception& except) \
+ { \
+ JniThrowException(jEnv, except); \
+ return retVal; \
+ } \
+ catch (...) \
+ { \
+ JniThrowException(jEnv); \
+ return retVal; \
+ }
const std::string GetString(JNIEnv *pEnv, jstring str);

Powered by Google App Engine
This is Rietveld