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

Delta Between Two Patch Sets: jni/JniFilterEngine.cpp

Issue 5327480814567424: Issue 1108 - Support notifications (Closed)
Left Patch Set: Created Jan. 30, 2015, 12:44 p.m.
Right Patch Set: Only one Notification displayed now Created Feb. 18, 2015, 3:42 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « jni/Android.mk ('k') | jni/JniNotification.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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);
108 std::string url = JniJavaToStdString(env, jUrl); 110 std::string url = JniJavaToStdString(env, jUrl);
109 111
110 try 112 try
111 { 113 {
112 AdblockPlus::SubscriptionPtr subscription = engine->GetSubscription(url); 114 AdblockPlus::SubscriptionPtr subscription = engine->GetSubscription(url);
113 115
114 return NewJniSubscription(env, subscription); 116 return NewJniSubscription(env, subscription);
115 } 117 }
116 CATCH_THROW_AND_RETURN(env, 0); 118 CATCH_THROW_AND_RETURN(env, 0);
117 } 119 }
118 120
119 static jobject JNICALL JniGetNotificationToShow(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl) 121 static jobject JNICALL JniGetNextNotificationToShow(JNIEnv* env, jclass clazz, j long ptr, jstring jUrl)
120 { 122 {
121 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 123 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr);
122 std::string url; 124 std::string url = JniJavaToStdString(env, jUrl);
123 if (jUrl)
124 {
125 url = JniJavaToStdString(env, jUrl);
126 }
127 125
128 try 126 try
129 { 127 {
130 AdblockPlus::NotificationPtr notification = engine->GetNextNotificationToSho w(url); 128 AdblockPlus::NotificationPtr notification = engine->GetNextNotificationToSho w(url);
131 129
132 return NewJniNotification(env, notification); 130 return NewJniNotification(env, notification);
133 } 131 }
134 CATCH_THROW_AND_RETURN(env, 0); 132 CATCH_THROW_AND_RETURN(env, 0);
135 } 133 }
136 134
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 { 191 {
194 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 192 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr);
195 193
196 try 194 try
197 { 195 {
198 engine->RemoveFilterChangeCallback(); 196 engine->RemoveFilterChangeCallback();
199 } 197 }
200 CATCH_AND_THROW(env) 198 CATCH_AND_THROW(env)
201 } 199 }
202 200
203 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, jlong ptr, jlong filterPtr) 201 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz,
204 { 202 jlong ptr, jlong filterPtr)
205 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 203 {
206 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>( filterPtr); 204 AdblockPlus::FilterEngine* engine =
205 JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
206 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>(
207 filterPtr);
207 208
208 AdblockPlus::FilterEngine::FilterChangeCallback filterCallback = 209 AdblockPlus::FilterEngine::FilterChangeCallback filterCallback =
209 std::tr1::bind(&JniFilterChangeCallback::Callback, callback, std::tr1::pla ceholders::_1, std::tr1::placeholders::_2); 210 std::tr1::bind(&JniFilterChangeCallback::Callback, callback,
211 std::tr1::placeholders::_1, std::tr1::placeholders::_2);
210 212
211 try 213 try
212 { 214 {
213 engine->SetFilterChangeCallback(filterCallback); 215 engine->SetFilterChangeCallback(filterCallback);
214 } 216 }
215 CATCH_AND_THROW(env) 217 CATCH_AND_THROW(env)
216 } 218 }
217 219
218 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr) 220 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr)
219 { 221 {
(...skipping 11 matching lines...) Expand all
231 std::tr1::placeholders::_1); 233 std::tr1::placeholders::_1);
232 } 234 }
233 235
234 try 236 try
235 { 237 {
236 engine->ForceUpdateCheck(updateCheckDoneCallback); 238 engine->ForceUpdateCheck(updateCheckDoneCallback);
237 } 239 }
238 CATCH_AND_THROW(env) 240 CATCH_AND_THROW(env)
239 } 241 }
240 242
241 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz, j long ptr, jstring jDomain) 243 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz,
242 { 244 jlong ptr, jstring jDomain)
243 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 245 {
246 AdblockPlus::FilterEngine* engine =
247 JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
244 248
245 std::string domain = JniJavaToStdString(env, jDomain); 249 std::string domain = JniJavaToStdString(env, jDomain);
246 250
247 try 251 try
248 { 252 {
249 std::vector<std::string> selectors = engine->GetElementHidingSelectors(domai n); 253 std::vector<std::string> selectors = engine->GetElementHidingSelectors(
254 domain);
250 255
251 jobject list = NewJniArrayList(env); 256 jobject list = NewJniArrayList(env);
252 257
253 for (std::vector<std::string>::iterator it = selectors.begin(), end = select ors.end(); it != end; it++) 258 for (std::vector<std::string>::iterator it = selectors.begin(), end =
259 selectors.end(); it != end; it++)
254 { 260 {
255 JniAddObjectToList(env, list, env->NewStringUTF(it->c_str())); 261 JniAddObjectToList(env, list,
262 *JniLocalReference<jstring>(env, env->NewStringUTF(it->c_str())));
256 } 263 }
257 264
258 return list; 265 return list;
259 } 266 }
260 CATCH_THROW_AND_RETURN(env, 0) 267 CATCH_THROW_AND_RETURN(env, 0)
261 } 268 }
262 269
263 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl) 270 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl)
264 { 271 {
265 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 272 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr);
266 273
267 std::string url = JniJavaToStdString(env, jUrl); 274 std::string url = JniJavaToStdString(env, jUrl);
268 AdblockPlus::FilterEngine::ContentType contentType = 275 AdblockPlus::FilterEngine::ContentType contentType =
269 ConvertContentType(env, jContentType); 276 ConvertContentType(env, jContentType);
270 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl); 277 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl);
271 278
272 try 279 try
273 { 280 {
274 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUr l); 281 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUr l);
275 282
276 return NewJniFilter(env, filter); 283 return NewJniFilter(env, filter);
277 } 284 }
278 CATCH_THROW_AND_RETURN(env, 0) 285 CATCH_THROW_AND_RETURN(env, 0)
279 } 286 }
280 287
281 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr, jstr ing jUrl, jobject jContentType, jobjectArray jDocumentUrls) 288 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr,
282 { 289 jstring jUrl, jobject jContentType, jobjectArray jDocumentUrls)
283 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); 290 {
291 AdblockPlus::FilterEngine* engine =
292 JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
284 293
285 std::string url = JniJavaToStdString(env, jUrl); 294 std::string url = JniJavaToStdString(env, jUrl);
286 AdblockPlus::FilterEngine::ContentType contentType = 295 AdblockPlus::FilterEngine::ContentType contentType =
287 ConvertContentType(env, jContentType); 296 ConvertContentType(env, jContentType);
288 297
289 std::vector<std::string> documentUrls; 298 std::vector<std::string> documentUrls;
290 299
291 jsize len = env->GetArrayLength(jDocumentUrls); 300 jsize len = env->GetArrayLength(jDocumentUrls);
292 301
293 for (jsize i = 0; i < len; i++) 302 for (jsize i = 0; i < len; i++)
294 { 303 {
295 documentUrls.push_back(JniJavaToStdString(env, static_cast<jstring>(env->Get ObjectArrayElement(jDocumentUrls, i)))); 304 documentUrls.push_back(
296 } 305 JniJavaToStdString(env,
297 306 *JniLocalReference<jstring>(env,
298 try 307 static_cast<jstring>(env->GetObjectArrayElement(jDocumentUrls,
299 { 308 i)))));
300 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUr ls); 309 }
310
311 try
312 {
313 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType,
314 documentUrls);
301 315
302 return NewJniFilter(env, filter); 316 return NewJniFilter(env, filter);
303 } 317 }
304 CATCH_THROW_AND_RETURN(env, 0) 318 CATCH_THROW_AND_RETURN(env, 0)
305 } 319 }
306 320
307 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref) 321 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref)
308 { 322 {
309 AdblockPlus::FilterEngine* engine = JniLongToTypePtr < AdblockPlus::FilterEngi ne > (ptr); 323 AdblockPlus::FilterEngine* engine = JniLongToTypePtr < AdblockPlus::FilterEngi ne > (ptr);
310 324
(...skipping 22 matching lines...) Expand all
333 CATCH_AND_THROW(env) 347 CATCH_AND_THROW(env)
334 } 348 }
335 349
336 static JNINativeMethod methods[] = 350 static JNINativeMethod methods[] =
337 { 351 {
338 { (char*)"ctor", (char*)"(J)J", (void*)JniCtor }, 352 { (char*)"ctor", (char*)"(J)J", (void*)JniCtor },
339 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun }, 353 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun },
340 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni GetFilter }, 354 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni GetFilter },
341 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed Filters }, 355 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed Filters },
342 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription") , (void*)JniGetSubscription }, 356 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription") , (void*)JniGetSubscription },
343 { (char*)"getNotificationToShow", (char*)"(JLjava/lang/String;)" TYP("Notifica tion"), (void*)JniGetNotificationToShow }, 357 { (char*)"getNextNotificationToShow", (char*)"(JLjava/lang/String;)" TYP("Noti fication"), (void*)JniGetNextNotificationToShow },
344 { (char*)"getListedSubscriptions", (char*)"(J)Ljava/util/List;", (void*)JniGet ListedSubscriptions }, 358 { (char*)"getListedSubscriptions", (char*)"(J)Ljava/util/List;", (void*)JniGet ListedSubscriptions },
345 { (char*)"fetchAvailableSubscriptions", (char*)"(J)Ljava/util/List;", (void*)J niFetchAvailableSubscriptions }, 359 { (char*)"fetchAvailableSubscriptions", (char*)"(J)Ljava/util/List;", (void*)J niFetchAvailableSubscriptions },
346 { (char*)"setUpdateAvailableCallback", (char*)"(JJ)V", (void*)JniSetUpdateAvai lableCallback }, 360 { (char*)"setUpdateAvailableCallback", (char*)"(JJ)V", (void*)JniSetUpdateAvai lableCallback },
347 { (char*)"removeUpdateAvailableCallback", (char*)"(J)V", (void*)JniRemoveUpdat eAvailableCallback }, 361 { (char*)"removeUpdateAvailableCallback", (char*)"(J)V", (void*)JniRemoveUpdat eAvailableCallback },
348 { (char*)"setFilterChangeCallback", (char*)"(JJ)V", (void*)JniSetFilterChangeC allback }, 362 { (char*)"setFilterChangeCallback", (char*)"(JJ)V", (void*)JniSetFilterChangeC allback },
349 { (char*)"removeFilterChangeCallback", (char*)"(J)V", (void*)JniRemoveFilterCh angeCallback }, 363 { (char*)"removeFilterChangeCallback", (char*)"(J)V", (void*)JniRemoveFilterCh angeCallback },
350 { (char*)"forceUpdateCheck", (char*)"(JJ)V", (void*)JniForceUpdateCheck }, 364 { (char*)"forceUpdateCheck", (char*)"(JJ)V", (void*)JniForceUpdateCheck },
351 { (char*)"getElementHidingSelectors", (char*)"(JLjava/lang/String;)Ljava/util/ List;", (void*)JniGetElementHidingSelectors }, 365 { (char*)"getElementHidingSelectors", (char*)"(JLjava/lang/String;)Ljava/util/ List;", (void*)JniGetElementHidingSelectors },
352 { (char*)"matches", (char*)"(JLjava/lang/String;" TYP("FilterEngine$ContentTyp e") "Ljava/lang/String;)" TYP("Filter"), (void*)JniMatches }, 366 { (char*)"matches", (char*)"(JLjava/lang/String;" TYP("FilterEngine$ContentTyp e") "Ljava/lang/String;)" TYP("Filter"), (void*)JniMatches },
353 { (char*)"matches", (char*)"(JLjava/lang/String;" TYP("FilterEngine$ContentTyp e") "[Ljava/lang/String;)" TYP("Filter"), (void*)JniMatchesMany }, 367 { (char*)"matches", (char*)"(JLjava/lang/String;" TYP("FilterEngine$ContentTyp e") "[Ljava/lang/String;)" TYP("Filter"), (void*)JniMatchesMany },
354 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG etPref }, 368 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG etPref },
355 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref }, 369 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref },
356 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 370 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
357 }; 371 };
358 372
359 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz) 373 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz)
360 { 374 {
361 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 375 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
362 } 376 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld