mozglue/android/nsGeckoUtils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mozglue/android/nsGeckoUtils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,126 @@
     1.4 +/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <jni.h>
    1.10 +
    1.11 +#include <stdlib.h>
    1.12 +#include <fcntl.h>
    1.13 +#include "APKOpen.h"
    1.14 +#include "Zip.h"
    1.15 +#include "mozilla/RefPtr.h"
    1.16 +
    1.17 +extern "C"
    1.18 +__attribute__ ((visibility("default")))
    1.19 +void JNICALL
    1.20 +Java_org_mozilla_gecko_mozglue_GeckoLoader_putenv(JNIEnv *jenv, jclass, jstring map)
    1.21 +{
    1.22 +    const char* str;
    1.23 +    // XXX: java doesn't give us true UTF8, we should figure out something
    1.24 +    // better to do here
    1.25 +    str = jenv->GetStringUTFChars(map, nullptr);
    1.26 +    if (str == nullptr)
    1.27 +        return;
    1.28 +    putenv(strdup(str));
    1.29 +    jenv->ReleaseStringUTFChars(map, str);
    1.30 +}
    1.31 +
    1.32 +extern "C"
    1.33 +__attribute__ ((visibility("default")))
    1.34 +jobject JNICALL
    1.35 +Java_org_mozilla_gecko_mozglue_DirectBufferAllocator_nativeAllocateDirectBuffer(JNIEnv *jenv, jclass, jlong size)
    1.36 +{
    1.37 +    jobject buffer = nullptr;
    1.38 +    void* mem = malloc(size);
    1.39 +    if (mem) {
    1.40 +        buffer = jenv->NewDirectByteBuffer(mem, size);
    1.41 +        if (!buffer)
    1.42 +            free(mem);
    1.43 +    }
    1.44 +    return buffer;
    1.45 +}
    1.46 +
    1.47 +extern "C"
    1.48 +__attribute__ ((visibility("default")))
    1.49 +void JNICALL
    1.50 +Java_org_mozilla_gecko_mozglue_DirectBufferAllocator_nativeFreeDirectBuffer(JNIEnv *jenv, jclass, jobject buf)
    1.51 +{
    1.52 +    free(jenv->GetDirectBufferAddress(buf));
    1.53 +}
    1.54 +
    1.55 +extern "C"
    1.56 +__attribute__ ((visibility("default")))
    1.57 +jlong JNICALL
    1.58 +Java_org_mozilla_gecko_mozglue_NativeZip_getZip(JNIEnv *jenv, jclass, jstring path)
    1.59 +{
    1.60 +    const char* str;
    1.61 +    str = jenv->GetStringUTFChars(path, nullptr);
    1.62 +    if (!str || !*str) {
    1.63 +        if (str)
    1.64 +            jenv->ReleaseStringUTFChars(path, str);
    1.65 +        JNI_Throw(jenv, "java/lang/IllegalArgumentException", "Invalid path");
    1.66 +        return 0;
    1.67 +    }
    1.68 +    mozilla::RefPtr<Zip> zip = ZipCollection::GetZip(str);
    1.69 +    jenv->ReleaseStringUTFChars(path, str);
    1.70 +    if (!zip) {
    1.71 +        JNI_Throw(jenv, "java/lang/IllegalArgumentException", "Invalid path or invalid zip");
    1.72 +        return 0;
    1.73 +    }
    1.74 +    zip->AddRef();
    1.75 +    return (jlong) zip.get();
    1.76 +}
    1.77 +
    1.78 +extern "C"
    1.79 +__attribute__ ((visibility("default")))
    1.80 +jlong JNICALL
    1.81 +Java_org_mozilla_gecko_mozglue_NativeZip_getZipFromByteBuffer(JNIEnv *jenv, jclass, jobject buffer)
    1.82 +{
    1.83 +    void *buf = jenv->GetDirectBufferAddress(buffer);
    1.84 +    size_t size = jenv->GetDirectBufferCapacity(buffer);
    1.85 +    mozilla::RefPtr<Zip> zip = Zip::Create(buf, size);
    1.86 +    if (!zip) {
    1.87 +        JNI_Throw(jenv, "java/lang/IllegalArgumentException", "Invalid zip");
    1.88 +        return 0;
    1.89 +    }
    1.90 +    zip->AddRef();
    1.91 +    return (jlong) zip.get();
    1.92 +}
    1.93 +
    1.94 + extern "C"
    1.95 +__attribute__ ((visibility("default")))
    1.96 +void JNICALL
    1.97 +Java_org_mozilla_gecko_mozglue_NativeZip__1release(JNIEnv *jenv, jclass, jlong obj)
    1.98 +{
    1.99 +    Zip *zip = (Zip *)obj;
   1.100 +    zip->Release();
   1.101 +}
   1.102 +
   1.103 +extern "C"
   1.104 +__attribute__ ((visibility("default")))
   1.105 +jobject JNICALL
   1.106 +Java_org_mozilla_gecko_mozglue_NativeZip__1getInputStream(JNIEnv *jenv, jobject jzip, jlong obj, jstring path)
   1.107 +{
   1.108 +    Zip *zip = (Zip *)obj;
   1.109 +    const char* str;
   1.110 +    str = jenv->GetStringUTFChars(path, nullptr);
   1.111 +
   1.112 +    Zip::Stream stream;
   1.113 +    bool res = zip->GetStream(str, &stream);
   1.114 +    jenv->ReleaseStringUTFChars(path, str);
   1.115 +    if (!res) {
   1.116 +        return nullptr;
   1.117 +    }
   1.118 +    jobject buf = jenv->NewDirectByteBuffer(const_cast<void *>(stream.GetBuffer()), stream.GetSize());
   1.119 +    if (!buf) {
   1.120 +        JNI_Throw(jenv, "java/lang/RuntimeException", "Failed to create ByteBuffer");
   1.121 +        return nullptr;
   1.122 +    }
   1.123 +    jclass nativeZip = jenv->GetObjectClass(jzip);
   1.124 +    jmethodID method = jenv->GetMethodID(nativeZip, "createInputStream", "(Ljava/nio/ByteBuffer;I)Ljava/io/InputStream;");
   1.125 +    // Since this function is only expected to be called from Java, it is safe
   1.126 +    // to skip exception checking for the method call below, as long as no
   1.127 +    // other Native -> Java call doesn't happen before returning to Java.
   1.128 +    return jenv->CallObjectMethod(jzip, method, buf, (jint) stream.GetType());
   1.129 +}

mercurial