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

Delta Between Two Patch Sets: compiled/ElemHide.cpp

Issue 29587914: Issue 5142 - Convert Element Hiding to C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Add proper delete for filters in tests. Created Nov. 8, 2017, 12:02 a.m.
Right Patch Set: mFiltersByDomain is now an OwnedStringMap Created Jan. 26, 2018, 8:41 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/ElemHide.h ('k') | compiled/bindings/main.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include "ElemHide.h" 18 #include "ElemHide.h"
19 19
20 DependentString _ElemHide_SelectorList::SelectorAt(size_t idx) const 20 OwnedString ElemHide_SelectorList::SelectorAt(size_t idx) const
21 { 21 {
22 return DependentString(mSelectors[idx]->GetSelector()); 22 return mSelectors[idx]->GetSelector();
23 } 23 }
24 24
25 const String& _ElemHide_SelectorList::FilterKeyAt(size_t idx) const 25 const String& ElemHide_SelectorList::FilterKeyAt(size_t idx) const
26 { 26 {
27 return mSelectors[idx]->GetText(); 27 return mSelectors[idx]->GetText();
28 } 28 }
29
30 ElemHide* ElemHide::mInstance = new ElemHide();
31 29
32 void ElemHide::Clear() 30 void ElemHide::Clear()
33 { 31 {
34 mFilters.clear(); 32 mFilters.clear();
35 mExceptions.clear(); 33 mExceptions.clear();
36 mFiltersByDomain.clear(); 34 mFiltersByDomain.clear();
37 mKnownExceptions.clear(); 35 mKnownExceptions.clear();
38 } 36 }
39 37
40 namespace { 38 namespace
41 39 {
42 ActiveFilter::DomainMap* DefaultDomains() 40 const ActiveFilter::DomainMap defaultDomains =
43 { 41 {
44 static ActiveFilter::DomainMap defaultDomains; 42 { ActiveFilter::DEFAULT_DOMAIN, true }
45 if (defaultDomains.empty()) 43 };
46 defaultDomains[u""_str] = true; 44 }
47 return &defaultDomains; 45
48 } 46 void ElemHide::AddToFiltersByDomain(const ElemHideBasePtr& filter)
49 47 {
50 } 48 const auto* domains = filter->GetDomains();
51
52 void ElemHide::AddToFiltersByDomain(ElemHideBase& filter)
53 {
54 auto domains = filter.GetDomains();
55 if (!domains) 49 if (!domains)
56 domains = DefaultDomains(); 50 domains = &defaultDomains;
57 51
58 DependentString text(filter.GetText()); 52 DependentString text(filter->GetText());
59 for (auto& domain : *domains) 53 for (const auto& domain : *domains)
60 { 54 {
61 auto& filters = mFiltersByDomain[domain.first]; 55 auto& filters = mFiltersByDomain[domain.first];
62 if (domain.second) 56 if (domain.second)
63 filters[text] = ElemHideBasePtr(&filter); 57 filters[text] = filter;
64 else 58 else
65 { 59 filters[text] = ElemHideBasePtr();
66 auto iter = filters.find(text);
67 if (iter != filters.end())
68 filters.erase(iter);
69 }
70 } 60 }
71 } 61 }
72 62
73 void ElemHide::Add(ElemHideBase& filter) 63 void ElemHide::Add(ElemHideBase& filter)
74 { 64 {
75 // we must ensure we have the right class. 65 // we must ensure we have the right class.
76 // This is an error, but we might get Invalid filters. 66 // This is an error, but we might get Invalid filters.
77 if (!filter.As<ElemHideBase>()) 67 if (!filter.As<ElemHideBase>())
78 return; 68 return;
79 69
80 DependentString text(filter.GetText()); 70 DependentString text(filter.GetText());
81 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION) 71 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION)
82 { 72 {
83 if (mKnownExceptions.find(text)) 73 if (mKnownExceptions.find(text))
84 return; 74 return;
85 75
86 auto selector = filter.GetSelector(); 76 auto selector = filter.GetSelector();
87 mExceptions[selector].push_back( 77 mExceptions[selector].emplace_back(filter.As<ElemHideException>());
88 ElemHideExceptionPtr(filter.As<ElemHideException>())); 78
89 79 // Selector is no longer unconditional
90 // Selector is not longer unconditional 80 auto entry = mUnconditionalSelectors.find(selector);
91 mUnconditionalSelectors.erase(text); 81 if (entry && entry->second)
92 mUnconditionalSelectorsCache.reset(); 82 {
83 AddToFiltersByDomain(entry->second);
84 mUnconditionalSelectors.erase(selector);
85 mUnconditionalSelectorsCache.reset();
86 }
93 mKnownExceptions.insert(text); 87 mKnownExceptions.insert(text);
94 } 88 }
95 else 89 else
96 { 90 {
97 if (mFilters.find(text)) 91 if (mFilters.find(text))
98 return; 92 return;
99 93
100 mFilters[text] = ElemHideBasePtr(filter.As<ElemHideBase>()); 94 auto selector = filter.GetSelector();
95 mFilters[text] = &filter;
101 if (!((filter.GetDomains() && filter.GetDomains()->size()) || 96 if (!((filter.GetDomains() && filter.GetDomains()->size()) ||
102 mExceptions.find(filter.GetSelector()))) 97 mExceptions.find(selector)))
103 { 98 {
104 // The new filter's selector is unconditionally applied to all domains 99 // The new filter's selector is unconditionally applied to all domains
105 mUnconditionalSelectors.insert(text); 100 mUnconditionalSelectors[selector] = ElemHideBasePtr(&filter);
106 mUnconditionalSelectorsCache.reset(); 101 mUnconditionalSelectorsCache.reset();
107 } 102 }
108 else 103 else
109 { 104 AddToFiltersByDomain(ElemHideBasePtr(&filter));
110 AddToFiltersByDomain(filter);
111 }
112 } 105 }
113 } 106 }
114 107
115 void ElemHide::Remove(ElemHideBase& filter) 108 void ElemHide::Remove(ElemHideBase& filter)
116 { 109 {
117 DependentString text(filter.GetText()); 110 DependentString text(filter.GetText());
118 111
119 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION) 112 auto selector = filter.GetSelector();
113 auto exceptionFilter = filter.As<ElemHideException>();
114 if (exceptionFilter)
120 { 115 {
121 // never seen the exception. 116 // never seen the exception.
122 if (!mKnownExceptions.find(text)) 117 if (!mKnownExceptions.find(text))
123 return; 118 return;
124 119
125 auto selector = filter.GetSelector();
126 auto& list = mExceptions[selector]; 120 auto& list = mExceptions[selector];
127 auto iter = std::find( 121 auto iter = std::find(list.begin(), list.end(),
128 list.begin(), list.end(), 122 ElemHideExceptionPtr(exceptionFilter));
129 ElemHideExceptionPtr(filter.As<ElemHideException>()));
130 if (iter != list.end()) 123 if (iter != list.end())
131 list.erase(iter); 124 list.erase(iter);
132 mKnownExceptions.erase(text); 125 mKnownExceptions.erase(text);
133 } 126 }
134 else 127 else
135 { 128 {
136 if (!mFilters.find(text)) 129 if (!mFilters.find(text))
137 return; 130 return;
138 131
139 if (mUnconditionalSelectors.find(text)) 132 if (mUnconditionalSelectors.find(selector))
140 { 133 {
141 mUnconditionalSelectors.erase(text); 134 mUnconditionalSelectors.erase(selector);
142 mUnconditionalSelectorsCache.reset(); 135 mUnconditionalSelectorsCache.reset();
143 } 136 }
144 else 137 else
145 { 138 {
146 auto domains = filter.GetDomains(); 139 const auto* domains = filter.GetDomains();
147 for (auto domain : *domains) 140 if (!domains)
141 domains = &defaultDomains;
142
143 for (const auto& domain : *domains)
148 { 144 {
149 auto& list = mFiltersByDomain[domain.first]; 145 auto& list = mFiltersByDomain[domain.first];
150 list.erase(text); 146 list.erase(text);
151 } 147 }
152 } 148 }
153 149
154 mFilters.erase(text); 150 mFilters.erase(text);
155 } 151 }
156 } 152 }
157 153
158 ElemHideException* ElemHide::GetException(const ElemHideBase& filter, 154 ElemHideException* ElemHide::GetException(const ElemHideBase& filter,
159 DependentString& docDomain) const 155 DependentString& docDomain) const
160 { 156 {
161 auto exception = mExceptions.find(filter.GetSelector()); 157 auto exception = mExceptions.find(filter.GetSelector());
162 if (!exception) 158 if (!exception)
163 return nullptr; 159 return nullptr;
164 160
165 auto& list = exception->second; 161 auto& list = exception->second;
166 for (auto iter = list.rbegin(); iter != list.rend(); iter++) 162 for (auto iter = list.rbegin(); iter != list.rend(); iter++)
167 { 163 {
168 DependentString domain(docDomain); 164 DependentString domain(docDomain);
169 if ((*iter)->IsActiveOnDomain(domain, u""_str)) 165 if (*iter && (*iter)->IsActiveOnDomain(domain))
170 { 166 {
171 ElemHideExceptionPtr filter(*iter); 167 ElemHideExceptionPtr filter(*iter);
172 return filter.release(); 168 return filter.release();
173 } 169 }
174 } 170 }
175 171
176 return nullptr; 172 return nullptr;
177 } 173 }
178 174
179 _ElemHide_SelectorList* ElemHide::GetUnconditionalSelectors() const 175 ElemHide_SelectorList* ElemHide::GetUnconditionalSelectors() const
180 { 176 {
181 if (!mUnconditionalSelectorsCache) 177 if (!mUnconditionalSelectorsCache)
182 { 178 {
183 mUnconditionalSelectorsCache = intrusive_ptr<_ElemHide_SelectorList>(new _El emHide_SelectorList, false); 179 mUnconditionalSelectorsCache =
184 annotate_address(mUnconditionalSelectorsCache.get(), "_ElemHide_SelectorList "); 180 intrusive_ptr<ElemHide_SelectorList>(new ElemHide_SelectorList(), false);
185 for (auto unconditional : mUnconditionalSelectors) 181 annotate_address(mUnconditionalSelectorsCache.get(), "ElemHide_SelectorList" );
186 { 182 for (const auto& unconditional : mUnconditionalSelectors)
187 auto filter = mFilters.find(unconditional.first); 183 {
188 if (filter) 184 if (!(unconditional.is_deleted() || unconditional.is_invalid()))
189 mUnconditionalSelectorsCache->push_back(filter->second); 185 {
190 } 186 auto entry = mFilters.find(unconditional.second->GetText());
191 } 187 if (entry)
192 return mUnconditionalSelectorsCache.get(); 188 mUnconditionalSelectorsCache->push_back(entry->second);
193 } 189 }
194 190 }
195 _ElemHide_SelectorList* ElemHide::GetSelectorsForDomain(const String& domain, 191 }
192 return intrusive_ptr<ElemHide_SelectorList>(mUnconditionalSelectorsCache).rele ase();
193 }
194
195 ElemHide_SelectorList* ElemHide::GetSelectorsForDomain(const String& domain,
196 Criteria criteria) const 196 Criteria criteria) const
197 { 197 {
198 _ElemHide_SelectorList* selectors = new _ElemHide_SelectorList; 198 intrusive_ptr<ElemHide_SelectorList> selectors(new ElemHide_SelectorList());
199 annotate_address(selectors, "_ElemHide_SelectorList"); 199 annotate_address(selectors.get(), "ElemHide_SelectorList");
200 200
201 if (criteria < NO_UNCONDITIONAL) 201 if (criteria < NO_UNCONDITIONAL)
202 selectors->append(GetUnconditionalSelectors()); 202 {
203 203 intrusive_ptr<ElemHide_SelectorList> selector(GetUnconditionalSelectors());
204 bool specificOnly = (criteria >= SPECIFIC_ONLY); 204 selectors->append(*selector);
205 }
206
207 bool specificOnly = criteria >= SPECIFIC_ONLY;
205 StringSet seenFilters; 208 StringSet seenFilters;
206 DependentString docDomain(domain); 209 DependentString docDomain(domain);
207 210
208 DependentString currentDomain(domain); 211 DependentString currentDomain(domain);
209 while (true) 212 while (true)
210 { 213 {
211 if (specificOnly && currentDomain.empty()) 214 if (specificOnly && currentDomain.empty())
212 break; 215 break;
213 216
214 auto filters = mFiltersByDomain.find(currentDomain); 217 auto filters = mFiltersByDomain.find(currentDomain);
215 if (filters) 218 if (filters)
216 { 219 {
217 for (auto& entry : filters->second) 220 for (const auto& entry : filters->second)
218 { 221 {
219 if (entry.first.is_invalid()) 222 if (entry.first.is_invalid() || entry.first.is_deleted())
220 continue; 223 continue;
221 224
222 if (seenFilters.find(entry.first)) 225 if (seenFilters.find(entry.first))
223 continue; 226 continue;
224 seenFilters.insert(entry.first); 227 seenFilters.insert(entry.first);
225 228
226 auto filter = entry.second; 229 auto filter = entry.second;
227 if (filter && !GetException(*filter, docDomain)) 230 if (filter && !GetException(*filter, docDomain))
228 selectors->push_back(filter); 231 selectors->push_back(filter);
229 } 232 }
230 } 233 }
231 234
232 if (currentDomain.empty()) 235 if (currentDomain.empty())
233 break; 236 break;
234 237
235 auto nextDot = currentDomain.find('.'); 238 auto nextDot = currentDomain.find(u'.');
236 currentDomain = nextDot == String::npos ? 239 currentDomain = nextDot == String::npos ?
237 u""_str : DependentString(currentDomain, nextDot + 1); 240 u""_str : DependentString(currentDomain, nextDot + 1);
238 } 241 }
239 242
240 return selectors; 243 return selectors.release();
241 } 244 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld