Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 |
(...skipping 17 matching lines...) Expand all Loading... | |
28 } | 28 } |
29 | 29 |
30 void ElemHide::Clear() | 30 void ElemHide::Clear() |
31 { | 31 { |
32 mFilters.clear(); | 32 mFilters.clear(); |
33 mExceptions.clear(); | 33 mExceptions.clear(); |
34 mFiltersByDomain.clear(); | 34 mFiltersByDomain.clear(); |
35 mKnownExceptions.clear(); | 35 mKnownExceptions.clear(); |
36 } | 36 } |
37 | 37 |
38 ActiveFilter::DomainMap ElemHide::defaultDomains; | 38 namespace |
sergei
2018/01/16 16:43:59
It should be just
namespace
{
const ActiveFilter
hub
2018/01/19 02:11:03
Done.
sergei
2018/01/22 15:40:07
Do we really need it not in the anonymous namespac
hub
2018/01/22 16:59:15
Indeed we don't.
Done
| |
39 | 39 { |
40 void ElemHide::AddToFiltersByDomain(ElemHideBase& filter) | 40 const ActiveFilter::DomainMap defaultDomains = |
41 { | 41 { |
42 auto domains = filter.GetDomains(); | 42 { ActiveFilter::DEFAULT_DOMAIN, true } |
43 }; | |
44 } | |
45 | |
46 void ElemHide::AddToFiltersByDomain(const ElemHideBasePtr& filter) | |
47 { | |
48 const auto* domains = filter->GetDomains(); | |
43 if (!domains) | 49 if (!domains) |
44 domains = &defaultDomains; | 50 domains = &defaultDomains; |
45 | 51 |
46 DependentString text(filter.GetText()); | 52 DependentString text(filter->GetText()); |
47 for (auto& domain : *domains) | 53 for (const auto& domain : *domains) |
sergei
2018/01/16 16:43:59
it should be `const auto&`.
hub
2018/01/19 02:11:04
Done.
| |
48 { | 54 { |
49 auto& filters = mFiltersByDomain[domain.first]; | 55 auto& filters = mFiltersByDomain[domain.first]; |
50 if (domain.second) | 56 if (domain.second) |
51 filters[text] = ElemHideBasePtr(&filter); | 57 filters[text] = filter; |
52 else | 58 else |
53 { | 59 filters[text] = ElemHideBasePtr(); |
54 auto iter = filters.find(text); | |
55 if (iter != filters.end()) | |
56 filters.erase(iter); | |
57 } | |
58 } | 60 } |
59 } | 61 } |
60 | 62 |
61 void ElemHide::Add(ElemHideBase& filter) | 63 void ElemHide::Add(ElemHideBase& filter) |
62 { | 64 { |
63 // we must ensure we have the right class. | 65 // we must ensure we have the right class. |
64 // This is an error, but we might get Invalid filters. | 66 // This is an error, but we might get Invalid filters. |
65 if (!filter.As<ElemHideBase>()) | 67 if (!filter.As<ElemHideBase>()) |
66 return; | 68 return; |
67 | 69 |
68 DependentString text(filter.GetText()); | 70 DependentString text(filter.GetText()); |
69 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION) | 71 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION) |
sergei
2018/01/16 18:05:27
what about
if (auto exceptionFilter = filter.As<El
hub
2018/01/19 02:11:03
Done.
| |
70 { | 72 { |
71 if (mKnownExceptions.find(text)) | 73 if (mKnownExceptions.find(text)) |
72 return; | 74 return; |
73 | 75 |
74 auto selector = filter.GetSelector(); | 76 auto selector = filter.GetSelector(); |
75 mExceptions[selector].emplace_back(filter.As<ElemHideException>()); | 77 mExceptions[selector].emplace_back(filter.As<ElemHideException>()); |
76 | 78 |
77 // Selector is not longer unconditional | 79 // Selector is no longer unconditional |
78 mUnconditionalSelectors.erase(text); | 80 auto entry = mUnconditionalSelectors.find(selector); |
79 mUnconditionalSelectorsCache.reset(); | 81 if (entry && entry->second) |
82 { | |
83 AddToFiltersByDomain(entry->second); | |
84 mUnconditionalSelectors.erase(selector); | |
85 mUnconditionalSelectorsCache.reset(); | |
86 } | |
80 mKnownExceptions.insert(text); | 87 mKnownExceptions.insert(text); |
81 } | 88 } |
82 else | 89 else |
83 { | 90 { |
84 if (mFilters.find(text)) | 91 if (mFilters.find(text)) |
85 return; | 92 return; |
86 | 93 |
94 auto selector = filter.GetSelector(); | |
87 mFilters[text] = &filter; | 95 mFilters[text] = &filter; |
88 if (!((filter.GetDomains() && filter.GetDomains()->size()) || | 96 if (!((filter.GetDomains() && filter.GetDomains()->size()) || |
89 mExceptions.find(filter.GetSelector()))) | 97 mExceptions.find(selector))) |
90 { | 98 { |
91 // The new filter's selector is unconditionally applied to all domains | 99 // The new filter's selector is unconditionally applied to all domains |
92 mUnconditionalSelectors.insert(text); | 100 mUnconditionalSelectors[selector] = ElemHideBasePtr(&filter); |
93 mUnconditionalSelectorsCache.reset(); | 101 mUnconditionalSelectorsCache.reset(); |
94 } | 102 } |
95 else | 103 else |
96 AddToFiltersByDomain(filter); | 104 AddToFiltersByDomain(ElemHideBasePtr(&filter)); |
97 } | 105 } |
98 } | 106 } |
99 | 107 |
100 void ElemHide::Remove(ElemHideBase& filter) | 108 void ElemHide::Remove(ElemHideBase& filter) |
101 { | 109 { |
102 DependentString text(filter.GetText()); | 110 DependentString text(filter.GetText()); |
103 | 111 |
104 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION) | 112 auto selector = filter.GetSelector(); |
113 auto exceptionFilter = filter.As<ElemHideException>(); | |
114 if (exceptionFilter) | |
105 { | 115 { |
106 // never seen the exception. | 116 // never seen the exception. |
107 if (!mKnownExceptions.find(text)) | 117 if (!mKnownExceptions.find(text)) |
108 return; | 118 return; |
109 | 119 |
110 auto selector = filter.GetSelector(); | |
111 auto& list = mExceptions[selector]; | 120 auto& list = mExceptions[selector]; |
112 auto iter = std::find( | 121 auto iter = std::find(list.begin(), list.end(), |
113 list.begin(), list.end(), | 122 ElemHideExceptionPtr(exceptionFilter)); |
114 ElemHideExceptionPtr(filter.As<ElemHideException>())); | |
115 if (iter != list.end()) | 123 if (iter != list.end()) |
116 list.erase(iter); | 124 list.erase(iter); |
117 mKnownExceptions.erase(text); | 125 mKnownExceptions.erase(text); |
118 } | 126 } |
119 else | 127 else |
120 { | 128 { |
121 if (!mFilters.find(text)) | 129 if (!mFilters.find(text)) |
122 return; | 130 return; |
123 | 131 |
124 if (mUnconditionalSelectors.find(text)) | 132 if (mUnconditionalSelectors.find(selector)) |
125 { | 133 { |
126 mUnconditionalSelectors.erase(text); | 134 mUnconditionalSelectors.erase(selector); |
127 mUnconditionalSelectorsCache.reset(); | 135 mUnconditionalSelectorsCache.reset(); |
128 } | 136 } |
129 else | 137 else |
130 { | 138 { |
131 auto domains = filter.GetDomains(); | 139 const auto* domains = filter.GetDomains(); |
132 for (auto domain : *domains) | 140 if (!domains) |
sergei
2018/01/16 18:05:27
Can domains be nullptr?
hub
2018/01/19 02:11:03
Anything can happen. I check it elsewhere too.
Do
| |
141 domains = &defaultDomains; | |
142 | |
143 for (const auto& domain : *domains) | |
133 { | 144 { |
134 auto& list = mFiltersByDomain[domain.first]; | 145 auto& list = mFiltersByDomain[domain.first]; |
135 list.erase(text); | 146 list.erase(text); |
136 } | 147 } |
137 } | 148 } |
138 | 149 |
139 mFilters.erase(text); | 150 mFilters.erase(text); |
140 } | 151 } |
141 } | 152 } |
142 | 153 |
143 ElemHideException* ElemHide::GetException(const ElemHideBase& filter, | 154 ElemHideException* ElemHide::GetException(const ElemHideBase& filter, |
144 DependentString& docDomain) const | 155 DependentString& docDomain) const |
145 { | 156 { |
146 auto exception = mExceptions.find(filter.GetSelector()); | 157 auto exception = mExceptions.find(filter.GetSelector()); |
147 if (!exception) | 158 if (!exception) |
148 return nullptr; | 159 return nullptr; |
149 | 160 |
150 auto& list = exception->second; | 161 auto& list = exception->second; |
151 for (auto iter = list.rbegin(); iter != list.rend(); iter++) | 162 for (auto iter = list.rbegin(); iter != list.rend(); iter++) |
152 { | 163 { |
153 DependentString domain(docDomain); | 164 DependentString domain(docDomain); |
154 if ((*iter)->IsActiveOnDomain(domain, DependentString())) | 165 if (*iter && (*iter)->IsActiveOnDomain(domain)) |
155 { | 166 { |
156 ElemHideExceptionPtr filter(*iter); | 167 ElemHideExceptionPtr filter(*iter); |
157 return filter.release(); | 168 return filter.release(); |
158 } | 169 } |
159 } | 170 } |
160 | 171 |
161 return nullptr; | 172 return nullptr; |
162 } | 173 } |
163 | 174 |
164 ElemHide_SelectorList* ElemHide::GetUnconditionalSelectors() const | 175 ElemHide_SelectorList* ElemHide::GetUnconditionalSelectors() const |
165 { | 176 { |
166 if (!mUnconditionalSelectorsCache) | 177 if (!mUnconditionalSelectorsCache) |
167 { | 178 { |
168 mUnconditionalSelectorsCache = | 179 mUnconditionalSelectorsCache = |
169 intrusive_ptr<ElemHide_SelectorList>(new ElemHide_SelectorList(), false); | 180 intrusive_ptr<ElemHide_SelectorList>(new ElemHide_SelectorList(), false); |
170 annotate_address(mUnconditionalSelectorsCache.get(), "ElemHide_SelectorList" ); | 181 annotate_address(mUnconditionalSelectorsCache.get(), "ElemHide_SelectorList" ); |
171 for (auto unconditional : mUnconditionalSelectors) | 182 for (const auto& unconditional : mUnconditionalSelectors) |
172 { | 183 { |
173 auto entry = mFilters.find(unconditional.first); | 184 if (!(unconditional.is_deleted() || unconditional.is_invalid())) |
174 if (entry) | 185 { |
175 mUnconditionalSelectorsCache->push_back(entry->second); | 186 auto entry = mFilters.find(unconditional.second->GetText()); |
187 if (entry) | |
188 mUnconditionalSelectorsCache->push_back(entry->second); | |
189 } | |
176 } | 190 } |
177 } | 191 } |
178 return intrusive_ptr<ElemHide_SelectorList>(mUnconditionalSelectorsCache).rele ase(); | 192 return intrusive_ptr<ElemHide_SelectorList>(mUnconditionalSelectorsCache).rele ase(); |
179 } | 193 } |
180 | 194 |
181 ElemHide_SelectorList* ElemHide::GetSelectorsForDomain(const String& domain, | 195 ElemHide_SelectorList* ElemHide::GetSelectorsForDomain(const String& domain, |
182 Criteria criteria) const | 196 Criteria criteria) const |
183 { | 197 { |
184 intrusive_ptr<ElemHide_SelectorList> selectors(new ElemHide_SelectorList()); | 198 intrusive_ptr<ElemHide_SelectorList> selectors(new ElemHide_SelectorList()); |
185 annotate_address(selectors.get(), "ElemHide_SelectorList"); | 199 annotate_address(selectors.get(), "ElemHide_SelectorList"); |
186 | 200 |
187 if (criteria < NO_UNCONDITIONAL) | 201 if (criteria < NO_UNCONDITIONAL) |
188 { | 202 { |
189 auto selector = GetUnconditionalSelectors(); | 203 intrusive_ptr<ElemHide_SelectorList> selector(GetUnconditionalSelectors()); |
sergei
2018/01/16 16:43:59
what about using of intrusive_ptr?
hub
2018/01/19 02:11:03
Done.
| |
190 selectors->append(*selector); | 204 selectors->append(*selector); |
191 selector->ReleaseRef(); | |
192 } | 205 } |
193 | 206 |
194 bool specificOnly = criteria >= SPECIFIC_ONLY; | 207 bool specificOnly = criteria >= SPECIFIC_ONLY; |
195 StringSet seenFilters; | 208 StringSet seenFilters; |
196 DependentString docDomain(domain); | 209 DependentString docDomain(domain); |
197 | 210 |
198 DependentString currentDomain(domain); | 211 DependentString currentDomain(domain); |
199 while (true) | 212 while (true) |
200 { | 213 { |
201 if (specificOnly && currentDomain.empty()) | 214 if (specificOnly && currentDomain.empty()) |
202 break; | 215 break; |
203 | 216 |
204 auto filters = mFiltersByDomain.find(currentDomain); | 217 auto filters = mFiltersByDomain.find(currentDomain); |
205 if (filters) | 218 if (filters) |
206 { | 219 { |
207 for (auto& entry : filters->second) | 220 for (const auto& entry : filters->second) |
208 { | 221 { |
209 if (entry.first.is_invalid() || entry.first.is_deleted()) | 222 if (entry.first.is_invalid() || entry.first.is_deleted()) |
sergei
2018/01/16 16:43:59
I have added https://issues.adblockplus.org/ticket
hub
2018/01/19 02:11:04
Acknowledged.
| |
210 continue; | 223 continue; |
211 | 224 |
212 if (seenFilters.find(entry.first)) | 225 if (seenFilters.find(entry.first)) |
213 continue; | 226 continue; |
214 seenFilters.insert(entry.first); | 227 seenFilters.insert(entry.first); |
215 | 228 |
216 auto filter = entry.second; | 229 auto filter = entry.second; |
217 if (filter && !GetException(*filter, docDomain)) | 230 if (filter && !GetException(*filter, docDomain)) |
218 selectors->push_back(filter); | 231 selectors->push_back(filter); |
219 } | 232 } |
220 } | 233 } |
221 | 234 |
222 if (currentDomain.empty()) | 235 if (currentDomain.empty()) |
223 break; | 236 break; |
224 | 237 |
225 auto nextDot = currentDomain.find(u'.'); | 238 auto nextDot = currentDomain.find(u'.'); |
226 currentDomain = nextDot == String::npos ? | 239 currentDomain = nextDot == String::npos ? |
227 u""_str : DependentString(currentDomain, nextDot + 1); | 240 u""_str : DependentString(currentDomain, nextDot + 1); |
228 } | 241 } |
229 | 242 |
230 return selectors.release(); | 243 return selectors.release(); |
231 } | 244 } |
LEFT | RIGHT |