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

Side by Side Diff: src/FilterEngine.cpp

Issue 29417624: Issue 5034 - Part 4: JsEngine::Evaluate() return a JsValue (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Rebased again Created April 20, 2017, 1:02 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 | « include/AdblockPlus/JsValue.h ('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 /* 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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 else if (className == "ElemHideException") 49 else if (className == "ElemHideException")
50 return TYPE_ELEMHIDE_EXCEPTION; 50 return TYPE_ELEMHIDE_EXCEPTION;
51 else if (className == "CommentFilter") 51 else if (className == "CommentFilter")
52 return TYPE_COMMENT; 52 return TYPE_COMMENT;
53 else 53 else
54 return TYPE_INVALID; 54 return TYPE_INVALID;
55 } 55 }
56 56
57 bool Filter::IsListed() const 57 bool Filter::IsListed() const
58 { 58 {
59 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); 59 JsValue func = jsEngine->Evaluate("API.isListedFilter");
60 return func->Call(*this).AsBool(); 60 return func.Call(*this).AsBool();
61 } 61 }
62 62
63 void Filter::AddToList() 63 void Filter::AddToList()
64 { 64 {
65 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList"); 65 JsValue func = jsEngine->Evaluate("API.addFilterToList");
66 func->Call(*this); 66 func.Call(*this);
67 } 67 }
68 68
69 void Filter::RemoveFromList() 69 void Filter::RemoveFromList()
70 { 70 {
71 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList"); 71 JsValue func = jsEngine->Evaluate("API.removeFilterFromList");
72 func->Call(*this); 72 func.Call(*this);
73 } 73 }
74 74
75 bool Filter::operator==(const Filter& filter) const 75 bool Filter::operator==(const Filter& filter) const
76 { 76 {
77 return GetProperty("text").AsString() == filter.GetProperty("text").AsString() ; 77 return GetProperty("text").AsString() == filter.GetProperty("text").AsString() ;
78 } 78 }
79 79
80 Subscription::Subscription(JsValue&& value) 80 Subscription::Subscription(JsValue&& value)
81 : JsValue(std::move(value)) 81 : JsValue(std::move(value))
82 { 82 {
83 if (!IsObject()) 83 if (!IsObject())
84 throw std::runtime_error("JavaScript value is not an object"); 84 throw std::runtime_error("JavaScript value is not an object");
85 } 85 }
86 86
87 bool Subscription::IsListed() const 87 bool Subscription::IsListed() const
88 { 88 {
89 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription"); 89 JsValue func = jsEngine->Evaluate("API.isListedSubscription");
90 return func->Call(*this).AsBool(); 90 return func.Call(*this).AsBool();
91 } 91 }
92 92
93 void Subscription::AddToList() 93 void Subscription::AddToList()
94 { 94 {
95 JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList"); 95 JsValue func = jsEngine->Evaluate("API.addSubscriptionToList");
96 func->Call(*this); 96 func.Call(*this);
97 } 97 }
98 98
99 void Subscription::RemoveFromList() 99 void Subscription::RemoveFromList()
100 { 100 {
101 JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList"); 101 JsValue func = jsEngine->Evaluate("API.removeSubscriptionFromList");
102 func->Call(*this); 102 func.Call(*this);
103 } 103 }
104 104
105 void Subscription::UpdateFilters() 105 void Subscription::UpdateFilters()
106 { 106 {
107 JsValuePtr func = jsEngine->Evaluate("API.updateSubscription"); 107 JsValue func = jsEngine->Evaluate("API.updateSubscription");
108 func->Call(*this); 108 func.Call(*this);
109 } 109 }
110 110
111 bool Subscription::IsUpdating() const 111 bool Subscription::IsUpdating() const
112 { 112 {
113 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating"); 113 JsValue func = jsEngine->Evaluate("API.isSubscriptionUpdating");
114 return func->Call(*this).AsBool(); 114 return func.Call(*this).AsBool();
115 } 115 }
116 116
117 bool Subscription::IsAA() const 117 bool Subscription::IsAA() const
118 { 118 {
119 return jsEngine->Evaluate("API.isAASubscription")->Call(*this).AsBool(); 119 return jsEngine->Evaluate("API.isAASubscription").Call(*this).AsBool();
120 } 120 }
121 121
122 bool Subscription::operator==(const Subscription& subscription) const 122 bool Subscription::operator==(const Subscription& subscription) const
123 { 123 {
124 return GetProperty("url").AsString() == subscription.GetProperty("url").AsStri ng(); 124 return GetProperty("url").AsString() == subscription.GetProperty("url").AsStri ng();
125 } 125 }
126 126
127 namespace 127 namespace
128 { 128 {
129 class Sync 129 class Sync
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 throw std::invalid_argument("Cannot convert argument to ContentType"); 271 throw std::invalid_argument("Cannot convert argument to ContentType");
272 } 272 }
273 273
274 bool FilterEngine::IsFirstRun() const 274 bool FilterEngine::IsFirstRun() const
275 { 275 {
276 return firstRun; 276 return firstRun;
277 } 277 }
278 278
279 FilterPtr FilterEngine::GetFilter(const std::string& text) const 279 FilterPtr FilterEngine::GetFilter(const std::string& text) const
280 { 280 {
281 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText"); 281 JsValue func = jsEngine->Evaluate("API.getFilterFromText");
282 return FilterPtr(new Filter(func->Call(jsEngine->NewValue(text)))); 282 return FilterPtr(new Filter(func.Call(jsEngine->NewValue(text))));
283 } 283 }
284 284
285 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) const 285 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) const
286 { 286 {
287 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl"); 287 JsValue func = jsEngine->Evaluate("API.getSubscriptionFromUrl");
288 return SubscriptionPtr(new Subscription(func->Call(jsEngine->NewValue(url)))); 288 return SubscriptionPtr(new Subscription(func.Call(jsEngine->NewValue(url))));
289 } 289 }
290 290
291 std::vector<FilterPtr> FilterEngine::GetListedFilters() const 291 std::vector<FilterPtr> FilterEngine::GetListedFilters() const
292 { 292 {
293 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters"); 293 JsValue func = jsEngine->Evaluate("API.getListedFilters");
294 JsValueList values = func->Call().AsList(); 294 JsValueList values = func.Call().AsList();
295 std::vector<FilterPtr> result; 295 std::vector<FilterPtr> result;
296 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 296 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
297 result.push_back(FilterPtr(new Filter(std::move(*it)))); 297 result.push_back(FilterPtr(new Filter(std::move(*it))));
298 return result; 298 return result;
299 } 299 }
300 300
301 std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const 301 std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const
302 { 302 {
303 JsValuePtr func = jsEngine->Evaluate("API.getListedSubscriptions"); 303 JsValue func = jsEngine->Evaluate("API.getListedSubscriptions");
304 JsValueList values = func->Call().AsList(); 304 JsValueList values = func.Call().AsList();
305 std::vector<SubscriptionPtr> result; 305 std::vector<SubscriptionPtr> result;
306 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 306 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
307 result.push_back(SubscriptionPtr(new Subscription(std::move(*it)))); 307 result.push_back(SubscriptionPtr(new Subscription(std::move(*it))));
308 return result; 308 return result;
309 } 309 }
310 310
311 std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const 311 std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const
312 { 312 {
313 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions"); 313 JsValue func = jsEngine->Evaluate("API.getRecommendedSubscriptions");
314 JsValueList values = func->Call().AsList(); 314 JsValueList values = func.Call().AsList();
315 std::vector<SubscriptionPtr> result; 315 std::vector<SubscriptionPtr> result;
316 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 316 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
317 result.push_back(SubscriptionPtr(new Subscription(std::move(*it)))); 317 result.push_back(SubscriptionPtr(new Subscription(std::move(*it))));
318 return result; 318 return result;
319 } 319 }
320 320
321 void FilterEngine::SetAAEnabled(bool enabled) 321 void FilterEngine::SetAAEnabled(bool enabled)
322 { 322 {
323 jsEngine->Evaluate("API.setAASubscriptionEnabled")->Call(jsEngine->NewValue(en abled)); 323 jsEngine->Evaluate("API.setAASubscriptionEnabled").Call(jsEngine->NewValue(ena bled));
324 } 324 }
325 325
326 bool FilterEngine::IsAAEnabled() const 326 bool FilterEngine::IsAAEnabled() const
327 { 327 {
328 return jsEngine->Evaluate("API.isAASubscriptionEnabled()")->AsBool(); 328 return jsEngine->Evaluate("API.isAASubscriptionEnabled()").AsBool();
329 } 329 }
330 330
331 std::string FilterEngine::GetAAUrl() const 331 std::string FilterEngine::GetAAUrl() const
332 { 332 {
333 return GetPref("subscriptions_exceptionsurl").AsString(); 333 return GetPref("subscriptions_exceptionsurl").AsString();
334 } 334 }
335 335
336 void FilterEngine::ShowNextNotification(const std::string& url) const 336 void FilterEngine::ShowNextNotification(const std::string& url) const
337 { 337 {
338 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification"); 338 JsValue func = jsEngine->Evaluate("API.showNextNotification");
339 JsValueList params; 339 JsValueList params;
340 if (!url.empty()) 340 if (!url.empty())
341 { 341 {
342 params.push_back(jsEngine->NewValue(url)); 342 params.push_back(jsEngine->NewValue(url));
343 } 343 }
344 func->Call(params); 344 func.Call(params);
345 } 345 }
346 346
347 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue) 347 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue)
348 { 348 {
349 if (!value) 349 if (!value)
350 return; 350 return;
351 351
352 jsEngine->SetEventCallback("_showNotification", 352 jsEngine->SetEventCallback("_showNotification",
353 std::bind(&FilterEngine::ShowNotification, this, value, 353 std::bind(&FilterEngine::ShowNotification, this, value,
354 std::placeholders::_1)); 354 std::placeholders::_1));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 bool FilterEngine::IsElemhideWhitelisted(const std::string& url, 397 bool FilterEngine::IsElemhideWhitelisted(const std::string& url,
398 const std::vector<std::string>& documentUrls) const 398 const std::vector<std::string>& documentUrls) const
399 { 399 {
400 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls); 400 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls);
401 } 401 }
402 402
403 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, 403 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url,
404 ContentTypeMask contentTypeMask, 404 ContentTypeMask contentTypeMask,
405 const std::string& documentUrl) const 405 const std::string& documentUrl) const
406 { 406 {
407 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); 407 JsValue func = jsEngine->Evaluate("API.checkFilterMatch");
408 JsValueList params; 408 JsValueList params;
409 params.push_back(jsEngine->NewValue(url)); 409 params.push_back(jsEngine->NewValue(url));
410 params.push_back(jsEngine->NewValue(contentTypeMask)); 410 params.push_back(jsEngine->NewValue(contentTypeMask));
411 params.push_back(jsEngine->NewValue(documentUrl)); 411 params.push_back(jsEngine->NewValue(documentUrl));
412 JsValue result = func->Call(params); 412 JsValue result = func.Call(params);
413 if (!result.IsNull()) 413 if (!result.IsNull())
414 return FilterPtr(new Filter(std::move(result))); 414 return FilterPtr(new Filter(std::move(result)));
415 else 415 else
416 return FilterPtr(); 416 return FilterPtr();
417 } 417 }
418 418
419 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const 419 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const
420 { 420 {
421 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); 421 JsValue func = jsEngine->Evaluate("API.getElementHidingSelectors");
422 JsValueList result = func->Call(jsEngine->NewValue(domain)).AsList(); 422 JsValueList result = func.Call(jsEngine->NewValue(domain)).AsList();
423 std::vector<std::string> selectors; 423 std::vector<std::string> selectors;
424 for (const auto& r: result) 424 for (const auto& r: result)
425 selectors.push_back(r.AsString()); 425 selectors.push_back(r.AsString());
426 return selectors; 426 return selectors;
427 } 427 }
428 428
429 JsValue FilterEngine::GetPref(const std::string& pref) const 429 JsValue FilterEngine::GetPref(const std::string& pref) const
430 { 430 {
431 JsValuePtr func = jsEngine->Evaluate("API.getPref"); 431 JsValue func = jsEngine->Evaluate("API.getPref");
432 return func->Call(jsEngine->NewValue(pref)); 432 return func.Call(jsEngine->NewValue(pref));
433 } 433 }
434 434
435 void FilterEngine::SetPref(const std::string& pref, const JsValue& value) 435 void FilterEngine::SetPref(const std::string& pref, const JsValue& value)
436 { 436 {
437 JsValuePtr func = jsEngine->Evaluate("API.setPref"); 437 JsValue func = jsEngine->Evaluate("API.setPref");
438 JsValueList params; 438 JsValueList params;
439 params.push_back(jsEngine->NewValue(pref)); 439 params.push_back(jsEngine->NewValue(pref));
440 params.push_back(value); 440 params.push_back(value);
441 func->Call(params); 441 func.Call(params);
442 } 442 }
443 443
444 std::string FilterEngine::GetHostFromURL(const std::string& url) const 444 std::string FilterEngine::GetHostFromURL(const std::string& url) const
445 { 445 {
446 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); 446 JsValue func = jsEngine->Evaluate("API.getHostFromUrl");
447 return func->Call(jsEngine->NewValue(url)).AsString(); 447 return func.Call(jsEngine->NewValue(url)).AsString();
448 } 448 }
449 449
450 void FilterEngine::SetUpdateAvailableCallback( 450 void FilterEngine::SetUpdateAvailableCallback(
451 const FilterEngine::UpdateAvailableCallback& callback) 451 const FilterEngine::UpdateAvailableCallback& callback)
452 { 452 {
453 jsEngine->SetEventCallback("updateAvailable", 453 jsEngine->SetEventCallback("updateAvailable",
454 std::bind(&FilterEngine::UpdateAvailable, this, callback, 454 std::bind(&FilterEngine::UpdateAvailable, this, callback,
455 std::placeholders::_1)); 455 std::placeholders::_1));
456 } 456 }
457 457
458 void FilterEngine::RemoveUpdateAvailableCallback() 458 void FilterEngine::RemoveUpdateAvailableCallback()
459 { 459 {
460 jsEngine->RemoveEventCallback("updateAvailable"); 460 jsEngine->RemoveEventCallback("updateAvailable");
461 } 461 }
462 462
463 void FilterEngine::UpdateAvailable( 463 void FilterEngine::UpdateAvailable(
464 const FilterEngine::UpdateAvailableCallback& callback, const JsValueList& pa rams) const 464 const FilterEngine::UpdateAvailableCallback& callback, const JsValueList& pa rams) const
465 { 465 {
466 if (params.size() >= 1 && !params[0].IsNull()) 466 if (params.size() >= 1 && !params[0].IsNull())
467 callback(params[0].AsString()); 467 callback(params[0].AsString());
468 } 468 }
469 469
470 void FilterEngine::ForceUpdateCheck( 470 void FilterEngine::ForceUpdateCheck(
471 const FilterEngine::UpdateCheckDoneCallback& callback) 471 const FilterEngine::UpdateCheckDoneCallback& callback)
472 { 472 {
473 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); 473 JsValue func = jsEngine->Evaluate("API.forceUpdateCheck");
474 JsValueList params; 474 JsValueList params;
475 if (callback) 475 if (callback)
476 { 476 {
477 std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId) ; 477 std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId) ;
478 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDo ne, 478 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDo ne,
479 this, eventName, callback, std::placeholders::_1)); 479 this, eventName, callback, std::placeholders::_1));
480 params.push_back(jsEngine->NewValue(eventName)); 480 params.push_back(jsEngine->NewValue(eventName));
481 } 481 }
482 func->Call(params); 482 func.Call(params);
483 } 483 }
484 484
485 void FilterEngine::UpdateCheckDone(const std::string& eventName, 485 void FilterEngine::UpdateCheckDone(const std::string& eventName,
486 const FilterEngine::UpdateCheckDoneCallback& callback, const JsValueList& pa rams) 486 const FilterEngine::UpdateCheckDoneCallback& callback, const JsValueList& pa rams)
487 { 487 {
488 jsEngine->RemoveEventCallback(eventName); 488 jsEngine->RemoveEventCallback(eventName);
489 489
490 std::string error(params.size() >= 1 && !params[0].IsNull() ? params[0].AsStri ng() : ""); 490 std::string error(params.size() >= 1 && !params[0].IsNull() ? params[0].AsStri ng() : "");
491 callback(error); 491 callback(error);
492 } 492 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 } 534 }
535 callback(NotificationPtr(new Notification(JsValue(params[0])))); 535 callback(NotificationPtr(new Notification(JsValue(params[0]))));
536 } 536 }
537 537
538 538
539 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const 539 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const
540 { 540 {
541 JsValueList params; 541 JsValueList params;
542 params.push_back(jsEngine->NewValue(v1)); 542 params.push_back(jsEngine->NewValue(v1));
543 params.push_back(jsEngine->NewValue(v2)); 543 params.push_back(jsEngine->NewValue(v2));
544 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); 544 JsValue func = jsEngine->Evaluate("API.compareVersions");
545 return func->Call(params).AsInt(); 545 return func.Call(params).AsInt();
546 } 546 }
547 547
548 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, 548 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url,
549 ContentTypeMask contentTypeMask, const std::string& documentUrl) const 549 ContentTypeMask contentTypeMask, const std::string& documentUrl) const
550 { 550 {
551 FilterPtr match = Matches(url, contentTypeMask, documentUrl); 551 FilterPtr match = Matches(url, contentTypeMask, documentUrl);
552 if (match && match->GetType() == Filter::TYPE_EXCEPTION) 552 if (match && match->GetType() == Filter::TYPE_EXCEPTION)
553 { 553 {
554 return match; 554 return match;
555 } 555 }
(...skipping 17 matching lines...) Expand all
573 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); 573 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url);
574 if (filter) 574 if (filter)
575 { 575 {
576 return filter; 576 return filter;
577 } 577 }
578 currentUrl = parentUrl; 578 currentUrl = parentUrl;
579 } 579 }
580 while (urlIterator != documentUrls.end()); 580 while (urlIterator != documentUrls.end());
581 return FilterPtr(); 581 return FilterPtr();
582 } 582 }
OLDNEW
« no previous file with comments | « include/AdblockPlus/JsValue.h ('k') | src/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld