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