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