OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 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 { |
| 37 tupleClass.reset(new JniGlobalReference<jclass>(env, env->FindClass("com/githu
b/rjeschke/neetutils/collections/Tuple"))); |
| 38 serverResponseClass.reset(new JniGlobalReference<jclass>(env, env->FindClass(P
KG("ServerResponse")))); |
| 39 } |
| 40 |
| 41 AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url, const Adb
lockPlus::HeaderList& requestHeaders) const |
| 42 { |
| 43 JNIEnvAcquire env(GetJavaVM()); |
| 44 |
| 45 jclass clazz = env->GetObjectClass(GetCallbackObject()); |
| 46 jmethodID method = env->GetMethodID(clazz, "httpGET", "(Ljava/lang/String;Ljav
a/util/List;)" TYP("ServerResponse")); |
| 47 |
| 48 AdblockPlus::ServerResponse sResponse; |
| 49 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE; |
| 50 |
| 51 if (method) |
| 52 { |
| 53 jobject arrayList = NewJniArrayList(*env); |
| 54 |
| 55 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), en
d = requestHeaders.end(); it != end; it++) |
| 56 { |
| 57 JniAddObjectToList(*env, arrayList, NewTuple(*env, it->first, it->second))
; |
| 58 } |
| 59 |
| 60 jobject response = env->CallObjectMethod(GetCallbackObject(), method, env->N
ewStringUTF(url.c_str()), arrayList); |
| 61 |
| 62 if (!env->ExceptionCheck()) |
| 63 { |
| 64 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(), respo
nse, "status"); |
| 65 sResponse.responseStatus = JniGetIntField(*env, serverResponseClass->Get()
, response, "responseStatus"); |
| 66 sResponse.responseText = JniGetStringField(*env, serverResponseClass->Get(
), response, "response"); |
| 67 // TODO: transform Headers |
| 68 } |
| 69 } |
| 70 |
| 71 CheckAndLogJavaException(*env); |
| 72 |
| 73 return sResponse; |
| 74 } |
| 75 |
| 76 jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a, const std::st
ring& b) const |
| 77 { |
| 78 jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>", "(Ljava/lang
/Object;Ljava/lang/Object;)V"); |
| 79 return env->NewObject(tupleClass->Get(), factory, env->NewStringUTF(a.c_str())
, env->NewStringUTF(b.c_str())); |
| 80 } |
| 81 |
| 82 static JNINativeMethod methods[] = |
| 83 { |
| 84 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor }, |
| 85 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } |
| 86 }; |
| 87 |
| 88 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest
_registerNatives(JNIEnv *env, jclass clazz) |
| 89 { |
| 90 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
| 91 } |
OLD | NEW |