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

Side by Side Diff: src/FilterEngine.cpp

Issue 29422625: Issue 5189 - use r-value references when objects should be transferred (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created April 26, 2017, 2:14 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/JsEngine.h ('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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 { 210 {
211 FilterEnginePtr filterEngine(new FilterEngine(jsEngine)); 211 FilterEnginePtr filterEngine(new FilterEngine(jsEngine));
212 auto sync = std::make_shared<Sync>(); 212 auto sync = std::make_shared<Sync>();
213 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback; 213 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback;
214 if (isConnectionAllowedCallback) 214 if (isConnectionAllowedCallback)
215 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool 215 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool
216 { 216 {
217 sync->Wait(); 217 sync->Wait();
218 return jsEngine->IsConnectionAllowed(); 218 return jsEngine->IsConnectionAllowed();
219 }); 219 });
220 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowedCallback](const JsValueList& params) 220 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowedCallback](JsValueList&& params)
221 { 221 {
222 filterEngine->firstRun = params.size() && params[0].AsBool(); 222 filterEngine->firstRun = params.size() && params[0].AsBool();
223 if (isConnectionAllowedCallback) 223 if (isConnectionAllowedCallback)
224 { 224 {
225 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine; 225 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine;
226 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl lowedCallback]()->bool 226 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl lowedCallback]()->bool
227 { 227 {
228 auto filterEngine = weakFilterEngine.lock(); 228 auto filterEngine = weakFilterEngine.lock();
229 if (!filterEngine) 229 if (!filterEngine)
230 return false; 230 return false;
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 { 381 {
382 JsValue func = jsEngine->Evaluate("API.showNextNotification"); 382 JsValue func = jsEngine->Evaluate("API.showNextNotification");
383 JsValueList params; 383 JsValueList params;
384 if (!url.empty()) 384 if (!url.empty())
385 { 385 {
386 params.push_back(jsEngine->NewValue(url)); 386 params.push_back(jsEngine->NewValue(url));
387 } 387 }
388 func.Call(params); 388 func.Call(params);
389 } 389 }
390 390
391 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue) 391 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& c allback)
392 { 392 {
393 if (!value) 393 if (!callback)
394 return; 394 return;
395 395
396 jsEngine->SetEventCallback("_showNotification", 396 jsEngine->SetEventCallback("_showNotification", [this, callback](JsValueList&& params)
397 std::bind(&FilterEngine::ShowNotification, this, value, 397 {
398 std::placeholders::_1)); 398 if (params.size() < 1 || !params[0].IsObject())
399 return;
400
401 callback(Notification(std::move(params[0])));
402 });
399 } 403 }
400 404
401 void FilterEngine::RemoveShowNotificationCallback() 405 void FilterEngine::RemoveShowNotificationCallback()
402 { 406 {
403 jsEngine->RemoveEventCallback("_showNotification"); 407 jsEngine->RemoveEventCallback("_showNotification");
404 } 408 }
405 409
406 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, 410 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
407 ContentTypeMask contentTypeMask, 411 ContentTypeMask contentTypeMask,
408 const std::string& documentUrl) const 412 const std::string& documentUrl) const
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 491
488 std::string FilterEngine::GetHostFromURL(const std::string& url) const 492 std::string FilterEngine::GetHostFromURL(const std::string& url) const
489 { 493 {
490 JsValue func = jsEngine->Evaluate("API.getHostFromUrl"); 494 JsValue func = jsEngine->Evaluate("API.getHostFromUrl");
491 return func.Call(jsEngine->NewValue(url)).AsString(); 495 return func.Call(jsEngine->NewValue(url)).AsString();
492 } 496 }
493 497
494 void FilterEngine::SetUpdateAvailableCallback( 498 void FilterEngine::SetUpdateAvailableCallback(
495 const FilterEngine::UpdateAvailableCallback& callback) 499 const FilterEngine::UpdateAvailableCallback& callback)
496 { 500 {
497 jsEngine->SetEventCallback("updateAvailable", 501 jsEngine->SetEventCallback("updateAvailable", [this, callback](JsValueList&& p arams)
498 std::bind(&FilterEngine::UpdateAvailable, this, callback, 502 {
499 std::placeholders::_1)); 503 if (params.size() >= 1 && !params[0].IsNull())
504 callback(params[0].AsString());
505 });
500 } 506 }
501 507
502 void FilterEngine::RemoveUpdateAvailableCallback() 508 void FilterEngine::RemoveUpdateAvailableCallback()
503 { 509 {
504 jsEngine->RemoveEventCallback("updateAvailable"); 510 jsEngine->RemoveEventCallback("updateAvailable");
505 } 511 }
506 512
507 void FilterEngine::UpdateAvailable(
508 const FilterEngine::UpdateAvailableCallback& callback, const JsValueList& pa rams) const
509 {
510 if (params.size() >= 1 && !params[0].IsNull())
511 callback(params[0].AsString());
512 }
513
514 void FilterEngine::ForceUpdateCheck( 513 void FilterEngine::ForceUpdateCheck(
515 const FilterEngine::UpdateCheckDoneCallback& callback) 514 const FilterEngine::UpdateCheckDoneCallback& callback)
516 { 515 {
517 JsValue func = jsEngine->Evaluate("API.forceUpdateCheck"); 516 JsValue func = jsEngine->Evaluate("API.forceUpdateCheck");
518 JsValueList params; 517 JsValueList params;
519 if (callback) 518 if (callback)
520 { 519 {
521 std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId) ; 520 std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId) ;
522 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDo ne, 521 jsEngine->SetEventCallback(eventName, [this, eventName, callback](JsValueLis t&& params)
523 this, eventName, callback, std::placeholders::_1)); 522 {
523 std::string error(params.size() >= 1 && !params[0].IsNull() ? params[0].As String() : "");
524 callback(error);
525 jsEngine->RemoveEventCallback(eventName);
526 });
524 params.push_back(jsEngine->NewValue(eventName)); 527 params.push_back(jsEngine->NewValue(eventName));
525 } 528 }
526 func.Call(params); 529 func.Call(params);
527 } 530 }
528 531
529 void FilterEngine::UpdateCheckDone(const std::string& eventName, 532 void FilterEngine::SetFilterChangeCallback(const FilterChangeCallback& callback)
530 const FilterEngine::UpdateCheckDoneCallback& callback, const JsValueList& pa rams)
531 { 533 {
532 jsEngine->RemoveEventCallback(eventName); 534 jsEngine->SetEventCallback("filterChange", [this, callback](JsValueList&& para ms)
533 535 {
534 std::string error(params.size() >= 1 && !params[0].IsNull() ? params[0].AsStri ng() : ""); 536 this->FilterChanged(callback, move(params));
535 callback(error); 537 });
536 }
537
538 void FilterEngine::SetFilterChangeCallback(const FilterEngine::FilterChangeCallb ack& callback)
539 {
540 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged,
541 this, callback, std::placeholders::_1));
542 } 538 }
543 539
544 void FilterEngine::RemoveFilterChangeCallback() 540 void FilterEngine::RemoveFilterChangeCallback()
545 { 541 {
546 jsEngine->RemoveEventCallback("filterChange"); 542 jsEngine->RemoveEventCallback("filterChange");
547 } 543 }
548 544
549 void FilterEngine::SetAllowedConnectionType(const std::string* value) 545 void FilterEngine::SetAllowedConnectionType(const std::string* value)
550 { 546 {
551 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : jsEngi ne->NewValue("")); 547 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : jsEngi ne->NewValue(""));
552 } 548 }
553 549
554 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() const 550 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() const
555 { 551 {
556 auto prefValue = GetPref("allowed_connection_type"); 552 auto prefValue = GetPref("allowed_connection_type");
557 if (prefValue.AsString().empty()) 553 if (prefValue.AsString().empty())
558 return nullptr; 554 return nullptr;
559 return std::unique_ptr<std::string>(new std::string(prefValue.AsString())); 555 return std::unique_ptr<std::string>(new std::string(prefValue.AsString()));
560 } 556 }
561 557
562 void FilterEngine::FilterChanged(const FilterEngine::FilterChangeCallback& callb ack, const JsValueList& params) const 558 void FilterEngine::FilterChanged(const FilterEngine::FilterChangeCallback& callb ack, JsValueList&& params) const
563 { 559 {
564 std::string action(params.size() >= 1 && !params[0].IsNull() ? params[0].AsStr ing() : ""); 560 std::string action(params.size() >= 1 && !params[0].IsNull() ? params[0].AsStr ing() : "");
565 JsValue item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); 561 JsValue item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false));
566 callback(action, item); 562 callback(action, std::move(item));
567 } 563 }
568 564
569 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback,
570 const JsValueList& params) const
571 {
572 if (params.size() < 1)
573 return;
574
575 if (!params[0].IsObject())
576 {
577 return;
578 }
579 auto notification = Notification(JsValue(params[0]));
580 callback(notification);
581 }
582
583
584 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const 565 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const
585 { 566 {
586 JsValueList params; 567 JsValueList params;
587 params.push_back(jsEngine->NewValue(v1)); 568 params.push_back(jsEngine->NewValue(v1));
588 params.push_back(jsEngine->NewValue(v2)); 569 params.push_back(jsEngine->NewValue(v2));
589 JsValue func = jsEngine->Evaluate("API.compareVersions"); 570 JsValue func = jsEngine->Evaluate("API.compareVersions");
590 return func.Call(params).AsInt(); 571 return func.Call(params).AsInt();
591 } 572 }
592 573
593 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, 574 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url,
(...skipping 24 matching lines...) Expand all
618 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); 599 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url);
619 if (filter) 600 if (filter)
620 { 601 {
621 return filter; 602 return filter;
622 } 603 }
623 currentUrl = parentUrl; 604 currentUrl = parentUrl;
624 } 605 }
625 while (urlIterator != documentUrls.end()); 606 while (urlIterator != documentUrls.end());
626 return FilterPtr(); 607 return FilterPtr();
627 } 608 }
OLDNEW
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('k') | src/GlobalJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld