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

Side by Side Diff: libadblockplus-android/jni/JniWebRequest.cpp

Issue 29354787: Issue 4442 - Cache class and ctors references in JNI_Load/UnLoad (Closed)
Patch Set: Created Sept. 23, 2016, 12:55 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
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 "JniCallbacks.h" 18 #include "JniCallbacks.h"
19 #include "Utils.h" 19 #include "Utils.h"
20 #include "JniWebRequest.h"
21
22 // precached in JNI_OnLoad and released in JNI_OnUnload
23 JniGlobalReference<jclass>* headerEntryClass;
24 JniGlobalReference<jclass>* serverResponseClass;
25
26 void JniWebRequest_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved)
27 {
28 headerEntryClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("Hea derEntry")));
29 serverResponseClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG(" ServerResponse")));
30 }
31
32 void JniWebRequest_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved)
33 {
34 if (headerEntryClass)
35 {
36 delete headerEntryClass;
37 headerEntryClass = NULL;
38 }
39
40 if (serverResponseClass)
41 {
42 delete serverResponseClass;
43 serverResponseClass = NULL;
44 }
45 }
20 46
21 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject callbackObject) 47 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject callbackObject)
22 { 48 {
23 try 49 try
24 { 50 {
25 return JniPtrToLong(new AdblockPlus::WebRequestPtr(new JniWebRequest(env, ca llbackObject))); 51 return JniPtrToLong(new AdblockPlus::WebRequestPtr(new JniWebRequest(env, ca llbackObject)));
26 } 52 }
27 CATCH_THROW_AND_RETURN(env, 0) 53 CATCH_THROW_AND_RETURN(env, 0)
28 } 54 }
29 55
30 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) 56 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
31 { 57 {
32 delete JniLongToTypePtr<AdblockPlus::WebRequestPtr>(ptr); 58 delete JniLongToTypePtr<AdblockPlus::WebRequestPtr>(ptr);
33 } 59 }
34 60
35 JniWebRequest::JniWebRequest(JNIEnv* env, jobject callbackObject) 61 JniWebRequest::JniWebRequest(JNIEnv* env, jobject callbackObject)
36 : JniCallbackBase(env, callbackObject), AdblockPlus::WebRequest(), 62 : JniCallbackBase(env, callbackObject), AdblockPlus::WebRequest()
37 tupleClass(new JniGlobalReference<jclass>(env, env->FindClass(PKG("HeaderEnt ry")))),
38 serverResponseClass(new JniGlobalReference<jclass>(env, env->FindClass(PKG(" ServerResponse"))))
39 { 63 {
40 } 64 }
41 65
42 AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url, 66 AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url,
43 const AdblockPlus::HeaderList& requestHeaders) const 67 const AdblockPlus::HeaderList& requestHeaders) const
44 { 68 {
45 JNIEnvAcquire env(GetJavaVM()); 69 JNIEnvAcquire env(GetJavaVM());
46 70
47 jmethodID method = env->GetMethodID( 71 jmethodID method = env->GetMethodID(
48 *JniLocalReference<jclass>(*env, 72 *JniLocalReference<jclass>(*env,
49 env->GetObjectClass(GetCallbackObject())), 73 env->GetObjectClass(GetCallbackObject())),
50 "httpGET", 74 "httpGET",
51 "(Ljava/lang/String;Ljava/util/List;)" TYP("ServerResponse")); 75 "(Ljava/lang/String;Ljava/util/List;)" TYP("ServerResponse"));
52 76
53 AdblockPlus::ServerResponse sResponse; 77 AdblockPlus::ServerResponse sResponse;
54 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE; 78 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE;
55 79
56 if (method) 80 if (method)
57 { 81 {
58 JniLocalReference<jobject> arrayList(*env, NewJniArrayList(*env)); 82 JniLocalReference<jobject> arrayList(*env, NewJniArrayList(*env));
anton 2016/09/23 13:05:25 Consider using LinkedList instead of ArrayList - f
diegocarloslima 2016/11/04 11:34:19 Yeah, LinkedList are better for insertion while Ar
83 jmethodID addMethod = JniGetAddToListMethod(*env, *arrayList);
anton 2016/09/23 13:05:25 Now searching for the method once
59 84
60 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), 85 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(),
61 end = requestHeaders.end(); it != end; it++) 86 end = requestHeaders.end(); it != end; it++)
62 { 87 {
63 JniLocalReference<jobject> tuple(*env, 88 JniLocalReference<jobject> headerEntry(*env, NewHeaderEntry(*env, it->firs t, it->second));
64 NewTuple(*env, it->first, it->second)); 89 JniAddObjectToList(*env, *arrayList, addMethod, *headerEntry);
65 JniAddObjectToList(*env, *arrayList, *tuple);
anton 2016/09/23 13:05:25 here "add" methods is found headers size times whi
66 } 90 }
67 91
68 JniLocalReference<jobject> response(*env, 92 JniLocalReference<jobject> response(*env,
69 env->CallObjectMethod(GetCallbackObject(), method, 93 env->CallObjectMethod(GetCallbackObject(), method,
70 *JniLocalReference<jstring>(*env, env->NewStringUTF(url.c_str())), 94 *JniLocalReference<jstring>(*env, env->NewStringUTF(url.c_str())),
71 *arrayList)); 95 *arrayList));
72 96
73 if (!env->ExceptionCheck()) 97 if (!env->ExceptionCheck())
74 { 98 {
75 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(), 99 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(),
76 *response, "status"); 100 *response, "status");
77 sResponse.responseStatus = JniGetIntField(*env, 101 sResponse.responseStatus = JniGetIntField(*env,
78 serverResponseClass->Get(), *response, "responseStatus"); 102 serverResponseClass->Get(),
103 *response,
104 "responseStatus");
79 sResponse.responseText = JniGetStringField(*env, 105 sResponse.responseText = JniGetStringField(*env,
80 serverResponseClass->Get(), *response, "response"); 106 serverResponseClass->Get(),
107 *response,
108 "response");
81 109
82 // map headers 110 // map headers
83 jobjectArray responseHeadersArray = JniGetStringArrayField(*env, 111 jobjectArray responseHeadersArray = JniGetStringArrayField(*env,
84 serverResponseClass->Get(), *response, "headers"); 112 serverResponseC lass->Get(),
113 *response,
114 "headers");
85 115
86 if (responseHeadersArray) 116 if (responseHeadersArray)
87 { 117 {
88 int itemsCount = env->GetArrayLength(responseHeadersArray) / 2; 118 int itemsCount = env->GetArrayLength(responseHeadersArray) / 2;
89 for (int i = 0; i < itemsCount; i++) 119 for (int i = 0; i < itemsCount; i++)
90 { 120 {
91 jstring jKey = (jstring)env->GetObjectArrayElement(responseHeadersArra y, i * 2); 121 jstring jKey = (jstring)env->GetObjectArrayElement(responseHeadersArra y, i * 2);
92 std::string stdKey = JniJavaToStdString(*env, jKey); 122 std::string stdKey = JniJavaToStdString(*env, jKey);
93 123
94 jstring jValue = (jstring)env->GetObjectArrayElement(responseHeadersAr ray, i * 2 + 1); 124 jstring jValue = (jstring)env->GetObjectArrayElement(responseHeadersAr ray, i * 2 + 1);
95 std::string stdValue = JniJavaToStdString(*env, jValue); 125 std::string stdValue = JniJavaToStdString(*env, jValue);
96 126
97 std::pair<std::string,std::string> keyValue(stdKey, stdValue); 127 std::pair<std::string,std::string> keyValue(stdKey, stdValue);
98 sResponse.responseHeaders.push_back(keyValue); 128 sResponse.responseHeaders.push_back(keyValue);
99 } 129 }
100 } 130 }
101 } 131 }
102 } 132 }
103 133
104 CheckAndLogJavaException(*env); 134 CheckAndLogJavaException(*env);
105 135
106 return sResponse; 136 return sResponse;
107 } 137 }
108 138
109 jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a, 139 jobject JniWebRequest::NewHeaderEntry(JNIEnv* env, const std::string& a,
110 const std::string& b) const 140 const std::string& b) const
111 { 141 {
112 jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>", 142 jmethodID factory = env->GetMethodID(headerEntryClass->Get(), "<init>",
113 "(Ljava/lang/String;Ljava/lang/String;)V"); 143 "(Ljava/lang/String;Ljava/lang/String;)V");
114 144
115 JniLocalReference<jstring> strA(env, env->NewStringUTF(a.c_str())); 145 JniLocalReference<jstring> strA(env, env->NewStringUTF(a.c_str()));
116 JniLocalReference<jstring> strB(env, env->NewStringUTF(b.c_str())); 146 JniLocalReference<jstring> strB(env, env->NewStringUTF(b.c_str()));
117 147
118 return env->NewObject(tupleClass->Get(), factory, *strA, *strB); 148 return env->NewObject(headerEntryClass->Get(), factory, *strA, *strB);
119 } 149 }
120 150
121 static JNINativeMethod methods[] = 151 static JNINativeMethod methods[] =
122 { 152 {
123 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor }, 153 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor },
124 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 154 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
125 }; 155 };
126 156
127 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest _registerNatives(JNIEnv *env, jclass clazz) 157 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest _registerNatives(JNIEnv *env, jclass clazz)
128 { 158 {
129 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 159 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
130 } 160 }
OLDNEW

Powered by Google App Engine
This is Rietveld