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-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 #ifndef ADBLOCK_PLUS_FILTER_ENGINE_H | 18 #ifndef ADBLOCK_PLUS_FILTER_ENGINE_H |
19 #define ADBLOCK_PLUS_FILTER_ENGINE_H | 19 #define ADBLOCK_PLUS_FILTER_ENGINE_H |
20 | 20 |
21 #include <functional> | 21 #include <functional> |
22 #include <map> | 22 #include <map> |
23 #include <string> | 23 #include <string> |
24 #include <vector> | 24 #include <vector> |
25 #include <AdblockPlus/JsEngine.h> | 25 #include <AdblockPlus/JsEngine.h> |
26 #include <AdblockPlus/JsValue.h> | 26 #include <AdblockPlus/JsValue.h> |
27 #include <AdblockPlus/Notification.h> | 27 #include <AdblockPlus/Notification.h> |
28 | 28 |
29 namespace AdblockPlus | 29 namespace AdblockPlus |
30 { | 30 { |
31 class FilterEngine; | 31 class FilterEngine; |
| 32 typedef std::shared_ptr<FilterEngine> FilterEnginePtr; |
32 | 33 |
33 /** | 34 /** |
34 * Wrapper for an Adblock Plus filter object. | 35 * Wrapper for an Adblock Plus filter object. |
35 * There are no accessors for most | 36 * There are no accessors for most |
36 * [filter properties](https://adblockplus.org/jsdoc/adblockpluscore/Filter.ht
ml), | 37 * [filter properties](https://adblockplus.org/jsdoc/adblockpluscore/Filter.ht
ml), |
37 * use `GetProperty()` to retrieve them by name. | 38 * use `GetProperty()` to retrieve them by name. |
38 */ | 39 */ |
39 class Filter : public JsValue, | 40 class Filter : public JsValue, |
40 public std::enable_shared_from_this<Filter> | 41 public std::enable_shared_from_this<Filter> |
41 { | 42 { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 */ | 207 */ |
207 typedef std::map<std::string, AdblockPlus::JsValuePtr> Prefs; | 208 typedef std::map<std::string, AdblockPlus::JsValuePtr> Prefs; |
208 | 209 |
209 /** | 210 /** |
210 * Callback type invoked when a new notification should be shown. | 211 * Callback type invoked when a new notification should be shown. |
211 * The parameter is the Notification object to be shown. | 212 * The parameter is the Notification object to be shown. |
212 */ | 213 */ |
213 typedef std::function<void(const NotificationPtr&)> ShowNotificationCallback
; | 214 typedef std::function<void(const NotificationPtr&)> ShowNotificationCallback
; |
214 | 215 |
215 /** | 216 /** |
216 * Constructor. | 217 * Callback function returning false when current connection is not |
| 218 * allowedConnectionType, e.g. because it is a metered connection. |
| 219 */ |
| 220 typedef std::function<bool(const std::string* allowedConnectionType)> IsConn
ectionAllowedCallback; |
| 221 |
| 222 /** |
| 223 * FilterEngine creation parameters. |
| 224 */ |
| 225 struct CreationParameters |
| 226 { |
| 227 /** |
| 228 * `AdblockPlus::FilterEngine::Prefs` name - value list of preconfigured |
| 229 * prefs. |
| 230 */ |
| 231 Prefs preconfiguredPrefs; |
| 232 /** |
| 233 * `AdblockPlus::FilterEngine::IsConnectionAllowedCallback` a callback |
| 234 * checking whether the request from Adblock Plus should be blocked on |
| 235 * the current connection. |
| 236 */ |
| 237 IsConnectionAllowedCallback isConnectionAllowedCallback; |
| 238 }; |
| 239 |
| 240 /** |
| 241 * Callback type invoked when FilterEngine is created. |
| 242 */ |
| 243 typedef std::function<void(const FilterEnginePtr&)> OnCreatedCallback; |
| 244 |
| 245 /** |
| 246 * Asynchronously constructs FilterEngine. |
217 * @param jsEngine `JsEngine` instance used to run JavaScript code | 247 * @param jsEngine `JsEngine` instance used to run JavaScript code |
218 * internally. | 248 * internally. |
219 * @param preconfiguredPrefs `AdblockPlus::FilterEngine::Prefs` | 249 * @param onCreated A callback which is called when FilterEngine is ready |
220 * name-value list of preconfigured prefs. | 250 * for use. |
| 251 * @param parameters optional creation parameters. |
221 */ | 252 */ |
222 explicit FilterEngine(JsEnginePtr jsEngine, | 253 static void CreateAsync(const JsEnginePtr& jsEngine, |
223 const Prefs& preconfiguredPrefs = Prefs() | 254 const OnCreatedCallback& onCreated, |
224 ); | 255 const CreationParameters& parameters = CreationParameters()); |
| 256 |
| 257 /** |
| 258 * Synchronous interface to construct FilterEngine. For details see |
| 259 * asynchronous version CreateAsync. |
| 260 */ |
| 261 static FilterEnginePtr Create(const JsEnginePtr& jsEngine, |
| 262 const CreationParameters& params = CreationParameters()); |
225 | 263 |
226 /** | 264 /** |
227 * Retrieves the `JsEngine` instance associated with this `FilterEngine` | 265 * Retrieves the `JsEngine` instance associated with this `FilterEngine` |
228 * instance. | 266 * instance. |
229 */ | 267 */ |
230 JsEnginePtr GetJsEngine() const { return jsEngine; } | 268 JsEnginePtr GetJsEngine() const { return jsEngine; } |
231 | 269 |
232 /** | 270 /** |
233 * Checks if this is the first run of the application. | 271 * Checks if this is the first run of the application. |
234 * @return `true` if the application is running for the first time. | 272 * @return `true` if the application is running for the first time. |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 * `FilterEngine` will automatically check for updates in regular intervals, | 430 * `FilterEngine` will automatically check for updates in regular intervals, |
393 * so applications should only call this when the user triggers an update | 431 * so applications should only call this when the user triggers an update |
394 * check manually. | 432 * check manually. |
395 * @param callback Optional callback to invoke when the update check is | 433 * @param callback Optional callback to invoke when the update check is |
396 * finished. The string parameter will be empty when the update check | 434 * finished. The string parameter will be empty when the update check |
397 * succeeded, or contain an error message if it failed. | 435 * succeeded, or contain an error message if it failed. |
398 * Note that the callback will be invoked whether updates are | 436 * Note that the callback will be invoked whether updates are |
399 * available or not - to react to updates being available, use | 437 * available or not - to react to updates being available, use |
400 * `FilterEngine::SetUpdateAvailableCallback()`. | 438 * `FilterEngine::SetUpdateAvailableCallback()`. |
401 */ | 439 */ |
402 void ForceUpdateCheck(UpdateCheckDoneCallback callback); | 440 void ForceUpdateCheck(const UpdateCheckDoneCallback& callback = UpdateCheckD
oneCallback()); |
403 | 441 |
404 /** | 442 /** |
405 * Sets the callback invoked when the filters change. | 443 * Sets the callback invoked when the filters change. |
406 * @param callback Callback to invoke. | 444 * @param callback Callback to invoke. |
407 */ | 445 */ |
408 void SetFilterChangeCallback(FilterChangeCallback callback); | 446 void SetFilterChangeCallback(FilterChangeCallback callback); |
409 | 447 |
410 /** | 448 /** |
411 * Removes the callback invoked when the filters change. | 449 * Removes the callback invoked when the filters change. |
412 */ | 450 */ |
413 void RemoveFilterChangeCallback(); | 451 void RemoveFilterChangeCallback(); |
414 | 452 |
415 /** | 453 /** |
| 454 * Stores the value indicating what connection types are allowed, it is |
| 455 * passed to CreateParameters::isConnectionAllowed callback. |
| 456 * @param value Stored value. nullptr means removing of any previously |
| 457 * stored value. |
| 458 */ |
| 459 void SetAllowedConnectionType(const std::string* value); |
| 460 |
| 461 /** |
| 462 * Retrieves previously stored allowed connection type. |
| 463 * @return Preference value, or `nullptr` if it doesn't exist. |
| 464 */ |
| 465 std::unique_ptr<std::string> GetAllowedConnectionType(); |
| 466 |
| 467 /** |
416 * Compares two version strings in | 468 * Compares two version strings in |
417 * [Mozilla toolkit version format](https://developer.mozilla.org/en/docs/To
olkit_version_format). | 469 * [Mozilla toolkit version format](https://developer.mozilla.org/en/docs/To
olkit_version_format). |
418 * @param v1 First version string. | 470 * @param v1 First version string. |
419 * @param v2 Second version string. | 471 * @param v2 Second version string. |
420 * @return | 472 * @return |
421 * - `0` if `v1` and `v2` are identical. | 473 * - `0` if `v1` and `v2` are identical. |
422 * - A negative number if `v1` is less than `v2`. | 474 * - A negative number if `v1` is less than `v2`. |
423 * - A positive number if `v1` is greater than `v2`. | 475 * - A positive number if `v1` is greater than `v2`. |
424 */ | 476 */ |
425 int CompareVersions(const std::string& v1, const std::string& v2); | 477 int CompareVersions(const std::string& v1, const std::string& v2); |
426 | 478 |
427 /** | 479 /** |
428 * Retrieves the `ContentType` for the supplied string. | 480 * Retrieves the `ContentType` for the supplied string. |
429 * @param contentType Content type string. | 481 * @param contentType Content type string. |
430 * @return The `ContentType` for the string. | 482 * @return The `ContentType` for the string. |
431 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. | 483 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. |
432 */ | 484 */ |
433 static ContentType StringToContentType(const std::string& contentType); | 485 static ContentType StringToContentType(const std::string& contentType); |
434 | 486 |
435 /** | 487 /** |
436 * Retrieves the string representation of the supplied `ContentType`. | 488 * Retrieves the string representation of the supplied `ContentType`. |
437 * @param contentType `ContentType` value. | 489 * @param contentType `ContentType` value. |
438 * @return The string representation of `contentType`. | 490 * @return The string representation of `contentType`. |
439 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. | 491 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. |
440 */ | 492 */ |
441 static std::string ContentTypeToString(ContentType contentType); | 493 static std::string ContentTypeToString(ContentType contentType); |
442 | 494 |
443 private: | 495 private: |
444 JsEnginePtr jsEngine; | 496 JsEnginePtr jsEngine; |
445 bool initialized; | |
446 bool firstRun; | 497 bool firstRun; |
447 int updateCheckId; | 498 int updateCheckId; |
448 static const std::map<ContentType, std::string> contentTypes; | 499 static const std::map<ContentType, std::string> contentTypes; |
449 | 500 |
450 void InitDone(JsValueList& params); | 501 explicit FilterEngine(const JsEnginePtr& jsEngine); |
| 502 |
451 FilterPtr CheckFilterMatch(const std::string& url, | 503 FilterPtr CheckFilterMatch(const std::string& url, |
452 ContentTypeMask contentTypeMask, | 504 ContentTypeMask contentTypeMask, |
453 const std::string& documentUrl) const; | 505 const std::string& documentUrl) const; |
454 void UpdateAvailable(UpdateAvailableCallback callback, JsValueList& params); | 506 void UpdateAvailable(UpdateAvailableCallback callback, JsValueList& params); |
455 void UpdateCheckDone(const std::string& eventName, | 507 void UpdateCheckDone(const std::string& eventName, |
456 UpdateCheckDoneCallback callback, JsValueList& params); | 508 UpdateCheckDoneCallback callback, JsValueList& params); |
457 void FilterChanged(FilterChangeCallback callback, JsValueList& params); | 509 void FilterChanged(FilterChangeCallback callback, JsValueList& params); |
458 void ShowNotification(const ShowNotificationCallback& callback, | 510 void ShowNotification(const ShowNotificationCallback& callback, |
459 const JsValueList& params); | 511 const JsValueList& params); |
460 FilterPtr GetWhitelistingFilter(const std::string& url, | 512 FilterPtr GetWhitelistingFilter(const std::string& url, |
461 ContentTypeMask contentTypeMask, const std::string& documentUrl) const; | 513 ContentTypeMask contentTypeMask, const std::string& documentUrl) const; |
462 FilterPtr GetWhitelistingFilter(const std::string& url, | 514 FilterPtr GetWhitelistingFilter(const std::string& url, |
463 ContentTypeMask contentTypeMask, | 515 ContentTypeMask contentTypeMask, |
464 const std::vector<std::string>& documentUrls) const; | 516 const std::vector<std::string>& documentUrls) const; |
465 }; | 517 }; |
466 } | 518 } |
467 | 519 |
468 #endif | 520 #endif |
OLD | NEW |