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

Side by Side Diff: src/FilterEngine.cpp

Issue 29393589: Issue 5013 - Make more methods const.- introduced JsConstValuePtr and JsConstValueList- JsValue:… (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Now mostly use the single param Call() overload Created March 24, 2017, 2:10 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 | « src/FileSystemJsObject.cpp ('k') | src/GlobalJsObject.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 19 matching lines...) Expand all
30 30
31 extern std::string jsSources[]; 31 extern std::string jsSources[];
32 32
33 Filter::Filter(JsValue&& value) 33 Filter::Filter(JsValue&& value)
34 : JsValue(std::move(value)) 34 : JsValue(std::move(value))
35 { 35 {
36 if (!IsObject()) 36 if (!IsObject())
37 throw std::runtime_error("JavaScript value is not an object"); 37 throw std::runtime_error("JavaScript value is not an object");
38 } 38 }
39 39
40 Filter::Type Filter::GetType() 40 Filter::Type Filter::GetType() const
41 { 41 {
42 std::string className = GetClass(); 42 std::string className = GetClass();
43 if (className == "BlockingFilter") 43 if (className == "BlockingFilter")
44 return TYPE_BLOCKING; 44 return TYPE_BLOCKING;
45 else if (className == "WhitelistFilter") 45 else if (className == "WhitelistFilter")
46 return TYPE_EXCEPTION; 46 return TYPE_EXCEPTION;
47 else if (className == "ElemHideFilter") 47 else if (className == "ElemHideFilter")
48 return TYPE_ELEMHIDE; 48 return TYPE_ELEMHIDE;
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() 57 bool Filter::IsListed() const
58 { 58 {
59 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); 59 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter");
60 JsValueList params; 60 return func->Call(shared_from_this())->AsBool();
61 params.push_back(shared_from_this());
62 return func->Call(params)->AsBool();
63 } 61 }
64 62
65 void Filter::AddToList() 63 void Filter::AddToList()
66 { 64 {
67 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList"); 65 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList");
68 JsValueList params; 66 func->Call(shared_from_this());
69 params.push_back(shared_from_this());
70 func->Call(params);
71 } 67 }
72 68
73 void Filter::RemoveFromList() 69 void Filter::RemoveFromList()
74 { 70 {
75 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList"); 71 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList");
76 JsValueList params; 72 func->Call(shared_from_this());
77 params.push_back(shared_from_this());
78 func->Call(params);
79 } 73 }
80 74
81 bool Filter::operator==(const Filter& filter) const 75 bool Filter::operator==(const Filter& filter) const
82 { 76 {
83 return GetProperty("text")->AsString() == filter.GetProperty("text")->AsString (); 77 return GetProperty("text")->AsString() == filter.GetProperty("text")->AsString ();
84 } 78 }
85 79
86 Subscription::Subscription(JsValue&& value) 80 Subscription::Subscription(JsValue&& value)
87 : JsValue(std::move(value)) 81 : JsValue(std::move(value))
88 { 82 {
89 if (!IsObject()) 83 if (!IsObject())
90 throw std::runtime_error("JavaScript value is not an object"); 84 throw std::runtime_error("JavaScript value is not an object");
91 } 85 }
92 86
93 bool Subscription::IsListed() 87 bool Subscription::IsListed() const
94 { 88 {
95 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription"); 89 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription");
96 JsValueList params; 90 return func->Call(shared_from_this())->AsBool();
97 params.push_back(shared_from_this());
98 return func->Call(params)->AsBool();
99 } 91 }
100 92
101 void Subscription::AddToList() 93 void Subscription::AddToList()
102 { 94 {
103 JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList"); 95 JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList");
104 JsValueList params; 96 func->Call(shared_from_this());
105 params.push_back(shared_from_this());
106 func->Call(params);
107 } 97 }
108 98
109 void Subscription::RemoveFromList() 99 void Subscription::RemoveFromList()
110 { 100 {
111 JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList"); 101 JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList");
112 JsValueList params; 102 func->Call(shared_from_this());
113 params.push_back(shared_from_this());
114 func->Call(params);
115 } 103 }
116 104
117 void Subscription::UpdateFilters() 105 void Subscription::UpdateFilters()
118 { 106 {
119 JsValuePtr func = jsEngine->Evaluate("API.updateSubscription"); 107 JsValuePtr func = jsEngine->Evaluate("API.updateSubscription");
120 JsValueList params; 108 func->Call(shared_from_this());
121 params.push_back(shared_from_this());
122 func->Call(params);
123 } 109 }
124 110
125 bool Subscription::IsUpdating() 111 bool Subscription::IsUpdating() const
126 { 112 {
127 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating"); 113 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating");
128 JsValueList params; 114 JsValuePtr result = func->Call(shared_from_this());
129 params.push_back(shared_from_this());
130 JsValuePtr result = func->Call(params);
131 return result->AsBool(); 115 return result->AsBool();
132 } 116 }
133 117
134 bool Subscription::operator==(const Subscription& subscription) const 118 bool Subscription::operator==(const Subscription& subscription) const
135 { 119 {
136 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); 120 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring();
137 } 121 }
138 122
139 namespace 123 namespace
140 { 124 {
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 return it->first; 266 return it->first;
283 } 267 }
284 throw std::invalid_argument("Cannot convert argument to ContentType"); 268 throw std::invalid_argument("Cannot convert argument to ContentType");
285 } 269 }
286 270
287 bool FilterEngine::IsFirstRun() const 271 bool FilterEngine::IsFirstRun() const
288 { 272 {
289 return firstRun; 273 return firstRun;
290 } 274 }
291 275
292 FilterPtr FilterEngine::GetFilter(const std::string& text) 276 FilterPtr FilterEngine::GetFilter(const std::string& text) const
293 { 277 {
294 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText"); 278 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText");
295 JsValueList params; 279 return FilterPtr(new Filter(std::move(*func->Call(jsEngine->NewValue(text))))) ;
296 params.push_back(jsEngine->NewValue(text));
297 return FilterPtr(new Filter(std::move(*func->Call(params))));
298 } 280 }
299 281
300 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) 282 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) const
301 { 283 {
302 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl"); 284 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl");
303 JsValueList params; 285 return SubscriptionPtr(new Subscription(std::move(*func->Call(jsEngine->NewVal ue(url)))));
304 params.push_back(jsEngine->NewValue(url));
305 return SubscriptionPtr(new Subscription(std::move(*func->Call(params))));
306 } 286 }
307 287
308 std::vector<FilterPtr> FilterEngine::GetListedFilters() const 288 std::vector<FilterPtr> FilterEngine::GetListedFilters() const
309 { 289 {
310 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters"); 290 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters");
311 JsValueList values = func->Call()->AsList(); 291 JsValueList values = func->Call()->AsList();
312 std::vector<FilterPtr> result; 292 std::vector<FilterPtr> result;
313 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 293 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
314 result.push_back(FilterPtr(new Filter(std::move(**it)))); 294 result.push_back(FilterPtr(new Filter(std::move(**it))));
315 return result; 295 return result;
(...skipping 15 matching lines...) Expand all
331 JsValueList values = func->Call()->AsList(); 311 JsValueList values = func->Call()->AsList();
332 std::vector<SubscriptionPtr> result; 312 std::vector<SubscriptionPtr> result;
333 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 313 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
334 result.push_back(SubscriptionPtr(new Subscription(std::move(**it)))); 314 result.push_back(SubscriptionPtr(new Subscription(std::move(**it))));
335 return result; 315 return result;
336 } 316 }
337 317
338 void FilterEngine::ShowNextNotification(const std::string& url) 318 void FilterEngine::ShowNextNotification(const std::string& url)
339 { 319 {
340 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification"); 320 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification");
341 JsValueList params; 321 JsConstValueList params;
342 if (!url.empty()) 322 if (!url.empty())
343 { 323 {
344 params.push_back(jsEngine->NewValue(url)); 324 params.push_back(jsEngine->NewValue(url));
345 } 325 }
346 func->Call(params); 326 func->Call(params);
347 } 327 }
348 328
349 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue) 329 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue)
350 { 330 {
351 if (!value) 331 if (!value)
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 const std::vector<std::string>& documentUrls) const 382 const std::vector<std::string>& documentUrls) const
403 { 383 {
404 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls); 384 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls);
405 } 385 }
406 386
407 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, 387 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url,
408 ContentTypeMask contentTypeMask, 388 ContentTypeMask contentTypeMask,
409 const std::string& documentUrl) const 389 const std::string& documentUrl) const
410 { 390 {
411 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); 391 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch");
412 JsValueList params; 392 JsConstValueList params;
413 params.push_back(jsEngine->NewValue(url)); 393 params.push_back(jsEngine->NewValue(url));
414 params.push_back(jsEngine->NewValue(contentTypeMask)); 394 params.push_back(jsEngine->NewValue(contentTypeMask));
415 params.push_back(jsEngine->NewValue(documentUrl)); 395 params.push_back(jsEngine->NewValue(documentUrl));
416 JsValuePtr result = func->Call(params); 396 JsValuePtr result = func->Call(params);
417 if (!result->IsNull()) 397 if (!result->IsNull())
418 return FilterPtr(new Filter(std::move(*result))); 398 return FilterPtr(new Filter(std::move(*result)));
419 else 399 else
420 return FilterPtr(); 400 return FilterPtr();
421 } 401 }
422 402
423 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const 403 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const
424 { 404 {
425 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); 405 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors");
426 JsValueList params; 406 JsValueList result = func->Call(jsEngine->NewValue(domain))->AsList();
427 params.push_back(jsEngine->NewValue(domain));
428 JsValueList result = func->Call(params)->AsList();
429 std::vector<std::string> selectors; 407 std::vector<std::string> selectors;
430 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) 408 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it)
431 selectors.push_back((*it)->AsString()); 409 selectors.push_back((*it)->AsString());
432 return selectors; 410 return selectors;
433 } 411 }
434 412
435 JsValuePtr FilterEngine::GetPref(const std::string& pref) const 413 JsValuePtr FilterEngine::GetPref(const std::string& pref) const
436 { 414 {
437 JsValuePtr func = jsEngine->Evaluate("API.getPref"); 415 JsValuePtr func = jsEngine->Evaluate("API.getPref");
438 JsValueList params; 416 return func->Call(jsEngine->NewValue(pref));
439 params.push_back(jsEngine->NewValue(pref));
440 return func->Call(params);
441 } 417 }
442 418
443 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) 419 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value)
444 { 420 {
445 JsValuePtr func = jsEngine->Evaluate("API.setPref"); 421 JsValuePtr func = jsEngine->Evaluate("API.setPref");
446 JsValueList params; 422 JsConstValueList params;
447 params.push_back(jsEngine->NewValue(pref)); 423 params.push_back(jsEngine->NewValue(pref));
448 if (value) 424 if (value)
449 params.push_back(value); 425 params.push_back(value);
450 func->Call(params); 426 func->Call(params);
451 } 427 }
452 428
453 std::string FilterEngine::GetHostFromURL(const std::string& url) 429 std::string FilterEngine::GetHostFromURL(const std::string& url) const
454 { 430 {
455 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); 431 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl");
456 JsValueList params; 432 return func->Call(jsEngine->NewValue(url))->AsString();
457 params.push_back(jsEngine->NewValue(url));
458 return func->Call(params)->AsString();
459 } 433 }
460 434
461 void FilterEngine::SetUpdateAvailableCallback( 435 void FilterEngine::SetUpdateAvailableCallback(
462 FilterEngine::UpdateAvailableCallback callback) 436 FilterEngine::UpdateAvailableCallback callback)
463 { 437 {
464 jsEngine->SetEventCallback("updateAvailable", 438 jsEngine->SetEventCallback("updateAvailable",
465 std::bind(&FilterEngine::UpdateAvailable, this, callback, 439 std::bind(&FilterEngine::UpdateAvailable, this, callback,
466 std::placeholders::_1)); 440 std::placeholders::_1));
467 } 441 }
468 442
469 void FilterEngine::RemoveUpdateAvailableCallback() 443 void FilterEngine::RemoveUpdateAvailableCallback()
470 { 444 {
471 jsEngine->RemoveEventCallback("updateAvailable"); 445 jsEngine->RemoveEventCallback("updateAvailable");
472 } 446 }
473 447
474 void FilterEngine::UpdateAvailable( 448 void FilterEngine::UpdateAvailable(
475 FilterEngine::UpdateAvailableCallback callback, JsValueList& params) 449 FilterEngine::UpdateAvailableCallback callback, JsValueList& params)
476 { 450 {
477 if (params.size() >= 1 && !params[0]->IsNull()) 451 if (params.size() >= 1 && !params[0]->IsNull())
478 callback(params[0]->AsString()); 452 callback(params[0]->AsString());
479 } 453 }
480 454
481 void FilterEngine::ForceUpdateCheck( 455 void FilterEngine::ForceUpdateCheck(
482 const FilterEngine::UpdateCheckDoneCallback& callback) 456 const FilterEngine::UpdateCheckDoneCallback& callback)
483 { 457 {
484 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); 458 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck");
485 JsValueList params; 459 JsConstValueList params;
486 if (callback) 460 if (callback)
487 { 461 {
488 std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId) ; 462 std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId) ;
489 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDo ne, 463 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDo ne,
490 this, eventName, callback, std::placeholders::_1)); 464 this, eventName, callback, std::placeholders::_1));
491 params.push_back(jsEngine->NewValue(eventName)); 465 params.push_back(jsEngine->NewValue(eventName));
492 } 466 }
493 func->Call(params); 467 func->Call(params);
494 } 468 }
495 469
(...skipping 15 matching lines...) Expand all
511 void FilterEngine::RemoveFilterChangeCallback() 485 void FilterEngine::RemoveFilterChangeCallback()
512 { 486 {
513 jsEngine->RemoveEventCallback("filterChange"); 487 jsEngine->RemoveEventCallback("filterChange");
514 } 488 }
515 489
516 void FilterEngine::SetAllowedConnectionType(const std::string* value) 490 void FilterEngine::SetAllowedConnectionType(const std::string* value)
517 { 491 {
518 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : nullpt r); 492 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : nullpt r);
519 } 493 }
520 494
521 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() 495 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() const
522 { 496 {
523 auto prefValue = GetPref("allowed_connection_type"); 497 auto prefValue = GetPref("allowed_connection_type");
524 if (prefValue->IsUndefined()) 498 if (prefValue->IsUndefined())
525 return nullptr; 499 return nullptr;
526 return std::unique_ptr<std::string>(new std::string(prefValue->AsString())); 500 return std::unique_ptr<std::string>(new std::string(prefValue->AsString()));
527 } 501 }
528 502
529 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params) 503 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params)
530 { 504 {
531 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : ""); 505 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : "");
532 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); 506 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false));
533 callback(action, item); 507 callback(action, item);
534 } 508 }
535 509
536 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, 510 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback,
537 const JsValueList& params) 511 const JsValueList& params)
538 { 512 {
539 if (params.size() < 1) 513 if (params.size() < 1)
540 return; 514 return;
541 515
542 if (!params[0]->IsObject()) 516 if (!params[0]->IsObject())
543 { 517 {
544 return; 518 return;
545 } 519 }
546 callback(NotificationPtr(new Notification(std::move(*params[0])))); 520 callback(NotificationPtr(new Notification(std::move(*params[0]))));
547 } 521 }
548 522
549 523
550 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) 524 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const
551 { 525 {
552 JsValueList params; 526 JsConstValueList params;
553 params.push_back(jsEngine->NewValue(v1)); 527 params.push_back(jsEngine->NewValue(v1));
554 params.push_back(jsEngine->NewValue(v2)); 528 params.push_back(jsEngine->NewValue(v2));
555 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); 529 JsValuePtr func = jsEngine->Evaluate("API.compareVersions");
556 return func->Call(params)->AsInt(); 530 return func->Call(params)->AsInt();
557 } 531 }
558 532
559 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, 533 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url,
560 ContentTypeMask contentTypeMask, const std::string& documentUrl) const 534 ContentTypeMask contentTypeMask, const std::string& documentUrl) const
561 { 535 {
562 FilterPtr match = Matches(url, contentTypeMask, documentUrl); 536 FilterPtr match = Matches(url, contentTypeMask, documentUrl);
(...skipping 21 matching lines...) Expand all
584 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); 558 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url);
585 if (filter) 559 if (filter)
586 { 560 {
587 return filter; 561 return filter;
588 } 562 }
589 currentUrl = parentUrl; 563 currentUrl = parentUrl;
590 } 564 }
591 while (urlIterator != documentUrls.end()); 565 while (urlIterator != documentUrls.end());
592 return FilterPtr(); 566 return FilterPtr();
593 } 567 }
OLDNEW
« no previous file with comments | « src/FileSystemJsObject.cpp ('k') | src/GlobalJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld