OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 else if (manualUpdate) | 176 else if (manualUpdate) |
177 { | 177 { |
178 if (mid) | 178 if (mid) |
179 jniEnv->CallVoidMethod(jniObject, mid, NULL, NULL); | 179 jniEnv->CallVoidMethod(jniObject, mid, NULL, NULL); |
180 } | 180 } |
181 | 181 |
182 if (stat == JNI_EDETACHED) | 182 if (stat == JNI_EDETACHED) |
183 globalJvm->DetachCurrentThread(); | 183 globalJvm->DetachCurrentThread(); |
184 } | 184 } |
185 | 185 |
| 186 void ThrowJavaException(JNIEnv* env, const std::string& message) |
| 187 { |
| 188 jclass exceptionClass = env->FindClass("java/lang/Exception"); |
| 189 env->ThrowNew(exceptionClass, message.c_str()); |
| 190 } |
| 191 |
| 192 void ThrowJavaException(JNIEnv* env, const std::exception& e) |
| 193 { |
| 194 ThrowJavaException(env, std::string("Exception from libadblockplus: ") + e.wha
t()); |
| 195 } |
| 196 |
| 197 void ThrowJavaException(JNIEnv* env) |
| 198 { |
| 199 ThrowJavaException(env, "Unknown exception from libadblockplus"); |
| 200 } |
| 201 |
186 jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) | 202 jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) |
187 { | 203 { |
188 return JNI_VERSION_1_6; | 204 return JNI_VERSION_1_6; |
189 } | 205 } |
190 | 206 |
191 void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) | 207 void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) |
192 { | 208 { |
193 } | 209 } |
194 | 210 |
195 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_initialize(JNIEnv
*pEnv, jobject pObject, jstring basepath) | 211 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_initialize(JNIEnv
*pEnv, jobject pObject, jstring basepath) |
196 { | 212 { |
197 D(D_WARN, "nativeInitialize()"); | 213 D(D_WARN, "nativeInitialize()"); |
198 int status = pEnv->GetJavaVM(&globalJvm); | 214 try |
| 215 { |
| 216 int status = pEnv->GetJavaVM(&globalJvm); |
199 | 217 |
200 jniObject = pEnv->NewGlobalRef(pObject); | 218 jniObject = pEnv->NewGlobalRef(pObject); |
201 | 219 |
202 const std::string path = GetString(pEnv, basepath); | 220 const std::string path = GetString(pEnv, basepath); |
203 | 221 |
204 AdblockPlus::AppInfo appInfo; | 222 AdblockPlus::AppInfo appInfo; |
205 // TODO Should be extracted from the manifest | 223 // TODO Should be extracted from the manifest |
206 appInfo.version = "1.1.2"; | 224 appInfo.version = "1.1.2"; |
207 appInfo.name = "adblockplusandroid"; | 225 appInfo.name = "adblockplusandroid"; |
208 appInfo.application = "android"; | 226 appInfo.application = "android"; |
209 | 227 |
210 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New(appInfo)); | 228 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New(appInfo)); |
211 | 229 |
212 AdblockPlus::DefaultFileSystem* defaultFileSystem = new AdblockPlus::DefaultFi
leSystem(); | 230 AdblockPlus::DefaultFileSystem* defaultFileSystem = new AdblockPlus::Default
FileSystem(); |
213 AndroidLogSystem* androidLogSystem = new AndroidLogSystem(); | 231 AndroidLogSystem* androidLogSystem = new AndroidLogSystem(); |
214 AndroidWebRequest* androidWebRequest = new AndroidWebRequest(globalJvm); | 232 AndroidWebRequest* androidWebRequest = new AndroidWebRequest(globalJvm); |
215 | 233 |
216 defaultFileSystem->SetBasePath(path); | 234 defaultFileSystem->SetBasePath(path); |
217 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(androidLogSystem)); | 235 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(androidLogSystem)); |
218 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(defaultFileSystem)); | 236 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(defaultFileSystem)); |
219 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(androidWebRequest)); | 237 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(androidWebRequest)); |
220 jsEngine->SetEventCallback("updateAvailable", std::tr1::bind(&UpdateAvailableC
allback, std::tr1::placeholders::_1)); | 238 jsEngine->SetEventCallback("updateAvailable", std::tr1::bind(&UpdateAvailabl
eCallback, std::tr1::placeholders::_1)); |
221 | 239 |
222 filterEngine = new AdblockPlus::FilterEngine(jsEngine); | 240 filterEngine = new AdblockPlus::FilterEngine(jsEngine); |
223 filterEngine->SetFilterChangeCallback(&FilterChangedCallback); | 241 filterEngine->SetFilterChangeCallback(&FilterChangedCallback); |
| 242 } |
| 243 catch (const std::exception& e) |
| 244 { |
| 245 ThrowJavaException(pEnv, e); |
| 246 } |
| 247 catch (...) |
| 248 { |
| 249 ThrowJavaException(pEnv); |
| 250 } |
224 } | 251 } |
225 | 252 |
226 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_release(JNIEnv *pE
nv, jobject) | 253 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_release(JNIEnv *pE
nv, jobject) |
227 { | 254 { |
228 D(D_WARN, "nativeRelease()"); | 255 D(D_WARN, "nativeRelease()"); |
229 AdblockPlus::JsEnginePtr jsEngine = filterEngine->GetJsEngine(); | 256 try |
230 jsEngine->RemoveEventCallback("updateAvailable"); | 257 { |
231 filterEngine->RemoveFilterChangeCallback(); | 258 AdblockPlus::JsEnginePtr jsEngine = filterEngine->GetJsEngine(); |
232 delete filterEngine; | 259 jsEngine->RemoveEventCallback("updateAvailable"); |
233 pEnv->DeleteGlobalRef(jniObject); | 260 filterEngine->RemoveFilterChangeCallback(); |
234 jniObject = NULL; | 261 delete filterEngine; |
235 globalJvm = NULL; | 262 pEnv->DeleteGlobalRef(jniObject); |
| 263 jniObject = NULL; |
| 264 globalJvm = NULL; |
| 265 } |
| 266 catch (const std::exception& e) |
| 267 { |
| 268 ThrowJavaException(pEnv, e); |
| 269 } |
| 270 catch (...) |
| 271 { |
| 272 ThrowJavaException(pEnv); |
| 273 } |
236 } | 274 } |
237 | 275 |
238 JNIEXPORT jboolean JNICALL Java_org_adblockplus_android_ABPEngine_isFirstRun(JNI
Env *pEnv, jobject) | 276 JNIEXPORT jboolean JNICALL Java_org_adblockplus_android_ABPEngine_isFirstRun(JNI
Env *pEnv, jobject) |
239 { | 277 { |
240 return filterEngine->IsFirstRun() ? JNI_TRUE : JNI_FALSE; | 278 try |
| 279 { |
| 280 return filterEngine->IsFirstRun() ? JNI_TRUE : JNI_FALSE; |
| 281 } |
| 282 catch (const std::exception& e) |
| 283 { |
| 284 ThrowJavaException(pEnv, e); |
| 285 } |
| 286 catch (...) |
| 287 { |
| 288 ThrowJavaException(pEnv); |
| 289 } |
| 290 return JNI_FALSE; |
241 } | 291 } |
242 | 292 |
243 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getListedS
ubscriptions(JNIEnv *pEnv, jobject) | 293 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getListedS
ubscriptions(JNIEnv *pEnv, jobject) |
244 { | 294 { |
245 D(D_WARN, "getListedSubscriptions()"); | 295 D(D_WARN, "getListedSubscriptions()"); |
246 const std::vector<AdblockPlus::SubscriptionPtr> subscriptions = filterEngine->
GetListedSubscriptions(); | 296 try |
247 return subscriptionsAsJavaArray(pEnv, subscriptions); | 297 { |
| 298 const std::vector<AdblockPlus::SubscriptionPtr> subscriptions = filterEngine
->GetListedSubscriptions(); |
| 299 return subscriptionsAsJavaArray(pEnv, subscriptions); |
| 300 } |
| 301 catch (const std::exception& e) |
| 302 { |
| 303 ThrowJavaException(pEnv, e); |
| 304 } |
| 305 catch (...) |
| 306 { |
| 307 ThrowJavaException(pEnv); |
| 308 } |
| 309 return 0; |
248 } | 310 } |
249 | 311 |
250 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getRecomme
ndedSubscriptions(JNIEnv *pEnv, jobject) | 312 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getRecomme
ndedSubscriptions(JNIEnv *pEnv, jobject) |
251 { | 313 { |
252 D(D_WARN, "getRecommendedSubscriptions()"); | 314 D(D_WARN, "getRecommendedSubscriptions()"); |
253 try | 315 try |
254 { | 316 { |
255 const std::vector<AdblockPlus::SubscriptionPtr> subscriptions = filterEngine
->FetchAvailableSubscriptions(); | 317 const std::vector<AdblockPlus::SubscriptionPtr> subscriptions = filterEngine
->FetchAvailableSubscriptions(); |
256 return subscriptionsAsJavaArray(pEnv, subscriptions); | 318 return subscriptionsAsJavaArray(pEnv, subscriptions); |
257 } | 319 } |
258 catch (const std::exception& e) | 320 catch (const std::exception& e) |
259 { | 321 { |
260 D(D_ERROR, "Exception: %s", e.what()); | 322 ThrowJavaException(pEnv, e); |
261 } | 323 } |
262 catch (...) | 324 catch (...) |
263 { | 325 { |
264 D(D_ERROR, "Unknown exception"); | 326 ThrowJavaException(pEnv); |
265 } | 327 } |
266 return NULL; | 328 return 0; |
267 } | 329 } |
268 | 330 |
269 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_addSubscription(JN
IEnv *pEnv, jobject, jstring url) | 331 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_addSubscription(JN
IEnv *pEnv, jobject, jstring url) |
270 { | 332 { |
271 D(D_WARN, "addSubscription()"); | 333 D(D_WARN, "addSubscription()"); |
272 const std::string surl = GetString(pEnv, url); | 334 try |
273 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(surl
); | 335 { |
274 subscription->AddToList(); | 336 const std::string surl = GetString(pEnv, url); |
| 337 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(su
rl); |
| 338 subscription->AddToList(); |
| 339 } |
| 340 catch (const std::exception& e) |
| 341 { |
| 342 ThrowJavaException(pEnv, e); |
| 343 } |
| 344 catch (...) |
| 345 { |
| 346 ThrowJavaException(pEnv); |
| 347 } |
275 } | 348 } |
276 | 349 |
277 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_removeSubscription
(JNIEnv *pEnv, jobject, jstring url) | 350 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_removeSubscription
(JNIEnv *pEnv, jobject, jstring url) |
278 { | 351 { |
279 D(D_WARN, "removeSubscription()"); | 352 D(D_WARN, "removeSubscription()"); |
280 const std::string surl = GetString(pEnv, url); | 353 try |
281 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(surl
); | |
282 if (subscription->IsListed()) | |
283 { | 354 { |
284 subscription->RemoveFromList(); | 355 const std::string surl = GetString(pEnv, url); |
| 356 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(su
rl); |
| 357 if (subscription->IsListed()) |
| 358 { |
| 359 subscription->RemoveFromList(); |
| 360 } |
| 361 } |
| 362 catch (const std::exception& e) |
| 363 { |
| 364 ThrowJavaException(pEnv, e); |
| 365 } |
| 366 catch (...) |
| 367 { |
| 368 ThrowJavaException(pEnv); |
285 } | 369 } |
286 } | 370 } |
287 | 371 |
288 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_refreshSubscriptio
n(JNIEnv *pEnv, jobject, jstring url) | 372 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_refreshSubscriptio
n(JNIEnv *pEnv, jobject, jstring url) |
289 { | 373 { |
290 D(D_WARN, "refreshSubscription()"); | 374 D(D_WARN, "refreshSubscription()"); |
291 const std::string surl = GetString(pEnv, url); | 375 try |
292 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(surl
); | 376 { |
293 subscription->UpdateFilters(); | 377 const std::string surl = GetString(pEnv, url); |
| 378 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(su
rl); |
| 379 subscription->UpdateFilters(); |
| 380 } |
| 381 catch (const std::exception& e) |
| 382 { |
| 383 ThrowJavaException(pEnv, e); |
| 384 } |
| 385 catch (...) |
| 386 { |
| 387 ThrowJavaException(pEnv); |
| 388 } |
294 } | 389 } |
295 | 390 |
296 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_actualizeSubscript
ionStatus(JNIEnv *pEnv, jobject, jstring url) | 391 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_actualizeSubscript
ionStatus(JNIEnv *pEnv, jobject, jstring url) |
297 { | 392 { |
298 D(D_WARN, "actualizeSubscriptionStatus()"); | 393 D(D_WARN, "actualizeSubscriptionStatus()"); |
299 const std::string surl = GetString(pEnv, url); | 394 try |
300 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(surl
); | 395 { |
301 UpdateSubscriptionStatus(subscription); | 396 const std::string surl = GetString(pEnv, url); |
| 397 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(su
rl); |
| 398 UpdateSubscriptionStatus(subscription); |
| 399 } |
| 400 catch (const std::exception& e) |
| 401 { |
| 402 ThrowJavaException(pEnv, e); |
| 403 } |
| 404 catch (...) |
| 405 { |
| 406 ThrowJavaException(pEnv); |
| 407 } |
302 } | 408 } |
303 | 409 |
304 JNIEXPORT jboolean JNICALL Java_org_adblockplus_android_ABPEngine_matches(JNIEnv
*pEnv, jobject, jstring url, jstring contentType, jstring documentUrl) | 410 JNIEXPORT jboolean JNICALL Java_org_adblockplus_android_ABPEngine_matches(JNIEnv
*pEnv, jobject, jstring url, jstring contentType, jstring documentUrl) |
305 { | 411 { |
306 const std::string surl = GetString(pEnv, url); | 412 try |
307 const std::string stype = GetString(pEnv, contentType); | 413 { |
308 const std::string sdoc = GetString(pEnv, documentUrl); | 414 const std::string surl = GetString(pEnv, url); |
| 415 const std::string stype = GetString(pEnv, contentType); |
| 416 const std::string sdoc = GetString(pEnv, documentUrl); |
309 | 417 |
310 AdblockPlus::FilterPtr filter = filterEngine->Matches(surl, stype, sdoc); | 418 AdblockPlus::FilterPtr filter = filterEngine->Matches(surl, stype, sdoc); |
311 | 419 |
312 if (! filter) | 420 if (! filter) |
313 return JNI_FALSE; | 421 return JNI_FALSE; |
314 | 422 |
315 // hack: if there is no referrer block only if filter is domain-specific | 423 // hack: if there is no referrer block only if filter is domain-specific |
316 // (to re-enable in-app ads blocking, proposed on 12.11.2012 Monday meeting) | 424 // (to re-enable in-app ads blocking, proposed on 12.11.2012 Monday meeting) |
317 // documentUrl contains the referrer (Android application special case) | 425 // documentUrl contains the referrer (Android application special case) |
318 if (sdoc.empty() && (filter->GetProperty("text")->AsString()).find("||") != st
d::string::npos) | 426 if (sdoc.empty() && (filter->GetProperty("text")->AsString()).find("||") !=
std::string::npos) |
319 return JNI_FALSE; | 427 return JNI_FALSE; |
320 | 428 |
321 return filter->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION ? JNI_FALSE :
JNI_TRUE; | 429 return filter->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION ? JNI_FALSE
: JNI_TRUE; |
| 430 } |
| 431 catch (const std::exception& e) |
| 432 { |
| 433 ThrowJavaException(pEnv, e); |
| 434 } |
| 435 catch (...) |
| 436 { |
| 437 ThrowJavaException(pEnv); |
| 438 } |
| 439 return JNI_FALSE; |
322 } | 440 } |
323 | 441 |
324 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getSelecto
rsForDomain(JNIEnv *pEnv, jobject, jstring domain) | 442 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getSelecto
rsForDomain(JNIEnv *pEnv, jobject, jstring domain) |
325 { | 443 { |
326 const std::string sdomain = GetString(pEnv, domain); | 444 try |
327 const std::vector<std::string> selectors = filterEngine->GetElementHidingSelec
tors(sdomain); | 445 { |
| 446 const std::string sdomain = GetString(pEnv, domain); |
| 447 const std::vector<std::string> selectors = filterEngine->GetElementHidingSel
ectors(sdomain); |
328 | 448 |
329 static jclass cls = reinterpret_cast<jclass>(pEnv->NewGlobalRef(pEnv->FindClas
s("java/lang/String"))); | 449 static jclass cls = reinterpret_cast<jclass>(pEnv->NewGlobalRef(pEnv->FindCl
ass("java/lang/String"))); |
330 | 450 |
331 D(D_WARN, "Selectors: %d", selectors.size()); | 451 D(D_WARN, "Selectors: %d", selectors.size()); |
332 const jobjectArray ret = (jobjectArray) pEnv->NewObjectArray(selectors.size(),
cls, NULL); | 452 const jobjectArray ret = (jobjectArray) pEnv->NewObjectArray(selectors.size(
), cls, NULL); |
333 | 453 |
334 int i = 0; | 454 int i = 0; |
335 for (std::vector<std::string>::const_iterator it = selectors.begin(); | 455 for (std::vector<std::string>::const_iterator it = selectors.begin(); |
336 it != selectors.end(); it++) | 456 it != selectors.end(); it++) |
| 457 { |
| 458 jstring selector = pEnv->NewStringUTF((*it).c_str()); |
| 459 pEnv->SetObjectArrayElement(ret, i, selector); |
| 460 pEnv->DeleteLocalRef(selector); |
| 461 i++; |
| 462 } |
| 463 |
| 464 return ret; |
| 465 } |
| 466 catch (const std::exception& e) |
337 { | 467 { |
338 jstring selector = pEnv->NewStringUTF((*it).c_str()); | 468 ThrowJavaException(pEnv, e); |
339 pEnv->SetObjectArrayElement(ret, i, selector); | |
340 pEnv->DeleteLocalRef(selector); | |
341 i++; | |
342 } | 469 } |
343 | 470 catch (...) |
344 return ret; | 471 { |
| 472 ThrowJavaException(pEnv); |
| 473 } |
| 474 return 0; |
345 } | 475 } |
346 | 476 |
347 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_checkUpdates(JNIEn
v *pEnv, jobject) | 477 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_checkUpdates(JNIEn
v *pEnv, jobject) |
348 { | 478 { |
349 manualUpdate = true; | 479 try |
350 filterEngine->ForceUpdateCheck(UpdaterCallback); | 480 { |
| 481 manualUpdate = true; |
| 482 filterEngine->ForceUpdateCheck(UpdaterCallback); |
| 483 } |
| 484 catch (const std::exception& e) |
| 485 { |
| 486 ThrowJavaException(pEnv, e); |
| 487 } |
| 488 catch (...) |
| 489 { |
| 490 ThrowJavaException(pEnv); |
| 491 } |
351 } | 492 } |
OLD | NEW |