OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of the Adblock Plus, |
| 3 * Copyright (C) 2006-2012 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include <string> |
| 19 #include "debug.h" |
| 20 #include "ops.h" |
| 21 |
| 22 v8::Handle<v8::Value> httpSendImpl(const v8::Arguments& args) |
| 23 { |
| 24 D(D_WARN, "httpSend()"); |
| 25 v8::HandleScope handle_scope; |
| 26 |
| 27 JNIEnv* jniEnv = NULL; |
| 28 if (globalJvm->AttachCurrentThread(&jniEnv, NULL) != 0) |
| 29 return v8::ThrowException(v8::String::New("Failed to get JNI environment")); |
| 30 |
| 31 if (args.Length() < 5) |
| 32 return v8::ThrowException(v8::String::New("Not enough parameters")); |
| 33 |
| 34 if (!args[0]->IsString()) |
| 35 return v8::ThrowException(v8::String::New("Parameter 0 must be a string")); |
| 36 if (!args[1]->IsString()) |
| 37 return v8::ThrowException(v8::String::New("Parameter 1 must be a string")); |
| 38 if (!args[2]->IsObject()) |
| 39 return v8::ThrowException(v8::String::New("Parameter 2 must be a hash object
")); |
| 40 if (!args[3]->IsBoolean()) |
| 41 return v8::ThrowException(v8::String::New("Parameter 3 must be a boolean")); |
| 42 if (!args[4]->IsFunction()) |
| 43 return v8::ThrowException(v8::String::New("Parameter 4 must be a function"))
; |
| 44 |
| 45 v8::String::Utf8Value method(args[0]); |
| 46 if (!*method) |
| 47 return v8::ThrowException(v8::String::New("Method must be set")); |
| 48 v8::String::Utf8Value url(args[1]); |
| 49 if (!*url) |
| 50 return v8::ThrowException(v8::String::New("Url must be set")); |
| 51 |
| 52 v8::Handle<v8::Object> headerObj = v8::Handle<v8::Object>::Cast(args[2]); |
| 53 v8::Handle<v8::Array> headers = headerObj->GetPropertyNames(); |
| 54 |
| 55 // v8::Handle<v8::BooleanObject> async = v8::Handle<v8::BooleanObject>::Cast(ar
gs[3]); |
| 56 jboolean jasync = args[3]->IsTrue() ? JNI_TRUE : JNI_FALSE; |
| 57 |
| 58 v8::Persistent<v8::Function> callback = v8::Persistent<v8::Function>::New(v8::
Handle<v8::Function>::Cast(args[4])); |
| 59 if (!*callback) |
| 60 return v8::ThrowException(v8::String::New("Callback must be set")); |
| 61 |
| 62 jstring jmethod = jniEnv->NewStringUTF(*method); |
| 63 jstring jurl = jniEnv->NewStringUTF(*url); |
| 64 |
| 65 static jclass stringClass = reinterpret_cast<jclass>(jniEnv->NewGlobalRef(jniE
nv->FindClass("java/lang/String"))); |
| 66 static jclass stringArrayClass = reinterpret_cast<jclass>(jniEnv->NewGlobalRef
(jniEnv->GetObjectClass(stringClass))); |
| 67 |
| 68 jobjectArray jheaders = jniEnv->NewObjectArray((jsize) headers->Length(), stri
ngArrayClass, NULL); |
| 69 |
| 70 for (unsigned int i = 0; i < headers->Length(); i++) |
| 71 { |
| 72 v8::String::Utf8Value name(headers->Get(v8::Integer::New(i))); |
| 73 v8::String::Utf8Value value(headerObj->Get(headers->Get(v8::Integer::New(i))
)); |
| 74 jobjectArray stringArray = jniEnv->NewObjectArray(2, stringClass, NULL); |
| 75 jniEnv->SetObjectArrayElement(stringArray, 0, jniEnv->NewStringUTF(*name)); |
| 76 jniEnv->SetObjectArrayElement(stringArray, 1, jniEnv->NewStringUTF(*value)); |
| 77 jniEnv->SetObjectArrayElement(jheaders, (jsize) i, stringArray); |
| 78 jniEnv->DeleteLocalRef(stringArray); |
| 79 } |
| 80 |
| 81 jlong jcallback = (jlong) *callback; |
| 82 |
| 83 static jclass cls = reinterpret_cast<jclass>(jniEnv->NewGlobalRef(jniEnv->GetO
bjectClass(jniCallback))); |
| 84 static jmethodID mid = jniEnv->GetMethodID(cls, "httpSend", "(Ljava/lang/Strin
g;Ljava/lang/String;[[Ljava/lang/String;ZJ)V"); |
| 85 if (mid) |
| 86 jniEnv->CallVoidMethod(jniCallback, mid, jmethod, jurl, jheaders, jasync, jc
allback); |
| 87 |
| 88 jniEnv->DeleteLocalRef(jmethod); |
| 89 jniEnv->DeleteLocalRef(jurl); |
| 90 jniEnv->DeleteLocalRef(jheaders); |
| 91 |
| 92 return v8::Undefined(); |
| 93 } |
OLD | NEW |