OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 13 matching lines...) Expand all Loading... |
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 | 27 |
28 #include "tr1_memory.h" | 28 #include "tr1_memory.h" |
29 | 29 |
30 namespace AdblockPlus | 30 namespace AdblockPlus |
31 { | 31 { |
32 class FilterEngine; | 32 class FilterEngine; |
33 | 33 |
| 34 /** |
| 35 * Wrapper for a filter object. |
| 36 */ |
34 class Filter : public JsValue, | 37 class Filter : public JsValue, |
35 public std::tr1::enable_shared_from_this<Filter> | 38 public std::tr1::enable_shared_from_this<Filter> |
36 { | 39 { |
37 public: | 40 public: |
| 41 /** |
| 42 * Filter types, see https://adblockplus.org/en/filters. |
| 43 */ |
38 enum Type {TYPE_BLOCKING, TYPE_EXCEPTION, | 44 enum Type {TYPE_BLOCKING, TYPE_EXCEPTION, |
39 TYPE_ELEMHIDE, TYPE_ELEMHIDE_EXCEPTION, | 45 TYPE_ELEMHIDE, TYPE_ELEMHIDE_EXCEPTION, |
40 TYPE_COMMENT, TYPE_INVALID}; | 46 TYPE_COMMENT, TYPE_INVALID}; |
41 | 47 |
42 Type GetType(); | 48 Type GetType(); |
| 49 |
| 50 /** |
| 51 * Checks whether this filter has been added to its subscription. |
| 52 * @return `true` if this filter has been added to its subscription. |
| 53 */ |
43 bool IsListed(); | 54 bool IsListed(); |
| 55 |
| 56 /** |
| 57 * Adds this filter to its subscription. |
| 58 */ |
44 void AddToList(); | 59 void AddToList(); |
| 60 |
| 61 /** |
| 62 * Removes this filter from its subscription. |
| 63 */ |
45 void RemoveFromList(); | 64 void RemoveFromList(); |
| 65 |
46 bool operator==(const Filter& filter) const; | 66 bool operator==(const Filter& filter) const; |
47 | 67 |
48 Filter(JsValuePtr value); | 68 Filter(JsValuePtr value); |
49 }; | 69 }; |
50 | 70 |
| 71 /** |
| 72 * Wrapper for a subscription object. |
| 73 */ |
51 class Subscription : public JsValue, | 74 class Subscription : public JsValue, |
52 public std::tr1::enable_shared_from_this<Subscription> | 75 public std::tr1::enable_shared_from_this<Subscription> |
53 { | 76 { |
54 public: | 77 public: |
| 78 /** |
| 79 * Checks if this subscription has been added to the known subscriptions. |
| 80 * @return `true` if this subscription has been added to the known |
| 81 * subscriptions. |
| 82 */ |
55 bool IsListed(); | 83 bool IsListed(); |
| 84 |
| 85 /** |
| 86 * Adds this subscription to the known subscriptions. |
| 87 */ |
56 void AddToList(); | 88 void AddToList(); |
| 89 |
| 90 /** |
| 91 * Removes this subscription from the known subscriptions. |
| 92 */ |
57 void RemoveFromList(); | 93 void RemoveFromList(); |
| 94 |
| 95 /** |
| 96 * Updates this subscription, i.e.\ retrieves the latest version from the |
| 97 * subscription URL. |
| 98 */ |
58 void UpdateFilters(); | 99 void UpdateFilters(); |
| 100 |
| 101 /** |
| 102 * Checks if this subscriptions is currently being updated. |
| 103 * @return `true` if this subscriptions is currently being updated. |
| 104 */ |
59 bool IsUpdating(); | 105 bool IsUpdating(); |
| 106 |
60 bool operator==(const Subscription& subscription) const; | 107 bool operator==(const Subscription& subscription) const; |
61 | 108 |
62 Subscription(JsValuePtr value); | 109 Subscription(JsValuePtr value); |
63 }; | 110 }; |
64 | 111 |
| 112 /** |
| 113 * Shared smart pointer to a `Filter` instance. |
| 114 */ |
65 typedef std::tr1::shared_ptr<Filter> FilterPtr; | 115 typedef std::tr1::shared_ptr<Filter> FilterPtr; |
| 116 |
| 117 /** |
| 118 * Shared smart pointer to a `Subscription` instance. |
| 119 */ |
66 typedef std::tr1::shared_ptr<Subscription> SubscriptionPtr; | 120 typedef std::tr1::shared_ptr<Subscription> SubscriptionPtr; |
67 | 121 |
| 122 /** |
| 123 * Main component of Libadblockplus. |
| 124 * It handles: |
| 125 * - Filter management and matching. |
| 126 * - Subscription management and synchronization. |
| 127 * - Update checks for the application. |
| 128 */ |
68 class FilterEngine | 129 class FilterEngine |
69 { | 130 { |
70 public: | 131 public: |
| 132 /** |
| 133 * Callback invoked when an update is available. |
| 134 */ |
71 typedef std::tr1::function<void(const std::string&)> UpdaterCallback; | 135 typedef std::tr1::function<void(const std::string&)> UpdaterCallback; |
| 136 |
| 137 /** |
| 138 * Callback invoked when the filters change. |
| 139 */ |
72 typedef std::tr1::function<void(const std::string&, const JsValuePtr)> Filte
rChangeCallback; | 140 typedef std::tr1::function<void(const std::string&, const JsValuePtr)> Filte
rChangeCallback; |
73 | 141 |
74 explicit FilterEngine(JsEnginePtr jsEngine); | 142 explicit FilterEngine(JsEnginePtr jsEngine); |
75 JsEnginePtr GetJsEngine() const { return jsEngine; } | 143 JsEnginePtr GetJsEngine() const { return jsEngine; } |
| 144 |
| 145 /** |
| 146 * Checks if this is the first run of the application. |
| 147 * @return `true` if the application is running for the first time. |
| 148 */ |
76 bool IsFirstRun() const; | 149 bool IsFirstRun() const; |
| 150 |
| 151 /** |
| 152 * Retrieves a filter object from its text representation. |
| 153 * @param text Text representation of the filter, |
| 154 * see https://adblockplus.org/en/filters. |
| 155 * @return New filter object if the filter wasn't known, otherwise the |
| 156 * existing one. |
| 157 */ |
77 FilterPtr GetFilter(const std::string& text); | 158 FilterPtr GetFilter(const std::string& text); |
| 159 |
| 160 /** |
| 161 * Retrieves a subscription object for the supplied URL. |
| 162 * @param url Subscription URL. |
| 163 * @return New subscription object if the subscription wasn't known, |
| 164 * otherwise the existing one. |
| 165 */ |
78 SubscriptionPtr GetSubscription(const std::string& url); | 166 SubscriptionPtr GetSubscription(const std::string& url); |
| 167 |
| 168 /** |
| 169 * Retrieves all known filters. |
| 170 * @return List of known filters. |
| 171 */ |
79 std::vector<FilterPtr> GetListedFilters() const; | 172 std::vector<FilterPtr> GetListedFilters() const; |
| 173 |
| 174 /** |
| 175 * Retrieves all known subscriptions. |
| 176 * @return List of known subscriptions. |
| 177 */ |
80 std::vector<SubscriptionPtr> GetListedSubscriptions() const; | 178 std::vector<SubscriptionPtr> GetListedSubscriptions() const; |
| 179 |
| 180 /** |
| 181 * Retrieves all recommended subscriptions. |
| 182 * This will parse all subscriptions listed in `subscriptions.xml`. |
| 183 * @return List of recommended subscriptions. |
| 184 */ |
81 std::vector<SubscriptionPtr> FetchAvailableSubscriptions() const; | 185 std::vector<SubscriptionPtr> FetchAvailableSubscriptions() const; |
| 186 |
| 187 /** |
| 188 * Checks if any active filter matches the supplied URL. |
| 189 * @param url URL to match. |
| 190 * @param contentType Content type of the requested resource, possible |
| 191 * values are: |
| 192 * - OTHER |
| 193 * - SCRIPT |
| 194 * - IMAGE |
| 195 * - STYLESHEET |
| 196 * - OBJECT |
| 197 * - SUBDOCUMENT |
| 198 * - DOCUMENT |
| 199 * - XMLHTTPREQUEST |
| 200 * - OBJECT_SUBREQUEST |
| 201 * - FONT |
| 202 * - MEDIA |
| 203 * @param documentUrl URL of the document requesting the resource. |
| 204 * Note that there will be more than one document if frames are |
| 205 * involved, see |
| 206 * Matches(const std::string&, const std::string&, const std::vector<
std::string>&) const. |
| 207 * @return Matching filter, or `null` if there was no match. |
| 208 */ |
82 FilterPtr Matches(const std::string& url, | 209 FilterPtr Matches(const std::string& url, |
83 const std::string& contentType, | 210 const std::string& contentType, |
84 const std::string& documentUrl) const; | 211 const std::string& documentUrl) const; |
| 212 |
| 213 /** |
| 214 * Checks if any active filter matches the supplied URL. |
| 215 * @param url URL to match. |
| 216 * @param contentType Content type of the requested resource, possible |
| 217 * values are: |
| 218 * - OTHER |
| 219 * - SCRIPT |
| 220 * - IMAGE |
| 221 * - STYLESHEET |
| 222 * - OBJECT |
| 223 * - SUBDOCUMENT |
| 224 * - DOCUMENT |
| 225 * - XMLHTTPREQUEST |
| 226 * - OBJECT_SUBREQUEST |
| 227 * - FONT |
| 228 * - MEDIA |
| 229 * @param documentUrls Chain of documents requesting the resource. |
| 230 * This will typically be the frame structure. If the application is |
| 231 * not capable of identifying the frame structure, e.g. because it is |
| 232 * a proxy, it can be approximated using `ReferrerMapping`. |
| 233 * @return Matching filter, or `null` if there was no match. |
| 234 */ |
85 FilterPtr Matches(const std::string& url, | 235 FilterPtr Matches(const std::string& url, |
86 const std::string& contentType, | 236 const std::string& contentType, |
87 const std::vector<std::string>& documentUrls) const; | 237 const std::vector<std::string>& documentUrls) const; |
| 238 |
| 239 /** |
| 240 * Retrieves CSS selectors for all active element hiding filters. |
| 241 * @param domain Domain to retrieve CSS selectors for. |
| 242 * @return List of CSS selectors. |
| 243 */ |
88 std::vector<std::string> GetElementHidingSelectors(const std::string& domain
) const; | 244 std::vector<std::string> GetElementHidingSelectors(const std::string& domain
) const; |
| 245 |
| 246 /** |
| 247 * Retrieves a preference value. |
| 248 * @param pref Preference name. |
| 249 * @return Preference value, or `null` if it doesn't exist. |
| 250 */ |
89 JsValuePtr GetPref(const std::string& pref) const; | 251 JsValuePtr GetPref(const std::string& pref) const; |
| 252 |
| 253 /** |
| 254 * Sets a preference value. |
| 255 * @param pref Preference name. |
| 256 * @param value New value of the preference. |
| 257 */ |
90 void SetPref(const std::string& pref, JsValuePtr value); | 258 void SetPref(const std::string& pref, JsValuePtr value); |
| 259 |
| 260 /** |
| 261 * Extracts the host from an URL. |
| 262 * @param url URL to extract the host from. |
| 263 * @return Extracted host. |
| 264 */ |
91 std::string GetHostFromURL(const std::string& url); | 265 std::string GetHostFromURL(const std::string& url); |
| 266 |
| 267 /** |
| 268 * Forces an immediate update check. |
| 269 * @param Optional callback to invoke when updates are available. |
| 270 */ |
92 void ForceUpdateCheck(UpdaterCallback callback = 0); | 271 void ForceUpdateCheck(UpdaterCallback callback = 0); |
| 272 |
| 273 /** |
| 274 * Sets the callback invoked when the filters change. |
| 275 * @param The callback to invoke. |
| 276 */ |
93 void SetFilterChangeCallback(FilterChangeCallback callback); | 277 void SetFilterChangeCallback(FilterChangeCallback callback); |
| 278 |
| 279 /** |
| 280 * Removes the callback invoked when the filters change. |
| 281 */ |
94 void RemoveFilterChangeCallback(); | 282 void RemoveFilterChangeCallback(); |
| 283 |
| 284 /** |
| 285 * Compares two version strings in |
| 286 * [Mozilla toolkit version format](https://developer.mozilla.org/en/docs/To
olkit_version_format). |
| 287 * @param v1 First version string. |
| 288 * @param v2 Second version string. |
| 289 * @return |
| 290 * - `0` if `v1` and `v2` are identical. |
| 291 * - A negative number if `v1` is less than `v2`. |
| 292 * - A positive number if `v1` is greater than `v2`. |
| 293 */ |
95 int CompareVersions(const std::string& v1, const std::string& v2); | 294 int CompareVersions(const std::string& v1, const std::string& v2); |
96 | 295 |
97 private: | 296 private: |
98 JsEnginePtr jsEngine; | 297 JsEnginePtr jsEngine; |
99 bool initialized; | 298 bool initialized; |
100 bool firstRun; | 299 bool firstRun; |
101 int updateCheckId; | 300 int updateCheckId; |
102 | 301 |
103 void InitDone(JsValueList& params); | 302 void InitDone(JsValueList& params); |
104 FilterPtr CheckFilterMatch(const std::string& url, | 303 FilterPtr CheckFilterMatch(const std::string& url, |
105 const std::string& contentType, | 304 const std::string& contentType, |
106 const std::string& documentUrl) const; | 305 const std::string& documentUrl) const; |
107 void UpdateCheckDone(const std::string& eventName, UpdaterCallback callback,
JsValueList& params); | 306 void UpdateCheckDone(const std::string& eventName, UpdaterCallback callback,
JsValueList& params); |
108 void FilterChanged(FilterChangeCallback callback, JsValueList& params); | 307 void FilterChanged(FilterChangeCallback callback, JsValueList& params); |
109 }; | 308 }; |
110 } | 309 } |
111 | 310 |
112 #endif | 311 #endif |
OLD | NEW |