| 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 { |
| 42 public: | 43 public: |
| 43 /** | 44 /** |
| 44 * Filter types, see https://adblockplus.org/en/filters. | 45 * Filter types, see https://adblockplus.org/en/filters. |
| 45 */ | 46 */ |
| 46 enum Type {TYPE_BLOCKING, TYPE_EXCEPTION, | 47 enum Type {TYPE_BLOCKING, TYPE_EXCEPTION, |
| 47 TYPE_ELEMHIDE, TYPE_ELEMHIDE_EXCEPTION, | 48 TYPE_ELEMHIDE, TYPE_ELEMHIDE_EXCEPTION, |
| 48 TYPE_COMMENT, TYPE_INVALID}; | 49 TYPE_COMMENT, TYPE_INVALID}; |
| 49 | 50 |
| 50 /** | 51 /** |
| 51 * Retrieves the type of this filter. | 52 * Retrieves the type of this filter. |
| 52 * @return Type of this filter. | 53 * @return Type of this filter. |
| 53 */ | 54 */ |
| 54 Type GetType(); | 55 Type GetType() const; |
| 55 | 56 |
| 56 /** | 57 /** |
| 57 * Checks whether this filter has been added to the list of custom filters. | 58 * Checks whether this filter has been added to the list of custom filters. |
| 58 * @return `true` if this filter has been added. | 59 * @return `true` if this filter has been added. |
| 59 */ | 60 */ |
| 60 bool IsListed(); | 61 bool IsListed() const; |
| 61 | 62 |
| 62 /** | 63 /** |
| 63 * Adds this filter to the list of custom filters. | 64 * Adds this filter to the list of custom filters. |
| 64 */ | 65 */ |
| 65 void AddToList(); | 66 void AddToList(); |
| 66 | 67 |
| 67 /** | 68 /** |
| 68 * Removes this filter from the list of custom filters. | 69 * Removes this filter from the list of custom filters. |
| 69 */ | 70 */ |
| 70 void RemoveFromList(); | 71 void RemoveFromList(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 87 * use `GetProperty()` to retrieve them by name. | 88 * use `GetProperty()` to retrieve them by name. |
| 88 */ | 89 */ |
| 89 class Subscription : public JsValue, | 90 class Subscription : public JsValue, |
| 90 public std::enable_shared_from_this<Subscription> | 91 public std::enable_shared_from_this<Subscription> |
| 91 { | 92 { |
| 92 public: | 93 public: |
| 93 /** | 94 /** |
| 94 * Checks if this subscription has been added to the list of subscriptions. | 95 * Checks if this subscription has been added to the list of subscriptions. |
| 95 * @return `true` if this subscription has been added. | 96 * @return `true` if this subscription has been added. |
| 96 */ | 97 */ |
| 97 bool IsListed(); | 98 bool IsListed() const; |
| 98 | 99 |
| 99 /** | 100 /** |
| 100 * Adds this subscription to the list of subscriptions. | 101 * Adds this subscription to the list of subscriptions. |
| 101 */ | 102 */ |
| 102 void AddToList(); | 103 void AddToList(); |
| 103 | 104 |
| 104 /** | 105 /** |
| 105 * Removes this subscription from the list of subscriptions. | 106 * Removes this subscription from the list of subscriptions. |
| 106 */ | 107 */ |
| 107 void RemoveFromList(); | 108 void RemoveFromList(); |
| 108 | 109 |
| 109 /** | 110 /** |
| 110 * Updates this subscription, i.e.\ retrieves the current filters from the | 111 * Updates this subscription, i.e.\ retrieves the current filters from the |
| 111 * subscription URL. | 112 * subscription URL. |
| 112 */ | 113 */ |
| 113 void UpdateFilters(); | 114 void UpdateFilters(); |
| 114 | 115 |
| 115 /** | 116 /** |
| 116 * Checks if the subscription is currently being updated. | 117 * Checks if the subscription is currently being updated. |
| 117 * @return `true` if the subscription is currently being updated. | 118 * @return `true` if the subscription is currently being updated. |
| 118 */ | 119 */ |
| 119 bool IsUpdating(); | 120 bool IsUpdating() const; |
| 120 | 121 |
| 121 bool operator==(const Subscription& subscription) const; | 122 bool operator==(const Subscription& subscription) const; |
| 122 | 123 |
| 123 /** | 124 /** |
| 124 * Creates a wrapper for an existing JavaScript subscription object. | 125 * Creates a wrapper for an existing JavaScript subscription object. |
| 125 * Normally you shouldn't call this directly, but use | 126 * Normally you shouldn't call this directly, but use |
| 126 * FilterEngine::GetSubscription() instead. | 127 * FilterEngine::GetSubscription() instead. |
| 127 * @param value JavaScript subscription object. | 128 * @param value JavaScript subscription object. |
| 128 */ | 129 */ |
| 129 Subscription(JsValue&& value); | 130 Subscription(JsValue&& value); |
| (...skipping 76 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. |
| 235 */ | 273 */ |
| 236 bool IsFirstRun() const; | 274 bool IsFirstRun() const; |
| 237 | 275 |
| 238 /** | 276 /** |
| 239 * Retrieves a filter object from its text representation. | 277 * Retrieves a filter object from its text representation. |
| 240 * @param text Text representation of the filter, | 278 * @param text Text representation of the filter, |
| 241 * see https://adblockplus.org/en/filters. | 279 * see https://adblockplus.org/en/filters. |
| 242 * @return New `Filter` instance. | 280 * @return New `Filter` instance. |
| 243 */ | 281 */ |
| 244 FilterPtr GetFilter(const std::string& text); | 282 FilterPtr GetFilter(const std::string& text) const; |
| 245 | 283 |
| 246 /** | 284 /** |
| 247 * Retrieves a subscription object for the supplied URL. | 285 * Retrieves a subscription object for the supplied URL. |
| 248 * @param url Subscription URL. | 286 * @param url Subscription URL. |
| 249 * @return New `Subscription` instance. | 287 * @return New `Subscription` instance. |
| 250 */ | 288 */ |
| 251 SubscriptionPtr GetSubscription(const std::string& url); | 289 SubscriptionPtr GetSubscription(const std::string& url) const; |
| 252 | 290 |
| 253 /** | 291 /** |
| 254 * Retrieves the list of custom filters. | 292 * Retrieves the list of custom filters. |
| 255 * @return List of custom filters. | 293 * @return List of custom filters. |
| 256 */ | 294 */ |
| 257 std::vector<FilterPtr> GetListedFilters() const; | 295 std::vector<FilterPtr> GetListedFilters() const; |
| 258 | 296 |
| 259 /** | 297 /** |
| 260 * Retrieves all subscriptions. | 298 * Retrieves all subscriptions. |
| 261 * @return List of subscriptions. | 299 * @return List of subscriptions. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 * @param pref Preference name. | 404 * @param pref Preference name. |
| 367 * @param value New value of the preference. | 405 * @param value New value of the preference. |
| 368 */ | 406 */ |
| 369 void SetPref(const std::string& pref, JsValuePtr value); | 407 void SetPref(const std::string& pref, JsValuePtr value); |
| 370 | 408 |
| 371 /** | 409 /** |
| 372 * Extracts the host from a URL. | 410 * Extracts the host from a URL. |
| 373 * @param url URL to extract the host from. | 411 * @param url URL to extract the host from. |
| 374 * @return Extracted host. | 412 * @return Extracted host. |
| 375 */ | 413 */ |
| 376 std::string GetHostFromURL(const std::string& url); | 414 std::string GetHostFromURL(const std::string& url) const; |
| 377 | 415 |
| 378 /** | 416 /** |
| 379 * Sets the callback invoked when an application update becomes available. | 417 * Sets the callback invoked when an application update becomes available. |
| 380 * @param callback Callback to invoke. | 418 * @param callback Callback to invoke. |
| 381 */ | 419 */ |
| 382 void SetUpdateAvailableCallback(UpdateAvailableCallback callback); | 420 void SetUpdateAvailableCallback(UpdateAvailableCallback callback); |
| 383 | 421 |
| 384 /** | 422 /** |
| 385 * Removes the callback invoked when an application update becomes | 423 * Removes the callback invoked when an application update becomes |
| 386 * available. | 424 * available. |
| 387 */ | 425 */ |
| 388 void RemoveUpdateAvailableCallback(); | 426 void RemoveUpdateAvailableCallback(); |
| 389 | 427 |
| 390 /** | 428 /** |
| 391 * Forces an immediate update check. | 429 * Forces an immediate update check. |
| 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() const; |
| 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) const; |
| 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 |