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 |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 | 218 |
219 void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine, | 219 void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine, |
220 const FilterEngine::OnCreatedCallback& onCreated, | 220 const FilterEngine::OnCreatedCallback& onCreated, |
221 const FilterEngine::CreationParameters& params) | 221 const FilterEngine::CreationParameters& params) |
222 { | 222 { |
223 FilterEnginePtr filterEngine(new FilterEngine(jsEngine)); | 223 FilterEnginePtr filterEngine(new FilterEngine(jsEngine)); |
224 { | 224 { |
225 // TODO: replace weakFilterEngine by this when it's possible to control the | 225 // TODO: replace weakFilterEngine by this when it's possible to control the |
226 // execution time of the asynchronous part below. | 226 // execution time of the asynchronous part below. |
227 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine; | 227 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine; |
228 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback; | 228 auto isSubscriptionDowloadAllowedCallback = params.isSubscriptionDowloadAllo
wedCallback; |
229 jsEngine->SetEventCallback("_isSubscriptionDownloadAllowed", [weakFilterEngi
ne, isConnectionAllowedCallback](JsValueList&& params){ | 229 jsEngine->SetEventCallback("_isSubscriptionDownloadAllowed", [weakFilterEngi
ne, isSubscriptionDowloadAllowedCallback](JsValueList&& params){ |
230 auto filterEngine = weakFilterEngine.lock(); | 230 auto filterEngine = weakFilterEngine.lock(); |
231 if (!filterEngine) | 231 if (!filterEngine) |
232 return; | 232 return; |
233 auto jsEngine = filterEngine->GetJsEngine(); | 233 auto jsEngine = filterEngine->GetJsEngine(); |
234 | 234 |
235 // param[0] - nullable string Prefs.allowed_connection_type | 235 // param[0] - nullable string Prefs.allowed_connection_type |
236 // param[1] - function(Boolean) | 236 // param[1] - function(Boolean) |
237 bool areArgumentsValid = params.size() == 2 && (params[0].IsNull() || para
ms[0].IsString()) && params[1].IsFunction(); | 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"); | 238 assert(areArgumentsValid && "Invalid argument: there should be two args an
d the second one should be a function"); |
239 if (!areArgumentsValid) | 239 if (!areArgumentsValid) |
240 return; | 240 return; |
241 if (!isConnectionAllowedCallback) | 241 if (!isSubscriptionDowloadAllowedCallback) |
242 { | 242 { |
243 params[1].Call(jsEngine->NewValue(true)); | 243 params[1].Call(jsEngine->NewValue(true)); |
244 return; | 244 return; |
245 } | 245 } |
246 auto valuesID = jsEngine->StoreJsValues(params); | 246 auto valuesID = jsEngine->StoreJsValues(params); |
247 auto callJsCallback = [weakFilterEngine, valuesID](bool isAllowed) | 247 auto callJsCallback = [weakFilterEngine, valuesID](bool isAllowed) |
248 { | 248 { |
249 auto filterEngine = weakFilterEngine.lock(); | 249 auto filterEngine = weakFilterEngine.lock(); |
250 if (!filterEngine) | 250 if (!filterEngine) |
251 return; | 251 return; |
252 auto jsEngine = filterEngine->GetJsEngine(); | 252 auto jsEngine = filterEngine->GetJsEngine(); |
253 auto jsParams = jsEngine->TakeJsValues(valuesID); | 253 auto jsParams = jsEngine->TakeJsValues(valuesID); |
254 jsParams[1].Call(jsEngine->NewValue(isAllowed)); | 254 jsParams[1].Call(jsEngine->NewValue(isAllowed)); |
255 }; | 255 }; |
256 std::shared_ptr<std::string> allowedConnectionType = params[0].IsString()
? std::make_shared<std::string>(params[0].AsString()) : nullptr; | 256 std::string allowedConnectionType = params[0].IsString() ? params[0].AsStr
ing() : std::string(); |
257 // temporary hack with thread: | 257 isSubscriptionDowloadAllowedCallback(params[0].IsString() ? &allowedConnec
tionType : nullptr, callJsCallback); |
258 std::thread([isConnectionAllowedCallback, allowedConnectionType, callJsCal
lback] | |
259 { | |
260 callJsCallback(isConnectionAllowedCallback(allowedConnectionType.get()))
; | |
261 }).detach(); | |
262 }); | 258 }); |
263 } | 259 } |
264 | 260 |
265 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated](JsValu
eList&& params) | 261 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated](JsValu
eList&& params) |
266 { | 262 { |
267 filterEngine->firstRun = params.size() && params[0].AsBool(); | 263 filterEngine->firstRun = params.size() && params[0].AsBool(); |
268 onCreated(filterEngine); | 264 onCreated(filterEngine); |
269 jsEngine->RemoveEventCallback("_init"); | 265 jsEngine->RemoveEventCallback("_init"); |
270 }); | 266 }); |
271 | 267 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent
Url); | 626 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent
Url); |
631 if (filter) | 627 if (filter) |
632 { | 628 { |
633 return filter; | 629 return filter; |
634 } | 630 } |
635 currentUrl = parentUrl; | 631 currentUrl = parentUrl; |
636 } | 632 } |
637 while (urlIterator != documentUrls.end()); | 633 while (urlIterator != documentUrls.end()); |
638 return FilterPtr(); | 634 return FilterPtr(); |
639 } | 635 } |
OLD | NEW |