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: More test, cache the unconditional selectors. Various cleanup Created Oct. 26, 2017, 7:19 p.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 mUnconditionalSelectorsCache.reset();
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 mUnconditionalSelectorsCache.reset();
107 }
108 else
109 {
110 AddToFiltersByDomain(filter);
111 }
112 }
113 }
114
115 void ElemHide::Remove(ElemHideBase& filter)
116 {
117 DependentString text(filter.GetText());
118
119 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION)
120 {
121 // never seen the exception.
122 if (!mKnownExceptions.find(text))
123 return;
124
125 auto selector = filter.GetSelector();
126 auto& list = mExceptions[selector];
127 auto iter = std::find(
128 list.begin(), list.end(),
129 ElemHideExceptionPtr(filter.As<ElemHideException>()));
130 if (iter != list.end())
131 list.erase(iter);
132 mKnownExceptions.erase(text);
133 }
134 else
135 {
136 if (!mFilters.find(text))
137 return;
138
139 if (mUnconditionalSelectors.find(text))
140 {
141 mUnconditionalSelectors.erase(text);
142 mUnconditionalSelectorsCache.reset();
143 }
144 else
145 {
146 auto domains = filter.GetDomains();
147 for (auto domain : *domains)
148 {
149 auto& list = mFiltersByDomain[domain.first];
150 list.erase(text);
151 }
152 }
153
154 mFilters.erase(text);
155 }
156 }
157
158 ElemHideException* ElemHide::GetException(const ElemHideBase& filter,
159 DependentString& docDomain) const
160 {
161 auto exception = mExceptions.find(filter.GetSelector());
162 if (!exception)
163 return nullptr;
164
165 auto& list = exception->second;
166 for (auto iter = list.rbegin(); iter != list.rend(); iter++)
167 if ((*iter)->IsActiveOnDomain(docDomain, u""_str))
168 {
169 ElemHideExceptionPtr filter(*iter);
170 return filter.release();
171 }
172
173 return nullptr;
174 }
175
176 _ElemHide_SelectorList* ElemHide::GetUnconditionalSelectors() const
177 {
178 if (!mUnconditionalSelectorsCache)
179 {
180 mUnconditionalSelectorsCache = intrusive_ptr<_ElemHide_SelectorList>(new _El emHide_SelectorList, false);
181 annotate_address(mUnconditionalSelectorsCache.get(), "_ElemHide_SelectorList ");
182 for (auto unconditional : mUnconditionalSelectors)
183 {
184 auto filter = mFilters.find(unconditional.first);
185 if (filter)
186 mUnconditionalSelectorsCache->push_back(filter->second);
187 }
188 }
189 return mUnconditionalSelectorsCache.get();
190 }
191
192 _ElemHide_SelectorList* ElemHide::GetSelectorsForDomain(const String& domain,
193 Criteria criteria) const
194 {
195 _ElemHide_SelectorList* selectors = new _ElemHide_SelectorList;
196 annotate_address(selectors, "_ElemHide_SelectorList");
197
198 if (criteria < NO_UNCONDITIONAL)
199 selectors->append(GetUnconditionalSelectors());
200
201 bool specificOnly = (criteria >= SPECIFIC_ONLY);
202 StringSet seenFilters;
203 DependentString docDomain(domain);
204
205 DependentString currentDomain(domain);
206 while (true)
207 {
208 if (specificOnly && currentDomain.empty())
209 break;
210
211 auto filters = mFiltersByDomain.find(currentDomain);
212 if (filters)
213 {
214 for (auto& entry : filters->second)
215 {
216 if (entry.first.is_invalid())
217 continue;
218
219 if (seenFilters.find(entry.first))
220 continue;
221 seenFilters.insert(entry.first);
222
223 auto filter = entry.second;
224 if (filter && !GetException(*filter, docDomain))
225 selectors->push_back(filter);
226 }
227 }
228
229 if (currentDomain.empty())
230 break;
231
232 auto nextDot = currentDomain.find('.');
233 currentDomain = nextDot == String::npos ?
234 u""_str : DependentString(currentDomain, nextDot + 1);
235 }
236
237 return selectors;
238 }
OLDNEW
« compiled/ElemHide.h ('K') | « compiled/ElemHide.h ('k') | compiled/Map.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld