Bug 1286663 - Add JNI wrapper class for ByteBuffer; r=snorp
authorJim Chen <nchen@mozilla.com>
Thu, 21 Jul 2016 13:49:04 -0400
changeset 346235 c8a8de530e654f7cd0f83d0057bdb19b83cf8c24
parent 346234 f35cc482a71b0692778d0961b148c4e43aee277e
child 346236 a9946d087a152d5bface0783a73c5717cfaa2075
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1286663
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1286663 - Add JNI wrapper class for ByteBuffer; r=snorp Add a wrapper class for ByteBuffer and its associated calls in JNI.
build/annotationProcessors/utils/Utils.java
widget/android/jni/Refs.h
widget/android/jni/Utils.cpp
--- a/build/annotationProcessors/utils/Utils.java
+++ b/build/annotationProcessors/utils/Utils.java
@@ -7,16 +7,17 @@ package org.mozilla.gecko.annotationProc
 import org.mozilla.gecko.annotationProcessors.AnnotationInfo;
 
 import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Member;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.nio.ByteBuffer;
 import java.util.HashMap;
 
 /**
  * A collection of utility methods used by CodeGenerator. Largely used for translating types.
  */
 public class Utils {
 
     // A collection of lookup tables to simplify the functions to follow...
@@ -91,16 +92,20 @@ public class Utils {
             // to C, generating the corresponding code using this Java program. Really?!
             return "mozilla::jni::Class::Param";
         }
 
         if (type.equals(Throwable.class)) {
             return "mozilla::jni::Throwable::Param";
         }
 
+        if (type.equals(ByteBuffer.class)) {
+            return "mozilla::jni::ByteBuffer::Param";
+        }
+
         return "mozilla::jni::Object::Param";
     }
 
     public static String getNativeReturnType(Class<?> type, AnnotationInfo info) {
         final String name = type.getName().replace('.', '/');
 
         if (NATIVE_TYPES.containsKey(name)) {
             return NATIVE_TYPES.get(name);
@@ -123,16 +128,20 @@ public class Utils {
             // to C, generating the corresponding code using this Java program. Really?!
             return "mozilla::jni::Class::LocalRef";
         }
 
         if (type.equals(Throwable.class)) {
             return "mozilla::jni::Throwable::LocalRef";
         }
 
+        if (type.equals(ByteBuffer.class)) {
+            return "mozilla::jni::ByteBuffer::LocalRef";
+        }
+
         return "mozilla::jni::Object::LocalRef";
     }
 
     /**
      * Get the JNI class descriptor corresponding to the provided type parameter.
      *
      * @param type Class to determine the corresponding JNI descriptor for.
      * @return Class descripor as a String
--- a/widget/android/jni/Refs.h
+++ b/widget/android/jni/Refs.h
@@ -770,16 +770,48 @@ DEFINE_PRIMITIVE_ARRAY_REF(jshortArray, 
 DEFINE_PRIMITIVE_ARRAY_REF(jintArray,     int32_t);
 DEFINE_PRIMITIVE_ARRAY_REF(jlongArray,    int64_t);
 DEFINE_PRIMITIVE_ARRAY_REF(jfloatArray,   float);
 DEFINE_PRIMITIVE_ARRAY_REF(jdoubleArray,  double);
 
 #undef DEFINE_PRIMITIVE_ARRAY_REF
 
 
+class ByteBuffer : public ObjectBase<ByteBuffer, jobject>
+{
+public:
+    explicit ByteBuffer(const Context& ctx)
+        : ObjectBase<ByteBuffer, jobject>(ctx)
+    {}
+
+    static LocalRef New(void* data, size_t capacity)
+    {
+        JNIEnv* const env = GetEnvForThread();
+        const auto ret = LocalRef::Adopt(
+                env, env->NewDirectByteBuffer(data, jlong(capacity)));
+        MOZ_CATCH_JNI_EXCEPTION(env);
+        return ret;
+    }
+
+    void* Address()
+    {
+        void* const ret = Env()->GetDirectBufferAddress(Instance());
+        MOZ_CATCH_JNI_EXCEPTION(Env());
+        return ret;
+    }
+
+    size_t Capacity()
+    {
+        const size_t ret = size_t(Env()->GetDirectBufferCapacity(Instance()));
+        MOZ_CATCH_JNI_EXCEPTION(Env());
+        return ret;
+    }
+};
+
+
 template<>
 class TypedObject<jobjectArray>
     : public ObjectBase<TypedObject<jobjectArray>, jobjectArray>
 {
     using Base = ObjectBase<TypedObject<jobjectArray>, jobjectArray>;
 
 public:
     explicit TypedObject(const Context& ctx) : Base(ctx) {}
--- a/widget/android/jni/Utils.cpp
+++ b/widget/android/jni/Utils.cpp
@@ -55,16 +55,17 @@ template<> const char ObjectBase<TypedOb
 template<> const char ObjectBase<TypedObject<jbyteArray>, jbyteArray>::name[] = "[B";
 template<> const char ObjectBase<TypedObject<jcharArray>, jcharArray>::name[] = "[C";
 template<> const char ObjectBase<TypedObject<jshortArray>, jshortArray>::name[] = "[S";
 template<> const char ObjectBase<TypedObject<jintArray>, jintArray>::name[] = "[I";
 template<> const char ObjectBase<TypedObject<jlongArray>, jlongArray>::name[] = "[J";
 template<> const char ObjectBase<TypedObject<jfloatArray>, jfloatArray>::name[] = "[F";
 template<> const char ObjectBase<TypedObject<jdoubleArray>, jdoubleArray>::name[] = "[D";
 template<> const char ObjectBase<TypedObject<jobjectArray>, jobjectArray>::name[] = "[Ljava/lang/Object;";
+template<> const char ObjectBase<ByteBuffer, jobject>::name[] = "java/nio/ByteBuffer";
 
 
 JNIEnv* sGeckoThreadEnv;
 
 namespace {
 
 JavaVM* sJavaVM;
 pthread_key_t sThreadEnvKey;