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: Updated from feedback Created Jan. 25, 2018, 4:42 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
« no previous file with comments | « compiled/ElemHide.h ('k') | compiled/bindings/main.cpp » ('j') | no next file with comments »
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 #include "ElemHide.h"
19
20 OwnedString ElemHide_SelectorList::SelectorAt(size_t idx) const
21 {
22 return mSelectors[idx]->GetSelector();
23 }
24
25 const String& ElemHide_SelectorList::FilterKeyAt(size_t idx) const
26 {
27 return mSelectors[idx]->GetText();
28 }
29
30 void ElemHide::Clear()
31 {
32 mFilters.clear();
33 mExceptions.clear();
34 mFiltersByDomain.clear();
35 mKnownExceptions.clear();
36 }
37
38 namespace
39 {
40 const ActiveFilter::DomainMap defaultDomains =
41 {
42 { ActiveFilter::DEFAULT_DOMAIN, true }
43 };
44 }
45
46 void ElemHide::AddToFiltersByDomain(const ElemHideBasePtr& filter)
47 {
48 const auto* domains = filter->GetDomains();
49 if (!domains)
50 domains = &defaultDomains;
51
52 DependentString text(filter->GetText());
53 for (const auto& domain : *domains)
54 {
55 auto& filters = mFiltersByDomain[domain.first];
56 if (domain.second)
57 filters[text] = filter;
58 else
59 filters[text] = ElemHideBasePtr();
60 }
61 }
62
63 void ElemHide::Add(ElemHideBase& filter)
64 {
65 // we must ensure we have the right class.
66 // This is an error, but we might get Invalid filters.
67 if (!filter.As<ElemHideBase>())
68 return;
69
70 DependentString text(filter.GetText());
71 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION)
72 {
73 if (mKnownExceptions.find(text))
74 return;
75
76 auto selector = filter.GetSelector();
77 mExceptions[selector].emplace_back(filter.As<ElemHideException>());
78
79 // Selector is no longer unconditional
80 auto entry = mUnconditionalSelectors.find(selector);
81 if (entry && entry->second)
82 {
83 AddToFiltersByDomain(entry->second);
84 mUnconditionalSelectors.erase(selector);
85 mUnconditionalSelectorsCache.reset();
86 }
87 mKnownExceptions.insert(text);
88 }
89 else
90 {
91 if (mFilters.find(text))
92 return;
93
94 auto selector = filter.GetSelector();
95 mFilters[text] = &filter;
96 if (!((filter.GetDomains() && filter.GetDomains()->size()) ||
97 mExceptions.find(selector)))
98 {
99 // The new filter's selector is unconditionally applied to all domains
100 mUnconditionalSelectors[selector] = ElemHideBasePtr(&filter);
101 mUnconditionalSelectorsCache.reset();
102 }
103 else
104 AddToFiltersByDomain(ElemHideBasePtr(&filter));
105 }
106 }
107
108 void ElemHide::Remove(ElemHideBase& filter)
109 {
110 DependentString text(filter.GetText());
111
112 auto selector = filter.GetSelector();
113 auto exceptionFilter = filter.As<ElemHideException>();
114 if (exceptionFilter)
115 {
116 // never seen the exception.
117 if (!mKnownExceptions.find(text))
118 return;
119
120 auto& list = mExceptions[selector];
121 auto iter = std::find(list.begin(), list.end(),
122 ElemHideExceptionPtr(exceptionFilter));
123 if (iter != list.end())
124 list.erase(iter);
125 mKnownExceptions.erase(text);
126 }
127 else
128 {
129 if (!mFilters.find(text))
130 return;
131
132 if (mUnconditionalSelectors.find(selector))
133 {
134 mUnconditionalSelectors.erase(selector);
135 mUnconditionalSelectorsCache.reset();
136 }
137 else
138 {
139 const auto* domains = filter.GetDomains();
140 if (!domains)
141 domains = &defaultDomains;
142
143 for (const 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 {
164 DependentString domain(docDomain);
165 if (*iter && (*iter)->IsActiveOnDomain(domain))
166 {
167 ElemHideExceptionPtr filter(*iter);
168 return filter.release();
169 }
170 }
171
172 return nullptr;
173 }
174
175 ElemHide_SelectorList* ElemHide::GetUnconditionalSelectors() const
176 {
177 if (!mUnconditionalSelectorsCache)
178 {
179 mUnconditionalSelectorsCache =
180 intrusive_ptr<ElemHide_SelectorList>(new ElemHide_SelectorList(), false);
181 annotate_address(mUnconditionalSelectorsCache.get(), "ElemHide_SelectorList" );
182 for (const auto& unconditional : mUnconditionalSelectors)
183 {
184 if (!(unconditional.is_deleted() || unconditional.is_invalid()))
185 {
186 auto entry = mFilters.find(unconditional.second->GetText());
187 if (entry)
188 mUnconditionalSelectorsCache->push_back(entry->second);
189 }
190 }
191 }
192 return intrusive_ptr<ElemHide_SelectorList>(mUnconditionalSelectorsCache).rele ase();
193 }
194
195 ElemHide_SelectorList* ElemHide::GetSelectorsForDomain(const String& domain,
196 Criteria criteria) const
197 {
198 intrusive_ptr<ElemHide_SelectorList> selectors(new ElemHide_SelectorList());
199 annotate_address(selectors.get(), "ElemHide_SelectorList");
200
201 if (criteria < NO_UNCONDITIONAL)
202 {
203 intrusive_ptr<ElemHide_SelectorList> selector(GetUnconditionalSelectors());
204 selectors->append(*selector);
205 }
206
207 bool specificOnly = criteria >= SPECIFIC_ONLY;
208 StringSet seenFilters;
209 DependentString docDomain(domain);
210
211 DependentString currentDomain(domain);
212 while (true)
213 {
214 if (specificOnly && currentDomain.empty())
215 break;
216
217 auto filters = mFiltersByDomain.find(currentDomain);
218 if (filters)
219 {
220 for (const auto& entry : filters->second)
221 {
222 if (entry.first.is_invalid() || entry.first.is_deleted())
223 continue;
224
225 if (seenFilters.find(entry.first))
226 continue;
227 seenFilters.insert(entry.first);
228
229 auto filter = entry.second;
230 if (filter && !GetException(*filter, docDomain))
231 selectors->push_back(filter);
232 }
233 }
234
235 if (currentDomain.empty())
236 break;
237
238 auto nextDot = currentDomain.find(u'.');
239 currentDomain = nextDot == String::npos ?
240 u""_str : DependentString(currentDomain, nextDot + 1);
241 }
242
243 return selectors.release();
244 }
OLDNEW
« no previous file with comments | « compiled/ElemHide.h ('k') | compiled/bindings/main.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld