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

Side by Side Diff: jni/JniFilterEngine.cpp

Issue 4761138508070912: Issue 1848 - Clean up local reference handling (Closed)
Patch Set: Some formatting issues Created Jan. 23, 2015, 12:30 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/JniFilterChangeCallback.cpp ('k') | jni/JniJsValue.cpp » ('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 16 matching lines...) Expand all
27 { 27 {
28 JniAddObjectToList(env, list, NewJniSubscription(env, *it)); 28 JniAddObjectToList(env, list, NewJniSubscription(env, *it));
29 } 29 }
30 30
31 return list; 31 return list;
32 } 32 }
33 33
34 static AdblockPlus::FilterEngine::ContentType ConvertContentType(JNIEnv *env, 34 static AdblockPlus::FilterEngine::ContentType ConvertContentType(JNIEnv *env,
35 jobject jContentType) 35 jobject jContentType)
36 { 36 {
37 jclass contentTypeClass = env->GetObjectClass(jContentType); 37 JniLocalReference<jclass> contentTypeClass(env,
38 jmethodID nameMethod = env->GetMethodID(contentTypeClass, "name", 38 env->GetObjectClass(jContentType));
39 "()Ljava/lang/String;"); 39 jmethodID nameMethod = env->GetMethodID(*contentTypeClass, "name",
40 jstring jValue = (jstring) env->CallObjectMethod(jContentType, nameMethod); 40 "()Ljava/lang/String;");
41 const std::string value = JniJavaToStdString(env, jValue); 41 JniLocalReference<jstring> jValue(env,
42 (jstring) env->CallObjectMethod(jContentType, nameMethod));
43 const std::string value = JniJavaToStdString(env, *jValue);
42 return AdblockPlus::FilterEngine::StringToContentType(value); 44 return AdblockPlus::FilterEngine::StringToContentType(value);
43 } 45 }
44 46
45 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong enginePtr) 47 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong enginePtr)
46 { 48 {
47 try 49 try
48 { 50 {
49 AdblockPlus::JsEnginePtr& jsEngine = *JniLongToTypePtr<AdblockPlus::JsEngine Ptr>(enginePtr); 51 AdblockPlus::JsEnginePtr& jsEngine = *JniLongToTypePtr<AdblockPlus::JsEngine Ptr>(enginePtr);
50 return JniPtrToLong(new AdblockPlus::FilterEngine(jsEngine)); 52 return JniPtrToLong(new AdblockPlus::FilterEngine(jsEngine));
51 } 53 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 89 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr);
88 90
89 try 91 try
90 { 92 {
91 std::vector<AdblockPlus::FilterPtr> filters = engine->GetListedFilters(); 93 std::vector<AdblockPlus::FilterPtr> filters = engine->GetListedFilters();
92 94
93 jobject list = NewJniArrayList(env); 95 jobject list = NewJniArrayList(env);
94 96
95 for (std::vector<AdblockPlus::FilterPtr>::iterator it = filters.begin(), end = filters.end(); it != end; it++) 97 for (std::vector<AdblockPlus::FilterPtr>::iterator it = filters.begin(), end = filters.end(); it != end; it++)
96 { 98 {
97 JniAddObjectToList(env, list, NewJniFilter(env, *it)); 99 JniAddObjectToList(env, list, *JniLocalReference<jobject>(env, NewJniFilte r(env, *it)));
98 } 100 }
99 101
100 return list; 102 return list;
101 } 103 }
102 CATCH_THROW_AND_RETURN(env, 0); 104 CATCH_THROW_AND_RETURN(env, 0);
103 } 105 }
104 106
105 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl) 107 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl)
106 { 108 {
107 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 109 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 { 177 {
176 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 178 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr);
177 179
178 try 180 try
179 { 181 {
180 engine->RemoveFilterChangeCallback(); 182 engine->RemoveFilterChangeCallback();
181 } 183 }
182 CATCH_AND_THROW(env) 184 CATCH_AND_THROW(env)
183 } 185 }
184 186
185 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, jlong ptr, jlong filterPtr) 187 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz,
188 jlong ptr, jlong filterPtr)
186 { 189 {
187 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 190 AdblockPlus::FilterEngine* engine =
188 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>( filterPtr); 191 JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
192 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>(
193 filterPtr);
189 194
190 AdblockPlus::FilterEngine::FilterChangeCallback filterCallback = 195 AdblockPlus::FilterEngine::FilterChangeCallback filterCallback =
191 std::tr1::bind(&JniFilterChangeCallback::Callback, callback, std::tr1::pla ceholders::_1, std::tr1::placeholders::_2); 196 std::tr1::bind(&JniFilterChangeCallback::Callback, callback,
197 std::tr1::placeholders::_1, std::tr1::placeholders::_2);
192 198
193 try 199 try
194 { 200 {
195 engine->SetFilterChangeCallback(filterCallback); 201 engine->SetFilterChangeCallback(filterCallback);
196 } 202 }
197 CATCH_AND_THROW(env) 203 CATCH_AND_THROW(env)
198 } 204 }
199 205
200 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr) 206 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr)
201 { 207 {
(...skipping 11 matching lines...) Expand all
213 std::tr1::placeholders::_1); 219 std::tr1::placeholders::_1);
214 } 220 }
215 221
216 try 222 try
217 { 223 {
218 engine->ForceUpdateCheck(updateCheckDoneCallback); 224 engine->ForceUpdateCheck(updateCheckDoneCallback);
219 } 225 }
220 CATCH_AND_THROW(env) 226 CATCH_AND_THROW(env)
221 } 227 }
222 228
223 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz, j long ptr, jstring jDomain) 229 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz,
230 jlong ptr, jstring jDomain)
224 { 231 {
225 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 232 AdblockPlus::FilterEngine* engine =
233 JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
226 234
227 std::string domain = JniJavaToStdString(env, jDomain); 235 std::string domain = JniJavaToStdString(env, jDomain);
228 236
229 try 237 try
230 { 238 {
231 std::vector<std::string> selectors = engine->GetElementHidingSelectors(domai n); 239 std::vector<std::string> selectors = engine->GetElementHidingSelectors(
240 domain);
232 241
233 jobject list = NewJniArrayList(env); 242 jobject list = NewJniArrayList(env);
234 243
235 for (std::vector<std::string>::iterator it = selectors.begin(), end = select ors.end(); it != end; it++) 244 for (std::vector<std::string>::iterator it = selectors.begin(), end =
245 selectors.end(); it != end; it++)
236 { 246 {
237 JniAddObjectToList(env, list, env->NewStringUTF(it->c_str())); 247 JniAddObjectToList(env, list,
248 *JniLocalReference<jstring>(env, env->NewStringUTF(it->c_str())));
238 } 249 }
239 250
240 return list; 251 return list;
241 } 252 }
242 CATCH_THROW_AND_RETURN(env, 0) 253 CATCH_THROW_AND_RETURN(env, 0)
243 } 254 }
244 255
245 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl) 256 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl)
246 { 257 {
247 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 258 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr);
248 259
249 std::string url = JniJavaToStdString(env, jUrl); 260 std::string url = JniJavaToStdString(env, jUrl);
250 AdblockPlus::FilterEngine::ContentType contentType = 261 AdblockPlus::FilterEngine::ContentType contentType =
251 ConvertContentType(env, jContentType); 262 ConvertContentType(env, jContentType);
252 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl); 263 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl);
253 264
254 try 265 try
255 { 266 {
256 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUr l); 267 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUr l);
257 268
258 return NewJniFilter(env, filter); 269 return NewJniFilter(env, filter);
259 } 270 }
260 CATCH_THROW_AND_RETURN(env, 0) 271 CATCH_THROW_AND_RETURN(env, 0)
261 } 272 }
262 273
263 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr, jstr ing jUrl, jobject jContentType, jobjectArray jDocumentUrls) 274 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr,
275 jstring jUrl, jobject jContentType, jobjectArray jDocumentUrls)
264 { 276 {
265 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 277 AdblockPlus::FilterEngine* engine =
278 JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
266 279
267 std::string url = JniJavaToStdString(env, jUrl); 280 std::string url = JniJavaToStdString(env, jUrl);
268 AdblockPlus::FilterEngine::ContentType contentType = 281 AdblockPlus::FilterEngine::ContentType contentType =
269 ConvertContentType(env, jContentType); 282 ConvertContentType(env, jContentType);
270 283
271 std::vector<std::string> documentUrls; 284 std::vector<std::string> documentUrls;
272 285
273 jsize len = env->GetArrayLength(jDocumentUrls); 286 jsize len = env->GetArrayLength(jDocumentUrls);
274 287
275 for (jsize i = 0; i < len; i++) 288 for (jsize i = 0; i < len; i++)
276 { 289 {
277 documentUrls.push_back(JniJavaToStdString(env, static_cast<jstring>(env->Get ObjectArrayElement(jDocumentUrls, i)))); 290 documentUrls.push_back(
291 JniJavaToStdString(env,
292 *JniLocalReference<jstring>(env,
293 static_cast<jstring>(env->GetObjectArrayElement(jDocumentUrls,
294 i)))));
278 } 295 }
279 296
280 try 297 try
281 { 298 {
282 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUr ls); 299 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType,
300 documentUrls);
283 301
284 return NewJniFilter(env, filter); 302 return NewJniFilter(env, filter);
285 } 303 }
286 CATCH_THROW_AND_RETURN(env, 0) 304 CATCH_THROW_AND_RETURN(env, 0)
287 } 305 }
288 306
289 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref) 307 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref)
290 { 308 {
291 AdblockPlus::FilterEngine* engine = JniLongToTypePtr < AdblockPlus::FilterEngi ne > (ptr); 309 AdblockPlus::FilterEngine* engine = JniLongToTypePtr < AdblockPlus::FilterEngi ne > (ptr);
292 310
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 { (char*)"matches", (char*)"(JLjava/lang/String;" TYP("FilterEngine$ContentTyp e") "[Ljava/lang/String;)" TYP("Filter"), (void*)JniMatchesMany }, 352 { (char*)"matches", (char*)"(JLjava/lang/String;" TYP("FilterEngine$ContentTyp e") "[Ljava/lang/String;)" TYP("Filter"), (void*)JniMatchesMany },
335 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG etPref }, 353 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG etPref },
336 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref }, 354 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref },
337 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 355 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
338 }; 356 };
339 357
340 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz) 358 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz)
341 { 359 {
342 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 360 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
343 } 361 }
OLDNEW
« no previous file with comments | « jni/JniFilterChangeCallback.cpp ('k') | jni/JniJsValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld