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

Delta Between Two Patch Sets: src/FilterEngine.cpp

Issue 10100009: FilterEngine API improvements (Closed)
Left Patch Set: Added filter access and addressed review comments Created April 5, 2013, 12:10 p.m.
Right Patch Set: Changed filter type enum Created April 9, 2013, 5:55 a.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 | « shell/src/FiltersCommand.cpp ('k') | test/FilterEngineStubs.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 #include <algorithm> 1 #include <algorithm>
2 #include <AdblockPlus.h> 2 #include <AdblockPlus.h>
3 3
4 using namespace AdblockPlus; 4 using namespace AdblockPlus;
5 5
6 #if !FILTER_ENGINE_STUBS 6 #if !FILTER_ENGINE_STUBS
7 extern const char* jsSources[]; 7 extern const char* jsSources[];
8 #endif 8 #endif
9 9
10 #if FILTER_ENGINE_STUBS 10 #if FILTER_ENGINE_STUBS
11 JSObject::JSObject(FilterEngine& filterEngine) 11 JsObject::JsObject(FilterEngine& filterEngine)
12 : filterEngine(filterEngine) 12 : filterEngine(filterEngine)
13 { 13 {
14 } 14 }
15 #else 15 #else
16 JSObject::JSObject() 16 JsObject::JsObject()
17 { 17 {
18 } 18 }
19 #endif 19 #endif
20 20
21 std::string JSObject::GetProperty(const std::string& name, const std::string& de faultValue) const 21 std::string JsObject::GetProperty(const std::string& name, const std::string& de faultValue) const
22 { 22 {
23 #if FILTER_ENGINE_STUBS 23 #if FILTER_ENGINE_STUBS
24 std::map<std::string, std::string>::const_iterator it = stringProperties.find( name); 24 std::map<std::string, std::string>::const_iterator it = stringProperties.find( name);
25 if (it == stringProperties.end()) 25 if (it == stringProperties.end())
26 return defaultValue; 26 return defaultValue;
27 else 27 else
28 return it->second; 28 return it->second;
29 #endif 29 #endif
30 } 30 }
31 31
32 int JSObject::GetProperty(const std::string& name, int defaultValue) const 32 int JsObject::GetProperty(const std::string& name, int defaultValue) const
33 { 33 {
34 #if FILTER_ENGINE_STUBS 34 #if FILTER_ENGINE_STUBS
35 std::map<std::string, int>::const_iterator it = intProperties.find(name); 35 std::map<std::string, int>::const_iterator it = intProperties.find(name);
36 if (it == intProperties.end()) 36 if (it == intProperties.end())
37 return defaultValue; 37 return defaultValue;
38 else 38 else
39 return it->second; 39 return it->second;
40 #endif 40 #endif
41 } 41 }
42 42
43 bool JSObject::GetProperty(const std::string& name, bool defaultValue) const 43 bool JsObject::GetProperty(const std::string& name, bool defaultValue) const
44 { 44 {
45 #if FILTER_ENGINE_STUBS 45 #if FILTER_ENGINE_STUBS
46 std::map<std::string, bool>::const_iterator it = boolProperties.find(name); 46 std::map<std::string, bool>::const_iterator it = boolProperties.find(name);
47 if (it == boolProperties.end()) 47 if (it == boolProperties.end())
48 return defaultValue; 48 return defaultValue;
49 else 49 else
50 return it->second; 50 return it->second;
51 #endif 51 #endif
52 } 52 }
53 53
54 void JSObject::SetProperty(const std::string& name, const std::string& value) 54 void JsObject::SetProperty(const std::string& name, const std::string& value)
55 { 55 {
56 #if FILTER_ENGINE_STUBS 56 #if FILTER_ENGINE_STUBS
57 stringProperties[name] = value; 57 stringProperties[name] = value;
58 #endif 58 #endif
59 } 59 }
60 60
61 void JSObject::SetProperty(const std::string& name, int value) 61 void JsObject::SetProperty(const std::string& name, int value)
62 { 62 {
63 #if FILTER_ENGINE_STUBS 63 #if FILTER_ENGINE_STUBS
64 intProperties[name] = value; 64 intProperties[name] = value;
65 #endif 65 #endif
66 } 66 }
67 67
68 void JSObject::SetProperty(const std::string& name, bool value) 68 void JsObject::SetProperty(const std::string& name, bool value)
69 { 69 {
70 #if FILTER_ENGINE_STUBS 70 #if FILTER_ENGINE_STUBS
71 boolProperties[name] = value; 71 boolProperties[name] = value;
72 #endif 72 #endif
73 } 73 }
74 74
75 #if FILTER_ENGINE_STUBS 75 #if FILTER_ENGINE_STUBS
76 Filter::Filter(FilterEngine& filterEngine, const std::string& text) 76 Filter::Filter(FilterEngine& filterEngine, const std::string& text)
77 : JSObject(filterEngine) 77 : JsObject(filterEngine)
78 { 78 {
79 SetProperty("text", text); 79 SetProperty("text", text);
80 if (text.find("!") == 0) 80 if (text.find("!") == 0)
81 SetProperty("type", "comment"); 81 SetProperty("type", TYPE_COMMENT);
82 else if (text.find("@@") == 0) 82 else if (text.find("@@") == 0)
83 SetProperty("type", "exception"); 83 SetProperty("type", TYPE_EXCEPTION);
84 else if (text.find("#@") != std::string::npos) 84 else if (text.find("#@") != std::string::npos)
85 SetProperty("type", "elemhideexception"); 85 SetProperty("type", TYPE_ELEMHIDE_EXCEPTION);
86 else if (text.find("#") != std::string::npos) 86 else if (text.find("#") != std::string::npos)
87 SetProperty("type", "elemhide"); 87 SetProperty("type", TYPE_ELEMHIDE);
88 else 88 else
89 SetProperty("type", "blocking"); 89 SetProperty("type", TYPE_BLOCKING);
90 } 90 }
91 #else 91 #else
92 Filter::Filter() 92 Filter::Filter()
93 { 93 {
94 } 94 }
95 #endif 95 #endif
96 96
97 bool Filter::IsListed() const 97 bool Filter::IsListed() const
98 { 98 {
99 #if FILTER_ENGINE_STUBS 99 #if FILTER_ENGINE_STUBS
100 for (std::vector<Filter*>::iterator it = filterEngine.listedFilters.begin(); 100 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
101 it != filterEngine.listedFilters.end(); ++it) 101 it != filterEngine.listedFilters.end(); ++it)
102 { 102 {
103 if (*it == this) 103 if (it->get() == this)
104 return true; 104 return true;
105 } 105 }
106 return false; 106 return false;
107 #endif 107 #endif
108 } 108 }
109 109
110 void Filter::AddToList() 110 void Filter::AddToList()
111 { 111 {
112 #if FILTER_ENGINE_STUBS 112 #if FILTER_ENGINE_STUBS
113 if (!IsListed()) 113 if (!IsListed())
114 filterEngine.listedFilters.push_back(this); 114 filterEngine.listedFilters.push_back(shared_from_this());
115 #endif 115 #endif
116 } 116 }
117 117
118 void Filter::RemoveFromList() 118 void Filter::RemoveFromList()
119 { 119 {
120 for (std::vector<Filter*>::iterator it = filterEngine.listedFilters.begin(); 120 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
121 it != filterEngine.listedFilters.end();) 121 it != filterEngine.listedFilters.end();)
122 { 122 {
123 if (*it == this) 123 if (it->get() == this)
124 it = filterEngine.listedFilters.erase(it); 124 it = filterEngine.listedFilters.erase(it);
125 else 125 else
126 it++; 126 it++;
127 } 127 }
128 } 128 }
129 129
130 #if FILTER_ENGINE_STUBS 130 #if FILTER_ENGINE_STUBS
131 Subscription::Subscription(FilterEngine& filterEngine, const std::string& url) 131 Subscription::Subscription(FilterEngine& filterEngine, const std::string& url)
132 : JSObject(filterEngine) 132 : JsObject(filterEngine)
133 { 133 {
134 SetProperty("url", url); 134 SetProperty("url", url);
135 } 135 }
136 #else 136 #else
137 Subscription::Subscription() 137 Subscription::Subscription()
138 { 138 {
139 } 139 }
140 #endif 140 #endif
141 141
142 bool Subscription::IsListed() const 142 bool Subscription::IsListed() const
143 { 143 {
144 #if FILTER_ENGINE_STUBS 144 #if FILTER_ENGINE_STUBS
145 for (std::vector<Subscription*>::iterator it = filterEngine.listedSubscription s.begin(); 145 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin();
146 it != filterEngine.listedSubscriptions.end(); ++it) 146 it != filterEngine.listedSubscriptions.end(); ++it)
147 { 147 {
148 if (*it == this) 148 if (it->get() == this)
149 return true; 149 return true;
150 } 150 }
151 return false; 151 return false;
152 #endif 152 #endif
153 } 153 }
154 154
155 void Subscription::AddToList() 155 void Subscription::AddToList()
156 { 156 {
157 #if FILTER_ENGINE_STUBS 157 #if FILTER_ENGINE_STUBS
158 if (!IsListed()) 158 if (!IsListed())
159 filterEngine.listedSubscriptions.push_back(this); 159 filterEngine.listedSubscriptions.push_back(shared_from_this());
160 #endif 160 #endif
161 } 161 }
162 162
163 void Subscription::RemoveFromList() 163 void Subscription::RemoveFromList()
164 { 164 {
165 for (std::vector<Subscription*>::iterator it = filterEngine.listedSubscription s.begin(); 165 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin();
166 it != filterEngine.listedSubscriptions.end();) 166 it != filterEngine.listedSubscriptions.end();)
167 { 167 {
168 if (*it == this) 168 if (it->get() == this)
169 it = filterEngine.listedSubscriptions.erase(it); 169 it = filterEngine.listedSubscriptions.erase(it);
170 else 170 else
171 it++; 171 it++;
172 } 172 }
173 } 173 }
174 174
175 void Subscription::UpdateFilters() 175 void Subscription::UpdateFilters()
176 { 176 {
177 } 177 }
178 178
179 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) 179 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine)
180 { 180 {
181 #if !FILTER_ENGINE_STUBS 181 #if !FILTER_ENGINE_STUBS
182 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) 182 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2)
183 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); 183 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]);
184 #endif 184 #endif
185 } 185 }
186 186
187 Filter& FilterEngine::GetFilter(const std::string& text) 187 Filter& FilterEngine::GetFilter(const std::string& text)
188 { 188 {
189 #if FILTER_ENGINE_STUBS 189 #if FILTER_ENGINE_STUBS
190 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring 190 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
191 std::string trimmed(text); 191 std::string trimmed(text);
192 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace)))); 192 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace))));
193 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end()); 193 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end());
194 194
195 std::map<std::string, Filter*>::const_iterator it = knownFilters.find(trimmed) ; 195 std::map<std::string, FilterPtr>::const_iterator it = knownFilters.find(trimme d);
196 if (it != knownFilters.end()) 196 if (it != knownFilters.end())
197 return *it->second; 197 return *it->second;
198 198
199 Filter* result = new Filter(*this, trimmed); 199 FilterPtr result(new Filter(*this, trimmed));
200 knownFilters[trimmed] = result; 200 knownFilters[trimmed] = result->shared_from_this();
201 return *result; 201 return *result;
202 #endif 202 #endif
203 } 203 }
204 204
205 Subscription& FilterEngine::GetSubscription(const std::string& url) 205 Subscription& FilterEngine::GetSubscription(const std::string& url)
206 { 206 {
207 #if FILTER_ENGINE_STUBS 207 #if FILTER_ENGINE_STUBS
208 std::map<std::string, Subscription*>::const_iterator it = knownSubscriptions.f ind(url); 208 std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions .find(url);
209 if (it != knownSubscriptions.end()) 209 if (it != knownSubscriptions.end())
210 return *it->second; 210 return *it->second;
211 211
212 Subscription* result = new Subscription(*this, url); 212 SubscriptionPtr result(new Subscription(*this, url));
213 knownSubscriptions[url] = result; 213 knownSubscriptions[url] = result->shared_from_this();
214 return *result; 214 return *result;
215 #endif 215 #endif
216 } 216 }
217 217
218 const std::vector<Filter*>& FilterEngine::GetListedFilters() const 218 const std::vector<FilterPtr>& FilterEngine::GetListedFilters() const
219 { 219 {
220 #if FILTER_ENGINE_STUBS 220 #if FILTER_ENGINE_STUBS
221 return listedFilters; 221 return listedFilters;
222 #endif 222 #endif
223 } 223 }
224 224
225 const std::vector<Subscription*>& FilterEngine::GetListedSubscriptions() const 225 const std::vector<SubscriptionPtr>& FilterEngine::GetListedSubscriptions() const
226 { 226 {
227 #if FILTER_ENGINE_STUBS 227 #if FILTER_ENGINE_STUBS
228 return listedSubscriptions; 228 return listedSubscriptions;
229 #endif 229 #endif
230 } 230 }
231 231
232 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback) 232 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback)
233 { 233 {
234 #if FILTER_ENGINE_STUBS 234 #if FILTER_ENGINE_STUBS
235 std::vector<Subscription*> availableSubscriptions; 235 std::vector<SubscriptionPtr> availableSubscriptions;
236 236
237 Subscription& subscription1 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylist.txt"); 237 Subscription& subscription1 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylist.txt");
238 subscription1.SetProperty("title", "EasyList"); 238 subscription1.SetProperty("title", "EasyList");
239 availableSubscriptions.push_back(&subscription1); 239 availableSubscriptions.push_back(subscription1.shared_from_this());
240 240
241 Subscription& subscription2 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylistgermany+easylist.txt"); 241 Subscription& subscription2 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylistgermany+easylist.txt");
242 subscription2.SetProperty("title", "EasyList Germany+EasyList"); 242 subscription2.SetProperty("title", "EasyList Germany+EasyList");
243 availableSubscriptions.push_back(&subscription2); 243 availableSubscriptions.push_back(subscription2.shared_from_this());
244 244
245 callback(availableSubscriptions); 245 callback(availableSubscriptions);
246 #endif 246 #endif
247 } 247 }
248 248
249 Filter* FilterEngine::Matches(const std::string& url, 249 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
250 const std::string& contentType, 250 const std::string& contentType,
251 const std::string& documentUrl) 251 const std::string& documentUrl)
252 { 252 {
253 #if FILTER_ENGINE_STUBS 253 #if FILTER_ENGINE_STUBS
254 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ 254 //For test on http://simple-adblock.com/faq/testing-your-adblocker/
255 if (url.find("adbanner.gif") != std::string::npos) 255 if (url.find("adbanner.gif") != std::string::npos)
256 return &GetFilter("adbanner.gif"); 256 return GetFilter("adbanner.gif").shared_from_this();
257 else if (url.find("notbanner.gif") != std::string::npos) 257 else if (url.find("notbanner.gif") != std::string::npos)
258 return &GetFilter("@@notbanner.gif"); 258 return GetFilter("@@notbanner.gif").shared_from_this();
259 else 259 else
260 return 0; 260 return AdblockPlus::FilterPtr();
261 #endif 261 #endif
262 } 262 }
263 263
264 std::vector<std::string> FilterEngine::GetElementHidingRules() const 264 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const
265 { 265 {
266 #if FILTER_ENGINE_STUBS 266 #if FILTER_ENGINE_STUBS
267 std::vector<std::string> hidingRules; 267 std::vector<std::string> selectors;
268 hidingRules.push_back("###ad"); 268 selectors.push_back("#ad");
269 hidingRules.push_back("##.ad"); 269 selectors.push_back(".ad");
270 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ 270 //For test on http://simple-adblock.com/faq/testing-your-adblocker/
271 hidingRules.push_back("##.ad_300x250"); 271 if (domain == "simple-adblock.com")
272 return hidingRules; 272 selectors.push_back(".ad_300x250");
273 #endif 273 return selectors;
274 } 274 #endif
275 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld