michael@0: /* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include "APKOpen.h" michael@0: #include "Zip.h" michael@0: #include "mozilla/RefPtr.h" michael@0: michael@0: extern "C" michael@0: __attribute__ ((visibility("default"))) michael@0: void JNICALL michael@0: Java_org_mozilla_gecko_mozglue_GeckoLoader_putenv(JNIEnv *jenv, jclass, jstring map) michael@0: { michael@0: const char* str; michael@0: // XXX: java doesn't give us true UTF8, we should figure out something michael@0: // better to do here michael@0: str = jenv->GetStringUTFChars(map, nullptr); michael@0: if (str == nullptr) michael@0: return; michael@0: putenv(strdup(str)); michael@0: jenv->ReleaseStringUTFChars(map, str); michael@0: } michael@0: michael@0: extern "C" michael@0: __attribute__ ((visibility("default"))) michael@0: jobject JNICALL michael@0: Java_org_mozilla_gecko_mozglue_DirectBufferAllocator_nativeAllocateDirectBuffer(JNIEnv *jenv, jclass, jlong size) michael@0: { michael@0: jobject buffer = nullptr; michael@0: void* mem = malloc(size); michael@0: if (mem) { michael@0: buffer = jenv->NewDirectByteBuffer(mem, size); michael@0: if (!buffer) michael@0: free(mem); michael@0: } michael@0: return buffer; michael@0: } michael@0: michael@0: extern "C" michael@0: __attribute__ ((visibility("default"))) michael@0: void JNICALL michael@0: Java_org_mozilla_gecko_mozglue_DirectBufferAllocator_nativeFreeDirectBuffer(JNIEnv *jenv, jclass, jobject buf) michael@0: { michael@0: free(jenv->GetDirectBufferAddress(buf)); michael@0: } michael@0: michael@0: extern "C" michael@0: __attribute__ ((visibility("default"))) michael@0: jlong JNICALL michael@0: Java_org_mozilla_gecko_mozglue_NativeZip_getZip(JNIEnv *jenv, jclass, jstring path) michael@0: { michael@0: const char* str; michael@0: str = jenv->GetStringUTFChars(path, nullptr); michael@0: if (!str || !*str) { michael@0: if (str) michael@0: jenv->ReleaseStringUTFChars(path, str); michael@0: JNI_Throw(jenv, "java/lang/IllegalArgumentException", "Invalid path"); michael@0: return 0; michael@0: } michael@0: mozilla::RefPtr zip = ZipCollection::GetZip(str); michael@0: jenv->ReleaseStringUTFChars(path, str); michael@0: if (!zip) { michael@0: JNI_Throw(jenv, "java/lang/IllegalArgumentException", "Invalid path or invalid zip"); michael@0: return 0; michael@0: } michael@0: zip->AddRef(); michael@0: return (jlong) zip.get(); michael@0: } michael@0: michael@0: extern "C" michael@0: __attribute__ ((visibility("default"))) michael@0: jlong JNICALL michael@0: Java_org_mozilla_gecko_mozglue_NativeZip_getZipFromByteBuffer(JNIEnv *jenv, jclass, jobject buffer) michael@0: { michael@0: void *buf = jenv->GetDirectBufferAddress(buffer); michael@0: size_t size = jenv->GetDirectBufferCapacity(buffer); michael@0: mozilla::RefPtr zip = Zip::Create(buf, size); michael@0: if (!zip) { michael@0: JNI_Throw(jenv, "java/lang/IllegalArgumentException", "Invalid zip"); michael@0: return 0; michael@0: } michael@0: zip->AddRef(); michael@0: return (jlong) zip.get(); michael@0: } michael@0: michael@0: extern "C" michael@0: __attribute__ ((visibility("default"))) michael@0: void JNICALL michael@0: Java_org_mozilla_gecko_mozglue_NativeZip__1release(JNIEnv *jenv, jclass, jlong obj) michael@0: { michael@0: Zip *zip = (Zip *)obj; michael@0: zip->Release(); michael@0: } michael@0: michael@0: extern "C" michael@0: __attribute__ ((visibility("default"))) michael@0: jobject JNICALL michael@0: Java_org_mozilla_gecko_mozglue_NativeZip__1getInputStream(JNIEnv *jenv, jobject jzip, jlong obj, jstring path) michael@0: { michael@0: Zip *zip = (Zip *)obj; michael@0: const char* str; michael@0: str = jenv->GetStringUTFChars(path, nullptr); michael@0: michael@0: Zip::Stream stream; michael@0: bool res = zip->GetStream(str, &stream); michael@0: jenv->ReleaseStringUTFChars(path, str); michael@0: if (!res) { michael@0: return nullptr; michael@0: } michael@0: jobject buf = jenv->NewDirectByteBuffer(const_cast(stream.GetBuffer()), stream.GetSize()); michael@0: if (!buf) { michael@0: JNI_Throw(jenv, "java/lang/RuntimeException", "Failed to create ByteBuffer"); michael@0: return nullptr; michael@0: } michael@0: jclass nativeZip = jenv->GetObjectClass(jzip); michael@0: jmethodID method = jenv->GetMethodID(nativeZip, "createInputStream", "(Ljava/nio/ByteBuffer;I)Ljava/io/InputStream;"); michael@0: // Since this function is only expected to be called from Java, it is safe michael@0: // to skip exception checking for the method call below, as long as no michael@0: // other Native -> Java call doesn't happen before returning to Java. michael@0: return jenv->CallObjectMethod(jzip, method, buf, (jint) stream.GetType()); michael@0: }