Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include <jni.h> |
michael@0 | 7 | |
michael@0 | 8 | #include <stdlib.h> |
michael@0 | 9 | #include <fcntl.h> |
michael@0 | 10 | #include "APKOpen.h" |
michael@0 | 11 | #include "Zip.h" |
michael@0 | 12 | #include "mozilla/RefPtr.h" |
michael@0 | 13 | |
michael@0 | 14 | extern "C" |
michael@0 | 15 | __attribute__ ((visibility("default"))) |
michael@0 | 16 | void JNICALL |
michael@0 | 17 | Java_org_mozilla_gecko_mozglue_GeckoLoader_putenv(JNIEnv *jenv, jclass, jstring map) |
michael@0 | 18 | { |
michael@0 | 19 | const char* str; |
michael@0 | 20 | // XXX: java doesn't give us true UTF8, we should figure out something |
michael@0 | 21 | // better to do here |
michael@0 | 22 | str = jenv->GetStringUTFChars(map, nullptr); |
michael@0 | 23 | if (str == nullptr) |
michael@0 | 24 | return; |
michael@0 | 25 | putenv(strdup(str)); |
michael@0 | 26 | jenv->ReleaseStringUTFChars(map, str); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | extern "C" |
michael@0 | 30 | __attribute__ ((visibility("default"))) |
michael@0 | 31 | jobject JNICALL |
michael@0 | 32 | Java_org_mozilla_gecko_mozglue_DirectBufferAllocator_nativeAllocateDirectBuffer(JNIEnv *jenv, jclass, jlong size) |
michael@0 | 33 | { |
michael@0 | 34 | jobject buffer = nullptr; |
michael@0 | 35 | void* mem = malloc(size); |
michael@0 | 36 | if (mem) { |
michael@0 | 37 | buffer = jenv->NewDirectByteBuffer(mem, size); |
michael@0 | 38 | if (!buffer) |
michael@0 | 39 | free(mem); |
michael@0 | 40 | } |
michael@0 | 41 | return buffer; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | extern "C" |
michael@0 | 45 | __attribute__ ((visibility("default"))) |
michael@0 | 46 | void JNICALL |
michael@0 | 47 | Java_org_mozilla_gecko_mozglue_DirectBufferAllocator_nativeFreeDirectBuffer(JNIEnv *jenv, jclass, jobject buf) |
michael@0 | 48 | { |
michael@0 | 49 | free(jenv->GetDirectBufferAddress(buf)); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | extern "C" |
michael@0 | 53 | __attribute__ ((visibility("default"))) |
michael@0 | 54 | jlong JNICALL |
michael@0 | 55 | Java_org_mozilla_gecko_mozglue_NativeZip_getZip(JNIEnv *jenv, jclass, jstring path) |
michael@0 | 56 | { |
michael@0 | 57 | const char* str; |
michael@0 | 58 | str = jenv->GetStringUTFChars(path, nullptr); |
michael@0 | 59 | if (!str || !*str) { |
michael@0 | 60 | if (str) |
michael@0 | 61 | jenv->ReleaseStringUTFChars(path, str); |
michael@0 | 62 | JNI_Throw(jenv, "java/lang/IllegalArgumentException", "Invalid path"); |
michael@0 | 63 | return 0; |
michael@0 | 64 | } |
michael@0 | 65 | mozilla::RefPtr<Zip> zip = ZipCollection::GetZip(str); |
michael@0 | 66 | jenv->ReleaseStringUTFChars(path, str); |
michael@0 | 67 | if (!zip) { |
michael@0 | 68 | JNI_Throw(jenv, "java/lang/IllegalArgumentException", "Invalid path or invalid zip"); |
michael@0 | 69 | return 0; |
michael@0 | 70 | } |
michael@0 | 71 | zip->AddRef(); |
michael@0 | 72 | return (jlong) zip.get(); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | extern "C" |
michael@0 | 76 | __attribute__ ((visibility("default"))) |
michael@0 | 77 | jlong JNICALL |
michael@0 | 78 | Java_org_mozilla_gecko_mozglue_NativeZip_getZipFromByteBuffer(JNIEnv *jenv, jclass, jobject buffer) |
michael@0 | 79 | { |
michael@0 | 80 | void *buf = jenv->GetDirectBufferAddress(buffer); |
michael@0 | 81 | size_t size = jenv->GetDirectBufferCapacity(buffer); |
michael@0 | 82 | mozilla::RefPtr<Zip> zip = Zip::Create(buf, size); |
michael@0 | 83 | if (!zip) { |
michael@0 | 84 | JNI_Throw(jenv, "java/lang/IllegalArgumentException", "Invalid zip"); |
michael@0 | 85 | return 0; |
michael@0 | 86 | } |
michael@0 | 87 | zip->AddRef(); |
michael@0 | 88 | return (jlong) zip.get(); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | extern "C" |
michael@0 | 92 | __attribute__ ((visibility("default"))) |
michael@0 | 93 | void JNICALL |
michael@0 | 94 | Java_org_mozilla_gecko_mozglue_NativeZip__1release(JNIEnv *jenv, jclass, jlong obj) |
michael@0 | 95 | { |
michael@0 | 96 | Zip *zip = (Zip *)obj; |
michael@0 | 97 | zip->Release(); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | extern "C" |
michael@0 | 101 | __attribute__ ((visibility("default"))) |
michael@0 | 102 | jobject JNICALL |
michael@0 | 103 | Java_org_mozilla_gecko_mozglue_NativeZip__1getInputStream(JNIEnv *jenv, jobject jzip, jlong obj, jstring path) |
michael@0 | 104 | { |
michael@0 | 105 | Zip *zip = (Zip *)obj; |
michael@0 | 106 | const char* str; |
michael@0 | 107 | str = jenv->GetStringUTFChars(path, nullptr); |
michael@0 | 108 | |
michael@0 | 109 | Zip::Stream stream; |
michael@0 | 110 | bool res = zip->GetStream(str, &stream); |
michael@0 | 111 | jenv->ReleaseStringUTFChars(path, str); |
michael@0 | 112 | if (!res) { |
michael@0 | 113 | return nullptr; |
michael@0 | 114 | } |
michael@0 | 115 | jobject buf = jenv->NewDirectByteBuffer(const_cast<void *>(stream.GetBuffer()), stream.GetSize()); |
michael@0 | 116 | if (!buf) { |
michael@0 | 117 | JNI_Throw(jenv, "java/lang/RuntimeException", "Failed to create ByteBuffer"); |
michael@0 | 118 | return nullptr; |
michael@0 | 119 | } |
michael@0 | 120 | jclass nativeZip = jenv->GetObjectClass(jzip); |
michael@0 | 121 | jmethodID method = jenv->GetMethodID(nativeZip, "createInputStream", "(Ljava/nio/ByteBuffer;I)Ljava/io/InputStream;"); |
michael@0 | 122 | // Since this function is only expected to be called from Java, it is safe |
michael@0 | 123 | // to skip exception checking for the method call below, as long as no |
michael@0 | 124 | // other Native -> Java call doesn't happen before returning to Java. |
michael@0 | 125 | return jenv->CallObjectMethod(jzip, method, buf, (jint) stream.GetType()); |
michael@0 | 126 | } |