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

Side by Side Diff: src/FilterEngine.cpp

Issue 10173031: Don`t use references to JsEngine to avoid use-after-free errors,switch to shared_ptr instead (Closed)
Patch Set: Created April 18, 2013, 4:15 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
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[];
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 { 138 {
139 #if FILTER_ENGINE_STUBS 139 #if FILTER_ENGINE_STUBS
140 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin(); 140 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
141 it != filterEngine.listedFilters.end(); ++it) 141 it != filterEngine.listedFilters.end(); ++it)
142 { 142 {
143 if (it->get() == this) 143 if (it->get() == this)
144 return true; 144 return true;
145 } 145 }
146 return false; 146 return false;
147 #else 147 #else
148 JsValuePtr func = jsEngine.Evaluate("API.isListedFilter"); 148 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter");
149 JsValueList params; 149 JsValueList params;
150 params.push_back(shared_from_this()); 150 params.push_back(shared_from_this());
151 return func->Call(params)->AsBool(); 151 return func->Call(params)->AsBool();
152 #endif 152 #endif
153 } 153 }
154 154
155 void Filter::AddToList() 155 void Filter::AddToList()
156 { 156 {
157 #if FILTER_ENGINE_STUBS 157 #if FILTER_ENGINE_STUBS
158 if (!IsListed()) 158 if (!IsListed())
159 filterEngine.listedFilters.push_back(shared_from_this()); 159 filterEngine.listedFilters.push_back(shared_from_this());
160 #else 160 #else
161 JsValuePtr func = jsEngine.Evaluate("API.addFilterToList"); 161 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList");
162 JsValueList params; 162 JsValueList params;
163 params.push_back(shared_from_this()); 163 params.push_back(shared_from_this());
164 func->Call(params); 164 func->Call(params);
165 #endif 165 #endif
166 } 166 }
167 167
168 void Filter::RemoveFromList() 168 void Filter::RemoveFromList()
169 { 169 {
170 #if FILTER_ENGINE_STUBS 170 #if FILTER_ENGINE_STUBS
171 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin(); 171 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
172 it != filterEngine.listedFilters.end();) 172 it != filterEngine.listedFilters.end();)
173 { 173 {
174 if (it->get() == this) 174 if (it->get() == this)
175 it = filterEngine.listedFilters.erase(it); 175 it = filterEngine.listedFilters.erase(it);
176 else 176 else
177 it++; 177 it++;
178 } 178 }
179 #else 179 #else
180 JsValuePtr func = jsEngine.Evaluate("API.removeFilterFromList"); 180 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList");
181 JsValueList params; 181 JsValueList params;
182 params.push_back(shared_from_this()); 182 params.push_back(shared_from_this());
183 func->Call(params); 183 func->Call(params);
184 #endif 184 #endif
185 } 185 }
186 186
187 bool Filter::operator==(const Filter& filter) const 187 bool Filter::operator==(const Filter& filter) const
188 { 188 {
189 return GetProperty("text", "") == filter.GetProperty("text", ""); 189 return GetProperty("text", "") == filter.GetProperty("text", "");
190 } 190 }
(...skipping 15 matching lines...) Expand all
206 { 206 {
207 #if FILTER_ENGINE_STUBS 207 #if FILTER_ENGINE_STUBS
208 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin(); 208 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin();
209 it != filterEngine.listedSubscriptions.end(); ++it) 209 it != filterEngine.listedSubscriptions.end(); ++it)
210 { 210 {
211 if (it->get() == this) 211 if (it->get() == this)
212 return true; 212 return true;
213 } 213 }
214 return false; 214 return false;
215 #else 215 #else
216 JsValuePtr func = jsEngine.Evaluate("API.isListedFilter"); 216 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter");
217 JsValueList params; 217 JsValueList params;
218 params.push_back(shared_from_this()); 218 params.push_back(shared_from_this());
219 return func->Call(params)->AsBool(); 219 return func->Call(params)->AsBool();
220 #endif 220 #endif
221 } 221 }
222 222
223 void Subscription::AddToList() 223 void Subscription::AddToList()
224 { 224 {
225 #if FILTER_ENGINE_STUBS 225 #if FILTER_ENGINE_STUBS
226 if (!IsListed()) 226 if (!IsListed())
227 filterEngine.listedSubscriptions.push_back(shared_from_this()); 227 filterEngine.listedSubscriptions.push_back(shared_from_this());
228 #else 228 #else
229 JsValuePtr func = jsEngine.Evaluate("API.addSubscriptionToList"); 229 JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList");
230 JsValueList params; 230 JsValueList params;
231 params.push_back(shared_from_this()); 231 params.push_back(shared_from_this());
232 func->Call(params); 232 func->Call(params);
233 #endif 233 #endif
234 } 234 }
235 235
236 void Subscription::RemoveFromList() 236 void Subscription::RemoveFromList()
237 { 237 {
238 #if FILTER_ENGINE_STUBS 238 #if FILTER_ENGINE_STUBS
239 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin(); 239 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin();
240 it != filterEngine.listedSubscriptions.end();) 240 it != filterEngine.listedSubscriptions.end();)
241 { 241 {
242 if (it->get() == this) 242 if (it->get() == this)
243 it = filterEngine.listedSubscriptions.erase(it); 243 it = filterEngine.listedSubscriptions.erase(it);
244 else 244 else
245 it++; 245 it++;
246 } 246 }
247 #else 247 #else
248 JsValuePtr func = jsEngine.Evaluate("API.removeSubscriptionFromList"); 248 JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList");
249 JsValueList params; 249 JsValueList params;
250 params.push_back(shared_from_this()); 250 params.push_back(shared_from_this());
251 func->Call(params); 251 func->Call(params);
252 #endif 252 #endif
253 } 253 }
254 254
255 void Subscription::UpdateFilters() 255 void Subscription::UpdateFilters()
256 { 256 {
257 #if !FILTER_ENGINE_STUBS 257 #if !FILTER_ENGINE_STUBS
258 JsValuePtr func = jsEngine.Evaluate("API.updateSubscription"); 258 JsValuePtr func = jsEngine->Evaluate("API.updateSubscription");
259 JsValueList params; 259 JsValueList params;
260 params.push_back(shared_from_this()); 260 params.push_back(shared_from_this());
261 func->Call(params); 261 func->Call(params);
262 #endif 262 #endif
263 } 263 }
264 264
265 bool Subscription::IsUpdating() 265 bool Subscription::IsUpdating()
266 { 266 {
267 #if FILTER_ENGINE_STUBS 267 #if FILTER_ENGINE_STUBS
268 return false; 268 return false;
269 #else 269 #else
270 JsValuePtr func = jsEngine.Evaluate("API.isSubscriptionUpdating"); 270 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating");
271 JsValueList params; 271 JsValueList params;
272 params.push_back(shared_from_this()); 272 params.push_back(shared_from_this());
273 JsValuePtr result = func->Call(params); 273 JsValuePtr result = func->Call(params);
274 return result->AsBool(); 274 return result->AsBool();
275 #endif 275 #endif
276 } 276 }
277 277
278 bool Subscription::operator==(const Subscription& subscription) const 278 bool Subscription::operator==(const Subscription& subscription) const
279 { 279 {
280 return GetProperty("url", "") == subscription.GetProperty("url", ""); 280 return GetProperty("url", "") == subscription.GetProperty("url", "");
281 } 281 }
282 282
283 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) 283 FilterEngine::FilterEngine(JsEnginePtr jsEngine) : jsEngine(jsEngine)
284 { 284 {
285 #if !FILTER_ENGINE_STUBS 285 #if !FILTER_ENGINE_STUBS
286 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) 286 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2)
287 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); 287 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]);
288 #endif 288 #endif
289 } 289 }
290 290
291 FilterPtr FilterEngine::GetFilter(const std::string& text) 291 FilterPtr FilterEngine::GetFilter(const std::string& text)
292 { 292 {
293 #if FILTER_ENGINE_STUBS 293 #if FILTER_ENGINE_STUBS
294 // 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
295 std::string trimmed(text); 295 std::string trimmed(text);
296 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))));
297 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());
298 298
299 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);
300 if (it != knownFilters.end()) 300 if (it != knownFilters.end())
301 return it->second; 301 return it->second;
302 302
303 FilterPtr result(new Filter(*this, trimmed)); 303 FilterPtr result(new Filter(*this, trimmed));
304 knownFilters[trimmed] = result; 304 knownFilters[trimmed] = result;
305 return result; 305 return result;
306 #else 306 #else
307 JsValuePtr func = jsEngine.Evaluate("API.getFilterFromText"); 307 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText");
308 JsValueList params; 308 JsValueList params;
309 params.push_back(jsEngine.NewValue(text)); 309 params.push_back(jsEngine->NewValue(text));
310 return FilterPtr(new Filter(func->Call(params))); 310 return FilterPtr(new Filter(func->Call(params)));
311 #endif 311 #endif
312 } 312 }
313 313
314 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) 314 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url)
315 { 315 {
316 #if FILTER_ENGINE_STUBS 316 #if FILTER_ENGINE_STUBS
317 std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions .find(url); 317 std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions .find(url);
318 if (it != knownSubscriptions.end()) 318 if (it != knownSubscriptions.end())
319 return it->second; 319 return it->second;
320 320
321 SubscriptionPtr result(new Subscription(*this, url)); 321 SubscriptionPtr result(new Subscription(*this, url));
322 knownSubscriptions[url] = result; 322 knownSubscriptions[url] = result;
323 return result; 323 return result;
324 #else 324 #else
325 JsValuePtr func = jsEngine.Evaluate("API.getSubscriptionFromUrl"); 325 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl");
326 JsValueList params; 326 JsValueList params;
327 params.push_back(jsEngine.NewValue(url)); 327 params.push_back(jsEngine->NewValue(url));
328 return SubscriptionPtr(new Subscription(func->Call(params))); 328 return SubscriptionPtr(new Subscription(func->Call(params)));
329 #endif 329 #endif
330 } 330 }
331 331
332 const std::vector<FilterPtr> FilterEngine::GetListedFilters() const 332 const std::vector<FilterPtr> FilterEngine::GetListedFilters() const
333 { 333 {
334 #if FILTER_ENGINE_STUBS 334 #if FILTER_ENGINE_STUBS
335 return listedFilters; 335 return listedFilters;
336 #else 336 #else
337 JsValuePtr func = jsEngine.Evaluate("API.getListedFilters"); 337 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters");
338 JsValueList values = func->Call()->AsList(); 338 JsValueList values = func->Call()->AsList();
339 std::vector<FilterPtr> result; 339 std::vector<FilterPtr> result;
340 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 340 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
341 result.push_back(FilterPtr(new Filter(*it))); 341 result.push_back(FilterPtr(new Filter(*it)));
342 return result; 342 return result;
343 #endif 343 #endif
344 } 344 }
345 345
346 const std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const 346 const std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const
347 { 347 {
348 #if FILTER_ENGINE_STUBS 348 #if FILTER_ENGINE_STUBS
349 return listedSubscriptions; 349 return listedSubscriptions;
350 #else 350 #else
351 JsValuePtr func = jsEngine.Evaluate("API.getListedSubscriptions"); 351 JsValuePtr func = jsEngine->Evaluate("API.getListedSubscriptions");
352 JsValueList values = func->Call()->AsList(); 352 JsValueList values = func->Call()->AsList();
353 std::vector<SubscriptionPtr> result; 353 std::vector<SubscriptionPtr> result;
354 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 354 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
355 result.push_back(SubscriptionPtr(new Subscription(*it))); 355 result.push_back(SubscriptionPtr(new Subscription(*it)));
356 return result; 356 return result;
357 #endif 357 #endif
358 } 358 }
359 359
360 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback) 360 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback)
361 { 361 {
(...skipping 20 matching lines...) Expand all
382 { 382 {
383 #if FILTER_ENGINE_STUBS 383 #if FILTER_ENGINE_STUBS
384 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ 384 //For test on http://simple-adblock.com/faq/testing-your-adblocker/
385 if (url.find("adbanner.gif") != std::string::npos) 385 if (url.find("adbanner.gif") != std::string::npos)
386 return GetFilter("adbanner.gif"); 386 return GetFilter("adbanner.gif");
387 else if (url.find("notbanner.gif") != std::string::npos) 387 else if (url.find("notbanner.gif") != std::string::npos)
388 return GetFilter("@@notbanner.gif"); 388 return GetFilter("@@notbanner.gif");
389 else 389 else
390 return AdblockPlus::FilterPtr(); 390 return AdblockPlus::FilterPtr();
391 #else 391 #else
392 JsValuePtr func = jsEngine.Evaluate("API.checkFilterMatch"); 392 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch");
393 JsValueList params; 393 JsValueList params;
394 params.push_back(jsEngine.NewValue(url)); 394 params.push_back(jsEngine->NewValue(url));
395 params.push_back(jsEngine.NewValue(contentType)); 395 params.push_back(jsEngine->NewValue(contentType));
396 params.push_back(jsEngine.NewValue(documentUrl)); 396 params.push_back(jsEngine->NewValue(documentUrl));
397 JsValuePtr result = func->Call(params); 397 JsValuePtr result = func->Call(params);
398 if (!result->IsNull()) 398 if (!result->IsNull())
399 return FilterPtr(new Filter(result)); 399 return FilterPtr(new Filter(result));
400 else 400 else
401 return FilterPtr(); 401 return FilterPtr();
402 #endif 402 #endif
403 } 403 }
404 404
405 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
406 { 406 {
407 #if FILTER_ENGINE_STUBS 407 #if FILTER_ENGINE_STUBS
408 std::vector<std::string> selectors; 408 std::vector<std::string> selectors;
409 selectors.push_back("#ad"); 409 selectors.push_back("#ad");
410 selectors.push_back(".ad"); 410 selectors.push_back(".ad");
411 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ 411 //For test on http://simple-adblock.com/faq/testing-your-adblocker/
412 if (domain == "simple-adblock.com") 412 if (domain == "simple-adblock.com")
413 selectors.push_back(".ad_300x250"); 413 selectors.push_back(".ad_300x250");
414 return selectors; 414 return selectors;
415 #else 415 #else
416 JsValuePtr func = jsEngine.Evaluate("API.getElementHidingSelectors"); 416 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors");
417 JsValueList params; 417 JsValueList params;
418 params.push_back(jsEngine.NewValue(domain)); 418 params.push_back(jsEngine->NewValue(domain));
419 JsValueList result = func->Call(params)->AsList(); 419 JsValueList result = func->Call(params)->AsList();
420 std::vector<std::string> selectors; 420 std::vector<std::string> selectors;
421 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) 421 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it)
422 selectors.push_back((*it)->AsString()); 422 selectors.push_back((*it)->AsString());
423 return selectors; 423 return selectors;
424 #endif 424 #endif
425 } 425 }
OLDNEW
« no previous file with comments | « src/FileSystemJsObject.cpp ('k') | src/GlobalJsObject.h » ('j') | src/JsEngine.cpp » ('J')

Powered by Google App Engine
This is Rietveld