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

Powered by Google App Engine
This is Rietveld