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: More formatting issues Created Feb. 4, 2015, 12:03 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 20 matching lines...) Expand all
31 delete JniLongToTypePtr<AdblockPlus::WebRequestPtr>(ptr); 31 delete JniLongToTypePtr<AdblockPlus::WebRequestPtr>(ptr);
32 } 32 }
33 33
34 JniWebRequest::JniWebRequest(JNIEnv* env, jobject callbackObject) 34 JniWebRequest::JniWebRequest(JNIEnv* env, jobject callbackObject)
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,
42 const AdblockPlus::HeaderList& requestHeaders) const
42 { 43 {
43 JNIEnvAcquire env(GetJavaVM()); 44 JNIEnvAcquire env(GetJavaVM());
44 45
45 jclass clazz = env->GetObjectClass(GetCallbackObject()); 46 jmethodID method = env->GetMethodID(
46 jmethodID method = env->GetMethodID(clazz, "httpGET", "(Ljava/lang/String;Ljav a/util/List;)" TYP("ServerResponse")); 47 *JniLocalReference<jclass>(*env,
48 env->GetObjectClass(GetCallbackObject())),
49 "httpGET",
50 "(Ljava/lang/String;Ljava/util/List;)" TYP("ServerResponse"));
47 51
48 AdblockPlus::ServerResponse sResponse; 52 AdblockPlus::ServerResponse sResponse;
49 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE; 53 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE;
50 54
51 if (method) 55 if (method)
52 { 56 {
53 jobject arrayList = NewJniArrayList(*env); 57 JniLocalReference<jobject> arrayList(*env, NewJniArrayList(*env));
54 58
55 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), en d = requestHeaders.end(); it != end; it++) 59 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(),
60 end = requestHeaders.end(); it != end; it++)
56 { 61 {
57 JniAddObjectToList(*env, arrayList, NewTuple(*env, it->first, it->second)) ; 62 JniLocalReference<jobject> tuple(*env,
63 NewTuple(*env, it->first, it->second));
64 JniAddObjectToList(*env, *arrayList, *tuple);
58 } 65 }
59 66
60 jobject response = env->CallObjectMethod(GetCallbackObject(), method, env->N ewStringUTF(url.c_str()), arrayList); 67 JniLocalReference<jobject> response(*env,
68 env->CallObjectMethod(GetCallbackObject(), method,
69 *JniLocalReference<jstring>(*env, env->NewStringUTF(url.c_str())),
70 *arrayList));
61 71
62 if (!env->ExceptionCheck()) 72 if (!env->ExceptionCheck())
63 { 73 {
64 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(), respo nse, "status"); 74 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(),
65 sResponse.responseStatus = JniGetIntField(*env, serverResponseClass->Get() , response, "responseStatus"); 75 *response, "status");
66 sResponse.responseText = JniGetStringField(*env, serverResponseClass->Get( ), response, "response"); 76 sResponse.responseStatus = JniGetIntField(*env,
77 serverResponseClass->Get(), *response, "responseStatus");
78 sResponse.responseText = JniGetStringField(*env,
79 serverResponseClass->Get(), *response, "response");
67 // TODO: transform Headers 80 // TODO: transform Headers
68 } 81 }
69 } 82 }
70 83
71 CheckAndLogJavaException(*env); 84 CheckAndLogJavaException(*env);
72 85
73 return sResponse; 86 return sResponse;
74 } 87 }
75 88
76 jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a, const std::st ring& b) const 89 jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a,
90 const std::string& b) const
77 { 91 {
78 jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>", "(Ljava/lang /String;Ljava/lang/String;)V"); 92 jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>",
79 return env->NewObject(tupleClass->Get(), factory, env->NewStringUTF(a.c_str()) , env->NewStringUTF(b.c_str())); 93 "(Ljava/lang/String;Ljava/lang/String;)V");
94
95 JniLocalReference<jstring> strA(env, env->NewStringUTF(a.c_str()));
96 JniLocalReference<jstring> strB(env, env->NewStringUTF(b.c_str()));
97
98 return env->NewObject(tupleClass->Get(), factory, *strA, *strB);
80 } 99 }
81 100
82 static JNINativeMethod methods[] = 101 static JNINativeMethod methods[] =
83 { 102 {
84 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor }, 103 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor },
85 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 104 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
86 }; 105 };
87 106
88 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest _registerNatives(JNIEnv *env, jclass clazz) 107 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest _registerNatives(JNIEnv *env, jclass clazz)
89 { 108 {
90 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 109 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
91 } 110 }
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