| LEFT | RIGHT |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 1 #include "PluginStdAfx.h" | 18 #include "PluginStdAfx.h" |
| 2 | 19 |
| 3 #include "PluginFilter.h" | 20 #include "PluginFilter.h" |
| 4 #include "PluginSettings.h" | 21 #include "PluginSettings.h" |
| 5 #include "PluginClient.h" | 22 #include "PluginClient.h" |
| 6 #include "PluginClientFactory.h" | 23 #include "PluginClientFactory.h" |
| 7 #include "PluginMutex.h" | 24 #include "PluginMutex.h" |
| 8 #include "PluginSettings.h" | 25 #include "PluginSettings.h" |
| 9 #include "PluginSystem.h" | 26 #include "PluginSystem.h" |
| 10 #include "PluginClass.h" | 27 #include "PluginClass.h" |
| 11 #include "mlang.h" | 28 #include "mlang.h" |
| 12 | 29 |
| 13 #include "..\shared\CriticalSection.h" | 30 #include "..\shared\CriticalSection.h" |
| 14 #include "..\shared\Utils.h" | 31 #include "..\shared\Utils.h" |
| 15 #include "..\shared\ContentType.h" | |
| 16 | |
| 17 | 32 |
| 18 // The filters are described at http://adblockplus.org/en/filters | 33 // The filters are described at http://adblockplus.org/en/filters |
| 19 | 34 |
| 20 static CriticalSection s_criticalSectionFilterMap; | 35 static CriticalSection s_criticalSectionFilterMap; |
| 21 | 36 |
| 22 namespace | 37 namespace |
| 23 { | 38 { |
| 24 struct GetHtmlElementAttributeResult | 39 struct GetHtmlElementAttributeResult |
| 25 { | 40 { |
| 26 GetHtmlElementAttributeResult() : isAttributeFound(false) | 41 GetHtmlElementAttributeResult() : isAttributeFound(false) |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 m_predecessor = filter.m_predecessor; | 264 m_predecessor = filter.m_predecessor; |
| 250 } | 265 } |
| 251 | 266 |
| 252 | 267 |
| 253 // ============================================================================ | 268 // ============================================================================ |
| 254 // CFilter | 269 // CFilter |
| 255 // ============================================================================ | 270 // ============================================================================ |
| 256 | 271 |
| 257 CFilter::CFilter(const CFilter& filter) | 272 CFilter::CFilter(const CFilter& filter) |
| 258 { | 273 { |
| 259 m_contentType = filter.m_contentType; | |
| 260 m_filterType = filter.m_filterType; | 274 m_filterType = filter.m_filterType; |
| 261 | 275 |
| 262 m_isFirstParty = filter.m_isFirstParty; | 276 m_isFirstParty = filter.m_isFirstParty; |
| 263 m_isThirdParty = filter.m_isThirdParty; | 277 m_isThirdParty = filter.m_isThirdParty; |
| 264 | 278 |
| 265 m_isMatchCase = filter.m_isMatchCase; | 279 m_isMatchCase = filter.m_isMatchCase; |
| 266 m_isFromStart = filter.m_isFromStart; | 280 m_isFromStart = filter.m_isFromStart; |
| 267 m_isFromEnd = filter.m_isFromEnd; | 281 m_isFromEnd = filter.m_isFromEnd; |
| 268 | 282 |
| 269 m_filterText = filter.m_filterText; | 283 m_filterText = filter.m_filterText; |
| 270 | 284 |
| 271 m_hitCount = filter.m_hitCount; | 285 m_hitCount = filter.m_hitCount; |
| 272 } | 286 } |
| 273 | 287 |
| 274 | 288 |
| 275 CFilter::CFilter() : m_isMatchCase(false), m_isFirstParty(false), | 289 CFilter::CFilter() : m_isMatchCase(false), m_isFirstParty(false), |
| 276 m_isThirdParty(false), m_contentType(AdblockPlus::FilterEngine::ContentType::C
ONTENT_TYPE_OTHER), | 290 m_isThirdParty(false), |
| 277 m_isFromStart(false), m_isFromEnd(false), m_hitCount(0) | 291 m_isFromStart(false), m_isFromEnd(false), m_hitCount(0) |
| 278 { | 292 { |
| 279 } | 293 } |
| 280 | 294 |
| 281 | 295 |
| 282 bool CFilterElementHide::IsMatchFilterElementHide(IHTMLElement* pEl) const | 296 bool CFilterElementHide::IsMatchFilterElementHide(IHTMLElement* pEl) const |
| 283 { | 297 { |
| 284 HRESULT hr; | 298 HRESULT hr; |
| 285 | 299 |
| 286 if (!m_tagId.IsEmpty()) | 300 if (!m_tagId.IsEmpty()) |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 if (!id.IsEmpty()) | 563 if (!id.IsEmpty()) |
| 550 { | 564 { |
| 551 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHideT
agsNamed::const_iterator> idItEnum = | 565 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHideT
agsNamed::const_iterator> idItEnum = |
| 552 m_elementHideTagsId.equal_range(std::make_pair(tagCString, id)); | 566 m_elementHideTagsId.equal_range(std::make_pair(tagCString, id)); |
| 553 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id
It != idItEnum.second; idIt ++) | 567 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id
It != idItEnum.second; idIt ++) |
| 554 { | 568 { |
| 555 if (idIt->second.IsMatchFilterElementHide(pEl)) | 569 if (idIt->second.IsMatchFilterElementHide(pEl)) |
| 556 { | 570 { |
| 557 #ifdef ENABLE_DEBUG_RESULT | 571 #ifdef ENABLE_DEBUG_RESULT |
| 558 DEBUG_HIDE_EL(indent + "HideEl::Found (tag/id) filter:" + idIt->second
.m_filterText) | 572 DEBUG_HIDE_EL(indent + "HideEl::Found (tag/id) filter:" + idIt->second
.m_filterText) |
| 559 CPluginDebug::DebugResultHiding(tagCString, "id:" + id, idIt->second
.m_filterText); | 573 CPluginDebug::DebugResultHiding(tagCString, L"id:" + id, idIt->secon
d.m_filterText); |
| 560 #endif | 574 #endif |
| 561 return true; | 575 return true; |
| 562 } | 576 } |
| 563 } | 577 } |
| 564 | 578 |
| 565 // Search general id | 579 // Search general id |
| 566 idItEnum = m_elementHideTagsId.equal_range(std::make_pair("", id)); | 580 idItEnum = m_elementHideTagsId.equal_range(std::make_pair("", id)); |
| 567 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id
It != idItEnum.second; idIt ++) | 581 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id
It != idItEnum.second; idIt ++) |
| 568 { | 582 { |
| 569 if (idIt->second.IsMatchFilterElementHide(pEl)) | 583 if (idIt->second.IsMatchFilterElementHide(pEl)) |
| 570 { | 584 { |
| 571 #ifdef ENABLE_DEBUG_RESULT | 585 #ifdef ENABLE_DEBUG_RESULT |
| 572 DEBUG_HIDE_EL(indent + "HideEl::Found (?/id) filter:" + idIt->second.m
_filterText) | 586 DEBUG_HIDE_EL(indent + "HideEl::Found (?/id) filter:" + idIt->second.m
_filterText) |
| 573 CPluginDebug::DebugResultHiding(tagCString, "id:" + id, idIt->second
.m_filterText); | 587 CPluginDebug::DebugResultHiding(tagCString, L"id:" + id, idIt->secon
d.m_filterText); |
| 574 #endif | 588 #endif |
| 575 return true; | 589 return true; |
| 576 } | 590 } |
| 577 } | 591 } |
| 578 } | 592 } |
| 579 | 593 |
| 580 // Search tag/className filters | 594 // Search tag/className filters |
| 581 if (!classNames.IsEmpty()) | 595 if (!classNames.IsEmpty()) |
| 582 { | 596 { |
| 583 int pos = 0; | 597 int pos = 0; |
| 584 CString className = classNames.Tokenize(L" \t\n\r", pos); | 598 CString className = classNames.Tokenize(L" \t\n\r", pos); |
| 585 while (pos >= 0) | 599 while (pos >= 0) |
| 586 { | 600 { |
| 587 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHid
eTagsNamed::const_iterator> classItEnum = | 601 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHid
eTagsNamed::const_iterator> classItEnum = |
| 588 m_elementHideTagsClass.equal_range(std::make_pair(tagCString, classNam
e)); | 602 m_elementHideTagsClass.equal_range(std::make_pair(tagCString, classNam
e)); |
| 589 | 603 |
| 590 for (TFilterElementHideTagsNamed::const_iterator classIt = classItEnum.f
irst; classIt != classItEnum.second; ++classIt) | 604 for (TFilterElementHideTagsNamed::const_iterator classIt = classItEnum.f
irst; classIt != classItEnum.second; ++classIt) |
| 591 { | 605 { |
| 592 if (classIt->second.IsMatchFilterElementHide(pEl)) | 606 if (classIt->second.IsMatchFilterElementHide(pEl)) |
| 593 { | 607 { |
| 594 #ifdef ENABLE_DEBUG_RESULT | 608 #ifdef ENABLE_DEBUG_RESULT |
| 595 DEBUG_HIDE_EL(indent + "HideEl::Found (tag/class) filter:" + classIt
->second.m_filterText) | 609 DEBUG_HIDE_EL(indent + "HideEl::Found (tag/class) filter:" + classIt
->second.m_filterText) |
| 596 CPluginDebug::DebugResultHiding(tagCString, "class:" + className,
classIt->second.m_filterText); | 610 CPluginDebug::DebugResultHiding(tagCString, L"class:" + className,
classIt->second.m_filterText); |
| 597 #endif | 611 #endif |
| 598 return true; | 612 return true; |
| 599 } | 613 } |
| 600 } | 614 } |
| 601 | 615 |
| 602 // Search general class name | 616 // Search general class name |
| 603 classItEnum = m_elementHideTagsClass.equal_range(std::make_pair("", clas
sName)); | 617 classItEnum = m_elementHideTagsClass.equal_range(std::make_pair("", clas
sName)); |
| 604 for (TFilterElementHideTagsNamed::const_iterator classIt = classItEnum.f
irst; classIt != classItEnum.second; ++ classIt) | 618 for (TFilterElementHideTagsNamed::const_iterator classIt = classItEnum.f
irst; classIt != classItEnum.second; ++ classIt) |
| 605 { | 619 { |
| 606 if (classIt->second.IsMatchFilterElementHide(pEl)) | 620 if (classIt->second.IsMatchFilterElementHide(pEl)) |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 } | 703 } |
| 690 | 704 |
| 691 m_elementHideTags.clear(); | 705 m_elementHideTags.clear(); |
| 692 m_elementHideTagsId.clear(); | 706 m_elementHideTagsId.clear(); |
| 693 m_elementHideTagsClass.clear(); | 707 m_elementHideTagsClass.clear(); |
| 694 } | 708 } |
| 695 } | 709 } |
| 696 | 710 |
| 697 bool CPluginFilter::ShouldBlock(const std::wstring& src, AdblockPlus::FilterEngi
ne::ContentType contentType, const std::wstring& domain, bool addDebug) const | 711 bool CPluginFilter::ShouldBlock(const std::wstring& src, AdblockPlus::FilterEngi
ne::ContentType contentType, const std::wstring& domain, bool addDebug) const |
| 698 { | 712 { |
| 699 CString srcCString = ToCString(src); | 713 std::wstring srcTrimmed = TrimString(src); |
| 700 | 714 |
| 701 // We should not block the empty string, so all filtering does not make sense | 715 // We should not block the empty string, so all filtering does not make sense |
| 702 // Therefore we just return | 716 // Therefore we just return |
| 703 if (srcCString.Trim().IsEmpty()) | 717 if (srcTrimmed.empty()) |
| 704 { | 718 { |
| 705 return false; | 719 return false; |
| 706 } | 720 } |
| 707 | 721 |
| 708 CPluginSettings* settings = CPluginSettings::GetInstance(); | 722 CPluginSettings* settings = CPluginSettings::GetInstance(); |
| 709 | 723 |
| 710 CPluginClient* client = CPluginClient::GetInstance(); | 724 CPluginClient* client = CPluginClient::GetInstance(); |
| 711 bool result = client->Matches(ToWstring(srcCString), contentType, domain); | 725 bool result = client->Matches(srcTrimmed, contentType, domain); |
| 712 | 726 |
| 713 #ifdef ENABLE_DEBUG_RESULT | 727 #ifdef ENABLE_DEBUG_RESULT |
| 714 if (addDebug) | 728 if (addDebug) |
| 715 { | 729 { |
| 716 std::wstring type = ToUtf16String(ContentTypeToString(contentType)); | 730 std::wstring type = ToUtf16String(AdblockPlus::FilterEngine::ContentTypeToSt
ring(contentType)); |
| 717 if (result) | 731 if (result) |
| 718 { | 732 { |
| 719 DEBUG_FILTER("Filter::ShouldBlock " + type + " YES"); | 733 CPluginDebug::DebugResultBlocking(ToCString(type), srcTrimmed, domain); |
| 720 CPluginDebug::DebugResultBlocking(ToCString(type), srcCString, domain); | |
| 721 } | 734 } |
| 722 else | 735 else |
| 723 { | 736 { |
| 724 CPluginDebug::DebugResultIgnoring(ToCString(type), srcCString, domain); | 737 CPluginDebug::DebugResultIgnoring(ToCString(type), srcTrimmed, domain); |
| 725 } | 738 } |
| 726 } | 739 } |
| 727 #endif | 740 #endif |
| 728 return result; | 741 return result; |
| 729 } | 742 } |
| LEFT | RIGHT |