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