Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: src/plugin/PluginFilter.cpp

Issue 6567422169448448: Issue 119 - Switch to injecting CSS for element hiding (Closed)
Left Patch Set: fix injecting of CSS when plugin is enabled/disabled and rearrange variables in InjectABPCSS Created April 13, 2015, 8:52 a.m.
Right Patch Set: rename OnQuit Created Sept. 30, 2016, 3:25 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/plugin/PluginFilter.h ('k') | src/plugin/PluginTabBase.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 19 #include "AdblockPlusClient.h"
20 #include "PluginFilter.h" 20 #include "PluginFilter.h"
21 #include "PluginSettings.h" 21 #include "PluginSettings.h"
22 #include "PluginClient.h"
23 #include "PluginClientFactory.h"
24 #include "PluginMutex.h" 22 #include "PluginMutex.h"
25 #include "PluginSettings.h" 23 #include "PluginSettings.h"
26 #include "PluginSystem.h" 24 #include "PluginSystem.h"
27 #include "PluginClass.h" 25 #include "PluginClass.h"
28 #include "mlang.h" 26 #include "mlang.h"
29
30 #include "..\shared\CriticalSection.h" 27 #include "..\shared\CriticalSection.h"
31 #include "..\shared\Utils.h" 28 #include "..\shared\Utils.h"
29 #include "..\shared\MsHTMLUtils.h"
32 30
33 // The filters are described at http://adblockplus.org/en/filters 31 // The filters are described at http://adblockplus.org/en/filters
34 32
35 static CriticalSection s_criticalSectionFilterMap; 33 static CriticalSection s_criticalSectionFilterMap;
36
37 namespace
38 {
39 struct GetHtmlElementAttributeResult
40 {
41 GetHtmlElementAttributeResult() : isAttributeFound(false)
42 {
43 }
44 std::wstring attributeValue;
45 bool isAttributeFound;
46 };
47
48 GetHtmlElementAttributeResult GetHtmlElementAttribute(IHTMLElement& htmlElemen t,
49 const ATL::CComBSTR& attributeName)
50 {
51 GetHtmlElementAttributeResult retValue;
52 ATL::CComVariant vAttr;
53 ATL::CComPtr<IHTMLElement4> htmlElement4;
54 if (FAILED(htmlElement.QueryInterface(&htmlElement4)) || !htmlElement4)
55 {
56 return retValue;
57 }
58 ATL::CComPtr<IHTMLDOMAttribute> attributeNode;
59 if (FAILED(htmlElement4->getAttributeNode(attributeName, &attributeNode)) || !attributeNode)
60 {
61 return retValue;
62 }
63 // we set that attribute found but it's not necessary that we can retrieve i ts value
64 retValue.isAttributeFound = true;
65 if (FAILED(attributeNode->get_nodeValue(&vAttr)))
66 {
67 return retValue;
68 }
69 if (vAttr.vt == VT_BSTR && vAttr.bstrVal)
70 {
71 retValue.attributeValue = vAttr.bstrVal;
72 }
73 else if (vAttr.vt == VT_I4)
74 {
75 retValue.attributeValue = std::to_wstring(vAttr.iVal);
76 }
77 return retValue;
78 }
79 }
80 34
81 // ============================================================================ 35 // ============================================================================
82 // CFilterElementHideAttrSelector 36 // CFilterElementHideAttrSelector
83 // ============================================================================ 37 // ============================================================================
84 38
85 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N ONE), m_pos(POS_NONE), m_bstrAttr(NULL) 39 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N ONE), m_pos(POS_NONE), m_bstrAttr(NULL)
86 { 40 {
87 } 41 }
88 42
89 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector(const CFilterElem entHideAttrSelector& filter) 43 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector(const CFilterElem entHideAttrSelector& filter)
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 416
463 return true; 417 return true;
464 } 418 }
465 419
466 420
467 421
468 // ============================================================================ 422 // ============================================================================
469 // CPluginFilter 423 // CPluginFilter
470 // ============================================================================ 424 // ============================================================================
471 425
472 CPluginFilter::CPluginFilter(const CString& dataPath) : m_dataPath(dataPath)
473 {
474 ClearFilters();
475 }
476
477
478 bool CPluginFilter::AddFilterElementHide(CString filterText) 426 bool CPluginFilter::AddFilterElementHide(CString filterText)
479 { 427 {
480 DEBUG_FILTER("Input: " + filterText + " filterFile" + filterFile); 428 DEBUG_FILTER("Input: " + filterText + " filterFile" + filterFile);
481 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); 429 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap);
482 { 430 {
483 CString filterString = filterText; 431 CString filterString = filterText;
484 // Create filter descriptor 432 // Create filter descriptor
485 std::auto_ptr<CFilterElementHide> filter; 433 std::auto_ptr<CFilterElementHide> filter;
486 434
487 CString wholeFilterString = filterString; 435 CString wholeFilterString = filterString;
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 CPluginDebug::DebugResultHiding(tag, L"-", ToWstring(tagIt->second.m_f ilterText)); 592 CPluginDebug::DebugResultHiding(tag, L"-", ToWstring(tagIt->second.m_f ilterText));
645 #endif 593 #endif
646 return true; 594 return true;
647 } 595 }
648 } 596 }
649 } 597 }
650 598
651 return false; 599 return false;
652 } 600 }
653 601
654 bool CPluginFilter::LoadHideFilters(const std::vector<std::wstring>& filters) 602 CPluginFilter::CPluginFilter(const std::vector<std::wstring>& filters)
655 { 603 {
656 ClearFilters();
657 m_hideFilters = filters; 604 m_hideFilters = filters;
658 bool isRead = false;
659 CPluginClient* client = CPluginClient::GetInstance(); 605 CPluginClient* client = CPluginClient::GetInstance();
660 606
661 // Parse hide string 607 // Parse hide string
662 int pos = 0; 608 int pos = 0;
663 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); 609 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap);
664 { 610 {
665 for (auto it = filters.begin(); it < filters.end(); ++it) 611 for (auto it = filters.begin(); it < filters.end(); ++it)
666 { 612 {
667 CString filter((*it).c_str()); 613 CString filter((*it).c_str());
668 // If the line is not commented out 614 // If the line is not commented out
669 if (!filter.Trim().IsEmpty() && filter.GetAt(0) != '!' && filter.GetAt(0) != '[') 615 if (!filter.Trim().IsEmpty() && filter.GetAt(0) != '!' && filter.GetAt(0) != '[')
670 { 616 {
671 int filterType = 0; 617 int filterType = 0;
672 618
673 // See http://adblockplus.org/en/filters for further documentation 619 // See http://adblockplus.org/en/filters for further documentation
674 620
675 try 621 try
676 { 622 {
677 AddFilterElementHide(filter); 623 AddFilterElementHide(filter);
678 } 624 }
679 catch(...) 625 catch(...)
680 { 626 {
681 #ifdef ENABLE_DEBUG_RESULT 627 #ifdef ENABLE_DEBUG_RESULT
682 CPluginDebug::DebugResult(L"Error loading hide filter: " + ToWstring(f ilter)); 628 CPluginDebug::DebugResult(L"Error loading hide filter: " + ToWstring(f ilter));
683 #endif 629 #endif
684 } 630 }
685 } 631 }
686 } 632 }
687 } 633 }
688
689 return isRead;
690 }
691
692 void CPluginFilter::ClearFilters()
693 {
694 // Clear filter maps
695 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap);
696 m_elementHideTags.clear();
697 m_elementHideTagsId.clear();
698 m_elementHideTagsClass.clear();
699 m_hideFilters.clear();
700 }
701
702 bool CPluginFilter::ShouldBlock(const std::wstring& src, AdblockPlus::FilterEngi ne::ContentType contentType, const std::wstring& domain, bool addDebug) const
703 {
704 std::wstring srcTrimmed = TrimString(src);
705
706 // We should not block the empty string, so all filtering does not make sense
707 // Therefore we just return
708 if (srcTrimmed.empty())
709 {
710 return false;
711 }
712
713 CPluginSettings* settings = CPluginSettings::GetInstance();
714
715 CPluginClient* client = CPluginClient::GetInstance();
716 bool result = client->Matches(srcTrimmed, contentType, domain);
717
718 #ifdef ENABLE_DEBUG_RESULT
719 if (addDebug)
720 {
721 std::wstring type = ToUtf16String(AdblockPlus::FilterEngine::ContentTypeToSt ring(contentType));
722 if (result)
723 {
724 CPluginDebug::DebugResultBlocking(type, srcTrimmed, domain);
725 }
726 else
727 {
728 CPluginDebug::DebugResultIgnoring(type, srcTrimmed, domain);
729 }
730 }
731 #endif
732 return result;
733 }
734
735 const std::vector<std::wstring>& CPluginFilter::GetHideFilters() const
736 {
737 DWORD res = WaitForSingleObject(hideFiltersLoadedEvent, ENGINE_STARTUP_TIMEOUT );
738 return m_hideFilters;
739 } 634 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld