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: Re-added elemHide.js test. Fixed test failures related. Addresses review comments. Created Jan. 19, 2018, 2: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
(...skipping 17 matching lines...) Expand all
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 const ActiveFilter::DomainMap ElemHide::defaultDomains = { 38 namespace
39 { ActiveFilter::DEFAULT_DOMAIN, true } 39 {
40 }; 40 const ActiveFilter::DomainMap defaultDomains =
41 41 {
42 void ElemHide::AddToFiltersByDomain(ElemHideBase& filter) 42 { ActiveFilter::DEFAULT_DOMAIN, true }
43 { 43 };
44 const auto* domains = filter.GetDomains(); 44 }
45
46 void ElemHide::AddToFiltersByDomain(const ElemHideBasePtr& filter)
47 {
48 const auto* domains = filter->GetDomains();
45 if (!domains) 49 if (!domains)
46 domains = &defaultDomains; 50 domains = &defaultDomains;
47 51
48 DependentString text(filter.GetText()); 52 DependentString text(filter->GetText());
49 for (const auto& domain : *domains) 53 for (const auto& domain : *domains)
50 { 54 {
51 auto& filters = mFiltersByDomain[domain.first]; 55 auto& filters = mFiltersByDomain[domain.first];
52 if (domain.second) 56 if (domain.second)
53 filters[text] = ElemHideBasePtr(&filter); 57 filters[text] = filter;
54 else 58 else
55 filters[text] = ElemHideBasePtr(); 59 filters[text] = ElemHideBasePtr();
56 } 60 }
57 } 61 }
58 62
59 void ElemHide::Add(ElemHideBase& filter) 63 void ElemHide::Add(ElemHideBase& filter)
60 { 64 {
61 // we must ensure we have the right class. 65 // we must ensure we have the right class.
62 // This is an error, but we might get Invalid filters. 66 // This is an error, but we might get Invalid filters.
63 if (!filter.As<ElemHideBase>()) 67 if (!filter.As<ElemHideBase>())
64 return; 68 return;
65 69
66 DependentString text(filter.GetText()); 70 DependentString text(filter.GetText());
67 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION) 71 if (filter.mType == Filter::Type::ELEMHIDEEXCEPTION)
68 { 72 {
69 if (mKnownExceptions.find(text)) 73 if (mKnownExceptions.find(text))
70 return; 74 return;
71 75
72 auto selector = filter.GetSelector(); 76 auto selector = filter.GetSelector();
73 mExceptions[selector].emplace_back(filter.As<ElemHideException>()); 77 mExceptions[selector].emplace_back(filter.As<ElemHideException>());
74 78
75 // Selector is no longer unconditional 79 // Selector is no longer unconditional
76 auto entry = mUnconditionalSelectors.find(selector); 80 auto entry = mUnconditionalSelectors.find(selector);
77 if (entry) 81 if (entry && entry->second)
78 { 82 {
79 AddToFiltersByDomain(*mFilters[entry->second]); 83 AddToFiltersByDomain(entry->second);
80 mUnconditionalSelectors.erase(selector); 84 mUnconditionalSelectors.erase(selector);
81 mUnconditionalSelectorsCache.reset(); 85 mUnconditionalSelectorsCache.reset();
82 } 86 }
83 mKnownExceptions.insert(text); 87 mKnownExceptions.insert(text);
84 } 88 }
85 else 89 else
86 { 90 {
87 if (mFilters.find(text)) 91 if (mFilters.find(text))
88 return; 92 return;
89 93
90 auto selector = filter.GetSelector(); 94 auto selector = filter.GetSelector();
91 mFilters[text] = &filter; 95 mFilters[text] = &filter;
92 if (!((filter.GetDomains() && filter.GetDomains()->size()) || 96 if (!((filter.GetDomains() && filter.GetDomains()->size()) ||
93 mExceptions.find(selector))) 97 mExceptions.find(selector)))
94 { 98 {
95 // The new filter's selector is unconditionally applied to all domains 99 // The new filter's selector is unconditionally applied to all domains
96 mUnconditionalSelectors[selector] = DependentString(filter.GetText()); 100 mUnconditionalSelectors[selector] = ElemHideBasePtr(&filter);
97 mUnconditionalSelectorsCache.reset(); 101 mUnconditionalSelectorsCache.reset();
98 } 102 }
99 else 103 else
100 AddToFiltersByDomain(filter); 104 AddToFiltersByDomain(ElemHideBasePtr(&filter));
101 } 105 }
102 } 106 }
103 107
104 void ElemHide::Remove(ElemHideBase& filter) 108 void ElemHide::Remove(ElemHideBase& filter)
105 { 109 {
106 DependentString text(filter.GetText()); 110 DependentString text(filter.GetText());
107 111
108 auto selector = filter.GetSelector(); 112 auto selector = filter.GetSelector();
109 auto exceptionFilter = filter.As<ElemHideException>(); 113 auto exceptionFilter = filter.As<ElemHideException>();
110 if (exceptionFilter) 114 if (exceptionFilter)
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 DependentString& docDomain) const 155 DependentString& docDomain) const
152 { 156 {
153 auto exception = mExceptions.find(filter.GetSelector()); 157 auto exception = mExceptions.find(filter.GetSelector());
154 if (!exception) 158 if (!exception)
155 return nullptr; 159 return nullptr;
156 160
157 auto& list = exception->second; 161 auto& list = exception->second;
158 for (auto iter = list.rbegin(); iter != list.rend(); iter++) 162 for (auto iter = list.rbegin(); iter != list.rend(); iter++)
159 { 163 {
160 DependentString domain(docDomain); 164 DependentString domain(docDomain);
161 if ((*iter)->IsActiveOnDomain(domain, DependentString())) 165 if (*iter && (*iter)->IsActiveOnDomain(domain))
162 { 166 {
163 ElemHideExceptionPtr filter(*iter); 167 ElemHideExceptionPtr filter(*iter);
164 return filter.release(); 168 return filter.release();
165 } 169 }
166 } 170 }
167 171
168 return nullptr; 172 return nullptr;
169 } 173 }
170 174
171 ElemHide_SelectorList* ElemHide::GetUnconditionalSelectors() const 175 ElemHide_SelectorList* ElemHide::GetUnconditionalSelectors() const
172 { 176 {
173 if (!mUnconditionalSelectorsCache) 177 if (!mUnconditionalSelectorsCache)
174 { 178 {
175 mUnconditionalSelectorsCache = 179 mUnconditionalSelectorsCache =
176 intrusive_ptr<ElemHide_SelectorList>(new ElemHide_SelectorList(), false); 180 intrusive_ptr<ElemHide_SelectorList>(new ElemHide_SelectorList(), false);
177 annotate_address(mUnconditionalSelectorsCache.get(), "ElemHide_SelectorList" ); 181 annotate_address(mUnconditionalSelectorsCache.get(), "ElemHide_SelectorList" );
178 for (auto unconditional : mUnconditionalSelectors) 182 for (const auto& unconditional : mUnconditionalSelectors)
179 { 183 {
180 if (!(unconditional.is_deleted() || unconditional.is_invalid())) 184 if (!(unconditional.is_deleted() || unconditional.is_invalid()))
181 { 185 {
182 auto entry = mFilters.find(unconditional.second); 186 auto entry = mFilters.find(unconditional.second->GetText());
183 if (entry) 187 if (entry)
184 mUnconditionalSelectorsCache->push_back(entry->second); 188 mUnconditionalSelectorsCache->push_back(entry->second);
185 } 189 }
186 } 190 }
187 } 191 }
188 return intrusive_ptr<ElemHide_SelectorList>(mUnconditionalSelectorsCache).rele ase(); 192 return intrusive_ptr<ElemHide_SelectorList>(mUnconditionalSelectorsCache).rele ase();
189 } 193 }
190 194
191 ElemHide_SelectorList* ElemHide::GetSelectorsForDomain(const String& domain, 195 ElemHide_SelectorList* ElemHide::GetSelectorsForDomain(const String& domain,
192 Criteria criteria) const 196 Criteria criteria) const
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 if (currentDomain.empty()) 235 if (currentDomain.empty())
232 break; 236 break;
233 237
234 auto nextDot = currentDomain.find(u'.'); 238 auto nextDot = currentDomain.find(u'.');
235 currentDomain = nextDot == String::npos ? 239 currentDomain = nextDot == String::npos ?
236 u""_str : DependentString(currentDomain, nextDot + 1); 240 u""_str : DependentString(currentDomain, nextDot + 1);
237 } 241 }
238 242
239 return selectors.release(); 243 return selectors.release();
240 } 244 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld