LEFT | RIGHT |
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 Loading... |
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; |
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 auto prefValue = filterEngine->GetAllowedConnectionType(); | 199 return isConnectionAllowedCallback(filterEngine->GetAllowedConnectionTyp
e().get()); |
200 if (prefValue->IsUndefined()) | |
201 return isConnectionAllowed(nullptr); | |
202 auto allowedConnectionType = prefValue->AsString(); | |
203 return isConnectionAllowed(&allowedConnectionType); | |
204 }); | 200 }); |
205 } | 201 } |
206 sync->Set(); | 202 sync->Set(); |
207 onCreated(filterEngine); | 203 onCreated(filterEngine); |
208 jsEngine->RemoveEventCallback("_init"); | 204 jsEngine->RemoveEventCallback("_init"); |
209 }); | 205 }); |
210 | 206 |
211 // 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 |
212 // until we are done. | 208 // until we are done. |
213 const JsContext context(jsEngine); | 209 const JsContext context(jsEngine); |
214 | 210 |
215 // Set the preconfigured prefs | 211 // Set the preconfigured prefs |
216 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject(); | 212 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject(); |
217 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin(
); | 213 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin(
); |
218 it != params.preconfiguredPrefs.end(); it++) | 214 it != params.preconfiguredPrefs.end(); it++) |
219 { | 215 { |
220 preconfiguredPrefsObject->SetProperty(it->first, it->second); | 216 preconfiguredPrefsObject->SetProperty(it->first, it->second); |
221 } | 217 } |
222 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject); | 218 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject); |
223 // Load adblockplus scripts | 219 // Load adblockplus scripts |
224 for (int i = 0; !jsSources[i].empty(); i += 2) | 220 for (int i = 0; !jsSources[i].empty(); i += 2) |
225 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); | 221 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); |
226 } | 222 } |
227 | 223 |
228 FilterEnginePtr FilterEngine::Create(const JsEnginePtr& jsEngine, | 224 FilterEnginePtr FilterEngine::Create(const JsEnginePtr& jsEngine, |
229 const FilterEngine::CreateParameters& params) | 225 const FilterEngine::CreationParameters& params) |
230 { | 226 { |
231 FilterEnginePtr retValue; | 227 FilterEnginePtr retValue; |
232 Sync sync; | 228 Sync sync; |
233 CreateAsync(jsEngine, [&retValue, &sync](const FilterEnginePtr& filterEngine) | 229 CreateAsync(jsEngine, [&retValue, &sync](const FilterEnginePtr& filterEngine) |
234 { | 230 { |
235 retValue = filterEngine; | 231 retValue = filterEngine; |
236 sync.Set(); | 232 sync.Set(); |
237 }, params); | 233 }, params); |
238 sync.Wait(); | 234 sync.Wait(); |
239 return retValue; | 235 return retValue; |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 void FilterEngine::RemoveFilterChangeCallback() | 511 void FilterEngine::RemoveFilterChangeCallback() |
516 { | 512 { |
517 jsEngine->RemoveEventCallback("filterChange"); | 513 jsEngine->RemoveEventCallback("filterChange"); |
518 } | 514 } |
519 | 515 |
520 void FilterEngine::SetAllowedConnectionType(const std::string* value) | 516 void FilterEngine::SetAllowedConnectionType(const std::string* value) |
521 { | 517 { |
522 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : nullpt
r); | 518 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : nullpt
r); |
523 } | 519 } |
524 | 520 |
525 JsValuePtr FilterEngine::GetAllowedConnectionType() | 521 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() |
526 { | 522 { |
527 return GetPref("allowed_connection_type"); | 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())); |
528 } | 527 } |
529 | 528 |
530 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js
ValueList& params) | 529 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js
ValueList& params) |
531 { | 530 { |
532 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() : ""); |
533 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); | 532 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); |
534 callback(action, item); | 533 callback(action, item); |
535 } | 534 } |
536 | 535 |
537 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, | 536 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent
Url); | 584 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent
Url); |
586 if (filter) | 585 if (filter) |
587 { | 586 { |
588 return filter; | 587 return filter; |
589 } | 588 } |
590 currentUrl = parentUrl; | 589 currentUrl = parentUrl; |
591 } | 590 } |
592 while (urlIterator != documentUrls.end()); | 591 while (urlIterator != documentUrls.end()); |
593 return FilterPtr(); | 592 return FilterPtr(); |
594 } | 593 } |
LEFT | RIGHT |