Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: jni/JniWebRequest.cpp

Issue 4761138508070912: Issue 1848 - Clean up local reference handling (Closed)
Patch Set: Removed a Log.d Created Jan. 22, 2015, 6:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « jni/JniUpdateCheckDoneCallback.cpp ('k') | jni/Utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 24 matching lines...) Expand all
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, const Adb lockPlus::HeaderList& requestHeaders) const
42 { 42 {
43 JNIEnvAcquire env(GetJavaVM()); 43 JNIEnvAcquire env(GetJavaVM());
44 44
45 jclass clazz = env->GetObjectClass(GetCallbackObject()); 45 jmethodID method = env->GetMethodID(
46 jmethodID method = env->GetMethodID(clazz, "httpGET", "(Ljava/lang/String;Ljav a/util/List;)" TYP("ServerResponse")); 46 *JniLocalReference<jclass>(*env, env->GetObjectClass(GetCallbackObject())) ,
47 "httpGET", "(Ljava/lang/String;Ljava/util/List;)" TYP("ServerResponse"));
47 48
48 AdblockPlus::ServerResponse sResponse; 49 AdblockPlus::ServerResponse sResponse;
49 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE; 50 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE;
50 51
51 if (method) 52 if (method)
52 { 53 {
53 jobject arrayList = NewJniArrayList(*env); 54 JniLocalReference<jobject> arrayList(*env, NewJniArrayList(*env));
54 55
55 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), en d = requestHeaders.end(); it != end; it++) 56 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), en d = requestHeaders.end(); it != end; it++)
56 { 57 {
57 JniAddObjectToList(*env, arrayList, NewTuple(*env, it->first, it->second)) ; 58 JniLocalReference<jobject> tuple(*env, NewTuple(*env, it->first, it->secon d));
59 JniAddObjectToList(*env, *arrayList, *tuple);
58 } 60 }
59 61
60 jobject response = env->CallObjectMethod(GetCallbackObject(), method, env->N ewStringUTF(url.c_str()), arrayList); 62 JniLocalReference<jobject> response(*env, env->CallObjectMethod(
63 GetCallbackObject(),
64 method,
65 *JniLocalReference<jstring>(*env, env->NewStringUTF(url.c_str())),
66 *arrayList));
61 67
62 if (!env->ExceptionCheck()) 68 if (!env->ExceptionCheck())
63 { 69 {
64 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(), respo nse, "status"); 70 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(), *resp onse, "status");
65 sResponse.responseStatus = JniGetIntField(*env, serverResponseClass->Get() , response, "responseStatus"); 71 sResponse.responseStatus = JniGetIntField(*env, serverResponseClass->Get() , *response, "responseStatus");
66 sResponse.responseText = JniGetStringField(*env, serverResponseClass->Get( ), response, "response"); 72 sResponse.responseText = JniGetStringField(*env, serverResponseClass->Get( ), *response, "response");
67 // TODO: transform Headers 73 // TODO: transform Headers
68 } 74 }
69 } 75 }
70 76
71 CheckAndLogJavaException(*env); 77 CheckAndLogJavaException(*env);
72 78
73 return sResponse; 79 return sResponse;
74 } 80 }
75 81
76 jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a, const std::st ring& b) const 82 jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a, const std::st ring& b) const
77 { 83 {
78 jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>", "(Ljava/lang /String;Ljava/lang/String;)V"); 84 jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>", "(Ljava/lang /String;Ljava/lang/String;)V");
79 return env->NewObject(tupleClass->Get(), factory, env->NewStringUTF(a.c_str()) , env->NewStringUTF(b.c_str())); 85
86 JniLocalReference<jstring> strA(env, env->NewStringUTF(a.c_str()));
87 JniLocalReference<jstring> strB(env, env->NewStringUTF(b.c_str()));
88
89 return env->NewObject(tupleClass->Get(), factory, *strA, *strB);
80 } 90 }
81 91
82 static JNINativeMethod methods[] = 92 static JNINativeMethod methods[] =
83 { 93 {
84 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor }, 94 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor },
85 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 95 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
86 }; 96 };
87 97
88 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest _registerNatives(JNIEnv *env, jclass clazz) 98 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest _registerNatives(JNIEnv *env, jclass clazz)
89 { 99 {
90 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 100 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
91 } 101 }
OLDNEW
« no previous file with comments | « jni/JniUpdateCheckDoneCallback.cpp ('k') | jni/Utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld