Left: | ||
Right: |
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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 params.push_back(shared_from_this()); | 127 params.push_back(shared_from_this()); |
128 JsValuePtr result = func->Call(params); | 128 JsValuePtr result = func->Call(params); |
129 return result->AsBool(); | 129 return result->AsBool(); |
130 } | 130 } |
131 | 131 |
132 bool Subscription::operator==(const Subscription& subscription) const | 132 bool Subscription::operator==(const Subscription& subscription) const |
133 { | 133 { |
134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); | 134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); |
135 } | 135 } |
136 | 136 |
137 FilterEngine::FilterEngine(JsEnginePtr jsEngine) | 137 FilterEngine::FilterEngine(JsEnginePtr jsEngine, |
138 const FilterEngine::Prefs& preconfiguredPrefs) | |
138 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0) | 139 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0) |
139 { | 140 { |
140 jsEngine->SetEventCallback("init", std::tr1::bind(&FilterEngine::InitDone, | 141 jsEngine->SetEventCallback("_init", std::bind(&FilterEngine::InitDone, |
141 this, std::tr1::placeholders::_1)); | 142 this, std::placeholders::_1)); |
142 | 143 |
143 { | 144 { |
144 // Lock the JS engine while we are loading scripts, no timeouts should fire | 145 // Lock the JS engine while we are loading scripts, no timeouts should fire |
145 // until we are done. | 146 // until we are done. |
146 const JsContext context(jsEngine); | 147 const JsContext context(jsEngine); |
148 | |
149 // Set the preconfigured prefs | |
150 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject(); | |
151 for (FilterEngine::Prefs::const_iterator it = preconfiguredPrefs.begin(); | |
152 it != preconfiguredPrefs.end(); it++) | |
153 { | |
154 preconfiguredPrefsObject->SetProperty(it->first, it->second); | |
155 } | |
156 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject) ; | |
157 // Load adblockplus scripts | |
147 for (int i = 0; !jsSources[i].empty(); i += 2) | 158 for (int i = 0; !jsSources[i].empty(); i += 2) |
148 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); | 159 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); |
149 } | 160 } |
150 | 161 |
151 // TODO: This should really be implemented via a conditional variable | 162 // TODO: This should really be implemented via a conditional variable |
152 while (!initialized) | 163 while (!initialized) |
153 ::Sleep(10); | 164 ::Sleep(10); |
154 } | 165 } |
155 | 166 |
156 namespace | 167 namespace |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 it != contentTypes.end(); it++) | 205 it != contentTypes.end(); it++) |
195 { | 206 { |
196 if (it->second == contentTypeUpper) | 207 if (it->second == contentTypeUpper) |
197 return it->first; | 208 return it->first; |
198 } | 209 } |
199 throw std::invalid_argument("Cannot convert argument to ContentType"); | 210 throw std::invalid_argument("Cannot convert argument to ContentType"); |
200 } | 211 } |
201 | 212 |
202 void FilterEngine::InitDone(JsValueList& params) | 213 void FilterEngine::InitDone(JsValueList& params) |
203 { | 214 { |
204 jsEngine->RemoveEventCallback("init"); | 215 jsEngine->RemoveEventCallback("_init"); |
205 initialized = true; | 216 initialized = true; |
206 firstRun = params.size() && params[0]->AsBool(); | 217 firstRun = params.size() && params[0]->AsBool(); |
207 } | 218 } |
208 | 219 |
209 bool FilterEngine::IsFirstRun() const | 220 bool FilterEngine::IsFirstRun() const |
210 { | 221 { |
211 return firstRun; | 222 return firstRun; |
212 } | 223 } |
213 | 224 |
214 FilterPtr FilterEngine::GetFilter(const std::string& text) | 225 FilterPtr FilterEngine::GetFilter(const std::string& text) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const | 261 std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const |
251 { | 262 { |
252 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions"); | 263 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions"); |
253 JsValueList values = func->Call()->AsList(); | 264 JsValueList values = func->Call()->AsList(); |
254 std::vector<SubscriptionPtr> result; | 265 std::vector<SubscriptionPtr> result; |
255 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) | 266 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) |
256 result.push_back(SubscriptionPtr(new Subscription(*it))); | 267 result.push_back(SubscriptionPtr(new Subscription(*it))); |
257 return result; | 268 return result; |
258 } | 269 } |
259 | 270 |
260 NotificationPtr FilterEngine::GetNextNotificationToShow(const std::string& url) | 271 void FilterEngine::ShowNextNotification(const std::string& url) |
261 { | 272 { |
262 JsValuePtr func = jsEngine->Evaluate("API.getNextNotificationToShow"); | 273 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification"); |
263 JsValueList params; | 274 JsValueList params; |
264 if (!url.empty()) | 275 if (!url.empty()) |
265 { | 276 { |
266 params.push_back(jsEngine->NewValue(url)); | 277 params.push_back(jsEngine->NewValue(url)); |
267 } | 278 } |
268 auto jsNotification = func->Call(params); | 279 func->Call(params); |
Felix Dahlke
2015/01/27 13:09:09
C++03! :)
sergei
2015/01/27 13:20:12
fixed, sorry
| |
269 if (!jsNotification->IsObject()) | 280 } |
270 { | 281 |
271 return NotificationPtr(); | 282 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue) |
272 } | 283 { |
273 return NotificationPtr(new Notification(jsNotification)); | 284 if (!value) |
285 return; | |
286 | |
287 jsEngine->SetEventCallback("_showNotification", | |
288 std::bind(&FilterEngine::ShowNotification, this, value, | |
289 std::placeholders::_1)); | |
290 } | |
291 | |
292 void FilterEngine::RemoveShowNotificationCallback() | |
293 { | |
294 jsEngine->RemoveEventCallback("_showNotification"); | |
274 } | 295 } |
275 | 296 |
276 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, | 297 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
277 ContentType contentType, | 298 ContentType contentType, |
278 const std::string& documentUrl) const | 299 const std::string& documentUrl) const |
279 { | 300 { |
280 std::vector<std::string> documentUrls; | 301 std::vector<std::string> documentUrls; |
281 documentUrls.push_back(documentUrl); | 302 documentUrls.push_back(documentUrl); |
282 return Matches(url, contentType, documentUrls); | 303 return Matches(url, contentType, documentUrls); |
283 } | 304 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
354 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); | 375 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); |
355 JsValueList params; | 376 JsValueList params; |
356 params.push_back(jsEngine->NewValue(url)); | 377 params.push_back(jsEngine->NewValue(url)); |
357 return func->Call(params)->AsString(); | 378 return func->Call(params)->AsString(); |
358 } | 379 } |
359 | 380 |
360 void FilterEngine::SetUpdateAvailableCallback( | 381 void FilterEngine::SetUpdateAvailableCallback( |
361 FilterEngine::UpdateAvailableCallback callback) | 382 FilterEngine::UpdateAvailableCallback callback) |
362 { | 383 { |
363 jsEngine->SetEventCallback("updateAvailable", | 384 jsEngine->SetEventCallback("updateAvailable", |
364 std::tr1::bind(&FilterEngine::UpdateAvailable, this, callback, | 385 std::bind(&FilterEngine::UpdateAvailable, this, callback, |
365 std::tr1::placeholders::_1)); | 386 std::placeholders::_1)); |
366 } | 387 } |
367 | 388 |
368 void FilterEngine::RemoveUpdateAvailableCallback() | 389 void FilterEngine::RemoveUpdateAvailableCallback() |
369 { | 390 { |
370 jsEngine->RemoveEventCallback("updateAvailable"); | 391 jsEngine->RemoveEventCallback("updateAvailable"); |
371 } | 392 } |
372 | 393 |
373 void FilterEngine::UpdateAvailable( | 394 void FilterEngine::UpdateAvailable( |
374 FilterEngine::UpdateAvailableCallback callback, JsValueList& params) | 395 FilterEngine::UpdateAvailableCallback callback, JsValueList& params) |
375 { | 396 { |
376 if (params.size() >= 1 && !params[0]->IsNull()) | 397 if (params.size() >= 1 && !params[0]->IsNull()) |
377 callback(params[0]->AsString()); | 398 callback(params[0]->AsString()); |
378 } | 399 } |
379 | 400 |
380 void FilterEngine::ForceUpdateCheck( | 401 void FilterEngine::ForceUpdateCheck( |
381 FilterEngine::UpdateCheckDoneCallback callback) | 402 FilterEngine::UpdateCheckDoneCallback callback) |
382 { | 403 { |
383 std::string eventName = "updateCheckDone"; | 404 std::string eventName = "_updateCheckDone"; |
384 eventName += ++updateCheckId; | 405 eventName += ++updateCheckId; |
385 | 406 |
386 jsEngine->SetEventCallback(eventName, std::tr1::bind(&FilterEngine::UpdateChec kDone, | 407 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDone , |
387 this, eventName, callback, std::tr1::placeholders::_1)); | 408 this, eventName, callback, std::placeholders::_1)); |
388 | 409 |
389 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); | 410 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); |
390 JsValueList params; | 411 JsValueList params; |
391 params.push_back(jsEngine->NewValue(eventName)); | 412 params.push_back(jsEngine->NewValue(eventName)); |
392 func->Call(params); | 413 func->Call(params); |
393 } | 414 } |
394 | 415 |
395 void FilterEngine::UpdateCheckDone(const std::string& eventName, | 416 void FilterEngine::UpdateCheckDone(const std::string& eventName, |
396 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params) | 417 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params) |
397 { | 418 { |
398 jsEngine->RemoveEventCallback(eventName); | 419 jsEngine->RemoveEventCallback(eventName); |
399 | 420 |
400 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt ring() : ""); | 421 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt ring() : ""); |
401 callback(error); | 422 callback(error); |
402 } | 423 } |
403 | 424 |
404 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca llback) | 425 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca llback) |
405 { | 426 { |
406 jsEngine->SetEventCallback("filterChange", std::tr1::bind(&FilterEngine::Filte rChanged, | 427 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged, |
407 this, callback, std::tr1::placeholders::_1)); | 428 this, callback, std::placeholders::_1)); |
408 } | 429 } |
409 | 430 |
410 void FilterEngine::RemoveFilterChangeCallback() | 431 void FilterEngine::RemoveFilterChangeCallback() |
411 { | 432 { |
412 jsEngine->RemoveEventCallback("filterChange"); | 433 jsEngine->RemoveEventCallback("filterChange"); |
413 } | 434 } |
414 | 435 |
415 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params) | 436 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params) |
416 { | 437 { |
417 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : ""); | 438 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : ""); |
418 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); | 439 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); |
419 callback(action, item); | 440 callback(action, item); |
420 } | 441 } |
442 | |
443 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, | |
444 const JsValueList& params) | |
445 { | |
446 if (params.size() < 1) | |
447 return; | |
448 | |
449 if (!params[0]->IsObject()) | |
450 { | |
451 return; | |
452 } | |
453 callback(NotificationPtr(new Notification(params[0]))); | |
454 } | |
455 | |
421 | 456 |
422 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) | 457 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) |
423 { | 458 { |
424 JsValueList params; | 459 JsValueList params; |
425 params.push_back(jsEngine->NewValue(v1)); | 460 params.push_back(jsEngine->NewValue(v1)); |
426 params.push_back(jsEngine->NewValue(v2)); | 461 params.push_back(jsEngine->NewValue(v2)); |
427 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); | 462 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); |
428 return func->Call(params)->AsInt(); | 463 return func->Call(params)->AsInt(); |
429 } | 464 } |
LEFT | RIGHT |