LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 |
(...skipping 20 matching lines...) Expand all Loading... |
31 delete JniLongToTypePtr<AdblockPlus::WebRequestPtr>(ptr); | 31 delete JniLongToTypePtr<AdblockPlus::WebRequestPtr>(ptr); |
32 } | 32 } |
33 | 33 |
34 JniWebRequest::JniWebRequest(JNIEnv* env, jobject callbackObject) | 34 JniWebRequest::JniWebRequest(JNIEnv* env, jobject callbackObject) |
35 : JniCallbackBase(env, callbackObject), AdblockPlus::WebRequest(), | 35 : JniCallbackBase(env, callbackObject), AdblockPlus::WebRequest(), |
36 tupleClass(new JniGlobalReference<jclass>(env, env->FindClass(PKG("HeaderEnt
ry")))), | 36 tupleClass(new JniGlobalReference<jclass>(env, env->FindClass(PKG("HeaderEnt
ry")))), |
37 serverResponseClass(new JniGlobalReference<jclass>(env, env->FindClass(PKG("
ServerResponse")))) | 37 serverResponseClass(new JniGlobalReference<jclass>(env, env->FindClass(PKG("
ServerResponse")))) |
38 { | 38 { |
39 } | 39 } |
40 | 40 |
41 AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url, const Adb
lockPlus::HeaderList& requestHeaders) const | 41 AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url, |
| 42 const AdblockPlus::HeaderList& requestHeaders) const |
42 { | 43 { |
43 JNIEnvAcquire env(GetJavaVM()); | 44 JNIEnvAcquire env(GetJavaVM()); |
44 | 45 |
45 jmethodID method = env->GetMethodID( | 46 jmethodID method = env->GetMethodID( |
46 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject()))
, | 47 *JniLocalReference<jclass>(*env, |
47 "httpGET", "(Ljava/lang/String;Ljava/util/List;)" TYP("ServerResponse")); | 48 env->GetObjectClass(GetCallbackObject())), |
| 49 "httpGET", |
| 50 "(Ljava/lang/String;Ljava/util/List;)" TYP("ServerResponse")); |
48 | 51 |
49 AdblockPlus::ServerResponse sResponse; | 52 AdblockPlus::ServerResponse sResponse; |
50 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE; | 53 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE; |
51 | 54 |
52 if (method) | 55 if (method) |
53 { | 56 { |
54 JniLocalReference<jobject> arrayList(*env, NewJniArrayList(*env)); | 57 JniLocalReference<jobject> arrayList(*env, NewJniArrayList(*env)); |
55 | 58 |
56 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), en
d = requestHeaders.end(); it != end; it++) | 59 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), |
| 60 end = requestHeaders.end(); it != end; it++) |
57 { | 61 { |
58 JniLocalReference<jobject> tuple(*env, NewTuple(*env, it->first, it->secon
d)); | 62 JniLocalReference<jobject> tuple(*env, |
| 63 NewTuple(*env, it->first, it->second)); |
59 JniAddObjectToList(*env, *arrayList, *tuple); | 64 JniAddObjectToList(*env, *arrayList, *tuple); |
60 } | 65 } |
61 | 66 |
62 JniLocalReference<jobject> response(*env, env->CallObjectMethod( | 67 JniLocalReference<jobject> response(*env, |
63 GetCallbackObject(), | 68 env->CallObjectMethod(GetCallbackObject(), method, |
64 method, | 69 *JniLocalReference<jstring>(*env, env->NewStringUTF(url.c_str())), |
65 *JniLocalReference<jstring>(*env, env->NewStringUTF(url.c_str())), | 70 *arrayList)); |
66 *arrayList)); | |
67 | 71 |
68 if (!env->ExceptionCheck()) | 72 if (!env->ExceptionCheck()) |
69 { | 73 { |
70 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(), *resp
onse, "status"); | 74 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(), |
71 sResponse.responseStatus = JniGetIntField(*env, serverResponseClass->Get()
, *response, "responseStatus"); | 75 *response, "status"); |
72 sResponse.responseText = JniGetStringField(*env, serverResponseClass->Get(
), *response, "response"); | 76 sResponse.responseStatus = JniGetIntField(*env, |
| 77 serverResponseClass->Get(), *response, "responseStatus"); |
| 78 sResponse.responseText = JniGetStringField(*env, |
| 79 serverResponseClass->Get(), *response, "response"); |
73 // TODO: transform Headers | 80 // TODO: transform Headers |
74 } | 81 } |
75 } | 82 } |
76 | 83 |
77 CheckAndLogJavaException(*env); | 84 CheckAndLogJavaException(*env); |
78 | 85 |
79 return sResponse; | 86 return sResponse; |
80 } | 87 } |
81 | 88 |
82 jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a, const std::st
ring& b) const | 89 jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a, |
| 90 const std::string& b) const |
83 { | 91 { |
84 jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>", "(Ljava/lang
/String;Ljava/lang/String;)V"); | 92 jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>", |
| 93 "(Ljava/lang/String;Ljava/lang/String;)V"); |
85 | 94 |
86 JniLocalReference<jstring> strA(env, env->NewStringUTF(a.c_str())); | 95 JniLocalReference<jstring> strA(env, env->NewStringUTF(a.c_str())); |
87 JniLocalReference<jstring> strB(env, env->NewStringUTF(b.c_str())); | 96 JniLocalReference<jstring> strB(env, env->NewStringUTF(b.c_str())); |
88 | 97 |
89 return env->NewObject(tupleClass->Get(), factory, *strA, *strB); | 98 return env->NewObject(tupleClass->Get(), factory, *strA, *strB); |
90 } | 99 } |
91 | 100 |
92 static JNINativeMethod methods[] = | 101 static JNINativeMethod methods[] = |
93 { | 102 { |
94 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor }, | 103 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor }, |
95 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } | 104 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } |
96 }; | 105 }; |
97 | 106 |
98 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest
_registerNatives(JNIEnv *env, jclass clazz) | 107 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest
_registerNatives(JNIEnv *env, jclass clazz) |
99 { | 108 { |
100 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 109 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
101 } | 110 } |
LEFT | RIGHT |