OLD | NEW |
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 Loading... |
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 isConnectionAllowed = params.isConnectionAllowed; |
| 182 if (isConnectionAllowed) |
| 183 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool |
| 184 { |
| 185 sync->Wait(); |
| 186 return jsEngine->IsConnectionAllowed(); |
| 187 }); |
| 188 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync,
isConnectionAllowed](JsValueList& params) |
182 { | 189 { |
183 filterEngine->firstRun = params.size() && params[0]->AsBool(); | 190 filterEngine->firstRun = params.size() && params[0]->AsBool(); |
| 191 if (isConnectionAllowed) |
| 192 { |
| 193 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine; |
| 194 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl
lowed]()->bool |
| 195 { |
| 196 auto filterEngine = weakFilterEngine.lock(); |
| 197 if (!filterEngine) |
| 198 return false; |
| 199 auto prefValue = filterEngine->GetAllowedConnectionType(); |
| 200 if (prefValue->IsUndefined()) |
| 201 return isConnectionAllowed(nullptr); |
| 202 auto allowedConnectionType = prefValue->AsString(); |
| 203 return isConnectionAllowed(&allowedConnectionType); |
| 204 }); |
| 205 } |
184 sync->Set(); | 206 sync->Set(); |
185 onCreated(filterEngine); | 207 onCreated(filterEngine); |
186 jsEngine->RemoveEventCallback("_init"); | 208 jsEngine->RemoveEventCallback("_init"); |
187 }); | 209 }); |
188 | 210 |
189 // Lock the JS engine while we are loading scripts, no timeouts should fire | 211 // Lock the JS engine while we are loading scripts, no timeouts should fire |
190 // until we are done. | 212 // until we are done. |
191 const JsContext context(jsEngine); | 213 const JsContext context(jsEngine); |
192 | 214 |
193 // Set the preconfigured prefs | 215 // Set the preconfigured prefs |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 JsValueList params; | 442 JsValueList params; |
421 params.push_back(jsEngine->NewValue(pref)); | 443 params.push_back(jsEngine->NewValue(pref)); |
422 return func->Call(params); | 444 return func->Call(params); |
423 } | 445 } |
424 | 446 |
425 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) | 447 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) |
426 { | 448 { |
427 JsValuePtr func = jsEngine->Evaluate("API.setPref"); | 449 JsValuePtr func = jsEngine->Evaluate("API.setPref"); |
428 JsValueList params; | 450 JsValueList params; |
429 params.push_back(jsEngine->NewValue(pref)); | 451 params.push_back(jsEngine->NewValue(pref)); |
430 params.push_back(value); | 452 if (value) |
| 453 params.push_back(value); |
431 func->Call(params); | 454 func->Call(params); |
432 } | 455 } |
433 | 456 |
434 std::string FilterEngine::GetHostFromURL(const std::string& url) | 457 std::string FilterEngine::GetHostFromURL(const std::string& url) |
435 { | 458 { |
436 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); | 459 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); |
437 JsValueList params; | 460 JsValueList params; |
438 params.push_back(jsEngine->NewValue(url)); | 461 params.push_back(jsEngine->NewValue(url)); |
439 return func->Call(params)->AsString(); | 462 return func->Call(params)->AsString(); |
440 } | 463 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 { | 510 { |
488 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan
ged, | 511 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan
ged, |
489 this, callback, std::placeholders::_1)); | 512 this, callback, std::placeholders::_1)); |
490 } | 513 } |
491 | 514 |
492 void FilterEngine::RemoveFilterChangeCallback() | 515 void FilterEngine::RemoveFilterChangeCallback() |
493 { | 516 { |
494 jsEngine->RemoveEventCallback("filterChange"); | 517 jsEngine->RemoveEventCallback("filterChange"); |
495 } | 518 } |
496 | 519 |
| 520 void FilterEngine::SetAllowedConnectionType(const std::string* value) |
| 521 { |
| 522 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : nullpt
r); |
| 523 } |
| 524 |
| 525 JsValuePtr FilterEngine::GetAllowedConnectionType() |
| 526 { |
| 527 return GetPref("allowed_connection_type"); |
| 528 } |
| 529 |
497 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js
ValueList& params) | 530 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js
ValueList& params) |
498 { | 531 { |
499 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS
tring() : ""); | 532 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS
tring() : ""); |
500 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); | 533 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); |
501 callback(action, item); | 534 callback(action, item); |
502 } | 535 } |
503 | 536 |
504 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, | 537 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, |
505 const JsValueList& params) | 538 const JsValueList& params) |
506 { | 539 { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent
Url); | 585 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent
Url); |
553 if (filter) | 586 if (filter) |
554 { | 587 { |
555 return filter; | 588 return filter; |
556 } | 589 } |
557 currentUrl = parentUrl; | 590 currentUrl = parentUrl; |
558 } | 591 } |
559 while (urlIterator != documentUrls.end()); | 592 while (urlIterator != documentUrls.end()); |
560 return FilterPtr(); | 593 return FilterPtr(); |
561 } | 594 } |
OLD | NEW |