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: Rebased on master. Created Dec. 5, 2017, 6:01 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 OwnedString ElemHide_SelectorList::SelectorAt(size_t idx) const
21 {
22 return std::move(mSelectors[idx]->GetSelector());
sergei 2018/01/15 15:31:18 std::move is redundant for return values.
hub 2018/01/16 02:57:38 Done.
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()
sergei 2018/01/15 15:31:17 What about just making it as a variable? AFAIK the
hub 2018/01/16 02:57:37 Done, but I can't make it const since we need to s
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();
sergei 2018/01/15 15:31:17 it should be `const auto domains`.
hub 2018/01/16 02:57:37 this would prevent line 56 from compiling because
sergei 2018/01/16 16:43:57 One should use `const auto* domains = `.
hub 2018/01/19 02:11:02 Done.
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>())
sergei 2018/01/15 15:31:16 `filter` type is already `ElemHideBase`, it looks
hub 2018/01/16 02:57:36 It is not. The bindings don't guarantee it and in
sergei 2018/01/16 16:43:56 Acknowledged, then let's keep it as is.
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(
sergei 2018/01/15 15:31:16 with emplace_back it's not necessary to use ElemHi
hub 2018/01/16 02:57:38 Done.
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>());
sergei 2018/01/15 15:31:17 ElemHideBasePtr is not required here.
hub 2018/01/16 02:57:36 Done.
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 AddToFiltersByDomain(filter);
110 }
111 }
112
113 void ElemHide::Remove(ElemHideBase& filter)
114 {
115 DependentString text(filter.GetText());
116
117 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION)
118 {
119 // never seen the exception.
120 if (!mKnownExceptions.find(text))
121 return;
122
123 auto selector = filter.GetSelector();
124 auto& list = mExceptions[selector];
125 auto iter = std::find(
126 list.begin(), list.end(),
127 ElemHideExceptionPtr(filter.As<ElemHideException>()));
128 if (iter != list.end())
129 list.erase(iter);
130 mKnownExceptions.erase(text);
131 }
132 else
133 {
134 if (!mFilters.find(text))
135 return;
136
137 if (mUnconditionalSelectors.find(text))
138 {
139 mUnconditionalSelectors.erase(text);
140 mUnconditionalSelectorsCache.reset();
141 }
142 else
143 {
144 auto domains = filter.GetDomains();
145 for (auto domain : *domains)
146 {
147 auto& list = mFiltersByDomain[domain.first];
148 list.erase(text);
149 }
150 }
151
152 mFilters.erase(text);
153 }
154 }
155
156 ElemHideException* ElemHide::GetException(const ElemHideBase& filter,
157 DependentString& docDomain) const
158 {
159 auto exception = mExceptions.find(filter.GetSelector());
160 if (!exception)
161 return nullptr;
162
163 auto& list = exception->second;
164 for (auto iter = list.rbegin(); iter != list.rend(); iter++)
165 {
166 DependentString domain(docDomain);
167 if ((*iter)->IsActiveOnDomain(domain, u""_str))
sergei 2018/01/15 15:31:17 should the sitekey parameter be the invalid string
hub 2018/01/16 02:57:38 It's probably better.
168 {
169 ElemHideExceptionPtr filter(*iter);
170 return filter.release();
171 }
172 }
173
174 return nullptr;
175 }
176
177 ElemHide_SelectorList* ElemHide::GetUnconditionalSelectors() const
178 {
179 if (!mUnconditionalSelectorsCache)
180 {
181 mUnconditionalSelectorsCache = intrusive_ptr<ElemHide_SelectorList>(new Elem Hide_SelectorList, false);
sergei 2018/01/15 15:31:16 it woule be better to have it `new X()` with paren
hub 2018/01/16 02:57:38 Done.
182 annotate_address(mUnconditionalSelectorsCache.get(), "ElemHide_SelectorList" );
183 for (auto unconditional : mUnconditionalSelectors)
184 {
185 auto filter = mFilters.find(unconditional.first);
sergei 2018/01/15 15:31:18 what do you think about renaming it to something r
hub 2018/01/16 02:57:36 We check the entry just below and if it has been d
sergei 2018/01/16 16:43:56 It's actually correct but I thought that find (whi
186 if (filter)
187 mUnconditionalSelectorsCache->push_back(filter->second);
188 }
189 }
190 return intrusive_ptr<ElemHide_SelectorList>(mUnconditionalSelectorsCache).rele ase();
191 }
192
193 ElemHide_SelectorList* ElemHide::GetSelectorsForDomain(const String& domain,
194 Criteria criteria) const
195 {
196 intrusive_ptr<ElemHide_SelectorList> selectors(new ElemHide_SelectorList);
sergei 2018/01/15 15:31:17 it would be better to always use () or {} in new e
hub 2018/01/16 02:57:36 Done.
197 annotate_address(selectors.get(), "ElemHide_SelectorList");
198
199 if (criteria < NO_UNCONDITIONAL)
200 selectors->append(GetUnconditionalSelectors());
sergei 2018/01/15 15:31:18 GetUnconditionalSelectors() returns a raw pointer
hub 2018/01/16 02:57:37 Done.
201
202 bool specificOnly = (criteria >= SPECIFIC_ONLY);
sergei 2018/01/15 15:31:18 I think it would be better to remove the parenthes
hub 2018/01/16 02:57:38 Done.
203 StringSet seenFilters;
204 DependentString docDomain(domain);
205
206 DependentString currentDomain(domain);
207 while (true)
208 {
209 if (specificOnly && currentDomain.empty())
210 break;
211
212 auto filters = mFiltersByDomain.find(currentDomain);
213 if (filters)
214 {
215 for (auto& entry : filters->second)
216 {
217 if (entry.first.is_invalid())
sergei 2018/01/15 15:31:17 should deleted be also filtered out?
hub 2018/01/16 02:57:36 I think so. Done.
218 continue;
219
220 if (seenFilters.find(entry.first))
221 continue;
222 seenFilters.insert(entry.first);
223
224 auto filter = entry.second;
225 if (filter && !GetException(*filter, docDomain))
226 selectors->push_back(filter);
227 }
228 }
229
230 if (currentDomain.empty())
231 break;
232
233 auto nextDot = currentDomain.find('.');
sergei 2018/01/15 15:31:16 the parameter should be u'.'.
hub 2018/01/16 02:57:37 Done.
234 currentDomain = nextDot == String::npos ?
235 u""_str : DependentString(currentDomain, nextDot + 1);
236 }
237
238 return selectors.release();
239 }
OLDNEW

Powered by Google App Engine
This is Rietveld