| 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-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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include <algorithm> | 18 #include <algorithm> |
| 19 #include <cctype> | 19 #include <cctype> |
| 20 #include <functional> | 20 #include <functional> |
| 21 #include <string> | 21 #include <string> |
| 22 #include <cassert> |
| 23 #include <thread> |
| 22 | 24 |
| 23 #include <AdblockPlus.h> | 25 #include <AdblockPlus.h> |
| 24 #include "JsContext.h" | 26 #include "JsContext.h" |
| 25 #include "Thread.h" | 27 #include "Thread.h" |
| 26 #include <mutex> | 28 #include <mutex> |
| 27 #include <condition_variable> | 29 #include <condition_variable> |
| 28 | 30 |
| 29 using namespace AdblockPlus; | 31 using namespace AdblockPlus; |
| 30 | 32 |
| 31 extern std::string jsSources[]; | 33 extern std::string jsSources[]; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 FilterEngine::FilterEngine(const JsEnginePtr& jsEngine) | 214 FilterEngine::FilterEngine(const JsEnginePtr& jsEngine) |
| 213 : jsEngine(jsEngine), firstRun(false), updateCheckId(0) | 215 : jsEngine(jsEngine), firstRun(false), updateCheckId(0) |
| 214 { | 216 { |
| 215 } | 217 } |
| 216 | 218 |
| 217 void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine, | 219 void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine, |
| 218 const FilterEngine::OnCreatedCallback& onCreated, | 220 const FilterEngine::OnCreatedCallback& onCreated, |
| 219 const FilterEngine::CreationParameters& params) | 221 const FilterEngine::CreationParameters& params) |
| 220 { | 222 { |
| 221 FilterEnginePtr filterEngine(new FilterEngine(jsEngine)); | 223 FilterEnginePtr filterEngine(new FilterEngine(jsEngine)); |
| 222 auto sync = std::make_shared<Sync>(); | |
| 223 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback; | |
| 224 if (isConnectionAllowedCallback) | |
| 225 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool | |
| 226 { | |
| 227 sync->Wait(); | |
| 228 return jsEngine->IsConnectionAllowed(); | |
| 229 }); | |
| 230 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync,
isConnectionAllowedCallback](JsValueList&& params) | |
| 231 { | 224 { |
| 232 filterEngine->firstRun = params.size() && params[0].AsBool(); | 225 // TODO: replace weakFilterEngine by this when it's possible to control the |
| 233 if (isConnectionAllowedCallback) | 226 // execution time of the asynchronous part below. |
| 234 { | 227 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine; |
| 235 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine; | 228 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback; |
| 236 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl
lowedCallback]()->bool | 229 jsEngine->SetEventCallback("_isSubscriptionDownloadAllowed", [weakFilterEngi
ne, isConnectionAllowedCallback](JsValueList&& params){ |
| 230 auto filterEngine = weakFilterEngine.lock(); |
| 231 if (!filterEngine) |
| 232 return; |
| 233 auto jsEngine = filterEngine->GetJsEngine(); |
| 234 |
| 235 // param[0] - nullable string Prefs.allowed_connection_type |
| 236 // param[1] - function(Boolean) |
| 237 bool areArgumentsValid = params.size() == 2 && (params[0].IsNull() || para
ms[0].IsString()) && params[1].IsFunction(); |
| 238 assert(areArgumentsValid && "Invalid argument: there should be two args an
d the second one should be a function"); |
| 239 if (!areArgumentsValid) |
| 240 return; |
| 241 if (!isConnectionAllowedCallback) |
| 242 { |
| 243 params[1].Call(jsEngine->NewValue(true)); |
| 244 return; |
| 245 } |
| 246 auto valuesID = jsEngine->StoreJsValues(params); |
| 247 auto callJsCallback = [weakFilterEngine, valuesID](bool isAllowed) |
| 237 { | 248 { |
| 238 auto filterEngine = weakFilterEngine.lock(); | 249 auto filterEngine = weakFilterEngine.lock(); |
| 239 if (!filterEngine) | 250 if (!filterEngine) |
| 240 return false; | 251 return; |
| 241 return isConnectionAllowedCallback(filterEngine->GetAllowedConnectionTyp
e().get()); | 252 auto jsEngine = filterEngine->GetJsEngine(); |
| 242 }); | 253 auto jsParams = jsEngine->TakeJsValues(valuesID); |
| 243 } | 254 jsParams[1].Call(jsEngine->NewValue(isAllowed)); |
| 244 sync->Set(); | 255 }; |
| 256 std::shared_ptr<std::string> allowedConnectionType = params[0].IsString()
? std::make_shared<std::string>(params[0].AsString()) : nullptr; |
| 257 // temporary hack with thread: |
| 258 std::thread([isConnectionAllowedCallback, allowedConnectionType, callJsCal
lback] |
| 259 { |
| 260 callJsCallback(isConnectionAllowedCallback(allowedConnectionType.get()))
; |
| 261 }).detach(); |
| 262 }); |
| 263 } |
| 264 |
| 265 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated](JsValu
eList&& params) |
| 266 { |
| 267 filterEngine->firstRun = params.size() && params[0].AsBool(); |
| 245 onCreated(filterEngine); | 268 onCreated(filterEngine); |
| 246 jsEngine->RemoveEventCallback("_init"); | 269 jsEngine->RemoveEventCallback("_init"); |
| 247 }); | 270 }); |
| 248 | 271 |
| 249 // Lock the JS engine while we are loading scripts, no timeouts should fire | 272 // Lock the JS engine while we are loading scripts, no timeouts should fire |
| 250 // until we are done. | 273 // until we are done. |
| 251 const JsContext context(*jsEngine); | 274 const JsContext context(*jsEngine); |
| 252 | |
| 253 // Set the preconfigured prefs | 275 // Set the preconfigured prefs |
| 254 auto preconfiguredPrefsObject = jsEngine->NewObject(); | 276 auto preconfiguredPrefsObject = jsEngine->NewObject(); |
| 255 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin(
); | 277 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin(
); |
| 256 it != params.preconfiguredPrefs.end(); it++) | 278 it != params.preconfiguredPrefs.end(); it++) |
| 257 { | 279 { |
| 258 preconfiguredPrefsObject.SetProperty(it->first, it->second); | 280 preconfiguredPrefsObject.SetProperty(it->first, it->second); |
| 259 } | 281 } |
| 260 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject); | 282 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject); |
| 261 // Load adblockplus scripts | 283 // Load adblockplus scripts |
| 262 for (int i = 0; !jsSources[i].empty(); i += 2) | 284 for (int i = 0; !jsSources[i].empty(); i += 2) |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent
Url); | 631 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent
Url); |
| 610 if (filter) | 632 if (filter) |
| 611 { | 633 { |
| 612 return filter; | 634 return filter; |
| 613 } | 635 } |
| 614 currentUrl = parentUrl; | 636 currentUrl = parentUrl; |
| 615 } | 637 } |
| 616 while (urlIterator != documentUrls.end()); | 638 while (urlIterator != documentUrls.end()); |
| 617 return FilterPtr(); | 639 return FilterPtr(); |
| 618 } | 640 } |
| OLD | NEW |