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

Side by Side Diff: src/FilterEngine.cpp

Issue 29410664: Issue 5013 - Use const JsValue and pass reference where applicable (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Created April 12, 2017, 3:24 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 /* 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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 { 166 {
167 FilterEnginePtr filterEngine(new FilterEngine(jsEngine)); 167 FilterEnginePtr filterEngine(new FilterEngine(jsEngine));
168 auto sync = std::make_shared<Sync>(); 168 auto sync = std::make_shared<Sync>();
169 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback; 169 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback;
170 if (isConnectionAllowedCallback) 170 if (isConnectionAllowedCallback)
171 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool 171 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool
172 { 172 {
173 sync->Wait(); 173 sync->Wait();
174 return jsEngine->IsConnectionAllowed(); 174 return jsEngine->IsConnectionAllowed();
175 }); 175 });
176 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowedCallback](const JsValueList& params) 176 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowedCallback](const JsConstValueList& params)
177 { 177 {
178 filterEngine->firstRun = params.size() && params[0]->AsBool(); 178 filterEngine->firstRun = params.size() && params[0]->AsBool();
179 if (isConnectionAllowedCallback) 179 if (isConnectionAllowedCallback)
180 { 180 {
181 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine; 181 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine;
182 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl lowedCallback]()->bool 182 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl lowedCallback]()->bool
183 { 183 {
184 auto filterEngine = weakFilterEngine.lock(); 184 auto filterEngine = weakFilterEngine.lock();
185 if (!filterEngine) 185 if (!filterEngine)
186 return false; 186 return false;
187 return isConnectionAllowedCallback(filterEngine->GetAllowedConnectionTyp e().get()); 187 return isConnectionAllowedCallback(filterEngine->GetAllowedConnectionTyp e().get());
188 }); 188 });
189 } 189 }
190 sync->Set(); 190 sync->Set();
191 onCreated(filterEngine); 191 onCreated(filterEngine);
192 jsEngine->RemoveEventCallback("_init"); 192 jsEngine->RemoveEventCallback("_init");
193 }); 193 });
194 194
195 // Lock the JS engine while we are loading scripts, no timeouts should fire 195 // Lock the JS engine while we are loading scripts, no timeouts should fire
196 // until we are done. 196 // until we are done.
197 const JsContext context(jsEngine); 197 const JsContext context(jsEngine);
198 198
199 // Set the preconfigured prefs 199 // Set the preconfigured prefs
200 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject(); 200 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject();
201 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin( ); 201 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin( );
202 it != params.preconfiguredPrefs.end(); it++) 202 it != params.preconfiguredPrefs.end(); it++)
203 { 203 {
204 preconfiguredPrefsObject->SetProperty(it->first, it->second); 204 preconfiguredPrefsObject->SetProperty(it->first, *it->second);
205 } 205 }
206 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject); 206 jsEngine->SetGlobalProperty("_preconfiguredPrefs", *preconfiguredPrefsObject);
207 // Load adblockplus scripts 207 // Load adblockplus scripts
208 for (int i = 0; !jsSources[i].empty(); i += 2) 208 for (int i = 0; !jsSources[i].empty(); i += 2)
209 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); 209 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]);
210 } 210 }
211 211
212 FilterEnginePtr FilterEngine::Create(const JsEnginePtr& jsEngine, 212 FilterEnginePtr FilterEngine::Create(const JsEnginePtr& jsEngine,
213 const FilterEngine::CreationParameters& params) 213 const FilterEngine::CreationParameters& params)
214 { 214 {
215 FilterEnginePtr retValue; 215 FilterEnginePtr retValue;
216 Sync sync; 216 Sync sync;
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 std::bind(&FilterEngine::UpdateAvailable, this, callback, 455 std::bind(&FilterEngine::UpdateAvailable, this, callback,
456 std::placeholders::_1)); 456 std::placeholders::_1));
457 } 457 }
458 458
459 void FilterEngine::RemoveUpdateAvailableCallback() 459 void FilterEngine::RemoveUpdateAvailableCallback()
460 { 460 {
461 jsEngine->RemoveEventCallback("updateAvailable"); 461 jsEngine->RemoveEventCallback("updateAvailable");
462 } 462 }
463 463
464 void FilterEngine::UpdateAvailable( 464 void FilterEngine::UpdateAvailable(
465 const FilterEngine::UpdateAvailableCallback& callback, const JsValueList& pa rams) const 465 const FilterEngine::UpdateAvailableCallback& callback, const JsConstValueLis t& params) const
466 { 466 {
467 if (params.size() >= 1 && !params[0]->IsNull()) 467 if (params.size() >= 1 && !params[0]->IsNull())
468 callback(params[0]->AsString()); 468 callback(params[0]->AsString());
469 } 469 }
470 470
471 void FilterEngine::ForceUpdateCheck( 471 void FilterEngine::ForceUpdateCheck(
472 const FilterEngine::UpdateCheckDoneCallback& callback) 472 const FilterEngine::UpdateCheckDoneCallback& callback)
473 { 473 {
474 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); 474 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck");
475 JsConstValueList params; 475 JsConstValueList params;
476 if (callback) 476 if (callback)
477 { 477 {
478 std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId) ; 478 std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId) ;
479 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDo ne, 479 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDo ne,
480 this, eventName, callback, std::placeholders::_1)); 480 this, eventName, callback, std::placeholders::_1));
481 params.push_back(jsEngine->NewValue(eventName)); 481 params.push_back(jsEngine->NewValue(eventName));
482 } 482 }
483 func->Call(params); 483 func->Call(params);
484 } 484 }
485 485
486 void FilterEngine::UpdateCheckDone(const std::string& eventName, 486 void FilterEngine::UpdateCheckDone(const std::string& eventName,
487 const FilterEngine::UpdateCheckDoneCallback& callback, const JsValueList& pa rams) 487 const FilterEngine::UpdateCheckDoneCallback& callback, const JsConstValueLis t& params)
488 { 488 {
489 jsEngine->RemoveEventCallback(eventName); 489 jsEngine->RemoveEventCallback(eventName);
490 490
491 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt ring() : ""); 491 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt ring() : "");
492 callback(error); 492 callback(error);
493 } 493 }
494 494
495 void FilterEngine::SetFilterChangeCallback(const FilterEngine::FilterChangeCallb ack& callback) 495 void FilterEngine::SetFilterChangeCallback(const FilterEngine::FilterChangeCallb ack& callback)
496 { 496 {
497 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged, 497 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged,
(...skipping 11 matching lines...) Expand all
509 } 509 }
510 510
511 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() const 511 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() const
512 { 512 {
513 auto prefValue = GetPref("allowed_connection_type"); 513 auto prefValue = GetPref("allowed_connection_type");
514 if (prefValue->AsString().empty()) 514 if (prefValue->AsString().empty())
515 return nullptr; 515 return nullptr;
516 return std::unique_ptr<std::string>(new std::string(prefValue->AsString())); 516 return std::unique_ptr<std::string>(new std::string(prefValue->AsString()));
517 } 517 }
518 518
519 void FilterEngine::FilterChanged(const FilterEngine::FilterChangeCallback& callb ack, const JsValueList& params) const 519 void FilterEngine::FilterChanged(const FilterEngine::FilterChangeCallback& callb ack, const JsConstValueList& params) const
520 { 520 {
521 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : ""); 521 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : "");
522 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); 522 JsConstValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false ));
523 callback(action, *item); 523 callback(action, *item);
524 } 524 }
525 525
526 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, 526 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback,
527 const JsValueList& params) const 527 const JsConstValueList& params) const
528 { 528 {
529 if (params.size() < 1) 529 if (params.size() < 1)
530 return; 530 return;
531 531
532 if (!params[0]->IsObject()) 532 if (!params[0]->IsObject())
533 { 533 {
534 return; 534 return;
535 } 535 }
536 callback(NotificationPtr(new Notification(std::move(*params[0])))); 536 callback(NotificationPtr(new Notification(params[0]->Clone())));
hub 2017/04/12 15:28:20 This is the tricky part. Before that patch we rece
537 } 537 }
538 538
539 539
540 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const 540 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const
541 { 541 {
542 JsConstValueList params; 542 JsConstValueList params;
543 params.push_back(jsEngine->NewValue(v1)); 543 params.push_back(jsEngine->NewValue(v1));
544 params.push_back(jsEngine->NewValue(v2)); 544 params.push_back(jsEngine->NewValue(v2));
545 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); 545 JsValuePtr func = jsEngine->Evaluate("API.compareVersions");
546 return func->Call(params).AsInt(); 546 return func->Call(params).AsInt();
(...skipping 27 matching lines...) Expand all
574 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); 574 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url);
575 if (filter) 575 if (filter)
576 { 576 {
577 return filter; 577 return filter;
578 } 578 }
579 currentUrl = parentUrl; 579 currentUrl = parentUrl;
580 } 580 }
581 while (urlIterator != documentUrls.end()); 581 while (urlIterator != documentUrls.end());
582 return FilterPtr(); 582 return FilterPtr();
583 } 583 }
OLDNEW

Powered by Google App Engine
This is Rietveld