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

Side by Side Diff: libadblockplus-android/jni/JniFilterEngine.cpp

Issue 29465626: Noissue - move work with a raw jni pointer to a separate function (Closed) Base URL: github.com:abby-sergz/libadblockplus-android.git
Patch Set: address comment Created July 5, 2017, 1:45 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 | « no previous file | libadblockplus-android/jni/JniJsEngine.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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 27 matching lines...) Expand all
38 JniLocalReference<jclass> contentTypeClass(env, 38 JniLocalReference<jclass> contentTypeClass(env,
39 env->GetObjectClass(jContentType)); 39 env->GetObjectClass(jContentType));
40 jmethodID nameMethod = env->GetMethodID(*contentTypeClass, "name", 40 jmethodID nameMethod = env->GetMethodID(*contentTypeClass, "name",
41 "()Ljava/lang/String;"); 41 "()Ljava/lang/String;");
42 JniLocalReference<jstring> jValue(env, 42 JniLocalReference<jstring> jValue(env,
43 (jstring) env->CallObjectMethod(jContentType, nameMethod)); 43 (jstring) env->CallObjectMethod(jContentType, nameMethod));
44 const std::string value = JniJavaToStdString(env, *jValue); 44 const std::string value = JniJavaToStdString(env, *jValue);
45 return AdblockPlus::FilterEngine::StringToContentType(value); 45 return AdblockPlus::FilterEngine::StringToContentType(value);
46 } 46 }
47 47
48 namespace
49 {
50 AdblockPlus::FilterEnginePtr& GetFilterEnginePtrRef(jlong ptr)
51 {
52 return *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
53 }
54 }
55
48 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jsEnginePtr, jlong isAllowedConnectionCallbackPtr) 56 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jsEnginePtr, jlong isAllowedConnectionCallbackPtr)
49 { 57 {
50 try 58 try
51 { 59 {
52 AdblockPlus::JsEnginePtr& jsEngine = *JniLongToTypePtr<AdblockPlus::JsEngine Ptr>(jsEnginePtr); 60 AdblockPlus::JsEnginePtr& jsEngine = *JniLongToTypePtr<AdblockPlus::JsEngine Ptr>(jsEnginePtr);
53 AdblockPlus::FilterEnginePtr* filterEngine = NULL; 61 AdblockPlus::FilterEnginePtr* filterEngine = NULL;
54 62
55 if (isAllowedConnectionCallbackPtr != 0) 63 if (isAllowedConnectionCallbackPtr != 0)
56 { 64 {
57 AdblockPlus::FilterEngine::CreationParameters creationParameters; 65 AdblockPlus::FilterEngine::CreationParameters creationParameters;
(...skipping 30 matching lines...) Expand all
88 96
89 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) 97 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
90 { 98 {
91 delete JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); 99 delete JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
92 } 100 }
93 101
94 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr) 102 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr)
95 { 103 {
96 try 104 try
97 { 105 {
98 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::Filter EnginePtr>(ptr); 106 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
99 107
100 return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE; 108 return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE;
101 } 109 }
102 CATCH_THROW_AND_RETURN(env, JNI_FALSE); 110 CATCH_THROW_AND_RETURN(env, JNI_FALSE);
103 } 111 }
104 112
105 static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jText) 113 static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jText)
106 { 114 {
107 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 115 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
108 std::string text = JniJavaToStdString(env, jText); 116 std::string text = JniJavaToStdString(env, jText);
109 117
110 try 118 try
111 { 119 {
112 return NewJniFilter(env, engine->GetFilter(text)); 120 return NewJniFilter(env, engine->GetFilter(text));
113 } 121 }
114 CATCH_THROW_AND_RETURN(env, 0); 122 CATCH_THROW_AND_RETURN(env, 0);
115 } 123 }
116 124
117 static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr) 125 static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr)
118 { 126 {
119 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 127 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
120 128
121 try 129 try
122 { 130 {
123 std::vector<AdblockPlus::Filter> filters = engine->GetListedFilters(); 131 std::vector<AdblockPlus::Filter> filters = engine->GetListedFilters();
124 132
125 jobject list = NewJniArrayList(env); 133 jobject list = NewJniArrayList(env);
126 134
127 for (std::vector<AdblockPlus::Filter>::iterator it = filters.begin(), end = filters.end(); it != end; it++) 135 for (std::vector<AdblockPlus::Filter>::iterator it = filters.begin(), end = filters.end(); it != end; it++)
128 { 136 {
129 JniAddObjectToList(env, list, *JniLocalReference<jobject>(env, NewJniFilte r(env, std::move(*it)))); 137 JniAddObjectToList(env, list, *JniLocalReference<jobject>(env, NewJniFilte r(env, std::move(*it))));
130 } 138 }
131 139
132 return list; 140 return list;
133 } 141 }
134 CATCH_THROW_AND_RETURN(env, 0); 142 CATCH_THROW_AND_RETURN(env, 0);
135 } 143 }
136 144
137 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl) 145 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl)
138 { 146 {
139 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 147 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
140 std::string url = JniJavaToStdString(env, jUrl); 148 std::string url = JniJavaToStdString(env, jUrl);
141 149
142 try 150 try
143 { 151 {
144 return NewJniSubscription(env, engine->GetSubscription(url)); 152 return NewJniSubscription(env, engine->GetSubscription(url));
145 } 153 }
146 CATCH_THROW_AND_RETURN(env, 0); 154 CATCH_THROW_AND_RETURN(env, 0);
147 } 155 }
148 156
149 static void JNICALL JniShowNextNotification(JNIEnv* env, jclass clazz, jlong ptr , jstring jUrl) 157 static void JNICALL JniShowNextNotification(JNIEnv* env, jclass clazz, jlong ptr , jstring jUrl)
150 { 158 {
151 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 159 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
152 std::string url = JniJavaToStdString(env, jUrl); 160 std::string url = JniJavaToStdString(env, jUrl);
153 161
154 try 162 try
155 { 163 {
156 engine->ShowNextNotification(url); 164 engine->ShowNextNotification(url);
157 } 165 }
158 CATCH_AND_THROW(env); 166 CATCH_AND_THROW(env);
159 } 167 }
160 168
161 static void JNICALL JniSetShowNotificationCallback(JNIEnv* env, jclass clazz, 169 static void JNICALL JniSetShowNotificationCallback(JNIEnv* env, jclass clazz,
162 jlong ptr, jlong callbackPtr) 170 jlong ptr, jlong callbackPtr)
163 { 171 {
164 AdblockPlus::FilterEnginePtr& engine = 172 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
165 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
166 173
167 JniShowNotificationCallback* const callback = 174 JniShowNotificationCallback* const callback =
168 JniLongToTypePtr<JniShowNotificationCallback>(callbackPtr); 175 JniLongToTypePtr<JniShowNotificationCallback>(callbackPtr);
169 176
170 auto showNotificationCallback = [callback](AdblockPlus::Notification&& notific ation) 177 auto showNotificationCallback = [callback](AdblockPlus::Notification&& notific ation)
171 { 178 {
172 callback->Callback(std::move(notification)); 179 callback->Callback(std::move(notification));
173 }; 180 };
174 181
175 try 182 try
176 { 183 {
177 engine->SetShowNotificationCallback(showNotificationCallback); 184 engine->SetShowNotificationCallback(showNotificationCallback);
178 } 185 }
179 CATCH_AND_THROW(env) 186 CATCH_AND_THROW(env)
180 } 187 }
181 188
182 static void JNICALL JniRemoveShowNotificationCallback(JNIEnv* env, jclass clazz, jlong ptr) 189 static void JNICALL JniRemoveShowNotificationCallback(JNIEnv* env, jclass clazz, jlong ptr)
183 { 190 {
184 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 191 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
185 192
186 try 193 try
187 { 194 {
188 engine->RemoveShowNotificationCallback(); 195 engine->RemoveShowNotificationCallback();
189 } 196 }
190 CATCH_AND_THROW(env); 197 CATCH_AND_THROW(env);
191 } 198 }
192 199
193 static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon g ptr) 200 static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon g ptr)
194 { 201 {
195 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 202 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
196 203
197 try 204 try
198 { 205 {
199 return SubscriptionsToArrayList(env, engine->GetListedSubscriptions()); 206 return SubscriptionsToArrayList(env, engine->GetListedSubscriptions());
200 } 207 }
201 CATCH_THROW_AND_RETURN(env, 0); 208 CATCH_THROW_AND_RETURN(env, 0);
202 } 209 }
203 210
204 static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz, jlong ptr) 211 static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz, jlong ptr)
205 { 212 {
206 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 213 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
207 214
208 try 215 try
209 { 216 {
210 return SubscriptionsToArrayList(env, engine->FetchAvailableSubscriptions()); 217 return SubscriptionsToArrayList(env, engine->FetchAvailableSubscriptions());
211 } 218 }
212 CATCH_THROW_AND_RETURN(env, 0); 219 CATCH_THROW_AND_RETURN(env, 0);
213 } 220 }
214 221
215 static void JNICALL JniRemoveUpdateAvailableCallback(JNIEnv* env, jclass clazz, 222 static void JNICALL JniRemoveUpdateAvailableCallback(JNIEnv* env, jclass clazz,
216 jlong ptr) 223 jlong ptr)
217 { 224 {
218 AdblockPlus::FilterEnginePtr& engine = 225 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
219 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
220 try 226 try
221 { 227 {
222 engine->RemoveUpdateAvailableCallback(); 228 engine->RemoveUpdateAvailableCallback();
223 } 229 }
224 CATCH_AND_THROW(env) 230 CATCH_AND_THROW(env)
225 } 231 }
226 232
227 static void JNICALL JniSetUpdateAvailableCallback(JNIEnv* env, jclass clazz, 233 static void JNICALL JniSetUpdateAvailableCallback(JNIEnv* env, jclass clazz,
228 jlong ptr, jlong callbackPtr) 234 jlong ptr, jlong callbackPtr)
229 { 235 {
230 AdblockPlus::FilterEnginePtr& engine = 236 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
231 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
232 JniUpdateAvailableCallback* const callback = 237 JniUpdateAvailableCallback* const callback =
233 JniLongToTypePtr<JniUpdateAvailableCallback>(callbackPtr); 238 JniLongToTypePtr<JniUpdateAvailableCallback>(callbackPtr);
234 239
235 const AdblockPlus::FilterEngine::UpdateAvailableCallback updateAvailableCallba ck = 240 const AdblockPlus::FilterEngine::UpdateAvailableCallback updateAvailableCallba ck =
236 std::bind(&JniUpdateAvailableCallback::Callback, callback, 241 std::bind(&JniUpdateAvailableCallback::Callback, callback,
237 std::placeholders::_1); 242 std::placeholders::_1);
238 try 243 try
239 { 244 {
240 engine->SetUpdateAvailableCallback(updateAvailableCallback); 245 engine->SetUpdateAvailableCallback(updateAvailableCallback);
241 } 246 }
242 CATCH_AND_THROW(env) 247 CATCH_AND_THROW(env)
243 } 248 }
244 249
245 static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlo ng ptr) 250 static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlo ng ptr)
246 { 251 {
247 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 252 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
248 253
249 try 254 try
250 { 255 {
251 engine->RemoveFilterChangeCallback(); 256 engine->RemoveFilterChangeCallback();
252 } 257 }
253 CATCH_AND_THROW(env) 258 CATCH_AND_THROW(env)
254 } 259 }
255 260
256 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, 261 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz,
257 jlong ptr, jlong filterPtr) 262 jlong ptr, jlong filterPtr)
258 { 263 {
259 AdblockPlus::FilterEnginePtr& engine = 264 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
260 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
261 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>( 265 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>(
262 filterPtr); 266 filterPtr);
263 267
264 auto filterCallback = [callback](const std::string& arg, AdblockPlus::JsValue& & jsValue) 268 auto filterCallback = [callback](const std::string& arg, AdblockPlus::JsValue& & jsValue)
265 { 269 {
266 callback->Callback(arg, std::move(jsValue)); 270 callback->Callback(arg, std::move(jsValue));
267 }; 271 };
268 272
269 try 273 try
270 { 274 {
271 engine->SetFilterChangeCallback(filterCallback); 275 engine->SetFilterChangeCallback(filterCallback);
272 } 276 }
273 CATCH_AND_THROW(env) 277 CATCH_AND_THROW(env)
274 } 278 }
275 279
276 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr) 280 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr)
277 { 281 {
278 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 282 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
279 JniUpdateCheckDoneCallback* callback = 283 JniUpdateCheckDoneCallback* callback =
280 JniLongToTypePtr<JniUpdateCheckDoneCallback>(updaterPtr); 284 JniLongToTypePtr<JniUpdateCheckDoneCallback>(updaterPtr);
281 285
282 AdblockPlus::FilterEngine::UpdateCheckDoneCallback 286 AdblockPlus::FilterEngine::UpdateCheckDoneCallback
283 updateCheckDoneCallback = 0; 287 updateCheckDoneCallback = 0;
284 288
285 if (updaterPtr) 289 if (updaterPtr)
286 { 290 {
287 updateCheckDoneCallback = 291 updateCheckDoneCallback =
288 std::bind(&JniUpdateCheckDoneCallback::Callback, callback, 292 std::bind(&JniUpdateCheckDoneCallback::Callback, callback,
289 std::placeholders::_1); 293 std::placeholders::_1);
290 } 294 }
291 295
292 try 296 try
293 { 297 {
294 engine->ForceUpdateCheck(updateCheckDoneCallback); 298 engine->ForceUpdateCheck(updateCheckDoneCallback);
295 } 299 }
296 CATCH_AND_THROW(env) 300 CATCH_AND_THROW(env)
297 } 301 }
298 302
299 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz, 303 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz,
300 jlong ptr, jstring jDomain) 304 jlong ptr, jstring jDomain)
301 { 305 {
302 AdblockPlus::FilterEnginePtr& engine = 306 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
303 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
304 307
305 std::string domain = JniJavaToStdString(env, jDomain); 308 std::string domain = JniJavaToStdString(env, jDomain);
306 309
307 try 310 try
308 { 311 {
309 std::vector<std::string> selectors = engine->GetElementHidingSelectors( 312 std::vector<std::string> selectors = engine->GetElementHidingSelectors(
310 domain); 313 domain);
311 314
312 jobject list = NewJniArrayList(env); 315 jobject list = NewJniArrayList(env);
313 316
314 for (std::vector<std::string>::iterator it = selectors.begin(), end = 317 for (std::vector<std::string>::iterator it = selectors.begin(), end =
315 selectors.end(); it != end; it++) 318 selectors.end(); it != end; it++)
316 { 319 {
317 JniAddObjectToList(env, list, 320 JniAddObjectToList(env, list,
318 *JniLocalReference<jstring>(env, env->NewStringUTF(it->c_str()))); 321 *JniLocalReference<jstring>(env, env->NewStringUTF(it->c_str())));
319 } 322 }
320 323
321 return list; 324 return list;
322 } 325 }
323 CATCH_THROW_AND_RETURN(env, 0) 326 CATCH_THROW_AND_RETURN(env, 0)
324 } 327 }
325 328
326 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl) 329 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl)
327 { 330 {
328 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 331 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
329 332
330 std::string url = JniJavaToStdString(env, jUrl); 333 std::string url = JniJavaToStdString(env, jUrl);
331 AdblockPlus::FilterEngine::ContentType contentType = 334 AdblockPlus::FilterEngine::ContentType contentType =
332 ConvertContentType(env, jContentType); 335 ConvertContentType(env, jContentType);
333 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl); 336 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl);
334 337
335 try 338 try
336 { 339 {
337 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen tUrl); 340 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen tUrl);
338 341
(...skipping 16 matching lines...) Expand all
355 *JniLocalReference<jstring>(env, 358 *JniLocalReference<jstring>(env,
356 static_cast<jstring>( 359 static_cast<jstring>(
357 env->GetObjectArrayElement(jArray, i))))); 360 env->GetObjectArrayElement(jArray, i)))));
358 } 361 }
359 } 362 }
360 } 363 }
361 364
362 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr, 365 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr,
363 jstring jUrl, jobject jContentType, jobjectArray jDocumentUrls) 366 jstring jUrl, jobject jContentType, jobjectArray jDocumentUrls)
364 { 367 {
365 AdblockPlus::FilterEnginePtr& engine = 368 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
366 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
367 369
368 std::string url = JniJavaToStdString(env, jUrl); 370 std::string url = JniJavaToStdString(env, jUrl);
369 AdblockPlus::FilterEngine::ContentType contentType = 371 AdblockPlus::FilterEngine::ContentType contentType =
370 ConvertContentType(env, jContentType); 372 ConvertContentType(env, jContentType);
371 373
372 std::vector<std::string> documentUrls; 374 std::vector<std::string> documentUrls;
373 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); 375 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls);
374 376
375 try 377 try
376 { 378 {
377 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen tUrls); 379 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen tUrls);
378 380
379 return (filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0); 381 return (filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0);
380 } 382 }
381 CATCH_THROW_AND_RETURN(env, 0) 383 CATCH_THROW_AND_RETURN(env, 0)
382 } 384 }
383 385
384 static jboolean JNICALL JniIsDocumentWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr, 386 static jboolean JNICALL JniIsDocumentWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr,
385 jstring jUrl, jobjectArray jDocumentUrls) 387 jstring jUrl, jobjectArray jDocumentUrls)
386 { 388 {
387 AdblockPlus::FilterEnginePtr& engine = 389 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
388 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
389 390
390 std::string url = JniJavaToStdString(env, jUrl); 391 std::string url = JniJavaToStdString(env, jUrl);
391 std::vector<std::string> documentUrls; 392 std::vector<std::string> documentUrls;
392 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); 393 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls);
393 try 394 try
394 { 395 {
395 return engine->IsDocumentWhitelisted(url, documentUrls) ? 396 return engine->IsDocumentWhitelisted(url, documentUrls) ?
396 JNI_TRUE : JNI_FALSE; 397 JNI_TRUE : JNI_FALSE;
397 } 398 }
398 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 399 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
399 } 400 }
400 401
401 static jboolean JNICALL JniIsElemhideWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr, 402 static jboolean JNICALL JniIsElemhideWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr,
402 jstring jUrl, jobjectArray jDocumentUrls) 403 jstring jUrl, jobjectArray jDocumentUrls)
403 { 404 {
404 AdblockPlus::FilterEnginePtr& engine = 405 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
405 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
406 406
407 std::string url = JniJavaToStdString(env, jUrl); 407 std::string url = JniJavaToStdString(env, jUrl);
408 std::vector<std::string> documentUrls; 408 std::vector<std::string> documentUrls;
409 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); 409 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls);
410 try 410 try
411 { 411 {
412 return engine->IsElemhideWhitelisted(url, documentUrls) ? 412 return engine->IsElemhideWhitelisted(url, documentUrls) ?
413 JNI_TRUE : JNI_FALSE; 413 JNI_TRUE : JNI_FALSE;
414 } 414 }
415 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 415 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
416 } 416 }
417 417
418 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref) 418 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref)
419 { 419 {
420 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 420 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
421 421
422 std::string pref = JniJavaToStdString(env, jPref); 422 std::string pref = JniJavaToStdString(env, jPref);
423 423
424 try 424 try
425 { 425 {
426 return NewJniJsValue(env, engine->GetPref(pref)); 426 return NewJniJsValue(env, engine->GetPref(pref));
427 } 427 }
428 CATCH_THROW_AND_RETURN(env, 0) 428 CATCH_THROW_AND_RETURN(env, 0)
429 } 429 }
430 430
431 static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr ef, jlong jsValue) 431 static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr ef, jlong jsValue)
432 { 432 {
433 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 433 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
434 434
435 std::string pref = JniJavaToStdString(env, jPref); 435 std::string pref = JniJavaToStdString(env, jPref);
436 const AdblockPlus::JsValue& value = JniGetJsValue(jsValue); 436 const AdblockPlus::JsValue& value = JniGetJsValue(jsValue);
437 437
438 try 438 try
439 { 439 {
440 engine->SetPref(pref, value); 440 engine->SetPref(pref, value);
441 } 441 }
442 CATCH_AND_THROW(env) 442 CATCH_AND_THROW(env)
443 } 443 }
444 444
445 static jstring JNICALL JniGetHostFromURL(JNIEnv* env, jclass clazz, jlong ptr, j string jurl) 445 static jstring JNICALL JniGetHostFromURL(JNIEnv* env, jclass clazz, jlong ptr, j string jurl)
446 { 446 {
447 if (jurl == NULL) 447 if (jurl == NULL)
448 { 448 {
449 return NULL; 449 return NULL;
450 } 450 }
451 451
452 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 452 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
453 453
454 std::string url = JniJavaToStdString(env, jurl); 454 std::string url = JniJavaToStdString(env, jurl);
455 try 455 try
456 { 456 {
457 std::string host = engine->GetHostFromURL(url); 457 std::string host = engine->GetHostFromURL(url);
458 458
459 return JniStdStringToJava(env, host); 459 return JniStdStringToJava(env, host);
460 } 460 }
461 CATCH_THROW_AND_RETURN(env, 0) 461 CATCH_THROW_AND_RETURN(env, 0)
462 } 462 }
463 463
464 static void JNICALL JniSetAllowedConnectionType(JNIEnv* env, jclass clazz, jlong ptr, jstring jvalue) 464 static void JNICALL JniSetAllowedConnectionType(JNIEnv* env, jclass clazz, jlong ptr, jstring jvalue)
465 { 465 {
466 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 466 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
467 467
468 std::string stdValue; 468 std::string stdValue;
469 const std::string* value = (jvalue != NULL 469 const std::string* value = (jvalue != NULL
470 ? &(stdValue = JniJavaToStdString(env, jvalue)) 470 ? &(stdValue = JniJavaToStdString(env, jvalue))
471 : NULL); 471 : NULL);
472 472
473 try 473 try
474 { 474 {
475 engine->SetAllowedConnectionType(value); 475 engine->SetAllowedConnectionType(value);
476 } 476 }
477 CATCH_AND_THROW(env) 477 CATCH_AND_THROW(env)
478 } 478 }
479 479
480 static jstring JNICALL JniGetAllowedConnectionType(JNIEnv* env, jclass clazz, jl ong ptr) 480 static jstring JNICALL JniGetAllowedConnectionType(JNIEnv* env, jclass clazz, jl ong ptr)
481 { 481 {
482 try 482 try
483 { 483 {
484 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::Filter EnginePtr>(ptr); 484 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
485 std::unique_ptr<std::string> value = engine->GetAllowedConnectionType(); 485 std::unique_ptr<std::string> value = engine->GetAllowedConnectionType();
486 486
487 if (value == NULL) 487 if (value == NULL)
488 { 488 {
489 return NULL; 489 return NULL;
490 } 490 }
491 491
492 return JniStdStringToJava(env, *value.get()); 492 return JniStdStringToJava(env, *value.get());
493 } 493 }
494 CATCH_THROW_AND_RETURN(env, 0) 494 CATCH_THROW_AND_RETURN(env, 0)
495 } 495 }
496 496
497 static void JNICALL JniSetAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlong ptr, jboolean jvalue) 497 static void JNICALL JniSetAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlong ptr, jboolean jvalue)
498 { 498 {
499 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 499 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
500 500
501 try 501 try
502 { 502 {
503 engine->SetAAEnabled(jvalue == JNI_TRUE); 503 engine->SetAAEnabled(jvalue == JNI_TRUE);
504 } 504 }
505 CATCH_AND_THROW(env) 505 CATCH_AND_THROW(env)
506 } 506 }
507 507
508 static jboolean JNICALL JniIsAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlo ng ptr) 508 static jboolean JNICALL JniIsAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlo ng ptr)
509 { 509 {
510 try 510 try
511 { 511 {
512 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::Filter EnginePtr>(ptr); 512 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
513 return engine->IsAAEnabled() ? JNI_TRUE : JNI_FALSE; 513 return engine->IsAAEnabled() ? JNI_TRUE : JNI_FALSE;
514 } 514 }
515 CATCH_THROW_AND_RETURN(env, 0) 515 CATCH_THROW_AND_RETURN(env, 0)
516 } 516 }
517 517
518 static jstring JNICALL JniGetAcceptableAdsSubscriptionURL(JNIEnv* env, jclass cl azz, jlong ptr) 518 static jstring JNICALL JniGetAcceptableAdsSubscriptionURL(JNIEnv* env, jclass cl azz, jlong ptr)
519 { 519 {
520 try 520 try
521 { 521 {
522 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::Filter EnginePtr>(ptr); 522 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
523 std::string url = engine->GetAAUrl(); 523 std::string url = engine->GetAAUrl();
524 return JniStdStringToJava(env, url); 524 return JniStdStringToJava(env, url);
525 } 525 }
526 CATCH_THROW_AND_RETURN(env, 0) 526 CATCH_THROW_AND_RETURN(env, 0)
527 } 527 }
528 528
529 static JNINativeMethod methods[] = 529 static JNINativeMethod methods[] =
530 { 530 {
531 { (char*)"ctor", (char*)"(JJ)J", (void*)JniCtor }, 531 { (char*)"ctor", (char*)"(JJ)J", (void*)JniCtor },
532 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun }, 532 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun },
(...skipping 23 matching lines...) Expand all
556 { (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAds Enabled }, 556 { (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAds Enabled },
557 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna bled }, 557 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna bled },
558 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v oid*)JniGetAcceptableAdsSubscriptionURL }, 558 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v oid*)JniGetAcceptableAdsSubscriptionURL },
559 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 559 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
560 }; 560 };
561 561
562 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz) 562 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz)
563 { 563 {
564 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 564 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
565 } 565 }
OLDNEW
« no previous file with comments | « no previous file | libadblockplus-android/jni/JniJsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld