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

Side by Side Diff: src/FilterEngine.cpp

Issue 10213003: Make JsEngine::Evaluate() return a wrapper for v8::Value to accessdifferent variable types easily (Closed)
Patch Set: Extended JsValue API to support all FilterEngine functions Created April 13, 2013, 8:42 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « shell/src/FiltersCommand.cpp ('k') | src/JsEngine.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include <algorithm> 1 #include <algorithm>
2 #include <cctype> 2 #include <cctype>
3 #include <functional> 3 #include <functional>
4 4
5 #include <AdblockPlus.h> 5 #include <AdblockPlus.h>
6 6
7 using namespace AdblockPlus; 7 using namespace AdblockPlus;
8 8
9 #if !FILTER_ENGINE_STUBS 9 #if !FILTER_ENGINE_STUBS
10 extern const char* jsSources[]; 10 extern const char* jsSources[];
11 #endif 11 #endif
12 12
13 #if FILTER_ENGINE_STUBS 13 #if FILTER_ENGINE_STUBS
14 JsObject::JsObject(FilterEngine& filterEngine) 14 JsObject::JsObject(FilterEngine& filterEngine)
15 : filterEngine(filterEngine) 15 : filterEngine(filterEngine)
16 { 16 {
17 } 17 }
18 #else 18 #else
19 JsObject::JsObject() 19 JsObject::JsObject(JsValuePtr value)
20 { 20 : JsValue(value->jsEngine, value->value)
21 {
22 if (!IsObject())
23 throw std::runtime_error("JavaScript value is not an object");
21 } 24 }
22 #endif 25 #endif
23 26
24 std::string JsObject::GetProperty(const std::string& name, const std::string& de faultValue) const 27 std::string JsObject::GetProperty(const std::string& name, const std::string& de faultValue) const
25 { 28 {
26 #if FILTER_ENGINE_STUBS 29 #if FILTER_ENGINE_STUBS
27 std::map<std::string, std::string>::const_iterator it = stringProperties.find( name); 30 std::map<std::string, std::string>::const_iterator it = stringProperties.find( name);
28 if (it == stringProperties.end()) 31 if (it == stringProperties.end())
29 return defaultValue; 32 return defaultValue;
30 else 33 else
31 return it->second; 34 return it->second;
32 #endif 35 #else
33 } 36 JsValuePtr value = JsValue::GetProperty(name);
34 37 if (value->IsString())
35 int JsObject::GetProperty(const std::string& name, int defaultValue) const 38 return value->AsString();
36 { 39 else
37 #if FILTER_ENGINE_STUBS 40 return defaultValue;
38 std::map<std::string, int>::const_iterator it = intProperties.find(name); 41 #endif
42 }
43
44 int64_t JsObject::GetProperty(const std::string& name, int64_t defaultValue) con st
45 {
46 #if FILTER_ENGINE_STUBS
47 std::map<std::string, int64_t>::const_iterator it = intProperties.find(name);
39 if (it == intProperties.end()) 48 if (it == intProperties.end())
40 return defaultValue; 49 return defaultValue;
41 else 50 else
42 return it->second; 51 return it->second;
52 #else
53 JsValuePtr value = JsValue::GetProperty(name);
54 if (value->IsNumber())
55 return value->AsInt();
56 else
57 return defaultValue;
43 #endif 58 #endif
44 } 59 }
45 60
46 bool JsObject::GetProperty(const std::string& name, bool defaultValue) const 61 bool JsObject::GetProperty(const std::string& name, bool defaultValue) const
47 { 62 {
48 #if FILTER_ENGINE_STUBS 63 #if FILTER_ENGINE_STUBS
49 std::map<std::string, bool>::const_iterator it = boolProperties.find(name); 64 std::map<std::string, bool>::const_iterator it = boolProperties.find(name);
50 if (it == boolProperties.end()) 65 if (it == boolProperties.end())
51 return defaultValue; 66 return defaultValue;
52 else 67 else
53 return it->second; 68 return it->second;
54 #endif 69 #else
55 } 70 JsValuePtr value = JsValue::GetProperty(name);
56 71 if (value->IsBool())
72 return value->AsBool();
73 else
74 return defaultValue;
75 #endif
76 }
77
78 #if FILTER_ENGINE_STUBS
57 void JsObject::SetProperty(const std::string& name, const std::string& value) 79 void JsObject::SetProperty(const std::string& name, const std::string& value)
58 { 80 {
59 #if FILTER_ENGINE_STUBS
60 stringProperties[name] = value; 81 stringProperties[name] = value;
61 #endif 82 }
62 } 83
63 84 void JsObject::SetProperty(const std::string& name, int64_t value)
64 void JsObject::SetProperty(const std::string& name, int value) 85 {
65 {
66 #if FILTER_ENGINE_STUBS
67 intProperties[name] = value; 86 intProperties[name] = value;
68 #endif
69 } 87 }
70 88
71 void JsObject::SetProperty(const std::string& name, bool value) 89 void JsObject::SetProperty(const std::string& name, bool value)
72 { 90 {
73 #if FILTER_ENGINE_STUBS
74 boolProperties[name] = value; 91 boolProperties[name] = value;
75 #endif 92 }
76 } 93 #endif
77 94
78 #if FILTER_ENGINE_STUBS 95 #if FILTER_ENGINE_STUBS
79 Filter::Filter(FilterEngine& filterEngine, const std::string& text) 96 Filter::Filter(FilterEngine& filterEngine, const std::string& text)
80 : JsObject(filterEngine) 97 : JsObject(filterEngine)
81 { 98 {
82 SetProperty("text", text); 99 SetProperty("text", text);
100
101 Type type;
83 if (text.find("!") == 0) 102 if (text.find("!") == 0)
84 SetProperty("type", TYPE_COMMENT); 103 type = TYPE_COMMENT;
85 else if (text.find("@@") == 0) 104 else if (text.find("@@") == 0)
86 SetProperty("type", TYPE_EXCEPTION); 105 type = TYPE_EXCEPTION;
87 else if (text.find("#@") != std::string::npos) 106 else if (text.find("#@") != std::string::npos)
88 SetProperty("type", TYPE_ELEMHIDE_EXCEPTION); 107 type = TYPE_ELEMHIDE_EXCEPTION;
89 else if (text.find("#") != std::string::npos) 108 else if (text.find("#") != std::string::npos)
90 SetProperty("type", TYPE_ELEMHIDE); 109 type = TYPE_ELEMHIDE;
91 else 110 else
92 SetProperty("type", TYPE_BLOCKING); 111 type = TYPE_BLOCKING;
93 } 112 SetProperty("type", (int64_t)type);
94 #else 113 }
95 Filter::Filter() 114 #else
96 { 115 Filter::Filter(JsValuePtr value)
97 } 116 : JsObject(value)
98 #endif 117 {
99 118 // Hack: set `type` property according to class name
100 bool Filter::IsListed() const 119 std::string className = GetClassName();
120 Type type;
121 if (className == "BlockingFilter")
122 type = TYPE_BLOCKING;
123 else if (className == "WhitelistFilter")
124 type = TYPE_EXCEPTION;
125 else if (className == "ElemHideFilter")
126 type = TYPE_ELEMHIDE;
127 else if (className == "ElemHideException")
128 type = TYPE_ELEMHIDE_EXCEPTION;
129 else if (className == "CommentFilter")
130 type = TYPE_COMMENT;
131 else
132 type = TYPE_INVALID;
133 SetProperty("type", (int64_t)type);
134 }
135 #endif
136
137 bool Filter::IsListed()
101 { 138 {
102 #if FILTER_ENGINE_STUBS 139 #if FILTER_ENGINE_STUBS
103 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin(); 140 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
104 it != filterEngine.listedFilters.end(); ++it) 141 it != filterEngine.listedFilters.end(); ++it)
105 { 142 {
106 if (it->get() == this) 143 if (it->get() == this)
107 return true; 144 return true;
108 } 145 }
109 return false; 146 return false;
147 #else
148 JsValuePtr func = jsEngine.Evaluate("API.isListedFilter");
149 JsValueList params;
150 params.push_back(shared_from_this());
151 return func->Call(params)->AsBool();
110 #endif 152 #endif
111 } 153 }
112 154
113 void Filter::AddToList() 155 void Filter::AddToList()
114 { 156 {
115 #if FILTER_ENGINE_STUBS 157 #if FILTER_ENGINE_STUBS
116 if (!IsListed()) 158 if (!IsListed())
117 filterEngine.listedFilters.push_back(shared_from_this()); 159 filterEngine.listedFilters.push_back(shared_from_this());
160 #else
161 JsValuePtr func = jsEngine.Evaluate("API.addFilterToList");
162 JsValueList params;
163 params.push_back(shared_from_this());
164 func->Call(params);
118 #endif 165 #endif
119 } 166 }
120 167
121 void Filter::RemoveFromList() 168 void Filter::RemoveFromList()
122 { 169 {
170 #if FILTER_ENGINE_STUBS
123 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin(); 171 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
124 it != filterEngine.listedFilters.end();) 172 it != filterEngine.listedFilters.end();)
125 { 173 {
126 if (it->get() == this) 174 if (it->get() == this)
127 it = filterEngine.listedFilters.erase(it); 175 it = filterEngine.listedFilters.erase(it);
128 else 176 else
129 it++; 177 it++;
130 } 178 }
179 #else
180 JsValuePtr func = jsEngine.Evaluate("API.removeFilterFromList");
181 JsValueList params;
182 params.push_back(shared_from_this());
183 func->Call(params);
184 #endif
185 }
186
187 bool Filter::operator==(const Filter& filter) const
188 {
189 return GetProperty("text", "") == filter.GetProperty("text", "");
131 } 190 }
132 191
133 #if FILTER_ENGINE_STUBS 192 #if FILTER_ENGINE_STUBS
134 Subscription::Subscription(FilterEngine& filterEngine, const std::string& url) 193 Subscription::Subscription(FilterEngine& filterEngine, const std::string& url)
135 : JsObject(filterEngine) 194 : JsObject(filterEngine)
136 { 195 {
137 SetProperty("url", url); 196 SetProperty("url", url);
138 } 197 }
139 #else 198 #else
140 Subscription::Subscription() 199 Subscription::Subscription(JsValuePtr value)
141 { 200 : JsObject(value)
142 } 201 {
143 #endif 202 }
144 203 #endif
145 bool Subscription::IsListed() const 204
205 bool Subscription::IsListed()
146 { 206 {
147 #if FILTER_ENGINE_STUBS 207 #if FILTER_ENGINE_STUBS
148 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin(); 208 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin();
149 it != filterEngine.listedSubscriptions.end(); ++it) 209 it != filterEngine.listedSubscriptions.end(); ++it)
150 { 210 {
151 if (it->get() == this) 211 if (it->get() == this)
152 return true; 212 return true;
153 } 213 }
154 return false; 214 return false;
215 #else
216 JsValuePtr func = jsEngine.Evaluate("API.isListedFilter");
217 JsValueList params;
218 params.push_back(shared_from_this());
219 return func->Call(params)->AsBool();
155 #endif 220 #endif
156 } 221 }
157 222
158 void Subscription::AddToList() 223 void Subscription::AddToList()
159 { 224 {
160 #if FILTER_ENGINE_STUBS 225 #if FILTER_ENGINE_STUBS
161 if (!IsListed()) 226 if (!IsListed())
162 filterEngine.listedSubscriptions.push_back(shared_from_this()); 227 filterEngine.listedSubscriptions.push_back(shared_from_this());
228 #else
229 JsValuePtr func = jsEngine.Evaluate("API.addSubscriptionToList");
230 JsValueList params;
231 params.push_back(shared_from_this());
232 func->Call(params);
163 #endif 233 #endif
164 } 234 }
165 235
166 void Subscription::RemoveFromList() 236 void Subscription::RemoveFromList()
167 { 237 {
238 #if FILTER_ENGINE_STUBS
168 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin(); 239 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin();
169 it != filterEngine.listedSubscriptions.end();) 240 it != filterEngine.listedSubscriptions.end();)
170 { 241 {
171 if (it->get() == this) 242 if (it->get() == this)
172 it = filterEngine.listedSubscriptions.erase(it); 243 it = filterEngine.listedSubscriptions.erase(it);
173 else 244 else
174 it++; 245 it++;
175 } 246 }
247 #else
248 JsValuePtr func = jsEngine.Evaluate("API.removeSubscriptionFromList");
249 JsValueList params;
250 params.push_back(shared_from_this());
251 func->Call(params);
252 #endif
176 } 253 }
177 254
178 void Subscription::UpdateFilters() 255 void Subscription::UpdateFilters()
179 { 256 {
180 } 257 }
181 258
259 bool Subscription::operator==(const Subscription& subscription) const
260 {
261 return GetProperty("url", "") == subscription.GetProperty("url", "");
262 }
263
182 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) 264 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine)
183 { 265 {
184 #if !FILTER_ENGINE_STUBS 266 #if !FILTER_ENGINE_STUBS
185 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) 267 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2)
186 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); 268 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]);
187 #endif 269 #endif
188 } 270 }
189 271
190 FilterPtr FilterEngine::GetFilter(const std::string& text) 272 FilterPtr FilterEngine::GetFilter(const std::string& text)
191 { 273 {
192 #if FILTER_ENGINE_STUBS 274 #if FILTER_ENGINE_STUBS
193 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring 275 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
194 std::string trimmed(text); 276 std::string trimmed(text);
195 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace)))); 277 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace))));
196 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end()); 278 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end());
197 279
198 std::map<std::string, FilterPtr>::const_iterator it = knownFilters.find(trimme d); 280 std::map<std::string, FilterPtr>::const_iterator it = knownFilters.find(trimme d);
199 if (it != knownFilters.end()) 281 if (it != knownFilters.end())
200 return it->second; 282 return it->second;
201 283
202 FilterPtr result(new Filter(*this, trimmed)); 284 FilterPtr result(new Filter(*this, trimmed));
203 knownFilters[trimmed] = result; 285 knownFilters[trimmed] = result;
204 return result; 286 return result;
287 #else
288 JsValuePtr func = jsEngine.Evaluate("API.getFilterFromText");
289 JsValueList params;
290 params.push_back(jsEngine.NewValue(text));
291 return FilterPtr(new Filter(func->Call(params)));
205 #endif 292 #endif
206 } 293 }
207 294
208 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) 295 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url)
209 { 296 {
210 #if FILTER_ENGINE_STUBS 297 #if FILTER_ENGINE_STUBS
211 std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions .find(url); 298 std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions .find(url);
212 if (it != knownSubscriptions.end()) 299 if (it != knownSubscriptions.end())
213 return it->second; 300 return it->second;
214 301
215 SubscriptionPtr result(new Subscription(*this, url)); 302 SubscriptionPtr result(new Subscription(*this, url));
216 knownSubscriptions[url] = result; 303 knownSubscriptions[url] = result;
217 return result; 304 return result;
305 #else
306 JsValuePtr func = jsEngine.Evaluate("API.getSubscriptionFromUrl");
307 JsValueList params;
308 params.push_back(jsEngine.NewValue(url));
309 return SubscriptionPtr(new Subscription(func->Call(params)));
218 #endif 310 #endif
219 } 311 }
220 312
221 const std::vector<FilterPtr>& FilterEngine::GetListedFilters() const 313 const std::vector<FilterPtr> FilterEngine::GetListedFilters() const
222 { 314 {
223 #if FILTER_ENGINE_STUBS 315 #if FILTER_ENGINE_STUBS
224 return listedFilters; 316 return listedFilters;
317 #else
318 JsValuePtr func = jsEngine.Evaluate("API.getListedFilters");
319 JsValueList values = func->Call()->AsList();
320 std::vector<FilterPtr> result;
321 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
322 result.push_back(FilterPtr(new Filter(*it)));
323 return result;
225 #endif 324 #endif
226 } 325 }
227 326
228 const std::vector<SubscriptionPtr>& FilterEngine::GetListedSubscriptions() const 327 const std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const
229 { 328 {
230 #if FILTER_ENGINE_STUBS 329 #if FILTER_ENGINE_STUBS
231 return listedSubscriptions; 330 return listedSubscriptions;
331 #else
332 JsValuePtr func = jsEngine.Evaluate("API.getListedSubscriptions");
333 JsValueList values = func->Call()->AsList();
334 std::vector<SubscriptionPtr> result;
335 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
336 result.push_back(SubscriptionPtr(new Subscription(*it)));
337 return result;
232 #endif 338 #endif
233 } 339 }
234 340
235 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback) 341 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback)
236 { 342 {
237 #if FILTER_ENGINE_STUBS 343 #if FILTER_ENGINE_STUBS
238 std::vector<SubscriptionPtr> availableSubscriptions; 344 std::vector<SubscriptionPtr> availableSubscriptions;
239 345
240 SubscriptionPtr subscription1 = GetSubscription("https://easylist-downloads.ad blockplus.org/easylist.txt"); 346 SubscriptionPtr subscription1 = GetSubscription("https://easylist-downloads.ad blockplus.org/easylist.txt");
241 subscription1->SetProperty("title", "EasyList"); 347 subscription1->SetProperty("title", "EasyList");
242 availableSubscriptions.push_back(subscription1); 348 availableSubscriptions.push_back(subscription1);
243 349
244 SubscriptionPtr subscription2 = GetSubscription("https://easylist-downloads.ad blockplus.org/easylistgermany+easylist.txt"); 350 SubscriptionPtr subscription2 = GetSubscription("https://easylist-downloads.ad blockplus.org/easylistgermany+easylist.txt");
245 subscription2->SetProperty("title", "EasyList Germany+EasyList"); 351 subscription2->SetProperty("title", "EasyList Germany+EasyList");
246 availableSubscriptions.push_back(subscription2); 352 availableSubscriptions.push_back(subscription2);
247 353
248 callback(availableSubscriptions); 354 callback(availableSubscriptions);
355 #else
356 // TODO!
249 #endif 357 #endif
250 } 358 }
251 359
252 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, 360 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
253 const std::string& contentType, 361 const std::string& contentType,
254 const std::string& documentUrl) 362 const std::string& documentUrl)
255 { 363 {
256 #if FILTER_ENGINE_STUBS 364 #if FILTER_ENGINE_STUBS
257 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ 365 //For test on http://simple-adblock.com/faq/testing-your-adblocker/
258 if (url.find("adbanner.gif") != std::string::npos) 366 if (url.find("adbanner.gif") != std::string::npos)
259 return GetFilter("adbanner.gif"); 367 return GetFilter("adbanner.gif");
260 else if (url.find("notbanner.gif") != std::string::npos) 368 else if (url.find("notbanner.gif") != std::string::npos)
261 return GetFilter("@@notbanner.gif"); 369 return GetFilter("@@notbanner.gif");
262 else 370 else
263 return AdblockPlus::FilterPtr(); 371 return AdblockPlus::FilterPtr();
372 #else
373 JsValuePtr func = jsEngine.Evaluate("API.checkFilterMatch");
374 JsValueList params;
375 params.push_back(jsEngine.NewValue(url));
376 params.push_back(jsEngine.NewValue(contentType));
377 params.push_back(jsEngine.NewValue(documentUrl));
378 JsValuePtr result = func->Call(params);
379 if (!result->IsNull())
380 return FilterPtr(new Filter(result));
381 else
382 return FilterPtr();
264 #endif 383 #endif
265 } 384 }
266 385
267 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const 386 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const
268 { 387 {
269 #if FILTER_ENGINE_STUBS 388 #if FILTER_ENGINE_STUBS
270 std::vector<std::string> selectors; 389 std::vector<std::string> selectors;
271 selectors.push_back("#ad"); 390 selectors.push_back("#ad");
272 selectors.push_back(".ad"); 391 selectors.push_back(".ad");
273 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ 392 //For test on http://simple-adblock.com/faq/testing-your-adblocker/
274 if (domain == "simple-adblock.com") 393 if (domain == "simple-adblock.com")
275 selectors.push_back(".ad_300x250"); 394 selectors.push_back(".ad_300x250");
276 return selectors; 395 return selectors;
396 #else
397 JsValuePtr func = jsEngine.Evaluate("API.getElementHidingSelectors");
398 JsValueList params;
399 params.push_back(jsEngine.NewValue(domain));
400 JsValueList result = func->Call(params)->AsList();
401 std::vector<std::string> selectors;
402 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it)
403 selectors.push_back((*it)->AsString());
404 return selectors;
277 #endif 405 #endif
278 } 406 }
OLDNEW
« no previous file with comments | « shell/src/FiltersCommand.cpp ('k') | src/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld