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-2016 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 #include "tr1_memory.h" | |
30 | |
31 namespace AdblockPlus | 29 namespace AdblockPlus |
32 { | 30 { |
33 class FilterEngine; | 31 class FilterEngine; |
34 | 32 |
35 /** | 33 /** |
36 * Wrapper for an Adblock Plus filter object. | 34 * Wrapper for an Adblock Plus filter object. |
37 * There are no accessors for most | 35 * There are no accessors for most |
38 * [filter properties](https://adblockplus.org/jsdoc/adblockplus/symbols/Filte
r.html), | 36 * [filter properties](https://adblockplus.org/jsdoc/adblockpluscore/Filter.ht
ml), |
39 * use `GetProperty()` to retrieve them by name. | 37 * use `GetProperty()` to retrieve them by name. |
40 */ | 38 */ |
41 class Filter : public JsValue, | 39 class Filter : public JsValue, |
42 public std::tr1::enable_shared_from_this<Filter> | 40 public std::enable_shared_from_this<Filter> |
43 { | 41 { |
44 public: | 42 public: |
45 /** | 43 /** |
46 * Filter types, see https://adblockplus.org/en/filters. | 44 * Filter types, see https://adblockplus.org/en/filters. |
47 */ | 45 */ |
48 enum Type {TYPE_BLOCKING, TYPE_EXCEPTION, | 46 enum Type {TYPE_BLOCKING, TYPE_EXCEPTION, |
49 TYPE_ELEMHIDE, TYPE_ELEMHIDE_EXCEPTION, | 47 TYPE_ELEMHIDE, TYPE_ELEMHIDE_EXCEPTION, |
50 TYPE_COMMENT, TYPE_INVALID}; | 48 TYPE_COMMENT, TYPE_INVALID}; |
51 | 49 |
52 /** | 50 /** |
(...skipping 25 matching lines...) Expand all Loading... |
78 * Normally you shouldn't call this directly, but use | 76 * Normally you shouldn't call this directly, but use |
79 * FilterEngine::GetFilter() instead. | 77 * FilterEngine::GetFilter() instead. |
80 * @param value JavaScript filter object. | 78 * @param value JavaScript filter object. |
81 */ | 79 */ |
82 Filter(JsValue&& value); | 80 Filter(JsValue&& value); |
83 }; | 81 }; |
84 | 82 |
85 /** | 83 /** |
86 * Wrapper for a subscription object. | 84 * Wrapper for a subscription object. |
87 * There are no accessors for most | 85 * There are no accessors for most |
88 * [subscription properties](https://adblockplus.org/jsdoc/adblockplus/symbols
/Subscription.html), | 86 * [subscription properties](https://adblockplus.org/jsdoc/adblockpluscore/Sub
scription.html), |
89 * use `GetProperty()` to retrieve them by name. | 87 * use `GetProperty()` to retrieve them by name. |
90 */ | 88 */ |
91 class Subscription : public JsValue, | 89 class Subscription : public JsValue, |
92 public std::tr1::enable_shared_from_this<Subscription> | 90 public std::enable_shared_from_this<Subscription> |
93 { | 91 { |
94 public: | 92 public: |
95 /** | 93 /** |
96 * Checks if this subscription has been added to the list of subscriptions. | 94 * Checks if this subscription has been added to the list of subscriptions. |
97 * @return `true` if this subscription has been added. | 95 * @return `true` if this subscription has been added. |
98 */ | 96 */ |
99 bool IsListed(); | 97 bool IsListed(); |
100 | 98 |
101 /** | 99 /** |
102 * Adds this subscription to the list of subscriptions. | 100 * Adds this subscription to the list of subscriptions. |
(...skipping 24 matching lines...) Expand all Loading... |
127 * Normally you shouldn't call this directly, but use | 125 * Normally you shouldn't call this directly, but use |
128 * FilterEngine::GetSubscription() instead. | 126 * FilterEngine::GetSubscription() instead. |
129 * @param value JavaScript subscription object. | 127 * @param value JavaScript subscription object. |
130 */ | 128 */ |
131 Subscription(JsValue&& value); | 129 Subscription(JsValue&& value); |
132 }; | 130 }; |
133 | 131 |
134 /** | 132 /** |
135 * Shared smart pointer to a `Filter` instance. | 133 * Shared smart pointer to a `Filter` instance. |
136 */ | 134 */ |
137 typedef std::tr1::shared_ptr<Filter> FilterPtr; | 135 typedef std::shared_ptr<Filter> FilterPtr; |
138 | 136 |
139 /** | 137 /** |
140 * Shared smart pointer to a `Subscription` instance. | 138 * Shared smart pointer to a `Subscription` instance. |
141 */ | 139 */ |
142 typedef std::tr1::shared_ptr<Subscription> SubscriptionPtr; | 140 typedef std::shared_ptr<Subscription> SubscriptionPtr; |
143 | 141 |
144 /** | 142 /** |
145 * Main component of libadblockplus. | 143 * Main component of libadblockplus. |
146 * It handles: | 144 * It handles: |
147 * - Filter management and matching. | 145 * - Filter management and matching. |
148 * - Subscription management and synchronization. | 146 * - Subscription management and synchronization. |
149 * - Update checks for the application. | 147 * - Update checks for the application. |
150 */ | 148 */ |
151 class FilterEngine | 149 class FilterEngine |
152 { | 150 { |
153 public: | 151 public: |
154 // Make sure to keep ContentType in sync with FilterEngine::contentTypes | 152 // Make sure to keep ContentType in sync with FilterEngine::contentTypes |
| 153 // and with RegExpFilter.typeMap from filterClasses.js. |
155 /** | 154 /** |
156 * Possible resource content types. | 155 * Possible resource content types. |
157 */ | 156 */ |
158 enum ContentType {CONTENT_TYPE_OTHER, CONTENT_TYPE_SCRIPT, | 157 enum ContentType |
159 CONTENT_TYPE_IMAGE, CONTENT_TYPE_STYLESHEET, | 158 { |
160 CONTENT_TYPE_OBJECT, CONTENT_TYPE_SUBDOCUMENT, | 159 CONTENT_TYPE_OTHER = 1, |
161 CONTENT_TYPE_DOCUMENT, CONTENT_TYPE_XMLHTTPREQUEST, | 160 CONTENT_TYPE_SCRIPT = 2, |
162 CONTENT_TYPE_OBJECT_SUBREQUEST, CONTENT_TYPE_FONT, | 161 CONTENT_TYPE_IMAGE = 4, |
163 CONTENT_TYPE_MEDIA, CONTENT_TYPE_ELEMHIDE}; | 162 CONTENT_TYPE_STYLESHEET = 8, |
| 163 CONTENT_TYPE_OBJECT = 16, |
| 164 CONTENT_TYPE_SUBDOCUMENT = 32, |
| 165 CONTENT_TYPE_DOCUMENT = 64, |
| 166 CONTENT_TYPE_PING = 1024, |
| 167 CONTENT_TYPE_XMLHTTPREQUEST = 2048, |
| 168 CONTENT_TYPE_OBJECT_SUBREQUEST = 4096, |
| 169 CONTENT_TYPE_MEDIA = 16384, |
| 170 CONTENT_TYPE_FONT = 32768, |
| 171 CONTENT_TYPE_GENERICBLOCK = 0x20000000, |
| 172 CONTENT_TYPE_ELEMHIDE = 0x40000000, |
| 173 CONTENT_TYPE_GENERICHIDE = 0x80000000 |
| 174 }; |
| 175 |
| 176 /** |
| 177 * Bitmask of `ContentType` values. |
| 178 * The underlying type is signed 32 bit integer because it is actually used |
| 179 * in JavaScript where it is converted into 32 bit signed integer. |
| 180 */ |
| 181 typedef int32_t ContentTypeMask; |
164 | 182 |
165 /** | 183 /** |
166 * Callback type invoked when an update becomes available. | 184 * Callback type invoked when an update becomes available. |
167 * The parameter is the download URL of the update. | 185 * The parameter is the download URL of the update. |
168 */ | 186 */ |
169 typedef std::tr1::function<void(const std::string&)> | 187 typedef std::function<void(const std::string&)> UpdateAvailableCallback; |
170 UpdateAvailableCallback; | |
171 | 188 |
172 /** | 189 /** |
173 * Callback type invoked when a manually triggered update check finishes. | 190 * Callback type invoked when a manually triggered update check finishes. |
174 * The parameter is an optional error message. | 191 * The parameter is an optional error message. |
175 */ | 192 */ |
176 typedef std::tr1::function<void(const std::string&)> | 193 typedef std::function<void(const std::string&)> UpdateCheckDoneCallback; |
177 UpdateCheckDoneCallback; | |
178 | 194 |
179 /** | 195 /** |
180 * Callback type invoked when the filters change. | 196 * Callback type invoked when the filters change. |
181 * The first parameter is the action event code (see | 197 * The first parameter is the action event code (see |
182 * [FilterNotifier.triggerListeners](https://adblockplus.org/jsdoc/adblockpl
us/symbols/FilterNotifier.html#.triggerListeners) | 198 * [FilterNotifier.triggerListeners](https://adblockplus.org/jsdoc/adblockpl
uscore/FilterNotifier.html#.triggerListeners) |
183 * for the full list). | 199 * for the full list). |
184 * The second parameter is the filter/subscription object affected, if any. | 200 * The second parameter is the filter/subscription object affected, if any. |
185 */ | 201 */ |
186 typedef std::tr1::function<void(const std::string&, const JsValuePtr)> Filte
rChangeCallback; | 202 typedef std::function<void(const std::string&, const JsValuePtr)> FilterChan
geCallback; |
| 203 |
| 204 /** |
| 205 * Container of name-value pairs representing a set of preferences. |
| 206 */ |
| 207 typedef std::map<std::string, AdblockPlus::JsValuePtr> Prefs; |
| 208 |
| 209 /** |
| 210 * Callback type invoked when a new notification should be shown. |
| 211 * The parameter is the Notification object to be shown. |
| 212 */ |
| 213 typedef std::function<void(const NotificationPtr&)> ShowNotificationCallback
; |
187 | 214 |
188 /** | 215 /** |
189 * Constructor. | 216 * Constructor. |
190 * @param jsEngine `JsEngine` instance used to run JavaScript code | 217 * @param jsEngine `JsEngine` instance used to run JavaScript code |
191 * internally. | 218 * internally. |
192 */ | 219 * @param preconfiguredPrefs `AdblockPlus::FilterEngine::Prefs` |
193 explicit FilterEngine(JsEnginePtr jsEngine); | 220 * name-value list of preconfigured prefs. |
| 221 */ |
| 222 explicit FilterEngine(JsEnginePtr jsEngine, |
| 223 const Prefs& preconfiguredPrefs = Prefs() |
| 224 ); |
194 | 225 |
195 /** | 226 /** |
196 * Retrieves the `JsEngine` instance associated with this `FilterEngine` | 227 * Retrieves the `JsEngine` instance associated with this `FilterEngine` |
197 * instance. | 228 * instance. |
198 */ | 229 */ |
199 JsEnginePtr GetJsEngine() const { return jsEngine; } | 230 JsEnginePtr GetJsEngine() const { return jsEngine; } |
200 | 231 |
201 /** | 232 /** |
202 * Checks if this is the first run of the application. | 233 * Checks if this is the first run of the application. |
203 * @return `true` if the application is running for the first time. | 234 * @return `true` if the application is running for the first time. |
(...skipping 27 matching lines...) Expand all Loading... |
231 */ | 262 */ |
232 std::vector<SubscriptionPtr> GetListedSubscriptions() const; | 263 std::vector<SubscriptionPtr> GetListedSubscriptions() const; |
233 | 264 |
234 /** | 265 /** |
235 * Retrieves all recommended subscriptions. | 266 * Retrieves all recommended subscriptions. |
236 * @return List of recommended subscriptions. | 267 * @return List of recommended subscriptions. |
237 */ | 268 */ |
238 std::vector<SubscriptionPtr> FetchAvailableSubscriptions() const; | 269 std::vector<SubscriptionPtr> FetchAvailableSubscriptions() const; |
239 | 270 |
240 /** | 271 /** |
241 * Determines which notification is to be shown next. | 272 * Invokes the listener set via SetNotificationAvailableCallback() with the |
| 273 * next notification to be shown. |
242 * @param url URL to match notifications to (optional). | 274 * @param url URL to match notifications to (optional). |
243 * @return Notification to be shown, or `null` if there is no any. | 275 */ |
244 */ | 276 void ShowNextNotification(const std::string& url = std::string()); |
245 NotificationPtr GetNextNotificationToShow( | 277 |
246 const std::string& url = std::string()); | 278 /** |
| 279 * Sets the callback invoked when a notification should be shown. |
| 280 * @param callback Callback to invoke. |
| 281 */ |
| 282 void SetShowNotificationCallback(const ShowNotificationCallback& value); |
| 283 |
| 284 /** |
| 285 * Removes the callback invoked when a notification should be shown. |
| 286 */ |
| 287 void RemoveShowNotificationCallback(); |
247 | 288 |
248 /** | 289 /** |
249 * Checks if any active filter matches the supplied URL. | 290 * Checks if any active filter matches the supplied URL. |
250 * @param url URL to match. | 291 * @param url URL to match. |
251 * @param contentType Content type of the requested resource. | 292 * @param contentTypeMask Content type mask of the requested resource. |
252 * @param documentUrl URL of the document requesting the resource. | 293 * @param documentUrl URL of the document requesting the resource. |
253 * Note that there will be more than one document if frames are | 294 * Note that there will be more than one document if frames are |
254 * involved, see | 295 * involved, see |
255 * Matches(const std::string&, const std::string&, const std::vector<
std::string>&) const. | 296 * Matches(const std::string&, const std::string&, const std::vector<
std::string>&) const. |
256 * @return Matching filter, or `null` if there was no match. | 297 * @return Matching filter, or `null` if there was no match. |
257 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. | 298 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. |
258 */ | 299 */ |
259 FilterPtr Matches(const std::string& url, | 300 FilterPtr Matches(const std::string& url, |
260 ContentType contentType, | 301 ContentTypeMask contentTypeMask, |
261 const std::string& documentUrl) const; | 302 const std::string& documentUrl) const; |
262 | 303 |
263 /** | 304 /** |
264 * Checks if any active filter matches the supplied URL. | 305 * Checks if any active filter matches the supplied URL. |
265 * @param url URL to match. | 306 * @param url URL to match. |
266 * @param contentType Content type of the requested resource. | 307 * @param contentTypeMask Content type mask of the requested resource. |
267 * @param documentUrls Chain of documents requesting the resource, starting | 308 * @param documentUrls Chain of documents requesting the resource, starting |
268 * with the current resource's parent frame, ending with the | 309 * with the current resource's parent frame, ending with the |
269 * top-level frame. | 310 * top-level frame. |
270 * If the application is not capable of identifying the frame | 311 * If the application is not capable of identifying the frame |
271 * structure, e.g. because it is a proxy, it can be approximated | 312 * structure, e.g. because it is a proxy, it can be approximated |
272 * using `ReferrerMapping`. | 313 * using `ReferrerMapping`. |
273 * @return Matching filter, or a `null` if there was no match. | 314 * @return Matching filter, or a `null` if there was no match. |
274 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. | 315 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. |
275 */ | 316 */ |
276 FilterPtr Matches(const std::string& url, | 317 FilterPtr Matches(const std::string& url, |
277 ContentType contentType, | 318 ContentTypeMask contentTypeMask, |
| 319 const std::vector<std::string>& documentUrls) const; |
| 320 |
| 321 /** |
| 322 * Checks whether the document at the supplied URL is whitelisted. |
| 323 * @param url URL of the document. |
| 324 * @param documentUrls Chain of document URLs requesting the document, |
| 325 * starting with the current document's parent frame, ending with |
| 326 * the top-level frame. |
| 327 * If the application is not capable of identifying the frame |
| 328 * structure, e.g. because it is a proxy, it can be approximated |
| 329 * using `ReferrerMapping`. |
| 330 * @return `true` if the URL is whitelisted. |
| 331 */ |
| 332 bool IsDocumentWhitelisted(const std::string& url, |
| 333 const std::vector<std::string>& documentUrls) const; |
| 334 |
| 335 /** |
| 336 * Checks whether element hiding is disabled at the supplied URL. |
| 337 * @param url URL of the document. |
| 338 * @param documentUrls Chain of document URLs requesting the document, |
| 339 * starting with the current document's parent frame, ending with |
| 340 * the top-level frame. |
| 341 * If the application is not capable of identifying the frame |
| 342 * structure, e.g. because it is a proxy, it can be approximated |
| 343 * using `ReferrerMapping`. |
| 344 * @return `true` if element hiding is whitelisted for the supplied URL. |
| 345 */ |
| 346 bool IsElemhideWhitelisted(const std::string& url, |
278 const std::vector<std::string>& documentUrls) const; | 347 const std::vector<std::string>& documentUrls) const; |
279 | 348 |
280 /** | 349 /** |
281 * Retrieves CSS selectors for all element hiding filters active on the | 350 * Retrieves CSS selectors for all element hiding filters active on the |
282 * supplied domain. | 351 * supplied domain. |
283 * @param domain Domain to retrieve CSS selectors for. | 352 * @param domain Domain to retrieve CSS selectors for. |
284 * @return List of CSS selectors. | 353 * @return List of CSS selectors. |
285 */ | 354 */ |
286 std::vector<std::string> GetElementHidingSelectors(const std::string& domain
) const; | 355 std::vector<std::string> GetElementHidingSelectors(const std::string& domain
) const; |
287 | 356 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 * `FilterEngine` will automatically check for updates in regular intervals, | 392 * `FilterEngine` will automatically check for updates in regular intervals, |
324 * so applications should only call this when the user triggers an update | 393 * so applications should only call this when the user triggers an update |
325 * check manually. | 394 * check manually. |
326 * @param callback Optional callback to invoke when the update check is | 395 * @param callback Optional callback to invoke when the update check is |
327 * finished. The string parameter will be empty when the update check | 396 * finished. The string parameter will be empty when the update check |
328 * succeeded, or contain an error message if it failed. | 397 * succeeded, or contain an error message if it failed. |
329 * Note that the callback will be invoked whether updates are | 398 * Note that the callback will be invoked whether updates are |
330 * available or not - to react to updates being available, use | 399 * available or not - to react to updates being available, use |
331 * `FilterEngine::SetUpdateAvailableCallback()`. | 400 * `FilterEngine::SetUpdateAvailableCallback()`. |
332 */ | 401 */ |
333 void ForceUpdateCheck(UpdateCheckDoneCallback callback = 0); | 402 void ForceUpdateCheck(UpdateCheckDoneCallback callback); |
334 | 403 |
335 /** | 404 /** |
336 * Sets the callback invoked when the filters change. | 405 * Sets the callback invoked when the filters change. |
337 * @param callback Callback to invoke. | 406 * @param callback Callback to invoke. |
338 */ | 407 */ |
339 void SetFilterChangeCallback(FilterChangeCallback callback); | 408 void SetFilterChangeCallback(FilterChangeCallback callback); |
340 | 409 |
341 /** | 410 /** |
342 * Removes the callback invoked when the filters change. | 411 * Removes the callback invoked when the filters change. |
343 */ | 412 */ |
(...skipping 29 matching lines...) Expand all Loading... |
373 | 442 |
374 private: | 443 private: |
375 JsEnginePtr jsEngine; | 444 JsEnginePtr jsEngine; |
376 bool initialized; | 445 bool initialized; |
377 bool firstRun; | 446 bool firstRun; |
378 int updateCheckId; | 447 int updateCheckId; |
379 static const std::map<ContentType, std::string> contentTypes; | 448 static const std::map<ContentType, std::string> contentTypes; |
380 | 449 |
381 void InitDone(JsValueList& params); | 450 void InitDone(JsValueList& params); |
382 FilterPtr CheckFilterMatch(const std::string& url, | 451 FilterPtr CheckFilterMatch(const std::string& url, |
383 ContentType contentType, | 452 ContentTypeMask contentTypeMask, |
384 const std::string& documentUrl) const; | 453 const std::string& documentUrl) const; |
385 void UpdateAvailable(UpdateAvailableCallback callback, JsValueList& params); | 454 void UpdateAvailable(UpdateAvailableCallback callback, JsValueList& params); |
386 void UpdateCheckDone(const std::string& eventName, | 455 void UpdateCheckDone(const std::string& eventName, |
387 UpdateCheckDoneCallback callback, JsValueList& params); | 456 UpdateCheckDoneCallback callback, JsValueList& params); |
388 void FilterChanged(FilterChangeCallback callback, JsValueList& params); | 457 void FilterChanged(FilterChangeCallback callback, JsValueList& params); |
| 458 void ShowNotification(const ShowNotificationCallback& callback, |
| 459 const JsValueList& params); |
| 460 FilterPtr GetWhitelistingFilter(const std::string& url, |
| 461 ContentTypeMask contentTypeMask, const std::string& documentUrl) const; |
| 462 FilterPtr GetWhitelistingFilter(const std::string& url, |
| 463 ContentTypeMask contentTypeMask, |
| 464 const std::vector<std::string>& documentUrls) const; |
389 }; | 465 }; |
390 } | 466 } |
391 | 467 |
392 #endif | 468 #endif |
LEFT | RIGHT |