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

Unified Diff: jni/JniWebRequest.cpp

Issue 4761138508070912: Issue 1848 - Clean up local reference handling (Closed)
Patch Set: Some formatting issues Created Jan. 23, 2015, 12:30 p.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 | « jni/JniUpdateCheckDoneCallback.cpp ('k') | jni/Utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: jni/JniWebRequest.cpp
diff --git a/jni/JniWebRequest.cpp b/jni/JniWebRequest.cpp
index a479301fcd31a8fceba2cccf4699c3ed108ef4a2..e704d4cc56f1712a01e9176656114fc0dda553e4 100644
--- a/jni/JniWebRequest.cpp
+++ b/jni/JniWebRequest.cpp
@@ -38,32 +38,44 @@ JniWebRequest::JniWebRequest(JNIEnv* env, jobject callbackObject)
{
}
-AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders) const
+AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url,
+ const AdblockPlus::HeaderList& requestHeaders) const
{
JNIEnvAcquire env(GetJavaVM());
- jclass clazz = env->GetObjectClass(GetCallbackObject());
- jmethodID method = env->GetMethodID(clazz, "httpGET", "(Ljava/lang/String;Ljava/util/List;)" TYP("ServerResponse"));
+ jmethodID method = env->GetMethodID(
+ *JniLocalReference<jclass>(*env,
+ env->GetObjectClass(GetCallbackObject())), "httpGET",
+ "(Ljava/lang/String;Ljava/util/List;)" TYP("ServerResponse"));
AdblockPlus::ServerResponse sResponse;
sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE;
if (method)
{
- jobject arrayList = NewJniArrayList(*env);
+ JniLocalReference<jobject> arrayList(*env, NewJniArrayList(*env));
- for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), end = requestHeaders.end(); it != end; it++)
+ for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(),
+ end = requestHeaders.end(); it != end; it++)
{
- JniAddObjectToList(*env, arrayList, NewTuple(*env, it->first, it->second));
+ JniLocalReference<jobject> tuple(*env,
+ NewTuple(*env, it->first, it->second));
+ JniAddObjectToList(*env, *arrayList, *tuple);
}
- jobject response = env->CallObjectMethod(GetCallbackObject(), method, env->NewStringUTF(url.c_str()), arrayList);
+ JniLocalReference<jobject> response(*env,
+ env->CallObjectMethod(GetCallbackObject(), method,
+ *JniLocalReference<jstring>(*env, env->NewStringUTF(url.c_str())),
+ *arrayList));
if (!env->ExceptionCheck())
{
- sResponse.status = JniGetLongField(*env, serverResponseClass->Get(), response, "status");
- sResponse.responseStatus = JniGetIntField(*env, serverResponseClass->Get(), response, "responseStatus");
- sResponse.responseText = JniGetStringField(*env, serverResponseClass->Get(), response, "response");
+ sResponse.status = JniGetLongField(*env, serverResponseClass->Get(),
+ *response, "status");
+ sResponse.responseStatus = JniGetIntField(*env,
+ serverResponseClass->Get(), *response, "responseStatus");
+ sResponse.responseText = JniGetStringField(*env,
+ serverResponseClass->Get(), *response, "response");
// TODO: transform Headers
}
}
@@ -73,10 +85,16 @@ AdblockPlus::ServerResponse JniWebRequest::GET(const std::string& url, const Adb
return sResponse;
}
-jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a, const std::string& b) const
+jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a,
+ const std::string& b) const
{
- jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
- return env->NewObject(tupleClass->Get(), factory, env->NewStringUTF(a.c_str()), env->NewStringUTF(b.c_str()));
+ jmethodID factory = env->GetMethodID(tupleClass->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);
}
static JNINativeMethod methods[] =
« 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