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

Side by Side Diff: compiled/ElemHide.cpp

Issue 29587914: Issue 5142 - Convert Element Hiding to C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Oct. 25, 2017, 1:07 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
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 #include "ElemHide.h"
19
20 DependentString _ElemHide_SelectorList::SelectorAt(size_t idx) const
21 {
22 return DependentString(mSelectors[idx]->GetSelector());
23 }
24
25 const String& _ElemHide_SelectorList::FilterKeyAt(size_t idx) const
26 {
27 return mSelectors[idx]->GetText();
28 }
29
30 ElemHide* ElemHide::mInstance = new ElemHide();
31
32 void ElemHide::Clear()
33 {
34 mFilters.clear();
35 mExceptions.clear();
36 mFiltersByDomain.clear();
37 mKnownExceptions.clear();
38 }
39
40 namespace {
41
42 ActiveFilter::DomainMap* DefaultDomains()
43 {
44 static ActiveFilter::DomainMap defaultDomains;
45 if (defaultDomains.empty())
46 defaultDomains[u""_str] = true;
47 return &defaultDomains;
48 }
49
50 }
51
52 void ElemHide::AddToFiltersByDomain(ElemHideBase& filter)
53 {
54 auto domains = filter.GetDomains();
55 if (!domains)
56 domains = DefaultDomains();
57
58 DependentString text(filter.GetText());
59 for (auto& domain : *domains)
60 {
61 auto& filters = mFiltersByDomain[domain.first];
62 if (domain.second)
63 filters[text] = ElemHideBasePtr(&filter);
64 else
65 {
66 auto iter = filters.find(text);
67 if (iter != filters.end())
68 filters.erase(iter);
69 }
70 }
71 }
72
73 void ElemHide::Add(ElemHideBase& filter)
74 {
75 // we must ensure we have the right class.
76 // This is an error, but we might get Invalid filters.
77 if (!filter.As<ElemHideBase>())
78 return;
79
80 DependentString text(filter.GetText());
81 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION)
82 {
83 if (mKnownExceptions.find(text))
84 return;
85
86 auto selector = filter.GetSelector();
87 mExceptions[selector].push_back(
88 ElemHideExceptionPtr(filter.As<ElemHideException>()));
89
90 // Selector is not longer unconditional
91 mUnconditionalSelectors.erase(text);
92
93 mKnownExceptions.insert(text);
94 }
95 else
96 {
97 if (mFilters.find(text))
98 return;
99
100 mFilters[text] = ElemHideBasePtr(filter.As<ElemHideBase>());
101 if (!((filter.GetDomains() && filter.GetDomains()->size()) ||
102 mExceptions.find(filter.GetSelector())))
103 {
104 // The new filter's selector is unconditionally applied to all domains
105 mUnconditionalSelectors.insert(text);
106 }
107 else
108 {
109 AddToFiltersByDomain(filter);
110 }
111 }
112 }
113
114 void ElemHide::Remove(ElemHideBase& filter)
115 {
116 DependentString text(filter.GetText());
117
118 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION)
119 {
120 // never seen the exception.
121 if (!mKnownExceptions.find(text))
122 return;
123
124 auto selector = filter.GetSelector();
125 auto& list = mExceptions[selector];
126 auto iter = std::find(
127 list.begin(), list.end(),
128 ElemHideExceptionPtr(filter.As<ElemHideException>()));
129 if (iter != list.end())
130 list.erase(iter);
131 mKnownExceptions.erase(text);
132 }
133 else
134 {
135 if (!mFilters.find(text))
136 return;
137
138 if (mUnconditionalSelectors.find(text))
139 mUnconditionalSelectors.erase(text);
140 else
141 {
142 auto domains = filter.GetDomains();
143 for (auto domain : *domains)
144 {
145 auto list = mFiltersByDomain[domain.first];
146 list.erase(text);
147 }
148 }
149
150 mFilters.erase(text);
151 }
152 }
153
154 ElemHideException* ElemHide::GetException(const ElemHideBase& filter,
155 DependentString& docDomain) const
156 {
157 auto exception = mExceptions.find(filter.GetSelector());
158 if (!exception)
159 return nullptr;
160
161 auto& list = exception->second;
162 for (auto iter = list.rbegin(); iter != list.rend(); iter++)
163 if ((*iter)->IsActiveOnDomain(docDomain, u""_str))
164 {
165 ElemHideExceptionPtr filter(*iter);
166 return filter.release();
167 }
168
169 return nullptr;
170 }
171
172 _ElemHide_SelectorList* ElemHide::GetUnconditionalSelectors() const
hub 2017/10/25 01:39:11 there is also a more efficient way to implement th
173 {
174 auto list = new _ElemHide_SelectorList;
175 annotate_address(list, "_ElemHide_SelectorList");
176 for (auto unconditional : mUnconditionalSelectors)
177 {
178 auto filter = mFilters.find(unconditional.first);
179 if (filter)
180 list->push_back(filter->second);
181 }
182 return list;
183 }
184
185 _ElemHide_SelectorList* ElemHide::GetSelectorsForDomain(const String& domain,
186 Criteria criteria) const
187 {
188 _ElemHide_SelectorList* selectors;
189
190 if (criteria < NO_UNCONDITIONAL)
191 selectors = GetUnconditionalSelectors();
192 else
193 {
194 selectors = new _ElemHide_SelectorList;
195 annotate_address(selectors, "_ElemHide_SelectorList");
196 }
197
198 bool specificOnly = (criteria >= SPECIFIC_ONLY);
199 StringSet seenFilters;
200 DependentString docDomain(domain);
201
202 DependentString currentDomain(domain);
203 while (true)
204 {
205 if (specificOnly && currentDomain.empty())
206 break;
207
208 auto filters = mFiltersByDomain.find(currentDomain);
209 if (filters)
210 {
211 for (auto& entry : filters->second)
212 {
213 if (entry.first.is_invalid())
214 continue;
215
216 if (seenFilters.find(entry.first))
217 continue;
218 seenFilters.insert(entry.first);
219
220 auto filter = entry.second;
221 if (filter && !GetException(*filter, docDomain))
222 selectors->push_back(filter);
223 }
224 }
225
226 if (currentDomain.empty())
227 break;
228
229 auto nextDot = currentDomain.find('.');
230 currentDomain = nextDot == String::npos ?
231 u""_str : DependentString(currentDomain, nextDot + 1);
232 }
233
234 return selectors;
235 }
OLDNEW

Powered by Google App Engine
This is Rietveld