libadblockplus
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
FilterEngine.h
1 /*
2  * This file is part of Adblock Plus <https://adblockplus.org/>,
3  * Copyright (C) 2006-present eyeo GmbH
4  *
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
7  * published by the Free Software Foundation.
8  *
9  * Adblock Plus is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
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/>.
16  */
17 
18 #ifndef ADBLOCK_PLUS_FILTER_ENGINE_H
19 #define ADBLOCK_PLUS_FILTER_ENGINE_H
20 
21 #include <functional>
22 #include <map>
23 #include <string>
24 #include <vector>
25 #include <AdblockPlus/JsEngine.h>
26 #include <AdblockPlus/JsValue.h>
27 #include <AdblockPlus/Notification.h>
28 
29 namespace AdblockPlus
30 {
31  class FilterEngine;
32  typedef std::shared_ptr<FilterEngine> FilterEnginePtr;
33 
40  class Filter : public JsValue
41  {
42  friend class FilterEngine;
43  public:
44  Filter(const Filter& src);
45  Filter(Filter&& src);
46  Filter& operator=(const Filter& src);
47  Filter& operator=(Filter&& src);
48 
52  enum Type {TYPE_BLOCKING, TYPE_EXCEPTION,
53  TYPE_ELEMHIDE, TYPE_ELEMHIDE_EXCEPTION,
54  TYPE_ELEMHIDE_EMULATION,
55  TYPE_COMMENT, TYPE_INVALID};
56 
61  Type GetType() const;
62 
67  bool IsListed() const;
68 
72  void AddToList();
73 
77  void RemoveFromList();
78 
79  bool operator==(const Filter& filter) const;
80 
81  protected:
88  Filter(JsValue&& value);
89  };
90 
97  class Subscription : public JsValue
98  {
99  friend class FilterEngine;
100  public:
104  Subscription(const Subscription& src);
105 
109  Subscription(Subscription&& src);
110 
114  Subscription& operator=(const Subscription& src);
115 
120 
125  bool IsDisabled() const;
126 
132  void SetDisabled(bool value);
133 
138  bool IsListed() const;
139 
143  void AddToList();
144 
148  void RemoveFromList();
149 
154  void UpdateFilters();
155 
160  bool IsUpdating() const;
161 
166  bool IsAA() const;
167 
168  bool operator==(const Subscription& subscription) const;
169 
170  protected:
177  Subscription(JsValue&& value);
178  };
179 
183  typedef std::unique_ptr<Filter> FilterPtr;
184 
193  {
194  public:
195  // Make sure to keep ContentType in sync with FilterEngine::contentTypes
196  // and with RegExpFilter.typeMap from filterClasses.js.
201  {
202  CONTENT_TYPE_OTHER = 1,
203  CONTENT_TYPE_SCRIPT = 2,
204  CONTENT_TYPE_IMAGE = 4,
205  CONTENT_TYPE_STYLESHEET = 8,
206  CONTENT_TYPE_OBJECT = 16,
207  CONTENT_TYPE_SUBDOCUMENT = 32,
208  CONTENT_TYPE_DOCUMENT = 64,
209  CONTENT_TYPE_WEBSOCKET = 128,
210  CONTENT_TYPE_WEBRTC = 256,
211  CONTENT_TYPE_PING = 1024,
212  CONTENT_TYPE_XMLHTTPREQUEST = 2048,
213  CONTENT_TYPE_OBJECT_SUBREQUEST = 4096,
214  CONTENT_TYPE_MEDIA = 16384,
215  CONTENT_TYPE_FONT = 32768,
216  CONTENT_TYPE_GENERICBLOCK = 0x20000000,
217  CONTENT_TYPE_ELEMHIDE = 0x40000000,
218  CONTENT_TYPE_GENERICHIDE = 0x80000000
219  };
220 
226  typedef int32_t ContentTypeMask;
227 
232  typedef std::function<void(const std::string&)> UpdateAvailableCallback;
233 
238  typedef std::function<void(const std::string&)> UpdateCheckDoneCallback;
239 
247  typedef std::function<void(const std::string&, JsValue&&)> FilterChangeCallback;
248 
252  typedef std::map<std::string, AdblockPlus::JsValue> Prefs;
253 
258  typedef std::function<void(Notification&&)> ShowNotificationCallback;
259 
265  typedef std::function<void(const std::string* allowedConnectionType, const std::function<void(bool)>&)> IsConnectionAllowedAsyncCallback;
266 
271  {
283  };
284 
288  typedef std::function<void(const FilterEnginePtr&)> OnCreatedCallback;
289 
298  static void CreateAsync(const JsEnginePtr& jsEngine,
299  const OnCreatedCallback& onCreated,
300  const CreationParameters& parameters = CreationParameters());
301 
306  JsEngine& GetJsEngine() const { return *jsEngine; }
307 
312  bool IsFirstRun() const;
313 
320  Filter GetFilter(const std::string& text) const;
321 
327  Subscription GetSubscription(const std::string& url) const;
328 
333  std::vector<Filter> GetListedFilters() const;
334 
339  std::vector<Subscription> GetListedSubscriptions() const;
340 
345  std::vector<Subscription> FetchAvailableSubscriptions() const;
346 
357  void SetAAEnabled(bool enabled);
358 
363  bool IsAAEnabled() const;
364 
370  std::string GetAAUrl() const;
371 
377  void ShowNextNotification(const std::string& url = std::string()) const;
378 
383  void SetShowNotificationCallback(const ShowNotificationCallback& value);
384 
389 
401  FilterPtr Matches(const std::string& url,
402  ContentTypeMask contentTypeMask,
403  const std::string& documentUrl) const;
404 
418  FilterPtr Matches(const std::string& url,
419  ContentTypeMask contentTypeMask,
420  const std::vector<std::string>& documentUrls) const;
421 
433  bool IsDocumentWhitelisted(const std::string& url,
434  const std::vector<std::string>& documentUrls) const;
435 
447  bool IsElemhideWhitelisted(const std::string& url,
448  const std::vector<std::string>& documentUrls) const;
449 
456  std::vector<std::string> GetElementHidingSelectors(const std::string& domain) const;
457 
463  JsValue GetPref(const std::string& pref) const;
464 
470  void SetPref(const std::string& pref, const JsValue& value);
471 
477  std::string GetHostFromURL(const std::string& url) const;
478 
483  void SetUpdateAvailableCallback(const UpdateAvailableCallback& callback);
484 
490 
503  void ForceUpdateCheck(const UpdateCheckDoneCallback& callback = UpdateCheckDoneCallback());
504 
509  void SetFilterChangeCallback(const FilterChangeCallback& callback);
510 
515 
522  void SetAllowedConnectionType(const std::string* value);
523 
528  std::unique_ptr<std::string> GetAllowedConnectionType() const;
529 
540  int CompareVersions(const std::string& v1, const std::string& v2) const;
541 
548  static ContentType StringToContentType(const std::string& contentType);
549 
556  static std::string ContentTypeToString(ContentType contentType);
557 
558  private:
559  JsEnginePtr jsEngine;
560  bool firstRun;
561  int updateCheckId;
562  static const std::map<ContentType, std::string> contentTypes;
563 
564  explicit FilterEngine(const JsEnginePtr& jsEngine);
565 
566  FilterPtr CheckFilterMatch(const std::string& url,
567  ContentTypeMask contentTypeMask,
568  const std::string& documentUrl) const;
569  void FilterChanged(const FilterChangeCallback& callback, JsValueList&& params) const;
570  FilterPtr GetWhitelistingFilter(const std::string& url,
571  ContentTypeMask contentTypeMask, const std::string& documentUrl) const;
572  FilterPtr GetWhitelistingFilter(const std::string& url,
573  ContentTypeMask contentTypeMask,
574  const std::vector<std::string>& documentUrls) const;
575  };
576 }
577 
578 #endif
bool IsAA() const
Indicates whether the subscription is the Acceptable Ads subscription.
Subscription(const Subscription &src)
Copy constructor.
int CompareVersions(const std::string &v1, const std::string &v2) const
Compares two version strings in Mozilla toolkit version format.
void AddToList()
Adds this subscription to the list of subscriptions.
bool IsUpdating() const
Checks if the subscription is currently being updated.
void SetAAEnabled(bool enabled)
Ensures that the Acceptable Ads subscription is enabled or disabled.
void ForceUpdateCheck(const UpdateCheckDoneCallback &callback=UpdateCheckDoneCallback())
Forces an immediate update check.
bool IsElemhideWhitelisted(const std::string &url, const std::vector< std::string > &documentUrls) const
Checks whether element hiding is disabled at the supplied URL.
JavaScript engine used by FilterEngine, wraps v8.
Definition: JsEngine.h:73
static ContentType StringToContentType(const std::string &contentType)
Retrieves the ContentType for the supplied string.
Wrapper for an Adblock Plus filter object.
Definition: FilterEngine.h:40
std::vector< AdblockPlus::JsValue > JsValueList
List of JavaScript values.
Definition: JsValue.h:48
bool IsFirstRun() const
Checks if this is the first run of the application.
bool IsDisabled() const
Checks if the subscription is disabled.
std::function< void(const std::string &)> UpdateCheckDoneCallback
Callback type invoked when a manually triggered update check finishes.
Definition: FilterEngine.h:238
bool IsDocumentWhitelisted(const std::string &url, const std::vector< std::string > &documentUrls) const
Checks whether the document at the supplied URL is whitelisted.
Wrapper for JavaScript values.
Definition: JsValue.h:54
bool IsAAEnabled() const
Checks whether the Acceptable Ads subscription is enabled.
void ShowNextNotification(const std::string &url=std::string()) const
Invokes the listener set via SetNotificationAvailableCallback() with the next notification to be show...
void SetUpdateAvailableCallback(const UpdateAvailableCallback &callback)
Sets the callback invoked when an application update becomes available.
Subscription & operator=(const Subscription &src)
Assignment operator.
void SetShowNotificationCallback(const ShowNotificationCallback &value)
Sets the callback invoked when a notification should be shown.
std::map< std::string, AdblockPlus::JsValue > Prefs
Container of name-value pairs representing a set of preferences.
Definition: FilterEngine.h:252
IsConnectionAllowedAsyncCallback isSubscriptionDownloadAllowedCallback
A callback of AdblockPlus::FilterEngine::IsConnectionAllowedAsyncCallback type checking whether the r...
Definition: FilterEngine.h:282
std::string GetHostFromURL(const std::string &url) const
Extracts the host from a URL.
Main component of libadblockplus.
Definition: FilterEngine.h:192
void RemoveFromList()
Removes this filter from the list of custom filters.
Main namespace of libadblockplus.
Definition: ActiveObject.h:23
void RemoveUpdateAvailableCallback()
Removes the callback invoked when an application update becomes available.
ContentType
Possible resource content types.
Definition: FilterEngine.h:200
void SetAllowedConnectionType(const std::string *value)
Stores the value indicating what connection types are allowed, it is passed to CreateParameters::isCo...
static std::string ContentTypeToString(ContentType contentType)
Retrieves the string representation of the supplied ContentType.
void SetPref(const std::string &pref, const JsValue &value)
Sets a preference value.
void RemoveFromList()
Removes this subscription from the list of subscriptions.
std::function< void(const std::string &, JsValue &&)> FilterChangeCallback
Callback type invoked when the filters change.
Definition: FilterEngine.h:247
std::unique_ptr< Filter > FilterPtr
A smart pointer to a Filter instance.
Definition: FilterEngine.h:183
void SetFilterChangeCallback(const FilterChangeCallback &callback)
Sets the callback invoked when the filters change.
Filter GetFilter(const std::string &text) const
Retrieves a filter object from its text representation.
FilterPtr Matches(const std::string &url, ContentTypeMask contentTypeMask, const std::string &documentUrl) const
Checks if any active filter matches the supplied URL.
std::vector< std::string > GetElementHidingSelectors(const std::string &domain) const
Retrieves CSS selectors for all element hiding filters active on the supplied domain.
void SetDisabled(bool value)
Allows to enable or disable current subscription.
JsEngine & GetJsEngine() const
Retrieves the JsEngine instance associated with this FilterEngine instance.
Definition: FilterEngine.h:306
Type
Filter types, see https://adblockplus.org/en/filters.
Definition: FilterEngine.h:52
std::vector< Subscription > GetListedSubscriptions() const
Retrieves all subscriptions.
void RemoveFilterChangeCallback()
Removes the callback invoked when the filters change.
std::function< void(const FilterEnginePtr &)> OnCreatedCallback
Callback type invoked when FilterEngine is created.
Definition: FilterEngine.h:288
std::function< void(const std::string &)> UpdateAvailableCallback
Callback type invoked when an update becomes available.
Definition: FilterEngine.h:232
Type GetType() const
Retrieves the type of this filter.
FilterEngine creation parameters.
Definition: FilterEngine.h:270
std::vector< Filter > GetListedFilters() const
Retrieves the list of custom filters.
bool IsListed() const
Checks whether this filter has been added to the list of custom filters.
std::vector< Subscription > FetchAvailableSubscriptions() const
Retrieves all recommended subscriptions.
bool IsListed() const
Checks if this subscription has been added to the list of subscriptions.
void AddToList()
Adds this filter to the list of custom filters.
std::function< void(const std::string *allowedConnectionType, const std::function< void(bool)> &)> IsConnectionAllowedAsyncCallback
Asynchronous callback function passing false when current connection type does not correspond to allo...
Definition: FilterEngine.h:265
static void CreateAsync(const JsEnginePtr &jsEngine, const OnCreatedCallback &onCreated, const CreationParameters &parameters=CreationParameters())
Asynchronously constructs FilterEngine.
void UpdateFilters()
Updates this subscription, i.e. retrieves the current filters from the subscription URL...
int32_t ContentTypeMask
Bitmask of ContentType values.
Definition: FilterEngine.h:226
std::function< void(Notification &&)> ShowNotificationCallback
Callback type invoked when a new notification should be shown.
Definition: FilterEngine.h:258
void RemoveShowNotificationCallback()
Removes the callback invoked when a notification should be shown.
Prefs preconfiguredPrefs
AdblockPlus::FilterEngine::Prefs name - value list of preconfigured prefs.
Definition: FilterEngine.h:276
std::unique_ptr< std::string > GetAllowedConnectionType() const
Retrieves previously stored allowed connection type.
JsValue GetPref(const std::string &pref) const
Retrieves a preference value.
Subscription GetSubscription(const std::string &url) const
Retrieves a subscription object for the supplied URL.
std::string GetAAUrl() const
Retrieves the URL of the Acceptable Ads subscription, what makes the URL available even if subscripti...
std::shared_ptr< JsEngine > JsEnginePtr
Shared smart pointer to a JsEngine instance.
Definition: JsEngine.h:48
Wrapper for a subscription object.
Definition: FilterEngine.h:97