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

Delta Between Two Patch Sets: src/FilterEngine.cpp

Issue 29377570: Issue 4931 - add possibility to not send data depending on connection properties (Closed)
Left Patch Set: Created Feb. 28, 2017, 10:21 p.m.
Right Patch Set: rebase Created March 16, 2017, 4:02 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/prefs.js ('k') | src/JsEngine.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 }; 167 };
168 } 168 }
169 169
170 FilterEngine::FilterEngine(const JsEnginePtr& jsEngine) 170 FilterEngine::FilterEngine(const JsEnginePtr& jsEngine)
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::CreationParameters& 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 auto isConnectionAllowed = params.isConnectionAllowed; 181 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback;
Oleksandr 2017/03/16 14:19:53 The naming here is highly confusing, IMO. How abou
sergei 2017/03/16 16:25:22 No objections, fixed.
182 if (isConnectionAllowed) 182 if (isConnectionAllowedCallback)
183 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool 183 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool
184 { 184 {
185 sync->Wait(); 185 sync->Wait();
186 return jsEngine->IsConnectionAllowed(); 186 return jsEngine->IsConnectionAllowed();
187 }); 187 });
188 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowed](JsValueList& params) 188 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowedCallback](JsValueList& params)
189 { 189 {
190 filterEngine->firstRun = params.size() && params[0]->AsBool(); 190 filterEngine->firstRun = params.size() && params[0]->AsBool();
191 if (isConnectionAllowed) 191 if (isConnectionAllowedCallback)
192 { 192 {
193 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine; 193 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine;
194 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl lowed]()->bool 194 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl lowedCallback]()->bool
195 { 195 {
196 auto filterEngine = weakFilterEngine.lock(); 196 auto filterEngine = weakFilterEngine.lock();
197 if (!filterEngine) 197 if (!filterEngine)
198 return false; 198 return false;
199 return isConnectionAllowed(filterEngine->GetAllowedConnectionType().get( )); 199 return isConnectionAllowedCallback(filterEngine->GetAllowedConnectionTyp e().get());
200 }); 200 });
201 } 201 }
202 sync->Set(); 202 sync->Set();
203 onCreated(filterEngine); 203 onCreated(filterEngine);
204 jsEngine->RemoveEventCallback("_init"); 204 jsEngine->RemoveEventCallback("_init");
205 }); 205 });
206 206
207 // 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
208 // until we are done. 208 // until we are done.
209 const JsContext context(jsEngine); 209 const JsContext context(jsEngine);
210 210
211 // Set the preconfigured prefs 211 // Set the preconfigured prefs
212 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject(); 212 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject();
213 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin( ); 213 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin( );
214 it != params.preconfiguredPrefs.end(); it++) 214 it != params.preconfiguredPrefs.end(); it++)
215 { 215 {
216 preconfiguredPrefsObject->SetProperty(it->first, it->second); 216 preconfiguredPrefsObject->SetProperty(it->first, it->second);
217 } 217 }
218 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject); 218 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject);
219 // Load adblockplus scripts 219 // Load adblockplus scripts
220 for (int i = 0; !jsSources[i].empty(); i += 2) 220 for (int i = 0; !jsSources[i].empty(); i += 2)
221 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); 221 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]);
222 } 222 }
223 223
224 FilterEnginePtr FilterEngine::Create(const JsEnginePtr& jsEngine, 224 FilterEnginePtr FilterEngine::Create(const JsEnginePtr& jsEngine,
225 const FilterEngine::CreateParameters& params) 225 const FilterEngine::CreationParameters& params)
226 { 226 {
227 FilterEnginePtr retValue; 227 FilterEnginePtr retValue;
228 Sync sync; 228 Sync sync;
229 CreateAsync(jsEngine, [&retValue, &sync](const FilterEnginePtr& filterEngine) 229 CreateAsync(jsEngine, [&retValue, &sync](const FilterEnginePtr& filterEngine)
230 { 230 {
231 retValue = filterEngine; 231 retValue = filterEngine;
232 sync.Set(); 232 sync.Set();
233 }, params); 233 }, params);
234 sync.Wait(); 234 sync.Wait();
235 return retValue; 235 return retValue;
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 JsValueList params; 438 JsValueList params;
439 params.push_back(jsEngine->NewValue(pref)); 439 params.push_back(jsEngine->NewValue(pref));
440 return func->Call(params); 440 return func->Call(params);
441 } 441 }
442 442
443 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) 443 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value)
444 { 444 {
445 JsValuePtr func = jsEngine->Evaluate("API.setPref"); 445 JsValuePtr func = jsEngine->Evaluate("API.setPref");
446 JsValueList params; 446 JsValueList params;
447 params.push_back(jsEngine->NewValue(pref)); 447 params.push_back(jsEngine->NewValue(pref));
448 if (value) 448 if (value)
Oleksandr 2017/03/16 14:19:53 Nit: this doesn't look related, is it?
sergei 2017/03/16 16:25:22 It's related, now we can call SetPref(nullptr) and
449 params.push_back(value); 449 params.push_back(value);
450 func->Call(params); 450 func->Call(params);
451 } 451 }
452 452
453 std::string FilterEngine::GetHostFromURL(const std::string& url) 453 std::string FilterEngine::GetHostFromURL(const std::string& url)
454 { 454 {
455 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); 455 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl");
456 JsValueList params; 456 JsValueList params;
457 params.push_back(jsEngine->NewValue(url)); 457 params.push_back(jsEngine->NewValue(url));
458 return func->Call(params)->AsString(); 458 return func->Call(params)->AsString();
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); 584 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url);
585 if (filter) 585 if (filter)
586 { 586 {
587 return filter; 587 return filter;
588 } 588 }
589 currentUrl = parentUrl; 589 currentUrl = parentUrl;
590 } 590 }
591 while (urlIterator != documentUrls.end()); 591 while (urlIterator != documentUrls.end());
592 return FilterPtr(); 592 return FilterPtr();
593 } 593 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld