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

Unified Diff: libadblockplus-android/jni/JniWebRequest.cpp

Issue 29354787: Issue 4442 - Cache class and ctors references in JNI_Load/UnLoad (Closed)
Patch Set: reverted to NewTuple Created Nov. 21, 2016, 8:25 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « libadblockplus-android/jni/JniWebRequest.h ('k') | libadblockplus-android/jni/Utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: libadblockplus-android/jni/JniWebRequest.cpp
diff --git a/libadblockplus-android/jni/JniWebRequest.cpp b/libadblockplus-android/jni/JniWebRequest.cpp
index 5d46b8f60fd754ee433ad703c856aaa68f12fc2f..f19c712dfe278f92d104ef70914989a52f0993b9 100644
--- a/libadblockplus-android/jni/JniWebRequest.cpp
+++ b/libadblockplus-android/jni/JniWebRequest.cpp
@@ -17,6 +17,32 @@
#include "JniCallbacks.h"
#include "Utils.h"
+#include "JniWebRequest.h"
+
+// precached in JNI_OnLoad and released in JNI_OnUnload
+JniGlobalReference<jclass>* headerEntryClass;
+JniGlobalReference<jclass>* serverResponseClass;
+
+void JniWebRequest_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved)
+{
+ headerEntryClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("HeaderEntry")));
+ serverResponseClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("ServerResponse")));
+}
+
+void JniWebRequest_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved)
+{
+ if (headerEntryClass)
+ {
+ delete headerEntryClass;
+ headerEntryClass = NULL;
+ }
+
+ if (serverResponseClass)
+ {
+ delete serverResponseClass;
+ serverResponseClass = NULL;
+ }
+}
static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject callbackObject)
{
@@ -33,9 +59,7 @@ static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
}
JniWebRequest::JniWebRequest(JNIEnv* env, jobject callbackObject)
- : JniCallbackBase(env, callbackObject), AdblockPlus::WebRequest(),
- tupleClass(new JniGlobalReference<jclass>(env, env->FindClass(PKG("HeaderEntry")))),
- serverResponseClass(new JniGlobalReference<jclass>(env, env->FindClass(PKG("ServerResponse"))))
+ : JniCallbackBase(env, callbackObject), AdblockPlus::WebRequest()
{
}
@@ -56,13 +80,13 @@ AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url,
if (method)
{
JniLocalReference<jobject> arrayList(*env, NewJniArrayList(*env));
+ jmethodID addMethod = JniGetAddToListMethod(*env, *arrayList);
for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(),
end = requestHeaders.end(); it != end; it++)
{
- JniLocalReference<jobject> tuple(*env,
- NewTuple(*env, it->first, it->second));
- JniAddObjectToList(*env, *arrayList, *tuple);
+ JniLocalReference<jobject> headerEntry(*env, NewTuple(*env, it->first, it->second));
+ JniAddObjectToList(*env, *arrayList, addMethod, *headerEntry);
}
JniLocalReference<jobject> response(*env,
@@ -75,13 +99,19 @@ AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url,
sResponse.status = JniGetLongField(*env, serverResponseClass->Get(),
*response, "status");
sResponse.responseStatus = JniGetIntField(*env,
- serverResponseClass->Get(), *response, "responseStatus");
+ serverResponseClass->Get(),
+ *response,
+ "responseStatus");
sResponse.responseText = JniGetStringField(*env,
- serverResponseClass->Get(), *response, "response");
+ serverResponseClass->Get(),
+ *response,
+ "response");
// map headers
jobjectArray responseHeadersArray = JniGetStringArrayField(*env,
- serverResponseClass->Get(), *response, "headers");
+ serverResponseClass->Get(),
+ *response,
+ "headers");
if (responseHeadersArray)
{
@@ -109,13 +139,13 @@ AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url,
jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a,
const std::string& b) const
{
- jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>",
+ jmethodID factory = env->GetMethodID(headerEntryClass->Get(), "<init>",
"(Ljava/lang/String;Ljava/lang/String;)V");
JniLocalReference<jstring> strA(env, env->NewStringUTF(a.c_str()));
JniLocalReference<jstring> strB(env, env->NewStringUTF(b.c_str()));
- return env->NewObject(tupleClass->Get(), factory, *strA, *strB);
+ return env->NewObject(headerEntryClass->Get(), factory, *strA, *strB);
}
static JNINativeMethod methods[] =
« no previous file with comments | « libadblockplus-android/jni/JniWebRequest.h ('k') | libadblockplus-android/jni/Utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld