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

Side by Side Diff: compiled/ElemHide.h

Issue 29587914: Issue 5142 - Convert Element Hiding to C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Reworked after review. Created Jan. 16, 2018, 2:56 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | compiled/ElemHide.cpp » ('j') | compiled/ElemHide.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present 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
18 /**
19 * @fileOverview Element hiding implementation.
20 */
21
22 #pragma once
23
24 #include <unordered_map>
25 #include <vector>
26
27 #include "bindings/runtime.h"
28 #include "intrusive_ptr.h"
29 #include "StringMap.h"
30 #include "filter/Filter.h"
31 #include "filter/ElemHideBase.h"
32 #include "filter/ElemHideException.h"
33
34 class ElemHide_SelectorList : public ref_counted
35 {
36 std::vector<ElemHideBasePtr> mSelectors;
37 public:
38 size_t BINDINGS_EXPORTED GetSelectorCount() const
39 {
40 return mSelectors.size();
41 }
42 OwnedString BINDINGS_EXPORTED SelectorAt(size_t idx) const;
43 const String& BINDINGS_EXPORTED FilterKeyAt(size_t idx) const;
44
45 void push_back(const ElemHideBasePtr& filter)
46 {
47 mSelectors.push_back(filter);
48 }
49
50 void append(const ElemHide_SelectorList& list)
51 {
52 mSelectors.insert(mSelectors.end(),
53 list.mSelectors.cbegin(), list.mSelectors.cend());
54 }
55 };
56
57 class ElemHide : public ref_counted
58 {
59 // All filters. Key is filter text. Exception filters excluded.
60 StringMap<ElemHideBasePtr> mFilters;
61 // StringMap is non copyable. std::unordered_map<> it is
62 // Filters by domain. Key is domain. Subkey is filter text.
63 StringMap<std::unordered_map<DependentString,ElemHideBasePtr,StringHash>> mFil tersByDomain;
64
65 // Exceptions. The key is the selector.
66 OwnedStringMap<std::vector<ElemHideExceptionPtr>> mExceptions;
67 // Known exceptions. Filter text as keys.
68 StringSet mKnownExceptions;
69
70 // Unconditional selectors. Filter text as key
71 StringSet mUnconditionalSelectors;
72
73 mutable intrusive_ptr<ElemHide_SelectorList> mUnconditionalSelectorsCache;
74
75 ElemHide()
76 {
77 if (defaultDomains.size() == 0)
78 defaultDomains[ActiveFilter::DEFAULT_DOMAIN] = true;
79 }
80 public:
81 static ElemHide* BINDINGS_EXPORTED Create()
82 {
83 return new ElemHide();
84 }
85 // Used by GetSelectorsForDomain to narrow down selectors to return.
86 enum Criteria
87 {
88 // Return all selectors applying to a particular hostname.
89 ALL_MATCHING = 0,
90 // Exclude selectors which apply to all websites without exception.
91 NO_UNCONDITIONAL = 1,
92 // Return only selectors for filters which specifically match the
93 // given host name.
94 SPECIFIC_ONLY = 2,
95 };
96
97 void BINDINGS_EXPORTED Clear();
98 void BINDINGS_EXPORTED Add(ElemHideBase& filter);
99 void BINDINGS_EXPORTED Remove(ElemHideBase& filter);
100
101 ElemHide_SelectorList* BINDINGS_EXPORTED GetSelectorsForDomain(const String& d omain,
102 Criteria criter ia) const;
103 ElemHide_SelectorList* BINDINGS_EXPORTED GetUnconditionalSelectors() const;
104
105 ElemHideException* BINDINGS_EXPORTED GetException(const ElemHideBase& filter,
106 DependentString& docDomain) const;
107
108 private:
109 static ActiveFilter::DomainMap defaultDomains;
110 void AddToFiltersByDomain(ElemHideBase& filter);
111 };
112
OLDNEW
« no previous file with comments | « no previous file | compiled/ElemHide.cpp » ('j') | compiled/ElemHide.cpp » ('J')

Powered by Google App Engine
This is Rietveld