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: Formatting, explicit, unrelated change Created Feb. 3, 2015, 1:51 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-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())), "httpGET",
Felix Dahlke 2015/02/04 04:11:51 Since you changed this in JniUpdateAvailableCallba
René Jeschke 2015/02/04 12:02:47 Done.
49 "(Ljava/lang/String;Ljava/util/List;)" TYP("ServerResponse"));
47 50
48 AdblockPlus::ServerResponse sResponse; 51 AdblockPlus::ServerResponse sResponse;
49 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE; 52 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE;
50 53
51 if (method) 54 if (method)
52 { 55 {
53 jobject arrayList = NewJniArrayList(*env); 56 JniLocalReference<jobject> arrayList(*env, NewJniArrayList(*env));
54 57
55 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), en d = requestHeaders.end(); it != end; it++) 58 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(),
59 end = requestHeaders.end(); it != end; it++)
56 { 60 {
57 JniAddObjectToList(*env, arrayList, NewTuple(*env, it->first, it->second)) ; 61 JniLocalReference<jobject> tuple(*env,
62 NewTuple(*env, it->first, it->second));
63 JniAddObjectToList(*env, *arrayList, *tuple);
58 } 64 }
59 65
60 jobject response = env->CallObjectMethod(GetCallbackObject(), method, env->N ewStringUTF(url.c_str()), arrayList); 66 JniLocalReference<jobject> response(*env,
67 env->CallObjectMethod(GetCallbackObject(), method,
68 *JniLocalReference<jstring>(*env, env->NewStringUTF(url.c_str())),
69 *arrayList));
61 70
62 if (!env->ExceptionCheck()) 71 if (!env->ExceptionCheck())
63 { 72 {
64 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(), respo nse, "status"); 73 sResponse.status = JniGetLongField(*env, serverResponseClass->Get(),
65 sResponse.responseStatus = JniGetIntField(*env, serverResponseClass->Get() , response, "responseStatus"); 74 *response, "status");
66 sResponse.responseText = JniGetStringField(*env, serverResponseClass->Get( ), response, "response"); 75 sResponse.responseStatus = JniGetIntField(*env,
76 serverResponseClass->Get(), *response, "responseStatus");
77 sResponse.responseText = JniGetStringField(*env,
78 serverResponseClass->Get(), *response, "response");
67 // TODO: transform Headers 79 // TODO: transform Headers
68 } 80 }
69 } 81 }
70 82
71 CheckAndLogJavaException(*env); 83 CheckAndLogJavaException(*env);
72 84
73 return sResponse; 85 return sResponse;
74 } 86 }
75 87
76 jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a, const std::st ring& b) const 88 jobject JniWebRequest::NewTuple(JNIEnv* env, const std::string& a,
89 const std::string& b) const
77 { 90 {
78 jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>", "(Ljava/lang /String;Ljava/lang/String;)V"); 91 jmethodID factory = env->GetMethodID(tupleClass->Get(), "<init>",
79 return env->NewObject(tupleClass->Get(), factory, env->NewStringUTF(a.c_str()) , env->NewStringUTF(b.c_str())); 92 "(Ljava/lang/String;Ljava/lang/String;)V");
93
94 JniLocalReference<jstring> strA(env, env->NewStringUTF(a.c_str()));
95 JniLocalReference<jstring> strB(env, env->NewStringUTF(b.c_str()));
96
97 return env->NewObject(tupleClass->Get(), factory, *strA, *strB);
80 } 98 }
81 99
82 static JNINativeMethod methods[] = 100 static JNINativeMethod methods[] =
83 { 101 {
84 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor }, 102 { (char*)"ctor", (char*)"(Ljava/lang/Object;)J", (void*)JniCtor },
85 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 103 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
86 }; 104 };
87 105
88 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest _registerNatives(JNIEnv *env, jclass clazz) 106 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_WebRequest _registerNatives(JNIEnv *env, jclass clazz)
89 { 107 {
90 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 108 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
91 } 109 }
OLDNEW

Powered by Google App Engine
This is Rietveld