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

Side by Side Diff: src/FilterEngine.cpp

Issue 29377570: Issue 4931 - add possibility to not send data depending on connection properties (Closed)
Patch Set: address comment Created March 16, 2017, 2:38 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 | « lib/prefs.js ('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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 : jsEngine(jsEngine), firstRun(false), updateCheckId(0) 171 : jsEngine(jsEngine), firstRun(false), updateCheckId(0)
172 { 172 {
173 } 173 }
174 174
175 void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine, 175 void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine,
176 const FilterEngine::OnCreatedCallback& onCreated, 176 const FilterEngine::OnCreatedCallback& onCreated,
177 const FilterEngine::CreateParameters& params) 177 const FilterEngine::CreateParameters& params)
178 { 178 {
179 FilterEnginePtr filterEngine(new FilterEngine(jsEngine)); 179 FilterEnginePtr filterEngine(new FilterEngine(jsEngine));
180 auto sync = std::make_shared<Sync>(); 180 auto sync = std::make_shared<Sync>();
181 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync]( JsValueList& params) 181 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback;
182 if (isConnectionAllowedCallback)
183 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool
184 {
185 sync->Wait();
186 return jsEngine->IsConnectionAllowed();
187 });
188 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowedCallback](JsValueList& params)
182 { 189 {
183 filterEngine->firstRun = params.size() && params[0]->AsBool(); 190 filterEngine->firstRun = params.size() && params[0]->AsBool();
191 if (isConnectionAllowedCallback)
192 {
193 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine;
194 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl lowedCallback]()->bool
195 {
196 auto filterEngine = weakFilterEngine.lock();
197 if (!filterEngine)
198 return false;
199 return isConnectionAllowedCallback(filterEngine->GetAllowedConnectionTyp e().get());
200 });
201 }
184 sync->Set(); 202 sync->Set();
185 onCreated(filterEngine); 203 onCreated(filterEngine);
186 jsEngine->RemoveEventCallback("_init"); 204 jsEngine->RemoveEventCallback("_init");
187 }); 205 });
188 206
189 // Lock the JS engine while we are loading scripts, no timeouts should fire 207 // Lock the JS engine while we are loading scripts, no timeouts should fire
190 // until we are done. 208 // until we are done.
191 const JsContext context(jsEngine); 209 const JsContext context(jsEngine);
192 210
193 // Set the preconfigured prefs 211 // Set the preconfigured prefs
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 JsValueList params; 438 JsValueList params;
421 params.push_back(jsEngine->NewValue(pref)); 439 params.push_back(jsEngine->NewValue(pref));
422 return func->Call(params); 440 return func->Call(params);
423 } 441 }
424 442
425 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) 443 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value)
426 { 444 {
427 JsValuePtr func = jsEngine->Evaluate("API.setPref"); 445 JsValuePtr func = jsEngine->Evaluate("API.setPref");
428 JsValueList params; 446 JsValueList params;
429 params.push_back(jsEngine->NewValue(pref)); 447 params.push_back(jsEngine->NewValue(pref));
430 params.push_back(value); 448 if (value)
449 params.push_back(value);
431 func->Call(params); 450 func->Call(params);
432 } 451 }
433 452
434 std::string FilterEngine::GetHostFromURL(const std::string& url) 453 std::string FilterEngine::GetHostFromURL(const std::string& url)
435 { 454 {
436 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); 455 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl");
437 JsValueList params; 456 JsValueList params;
438 params.push_back(jsEngine->NewValue(url)); 457 params.push_back(jsEngine->NewValue(url));
439 return func->Call(params)->AsString(); 458 return func->Call(params)->AsString();
440 } 459 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 { 506 {
488 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged, 507 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged,
489 this, callback, std::placeholders::_1)); 508 this, callback, std::placeholders::_1));
490 } 509 }
491 510
492 void FilterEngine::RemoveFilterChangeCallback() 511 void FilterEngine::RemoveFilterChangeCallback()
493 { 512 {
494 jsEngine->RemoveEventCallback("filterChange"); 513 jsEngine->RemoveEventCallback("filterChange");
495 } 514 }
496 515
516 void FilterEngine::SetAllowedConnectionType(const std::string* value)
517 {
518 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : nullpt r);
519 }
520
521 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType()
522 {
523 auto prefValue = GetPref("allowed_connection_type");
524 if (prefValue->IsUndefined())
525 return nullptr;
526 return std::unique_ptr<std::string>(new std::string(prefValue->AsString()));
527 }
528
497 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params) 529 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params)
498 { 530 {
499 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : ""); 531 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : "");
500 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); 532 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false));
501 callback(action, item); 533 callback(action, item);
502 } 534 }
503 535
504 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, 536 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback,
505 const JsValueList& params) 537 const JsValueList& params)
506 { 538 {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); 584 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url);
553 if (filter) 585 if (filter)
554 { 586 {
555 return filter; 587 return filter;
556 } 588 }
557 currentUrl = parentUrl; 589 currentUrl = parentUrl;
558 } 590 }
559 while (urlIterator != documentUrls.end()); 591 while (urlIterator != documentUrls.end());
560 return FilterPtr(); 592 return FilterPtr();
561 } 593 }
OLDNEW
« no previous file with comments | « lib/prefs.js ('k') | src/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld