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: Addressed review comments Created April 17, 2013, 7:56 a.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 | « libadblockplus.gyp ('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", 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", 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 {
257 #if !FILTER_ENGINE_STUBS
258 JsValuePtr func = jsEngine.Evaluate("API.updateSubscription");
259 JsValueList params;
260 params.push_back(shared_from_this());
261 func->Call(params);
262 #endif
263 }
264
265 bool Subscription::IsUpdating()
266 {
267 #if FILTER_ENGINE_STUBS
268 return false;
269 #else
270 JsValuePtr func = jsEngine.Evaluate("API.isSubscriptionUpdating");
271 JsValueList params;
272 params.push_back(shared_from_this());
273 JsValuePtr result = func->Call(params);
274 return result->AsBool();
275 #endif
276 }
277
278 bool Subscription::operator==(const Subscription& subscription) const
279 {
280 return GetProperty("url", "") == subscription.GetProperty("url", "");
180 } 281 }
181 282
182 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) 283 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine)
183 { 284 {
184 #if !FILTER_ENGINE_STUBS 285 #if !FILTER_ENGINE_STUBS
185 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) 286 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2)
186 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); 287 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]);
187 #endif 288 #endif
188 } 289 }
189 290
190 FilterPtr FilterEngine::GetFilter(const std::string& text) 291 FilterPtr FilterEngine::GetFilter(const std::string& text)
191 { 292 {
192 #if FILTER_ENGINE_STUBS 293 #if FILTER_ENGINE_STUBS
193 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring 294 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
194 std::string trimmed(text); 295 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)))); 296 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()); 297 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end());
197 298
198 std::map<std::string, FilterPtr>::const_iterator it = knownFilters.find(trimme d); 299 std::map<std::string, FilterPtr>::const_iterator it = knownFilters.find(trimme d);
199 if (it != knownFilters.end()) 300 if (it != knownFilters.end())
200 return it->second; 301 return it->second;
201 302
202 FilterPtr result(new Filter(*this, trimmed)); 303 FilterPtr result(new Filter(*this, trimmed));
203 knownFilters[trimmed] = result; 304 knownFilters[trimmed] = result;
204 return result; 305 return result;
306 #else
307 JsValuePtr func = jsEngine.Evaluate("API.getFilterFromText");
308 JsValueList params;
309 params.push_back(jsEngine.NewValue(text));
310 return FilterPtr(new Filter(func->Call(params)));
205 #endif 311 #endif
206 } 312 }
207 313
208 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) 314 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url)
209 { 315 {
210 #if FILTER_ENGINE_STUBS 316 #if FILTER_ENGINE_STUBS
211 std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions .find(url); 317 std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions .find(url);
212 if (it != knownSubscriptions.end()) 318 if (it != knownSubscriptions.end())
213 return it->second; 319 return it->second;
214 320
215 SubscriptionPtr result(new Subscription(*this, url)); 321 SubscriptionPtr result(new Subscription(*this, url));
216 knownSubscriptions[url] = result; 322 knownSubscriptions[url] = result;
217 return result; 323 return result;
324 #else
325 JsValuePtr func = jsEngine.Evaluate("API.getSubscriptionFromUrl");
326 JsValueList params;
327 params.push_back(jsEngine.NewValue(url));
328 return SubscriptionPtr(new Subscription(func->Call(params)));
218 #endif 329 #endif
219 } 330 }
220 331
221 const std::vector<FilterPtr>& FilterEngine::GetListedFilters() const 332 const std::vector<FilterPtr> FilterEngine::GetListedFilters() const
222 { 333 {
223 #if FILTER_ENGINE_STUBS 334 #if FILTER_ENGINE_STUBS
224 return listedFilters; 335 return listedFilters;
336 #else
337 JsValuePtr func = jsEngine.Evaluate("API.getListedFilters");
338 JsValueList values = func->Call()->AsList();
339 std::vector<FilterPtr> result;
340 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
341 result.push_back(FilterPtr(new Filter(*it)));
342 return result;
225 #endif 343 #endif
226 } 344 }
227 345
228 const std::vector<SubscriptionPtr>& FilterEngine::GetListedSubscriptions() const 346 const std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const
229 { 347 {
230 #if FILTER_ENGINE_STUBS 348 #if FILTER_ENGINE_STUBS
231 return listedSubscriptions; 349 return listedSubscriptions;
350 #else
351 JsValuePtr func = jsEngine.Evaluate("API.getListedSubscriptions");
352 JsValueList values = func->Call()->AsList();
353 std::vector<SubscriptionPtr> result;
354 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
355 result.push_back(SubscriptionPtr(new Subscription(*it)));
356 return result;
232 #endif 357 #endif
233 } 358 }
234 359
235 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback) 360 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback)
236 { 361 {
237 #if FILTER_ENGINE_STUBS 362 #if FILTER_ENGINE_STUBS
238 std::vector<SubscriptionPtr> availableSubscriptions; 363 std::vector<SubscriptionPtr> availableSubscriptions;
239 364
240 SubscriptionPtr subscription1 = GetSubscription("https://easylist-downloads.ad blockplus.org/easylist.txt"); 365 SubscriptionPtr subscription1 = GetSubscription("https://easylist-downloads.ad blockplus.org/easylist.txt");
241 subscription1->SetProperty("title", "EasyList"); 366 subscription1->SetProperty("title", "EasyList");
242 availableSubscriptions.push_back(subscription1); 367 availableSubscriptions.push_back(subscription1);
243 368
244 SubscriptionPtr subscription2 = GetSubscription("https://easylist-downloads.ad blockplus.org/easylistgermany+easylist.txt"); 369 SubscriptionPtr subscription2 = GetSubscription("https://easylist-downloads.ad blockplus.org/easylistgermany+easylist.txt");
245 subscription2->SetProperty("title", "EasyList Germany+EasyList"); 370 subscription2->SetProperty("title", "EasyList Germany+EasyList");
246 availableSubscriptions.push_back(subscription2); 371 availableSubscriptions.push_back(subscription2);
247 372
248 callback(availableSubscriptions); 373 callback(availableSubscriptions);
374 #else
375 // TODO!
249 #endif 376 #endif
250 } 377 }
251 378
252 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, 379 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
253 const std::string& contentType, 380 const std::string& contentType,
254 const std::string& documentUrl) 381 const std::string& documentUrl)
255 { 382 {
256 #if FILTER_ENGINE_STUBS 383 #if FILTER_ENGINE_STUBS
257 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ 384 //For test on http://simple-adblock.com/faq/testing-your-adblocker/
258 if (url.find("adbanner.gif") != std::string::npos) 385 if (url.find("adbanner.gif") != std::string::npos)
259 return GetFilter("adbanner.gif"); 386 return GetFilter("adbanner.gif");
260 else if (url.find("notbanner.gif") != std::string::npos) 387 else if (url.find("notbanner.gif") != std::string::npos)
261 return GetFilter("@@notbanner.gif"); 388 return GetFilter("@@notbanner.gif");
262 else 389 else
263 return AdblockPlus::FilterPtr(); 390 return AdblockPlus::FilterPtr();
391 #else
392 JsValuePtr func = jsEngine.Evaluate("API.checkFilterMatch");
393 JsValueList params;
394 params.push_back(jsEngine.NewValue(url));
395 params.push_back(jsEngine.NewValue(contentType));
396 params.push_back(jsEngine.NewValue(documentUrl));
397 JsValuePtr result = func->Call(params);
398 if (!result->IsNull())
399 return FilterPtr(new Filter(result));
400 else
401 return FilterPtr();
264 #endif 402 #endif
265 } 403 }
266 404
267 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const 405 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const
268 { 406 {
269 #if FILTER_ENGINE_STUBS 407 #if FILTER_ENGINE_STUBS
270 std::vector<std::string> selectors; 408 std::vector<std::string> selectors;
271 selectors.push_back("#ad"); 409 selectors.push_back("#ad");
272 selectors.push_back(".ad"); 410 selectors.push_back(".ad");
273 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ 411 //For test on http://simple-adblock.com/faq/testing-your-adblocker/
274 if (domain == "simple-adblock.com") 412 if (domain == "simple-adblock.com")
275 selectors.push_back(".ad_300x250"); 413 selectors.push_back(".ad_300x250");
276 return selectors; 414 return selectors;
415 #else
416 JsValuePtr func = jsEngine.Evaluate("API.getElementHidingSelectors");
417 JsValueList params;
418 params.push_back(jsEngine.NewValue(domain));
419 JsValueList result = func->Call(params)->AsList();
420 std::vector<std::string> selectors;
421 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it)
422 selectors.push_back((*it)->AsString());
423 return selectors;
277 #endif 424 #endif
278 } 425 }
OLDNEW
« no previous file with comments | « libadblockplus.gyp ('k') | src/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld