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