| LEFT | RIGHT |
| 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-2016 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 params.push_back(shared_from_this()); | 127 params.push_back(shared_from_this()); |
| 128 JsValuePtr result = func->Call(params); | 128 JsValuePtr result = func->Call(params); |
| 129 return result->AsBool(); | 129 return result->AsBool(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 bool Subscription::operator==(const Subscription& subscription) const | 132 bool Subscription::operator==(const Subscription& subscription) const |
| 133 { | 133 { |
| 134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt
ring(); | 134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt
ring(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 FilterEngine::FilterEngine(JsEnginePtr jsEngine, | 137 FilterEngine::FilterEngine(JsEnginePtr jsEngine, |
| 138 const FilterEngine::Prefs& preconfiguredPrefs) | 138 const FilterEngine::Prefs& preconfiguredPrefs) |
| 139 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0) | 139 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0) |
| 140 { | 140 { |
| 141 jsEngine->SetEventCallback("_init", std::tr1::bind(&FilterEngine::InitDone, | 141 jsEngine->SetEventCallback("_init", std::bind(&FilterEngine::InitDone, |
| 142 this, std::tr1::placeholders::_1)); | 142 this, std::placeholders::_1)); |
| 143 | 143 |
| 144 { | 144 { |
| 145 // Lock the JS engine while we are loading scripts, no timeouts should fire | 145 // Lock the JS engine while we are loading scripts, no timeouts should fire |
| 146 // until we are done. | 146 // until we are done. |
| 147 const JsContext context(jsEngine); | 147 const JsContext context(jsEngine); |
| 148 | 148 |
| 149 // Set the preconfigured prefs | 149 // Set the preconfigured prefs |
| 150 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject(); | 150 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject(); |
| 151 for (FilterEngine::Prefs::const_iterator it = preconfiguredPrefs.begin(); | 151 for (FilterEngine::Prefs::const_iterator it = preconfiguredPrefs.begin(); |
| 152 it != preconfiguredPrefs.end(); it++) | 152 it != preconfiguredPrefs.end(); it++) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 171 ContentTypeMap CreateContentTypeMap() | 171 ContentTypeMap CreateContentTypeMap() |
| 172 { | 172 { |
| 173 ContentTypeMap contentTypes; | 173 ContentTypeMap contentTypes; |
| 174 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER"; | 174 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER"; |
| 175 contentTypes[FilterEngine::CONTENT_TYPE_SCRIPT] = "SCRIPT"; | 175 contentTypes[FilterEngine::CONTENT_TYPE_SCRIPT] = "SCRIPT"; |
| 176 contentTypes[FilterEngine::CONTENT_TYPE_IMAGE] = "IMAGE"; | 176 contentTypes[FilterEngine::CONTENT_TYPE_IMAGE] = "IMAGE"; |
| 177 contentTypes[FilterEngine::CONTENT_TYPE_STYLESHEET] = "STYLESHEET"; | 177 contentTypes[FilterEngine::CONTENT_TYPE_STYLESHEET] = "STYLESHEET"; |
| 178 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT] = "OBJECT"; | 178 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT] = "OBJECT"; |
| 179 contentTypes[FilterEngine::CONTENT_TYPE_SUBDOCUMENT] = "SUBDOCUMENT"; | 179 contentTypes[FilterEngine::CONTENT_TYPE_SUBDOCUMENT] = "SUBDOCUMENT"; |
| 180 contentTypes[FilterEngine::CONTENT_TYPE_DOCUMENT] = "DOCUMENT"; | 180 contentTypes[FilterEngine::CONTENT_TYPE_DOCUMENT] = "DOCUMENT"; |
| 181 contentTypes[FilterEngine::CONTENT_TYPE_PING] = "PING"; |
| 181 contentTypes[FilterEngine::CONTENT_TYPE_XMLHTTPREQUEST] = "XMLHTTPREQUEST"; | 182 contentTypes[FilterEngine::CONTENT_TYPE_XMLHTTPREQUEST] = "XMLHTTPREQUEST"; |
| 182 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT_SUBREQUEST] = "OBJECT_SUBREQU
EST"; | 183 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT_SUBREQUEST] = "OBJECT_SUBREQU
EST"; |
| 183 contentTypes[FilterEngine::CONTENT_TYPE_FONT] = "FONT"; | 184 contentTypes[FilterEngine::CONTENT_TYPE_FONT] = "FONT"; |
| 184 contentTypes[FilterEngine::CONTENT_TYPE_MEDIA] = "MEDIA"; | 185 contentTypes[FilterEngine::CONTENT_TYPE_MEDIA] = "MEDIA"; |
| 185 contentTypes[FilterEngine::CONTENT_TYPE_ELEMHIDE] = "ELEMHIDE"; | 186 contentTypes[FilterEngine::CONTENT_TYPE_ELEMHIDE] = "ELEMHIDE"; |
| 187 contentTypes[FilterEngine::CONTENT_TYPE_GENERICBLOCK] = "GENERICBLOCK"; |
| 188 contentTypes[FilterEngine::CONTENT_TYPE_GENERICHIDE] = "GENERICHIDE"; |
| 186 return contentTypes; | 189 return contentTypes; |
| 187 } | 190 } |
| 188 } | 191 } |
| 189 | 192 |
| 190 const ContentTypeMap FilterEngine::contentTypes = CreateContentTypeMap(); | 193 const ContentTypeMap FilterEngine::contentTypes = CreateContentTypeMap(); |
| 191 | 194 |
| 192 std::string FilterEngine::ContentTypeToString(ContentType contentType) | 195 std::string FilterEngine::ContentTypeToString(ContentType contentType) |
| 193 { | 196 { |
| 194 ContentTypeMap::const_iterator it = contentTypes.find(contentType); | 197 ContentTypeMap::const_iterator it = contentTypes.find(contentType); |
| 195 if (it != contentTypes.end()) | 198 if (it != contentTypes.end()) |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 } | 281 } |
| 279 func->Call(params); | 282 func->Call(params); |
| 280 } | 283 } |
| 281 | 284 |
| 282 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v
alue) | 285 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v
alue) |
| 283 { | 286 { |
| 284 if (!value) | 287 if (!value) |
| 285 return; | 288 return; |
| 286 | 289 |
| 287 jsEngine->SetEventCallback("_showNotification", | 290 jsEngine->SetEventCallback("_showNotification", |
| 288 std::tr1::bind(&FilterEngine::ShowNotification, this, value, | 291 std::bind(&FilterEngine::ShowNotification, this, value, |
| 289 std::tr1::placeholders::_1)); | 292 std::placeholders::_1)); |
| 290 } | 293 } |
| 291 | 294 |
| 292 void FilterEngine::RemoveShowNotificationCallback() | 295 void FilterEngine::RemoveShowNotificationCallback() |
| 293 { | 296 { |
| 294 jsEngine->RemoveEventCallback("_showNotification"); | 297 jsEngine->RemoveEventCallback("_showNotification"); |
| 295 } | 298 } |
| 296 | 299 |
| 297 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, | 300 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
| 298 ContentType contentType, | 301 ContentTypeMask contentTypeMask, |
| 299 const std::string& documentUrl) const | 302 const std::string& documentUrl) const |
| 300 { | 303 { |
| 301 std::vector<std::string> documentUrls; | 304 std::vector<std::string> documentUrls; |
| 302 documentUrls.push_back(documentUrl); | 305 documentUrls.push_back(documentUrl); |
| 303 return Matches(url, contentType, documentUrls); | 306 return Matches(url, contentTypeMask, documentUrls); |
| 304 } | 307 } |
| 305 | 308 |
| 306 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, | 309 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
| 307 ContentType contentType, | 310 ContentTypeMask contentTypeMask, |
| 308 const std::vector<std::string>& documentUrls) const | 311 const std::vector<std::string>& documentUrls) const |
| 309 { | 312 { |
| 310 if (documentUrls.empty()) | 313 if (documentUrls.empty()) |
| 311 return CheckFilterMatch(url, contentType, ""); | 314 return CheckFilterMatch(url, contentTypeMask, ""); |
| 312 | 315 |
| 313 std::string lastDocumentUrl = documentUrls.front(); | 316 std::string lastDocumentUrl = documentUrls.front(); |
| 314 for (std::vector<std::string>::const_iterator it = documentUrls.begin(); | 317 for (std::vector<std::string>::const_iterator it = documentUrls.begin(); |
| 315 it != documentUrls.end(); it++) { | 318 it != documentUrls.end(); it++) { |
| 316 const std::string documentUrl = *it; | 319 const std::string documentUrl = *it; |
| 317 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, | 320 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, |
| 318 CONTENT_TYPE_DOCUMENT, | 321 CONTENT_TYPE_DOCUMENT, |
| 319 lastDocumentUrl); | 322 lastDocumentUrl); |
| 320 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) | 323 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) |
| 321 return match; | 324 return match; |
| 322 lastDocumentUrl = documentUrl; | 325 lastDocumentUrl = documentUrl; |
| 323 } | 326 } |
| 324 | 327 |
| 325 return CheckFilterMatch(url, contentType, lastDocumentUrl); | 328 return CheckFilterMatch(url, contentTypeMask, lastDocumentUrl); |
| 329 } |
| 330 |
| 331 bool FilterEngine::IsDocumentWhitelisted(const std::string& url, |
| 332 const std::vector<std::string>& documentUrls) const |
| 333 { |
| 334 return !!GetWhitelistingFilter(url, CONTENT_TYPE_DOCUMENT, documentUrls); |
| 335 } |
| 336 |
| 337 bool FilterEngine::IsElemhideWhitelisted(const std::string& url, |
| 338 const std::vector<std::string>& documentUrls) const |
| 339 { |
| 340 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls); |
| 326 } | 341 } |
| 327 | 342 |
| 328 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, | 343 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, |
| 329 ContentType contentType, | 344 ContentTypeMask contentTypeMask, |
| 330 const std::string& documentUrl) const | 345 const std::string& documentUrl) const |
| 331 { | 346 { |
| 332 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); | 347 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); |
| 333 JsValueList params; | 348 JsValueList params; |
| 334 params.push_back(jsEngine->NewValue(url)); | 349 params.push_back(jsEngine->NewValue(url)); |
| 335 params.push_back(jsEngine->NewValue(ContentTypeToString(contentType))); | 350 params.push_back(jsEngine->NewValue(contentTypeMask)); |
| 336 params.push_back(jsEngine->NewValue(documentUrl)); | 351 params.push_back(jsEngine->NewValue(documentUrl)); |
| 337 JsValuePtr result = func->Call(params); | 352 JsValuePtr result = func->Call(params); |
| 338 if (!result->IsNull()) | 353 if (!result->IsNull()) |
| 339 return FilterPtr(new Filter(std::move(*result))); | 354 return FilterPtr(new Filter(std::move(*result))); |
| 340 else | 355 else |
| 341 return FilterPtr(); | 356 return FilterPtr(); |
| 342 } | 357 } |
| 343 | 358 |
| 344 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri
ng& domain) const | 359 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri
ng& domain) const |
| 345 { | 360 { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 375 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); | 390 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); |
| 376 JsValueList params; | 391 JsValueList params; |
| 377 params.push_back(jsEngine->NewValue(url)); | 392 params.push_back(jsEngine->NewValue(url)); |
| 378 return func->Call(params)->AsString(); | 393 return func->Call(params)->AsString(); |
| 379 } | 394 } |
| 380 | 395 |
| 381 void FilterEngine::SetUpdateAvailableCallback( | 396 void FilterEngine::SetUpdateAvailableCallback( |
| 382 FilterEngine::UpdateAvailableCallback callback) | 397 FilterEngine::UpdateAvailableCallback callback) |
| 383 { | 398 { |
| 384 jsEngine->SetEventCallback("updateAvailable", | 399 jsEngine->SetEventCallback("updateAvailable", |
| 385 std::tr1::bind(&FilterEngine::UpdateAvailable, this, callback, | 400 std::bind(&FilterEngine::UpdateAvailable, this, callback, |
| 386 std::tr1::placeholders::_1)); | 401 std::placeholders::_1)); |
| 387 } | 402 } |
| 388 | 403 |
| 389 void FilterEngine::RemoveUpdateAvailableCallback() | 404 void FilterEngine::RemoveUpdateAvailableCallback() |
| 390 { | 405 { |
| 391 jsEngine->RemoveEventCallback("updateAvailable"); | 406 jsEngine->RemoveEventCallback("updateAvailable"); |
| 392 } | 407 } |
| 393 | 408 |
| 394 void FilterEngine::UpdateAvailable( | 409 void FilterEngine::UpdateAvailable( |
| 395 FilterEngine::UpdateAvailableCallback callback, JsValueList& params) | 410 FilterEngine::UpdateAvailableCallback callback, JsValueList& params) |
| 396 { | 411 { |
| 397 if (params.size() >= 1 && !params[0]->IsNull()) | 412 if (params.size() >= 1 && !params[0]->IsNull()) |
| 398 callback(params[0]->AsString()); | 413 callback(params[0]->AsString()); |
| 399 } | 414 } |
| 400 | 415 |
| 401 void FilterEngine::ForceUpdateCheck( | 416 void FilterEngine::ForceUpdateCheck( |
| 402 FilterEngine::UpdateCheckDoneCallback callback) | 417 FilterEngine::UpdateCheckDoneCallback callback) |
| 403 { | 418 { |
| 404 std::string eventName = "_updateCheckDone"; | 419 std::string eventName = "_updateCheckDone"; |
| 405 eventName += ++updateCheckId; | 420 eventName += ++updateCheckId; |
| 406 | 421 |
| 407 jsEngine->SetEventCallback(eventName, std::tr1::bind(&FilterEngine::UpdateChec
kDone, | 422 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDone
, |
| 408 this, eventName, callback, std::tr1::placeholders::_1)); | 423 this, eventName, callback, std::placeholders::_1)); |
| 409 | 424 |
| 410 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); | 425 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); |
| 411 JsValueList params; | 426 JsValueList params; |
| 412 params.push_back(jsEngine->NewValue(eventName)); | 427 params.push_back(jsEngine->NewValue(eventName)); |
| 413 func->Call(params); | 428 func->Call(params); |
| 414 } | 429 } |
| 415 | 430 |
| 416 void FilterEngine::UpdateCheckDone(const std::string& eventName, | 431 void FilterEngine::UpdateCheckDone(const std::string& eventName, |
| 417 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params) | 432 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params) |
| 418 { | 433 { |
| 419 jsEngine->RemoveEventCallback(eventName); | 434 jsEngine->RemoveEventCallback(eventName); |
| 420 | 435 |
| 421 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt
ring() : ""); | 436 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt
ring() : ""); |
| 422 callback(error); | 437 callback(error); |
| 423 } | 438 } |
| 424 | 439 |
| 425 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca
llback) | 440 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca
llback) |
| 426 { | 441 { |
| 427 jsEngine->SetEventCallback("filterChange", std::tr1::bind(&FilterEngine::Filte
rChanged, | 442 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan
ged, |
| 428 this, callback, std::tr1::placeholders::_1)); | 443 this, callback, std::placeholders::_1)); |
| 429 } | 444 } |
| 430 | 445 |
| 431 void FilterEngine::RemoveFilterChangeCallback() | 446 void FilterEngine::RemoveFilterChangeCallback() |
| 432 { | 447 { |
| 433 jsEngine->RemoveEventCallback("filterChange"); | 448 jsEngine->RemoveEventCallback("filterChange"); |
| 434 } | 449 } |
| 435 | 450 |
| 436 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js
ValueList& params) | 451 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js
ValueList& params) |
| 437 { | 452 { |
| 438 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS
tring() : ""); | 453 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS
tring() : ""); |
| 439 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); | 454 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); |
| 440 callback(action, item); | 455 callback(action, item); |
| 441 } | 456 } |
| 442 | 457 |
| 443 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, | 458 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, |
| 444 const JsValueList& params) | 459 const JsValueList& params) |
| 445 { | 460 { |
| 446 if (params.size() < 1) | 461 if (params.size() < 1) |
| 447 return; | 462 return; |
| 448 | 463 |
| 449 callback(Notification::JsValueToNotification(std::move(*params[0]))); | 464 if (!params[0]->IsObject()) |
| 465 { |
| 466 return; |
| 467 } |
| 468 callback(NotificationPtr(new Notification(std::move(*params[0])))); |
| 450 } | 469 } |
| 451 | 470 |
| 452 | 471 |
| 453 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) | 472 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) |
| 454 { | 473 { |
| 455 JsValueList params; | 474 JsValueList params; |
| 456 params.push_back(jsEngine->NewValue(v1)); | 475 params.push_back(jsEngine->NewValue(v1)); |
| 457 params.push_back(jsEngine->NewValue(v2)); | 476 params.push_back(jsEngine->NewValue(v2)); |
| 458 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); | 477 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); |
| 459 return func->Call(params)->AsInt(); | 478 return func->Call(params)->AsInt(); |
| 460 } | 479 } |
| 480 |
| 481 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, |
| 482 ContentTypeMask contentTypeMask, const std::string& documentUrl) const |
| 483 { |
| 484 FilterPtr match = Matches(url, contentTypeMask, documentUrl); |
| 485 if (match && match->GetType() == Filter::TYPE_EXCEPTION) |
| 486 { |
| 487 return match; |
| 488 } |
| 489 return FilterPtr(); |
| 490 } |
| 491 |
| 492 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, |
| 493 ContentTypeMask contentTypeMask, |
| 494 const std::vector<std::string>& documentUrls) const |
| 495 { |
| 496 if (documentUrls.empty()) |
| 497 { |
| 498 return GetWhitelistingFilter(url, contentTypeMask, ""); |
| 499 } |
| 500 |
| 501 std::vector<std::string>::const_iterator urlIterator = documentUrls.begin(); |
| 502 std::string currentUrl = url; |
| 503 do |
| 504 { |
| 505 std::string parentUrl = *urlIterator++; |
| 506 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent
Url); |
| 507 if (filter) |
| 508 { |
| 509 return filter; |
| 510 } |
| 511 currentUrl = parentUrl; |
| 512 } |
| 513 while (urlIterator != documentUrls.end()); |
| 514 return FilterPtr(); |
| 515 } |
| LEFT | RIGHT |