| 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 * |
| 14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include "PluginStdAfx.h" | 18 #include "PluginStdAfx.h" |
| 19 #include "AdblockPlusClient.h" | 19 #include "AdblockPlusClient.h" |
| 20 #include "PluginFilter.h" | 20 #include "PluginFilter.h" |
| 21 #include "PluginSettings.h" | 21 #include "PluginSettings.h" |
| 22 #include "PluginMutex.h" | 22 #include "PluginMutex.h" |
| 23 #include "PluginSettings.h" | 23 #include "PluginSettings.h" |
| 24 #include "PluginSystem.h" | 24 #include "PluginSystem.h" |
| 25 #include "PluginClass.h" | 25 #include "PluginClass.h" |
| 26 #include "PluginUtil.h" | 26 #include "PluginUtil.h" |
| 27 #include "mlang.h" | 27 #include "mlang.h" |
| 28 #include "TokenSequence.h" | |
| 29 #include "..\shared\CriticalSection.h" | 28 #include "..\shared\CriticalSection.h" |
| 30 #include "..\shared\Utils.h" | 29 #include "..\shared\Utils.h" |
| 31 #include "..\shared\MsHTMLUtils.h" | 30 #include "..\shared\MsHTMLUtils.h" |
| 32 | 31 |
| 33 // The filters are described at http://adblockplus.org/en/filters | 32 // The filters are described at http://adblockplus.org/en/filters |
| 34 | 33 |
| 35 static CriticalSection s_criticalSectionFilterMap; | 34 static CriticalSection s_criticalSectionFilterMap; |
| 36 | 35 |
| 37 // ============================================================================ | 36 // ============================================================================ |
| 38 // CFilterElementHide | 37 // CFilterElementHide |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // Any tag | 69 // Any tag |
| 71 filterSuffix = filterSuffix.substr(1); | 70 filterSuffix = filterSuffix.substr(1); |
| 72 } | 71 } |
| 73 else if (firstTag == '[' || firstTag == '.' || firstTag == '#') | 72 else if (firstTag == '[' || firstTag == '.' || firstTag == '#') |
| 74 { | 73 { |
| 75 // Any tag (implicitly) | 74 // Any tag (implicitly) |
| 76 } | 75 } |
| 77 else if (isalnum(firstTag)) | 76 else if (isalnum(firstTag)) |
| 78 { | 77 { |
| 79 // Real tag | 78 // Real tag |
| 80 //TODO: Add support for descendant selectors | 79 // TODO: Add support for descendant selectors |
| 81 auto pos = filterSuffix.find_first_of(L".#[("); | 80 auto pos = filterSuffix.find_first_of(L".#[("); |
| 82 if (pos == std::wstring::npos) | 81 if (pos == std::wstring::npos) |
| 83 { | 82 { |
| 84 pos = filterSuffix.length(); | 83 pos = filterSuffix.length(); |
| 85 } | 84 } |
| 86 m_tag = ToLowerString(filterSuffix.substr(0, pos)); | 85 m_tag = ToLowerString(filterSuffix.substr(0, pos)); |
| 87 filterSuffix = filterSuffix.substr(pos); | 86 filterSuffix = filterSuffix.substr(pos); |
| 88 } | 87 } |
| 89 else | 88 else |
| 90 { | 89 { |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 std::wstring classNameList(ToWstring(classNameListBstr)); | 277 std::wstring classNameList(ToWstring(classNameListBstr)); |
| 279 if (classNameList.empty()) | 278 if (classNameList.empty()) |
| 280 { | 279 { |
| 281 return false; | 280 return false; |
| 282 } | 281 } |
| 283 // TODO: Consider case of multiple classes. (m_tagClassName can be something
like "foo.bar") | 282 // TODO: Consider case of multiple classes. (m_tagClassName can be something
like "foo.bar") |
| 284 /* | 283 /* |
| 285 * Match when 'm_tagClassName' appears as a token within classNameList | 284 * Match when 'm_tagClassName' appears as a token within classNameList |
| 286 */ | 285 */ |
| 287 bool foundMatch = false; | 286 bool foundMatch = false; |
| 288 TokenSequence<std::wstring> ts(classNameList, L" "); | 287 wchar_t* nextToken = nullptr; |
| 289 for (auto iterClassName = ts.cbegin(); iterClassName != ts.cend(); ++iterCla
ssName) | 288 const wchar_t* token = wcstok_s(&classNameList[0], L" ", &nextToken); |
| 290 { | 289 while (token != nullptr) |
| 291 if (*iterClassName == m_tagClassName) | 290 { |
| 291 if (std::wstring(token) == m_tagClassName) |
| 292 { | 292 { |
| 293 foundMatch = true; | 293 foundMatch = true; |
| 294 break; | 294 break; |
| 295 } | 295 } |
| 296 token = wcstok_s(nullptr, L" ", &nextToken); |
| 296 } | 297 } |
| 297 if (!foundMatch) | 298 if (!foundMatch) |
| 298 { | 299 { |
| 299 return false; | 300 return false; |
| 300 } | 301 } |
| 301 } | 302 } |
| 302 /* | 303 /* |
| 303 * If a tag name is specified, it must match | 304 * If a tag name is specified, it must match |
| 304 */ | 305 */ |
| 305 if (!m_tag.empty()) | 306 if (!m_tag.empty()) |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 CPluginDebug::DebugResultHiding(tag, L"id:" + id, idIt->second.m_filte
rText); | 556 CPluginDebug::DebugResultHiding(tag, L"id:" + id, idIt->second.m_filte
rText); |
| 556 #endif | 557 #endif |
| 557 return true; | 558 return true; |
| 558 } | 559 } |
| 559 } | 560 } |
| 560 } | 561 } |
| 561 | 562 |
| 562 // Search tag/className filters | 563 // Search tag/className filters |
| 563 if (!classNames.empty()) | 564 if (!classNames.empty()) |
| 564 { | 565 { |
| 565 TokenSequence<std::wstring> ts(classNames, L" \t\n\r"); | 566 wchar_t* nextToken = nullptr; |
| 566 for (auto iterClassName = ts.cbegin(); iterClassName != ts.cend(); ++iterC
lassName) | 567 const wchar_t* token = wcstok_s(&classNames[0], L" \t\n\r", &nextToken); |
| 567 { | 568 while (token != nullptr) |
| 568 std::wstring className = *iterClassName; | 569 { |
| 570 std::wstring className(token); |
| 569 auto classItEnum = m_elementHideTagsClass.equal_range(std::make_pair(tag
, className)); | 571 auto classItEnum = m_elementHideTagsClass.equal_range(std::make_pair(tag
, className)); |
| 570 for (auto classIt = classItEnum.first; classIt != classItEnum.second; ++
classIt) | 572 for (auto classIt = classItEnum.first; classIt != classItEnum.second; ++
classIt) |
| 571 { | 573 { |
| 572 if (classIt->second.IsMatchFilterElementHide(pEl)) | 574 if (classIt->second.IsMatchFilterElementHide(pEl)) |
| 573 { | 575 { |
| 574 #ifdef ENABLE_DEBUG_RESULT | 576 #ifdef ENABLE_DEBUG_RESULT |
| 575 DEBUG_HIDE_EL(indent + L"HideEl::Found (tag/class) filter:" + classI
t->second.m_filterText); | 577 DEBUG_HIDE_EL(indent + L"HideEl::Found (tag/class) filter:" + classI
t->second.m_filterText); |
| 576 CPluginDebug::DebugResultHiding(tag, L"class:" + className, classIt-
>second.m_filterText); | 578 CPluginDebug::DebugResultHiding(tag, L"class:" + className, classIt-
>second.m_filterText); |
| 577 #endif | 579 #endif |
| 578 return true; | 580 return true; |
| 579 } | 581 } |
| 580 } | 582 } |
| 581 | 583 |
| 582 // Search general class name | 584 // Search general class name |
| 583 classItEnum = m_elementHideTagsClass.equal_range(std::make_pair(L"", cla
ssName)); | 585 classItEnum = m_elementHideTagsClass.equal_range(std::make_pair(L"", cla
ssName)); |
| 584 for (auto classIt = classItEnum.first; classIt != classItEnum.second; ++
classIt) | 586 for (auto classIt = classItEnum.first; classIt != classItEnum.second; ++
classIt) |
| 585 { | 587 { |
| 586 if (classIt->second.IsMatchFilterElementHide(pEl)) | 588 if (classIt->second.IsMatchFilterElementHide(pEl)) |
| 587 { | 589 { |
| 588 #ifdef ENABLE_DEBUG_RESULT | 590 #ifdef ENABLE_DEBUG_RESULT |
| 589 DEBUG_HIDE_EL(indent + L"HideEl::Found (?/class) filter:" + classIt-
>second.m_filterText); | 591 DEBUG_HIDE_EL(indent + L"HideEl::Found (?/class) filter:" + classIt-
>second.m_filterText); |
| 590 CPluginDebug::DebugResultHiding(tag, L"class:" + className, classIt-
>second.m_filterText); | 592 CPluginDebug::DebugResultHiding(tag, L"class:" + className, classIt-
>second.m_filterText); |
| 591 #endif | 593 #endif |
| 592 return true; | 594 return true; |
| 593 } | 595 } |
| 594 } | 596 } |
| 597 token = wcstok_s(nullptr, L" \t\n\r", &nextToken); |
| 595 } | 598 } |
| 596 } | 599 } |
| 597 | 600 |
| 598 // Search tag filters | 601 // Search tag filters |
| 599 auto tagItEnum = m_elementHideTags.equal_range(tag); | 602 auto tagItEnum = m_elementHideTags.equal_range(tag); |
| 600 for (auto tagIt = tagItEnum.first; tagIt != tagItEnum.second; ++tagIt) | 603 for (auto tagIt = tagItEnum.first; tagIt != tagItEnum.second; ++tagIt) |
| 601 { | 604 { |
| 602 if (tagIt->second.IsMatchFilterElementHide(pEl)) | 605 if (tagIt->second.IsMatchFilterElementHide(pEl)) |
| 603 { | 606 { |
| 604 #ifdef ENABLE_DEBUG_RESULT | 607 #ifdef ENABLE_DEBUG_RESULT |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 | 655 |
| 653 void CPluginFilter::ClearFilters() | 656 void CPluginFilter::ClearFilters() |
| 654 { | 657 { |
| 655 // Clear filter maps | 658 // Clear filter maps |
| 656 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); | 659 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); |
| 657 m_elementHideTags.clear(); | 660 m_elementHideTags.clear(); |
| 658 m_elementHideTagsId.clear(); | 661 m_elementHideTagsId.clear(); |
| 659 m_elementHideTagsClass.clear(); | 662 m_elementHideTagsClass.clear(); |
| 660 } | 663 } |
| 661 | 664 |
| 662 bool CPluginFilter::ShouldBlock(const std::wstring& src, AdblockPlus::FilterEngi
ne::ContentType contentType, const std::wstring& domain, bool addDebug) const | |
| 663 { | |
| 664 std::wstring srcTrimmed = TrimString(src); | |
| 665 | |
| 666 // We should not block the empty string, so all filtering does not make sense | |
| 667 // Therefore we just return | |
| 668 if (srcTrimmed.empty()) | |
| 669 { | |
| 670 return false; | |
| 671 } | |
| 672 | |
| 673 CPluginSettings* settings = CPluginSettings::GetInstance(); | |
| 674 | |
| 675 CPluginClient* client = CPluginClient::GetInstance(); | |
| 676 bool result = client->Matches(srcTrimmed, contentType, domain); | |
| 677 | |
| 678 #ifdef ENABLE_DEBUG_RESULT | |
| 679 if (addDebug) | |
| 680 { | |
| 681 std::wstring type = ToUtf16String(AdblockPlus::FilterEngine::ContentTypeToSt
ring(contentType)); | |
| 682 if (result) | |
| 683 { | |
| 684 CPluginDebug::DebugResultBlocking(type, srcTrimmed, domain); | |
| 685 } | |
| 686 else | |
| 687 { | |
| 688 CPluginDebug::DebugResultIgnoring(type, srcTrimmed, domain); | |
| 689 } | |
| 690 } | |
| 691 #endif | |
| 692 return result; | |
| 693 } | |
| LEFT | RIGHT |