Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 <memory> | |
24 #include <string> | 23 #include <string> |
25 #include <vector> | 24 #include <vector> |
26 #include <AdblockPlus/JsEngine.h> | 25 #include <AdblockPlus/JsEngine.h> |
27 #include <AdblockPlus/JsValue.h> | 26 #include <AdblockPlus/JsValue.h> |
27 #include <AdblockPlus/Notification.h> | |
28 | 28 |
29 namespace AdblockPlus | 29 namespace AdblockPlus |
30 { | 30 { |
31 class FilterEngine; | 31 class FilterEngine; |
32 | 32 |
33 /** | |
34 * Wrapper for an Adblock Plus filter object. | |
35 * There are no accessors for most | |
36 * [filter properties](https://adblockplus.org/jsdoc/adblockplus/symbols/Filte r.html), | |
37 * use `GetProperty()` to retrieve them by name. | |
38 */ | |
33 class Filter : public JsValue, | 39 class Filter : public JsValue, |
34 public std::enable_shared_from_this<Filter> | 40 public std::enable_shared_from_this<Filter> |
35 { | 41 { |
36 public: | 42 public: |
43 /** | |
44 * Filter types, see https://adblockplus.org/en/filters. | |
45 */ | |
37 enum Type {TYPE_BLOCKING, TYPE_EXCEPTION, | 46 enum Type {TYPE_BLOCKING, TYPE_EXCEPTION, |
38 TYPE_ELEMHIDE, TYPE_ELEMHIDE_EXCEPTION, | 47 TYPE_ELEMHIDE, TYPE_ELEMHIDE_EXCEPTION, |
39 TYPE_COMMENT, TYPE_INVALID}; | 48 TYPE_COMMENT, TYPE_INVALID}; |
40 | 49 |
50 /** | |
51 * Retrieves the type of this filter. | |
52 * @return Type of this filter. | |
53 */ | |
41 Type GetType(); | 54 Type GetType(); |
55 | |
56 /** | |
57 * Checks whether this filter has been added to the list of custom filters. | |
58 * @return `true` if this filter has been added. | |
59 */ | |
42 bool IsListed(); | 60 bool IsListed(); |
61 | |
62 /** | |
63 * Adds this filter to the list of custom filters. | |
64 */ | |
43 void AddToList(); | 65 void AddToList(); |
66 | |
67 /** | |
68 * Removes this filter from the list of custom filters. | |
69 */ | |
44 void RemoveFromList(); | 70 void RemoveFromList(); |
71 | |
45 bool operator==(const Filter& filter) const; | 72 bool operator==(const Filter& filter) const; |
46 | 73 |
74 /** | |
75 * Creates a wrapper for an existing JavaScript filter object. | |
76 * Normally you shouldn't call this directly, but use | |
77 * FilterEngine::GetFilter() instead. | |
78 * @param value JavaScript filter object. | |
79 */ | |
47 Filter(JsValuePtr value); | 80 Filter(JsValuePtr value); |
48 }; | 81 }; |
49 | 82 |
83 /** | |
84 * Wrapper for a subscription object. | |
85 * There are no accessors for most | |
86 * [subscription properties](https://adblockplus.org/jsdoc/adblockplus/symbols /Subscription.html), | |
87 * use `GetProperty()` to retrieve them by name. | |
88 */ | |
50 class Subscription : public JsValue, | 89 class Subscription : public JsValue, |
51 public std::enable_shared_from_this<Subscription> | 90 public std::enable_shared_from_this<Subscription> |
52 { | 91 { |
53 public: | 92 public: |
93 /** | |
94 * Checks if this subscription has been added to the list of subscriptions. | |
95 * @return `true` if this subscription has been added. | |
96 */ | |
54 bool IsListed(); | 97 bool IsListed(); |
98 | |
99 /** | |
100 * Adds this subscription to the list of subscriptions. | |
101 */ | |
55 void AddToList(); | 102 void AddToList(); |
103 | |
104 /** | |
105 * Removes this subscription from the list of subscriptions. | |
106 */ | |
56 void RemoveFromList(); | 107 void RemoveFromList(); |
108 | |
109 /** | |
110 * Updates this subscription, i.e.\ retrieves the current filters from the | |
111 * subscription URL. | |
112 */ | |
57 void UpdateFilters(); | 113 void UpdateFilters(); |
114 | |
115 /** | |
116 * Checks if the subscription is currently being updated. | |
117 * @return `true` if the subscription is currently being updated. | |
118 */ | |
58 bool IsUpdating(); | 119 bool IsUpdating(); |
120 | |
59 bool operator==(const Subscription& subscription) const; | 121 bool operator==(const Subscription& subscription) const; |
60 | 122 |
123 /** | |
124 * Creates a wrapper for an existing JavaScript subscription object. | |
125 * Normally you shouldn't call this directly, but use | |
126 * FilterEngine::GetSubscription() instead. | |
127 * @param value JavaScript subscription object. | |
128 */ | |
61 Subscription(JsValuePtr value); | 129 Subscription(JsValuePtr value); |
62 }; | 130 }; |
63 | 131 |
132 /** | |
133 * Shared smart pointer to a `Filter` instance. | |
134 */ | |
64 typedef std::shared_ptr<Filter> FilterPtr; | 135 typedef std::shared_ptr<Filter> FilterPtr; |
136 | |
137 /** | |
138 * Shared smart pointer to a `Subscription` instance. | |
139 */ | |
65 typedef std::shared_ptr<Subscription> SubscriptionPtr; | 140 typedef std::shared_ptr<Subscription> SubscriptionPtr; |
66 | 141 |
142 /** | |
143 * Main component of libadblockplus. | |
144 * It handles: | |
145 * - Filter management and matching. | |
146 * - Subscription management and synchronization. | |
147 * - Update checks for the application. | |
148 */ | |
67 class FilterEngine | 149 class FilterEngine |
68 { | 150 { |
69 public: | 151 public: |
70 typedef std::function<void(const std::string&)> UpdaterCallback; | 152 // Make sure to keep ContentType in sync with FilterEngine::contentTypes |
153 /** | |
154 * Possible resource content types. | |
155 */ | |
156 enum ContentType {CONTENT_TYPE_OTHER, CONTENT_TYPE_SCRIPT, | |
157 CONTENT_TYPE_IMAGE, CONTENT_TYPE_STYLESHEET, | |
158 CONTENT_TYPE_OBJECT, CONTENT_TYPE_SUBDOCUMENT, | |
159 CONTENT_TYPE_DOCUMENT, CONTENT_TYPE_XMLHTTPREQUEST, | |
160 CONTENT_TYPE_OBJECT_SUBREQUEST, CONTENT_TYPE_FONT, | |
161 CONTENT_TYPE_MEDIA, CONTENT_TYPE_ELEMHIDE}; | |
162 | |
163 /** | |
164 * Callback type invoked when an update becomes available. | |
165 * The parameter is the download URL of the update. | |
166 */ | |
167 typedef std::function<void(const std::string&)> UpdateAvailableCallback; | |
168 | |
169 /** | |
170 * Callback type invoked when a manually triggered update check finishes. | |
171 * The parameter is an optional error message. | |
172 */ | |
173 typedef std::function<void(const std::string&)> UpdateCheckDoneCallback; | |
174 | |
175 /** | |
176 * Callback type invoked when the filters change. | |
177 * The first parameter is the action event code (see | |
178 * [FilterNotifier.triggerListeners](https://adblockplus.org/jsdoc/adblockpl us/symbols/FilterNotifier.html#.triggerListeners) | |
179 * for the full list). | |
180 * The second parameter is the filter/subscription object affected, if any. | |
181 */ | |
71 typedef std::function<void(const std::string&, const JsValuePtr)> FilterChan geCallback; | 182 typedef std::function<void(const std::string&, const JsValuePtr)> FilterChan geCallback; |
72 | 183 |
73 explicit FilterEngine(JsEnginePtr jsEngine); | 184 /** |
185 * Container of name-value pairs representing a set of preferences. | |
186 */ | |
187 typedef std::map<std::string, AdblockPlus::JsValuePtr> Prefs; | |
188 | |
189 /** | |
190 * Callback type invoked when a new notification should be shown. | |
191 * The parameter is the Notification object to be shown. | |
192 */ | |
193 typedef std::function<void(const NotificationPtr&)> ShowNotificationCallback ; | |
194 | |
195 /** | |
196 * Constructor. | |
197 * @param jsEngine `JsEngine` instance used to run JavaScript code | |
198 * internally. | |
199 * @param preconfiguredPrefs `AdblockPlus::FilterEngine::Prefs` | |
200 * name-value list of preconfigured prefs. | |
201 */ | |
202 explicit FilterEngine(JsEnginePtr jsEngine, | |
203 const Prefs& preconfiguredPrefs = Prefs() | |
204 ); | |
205 | |
206 /** | |
207 * Retrieves the `JsEngine` instance associated with this `FilterEngine` | |
208 * instance. | |
209 */ | |
74 JsEnginePtr GetJsEngine() const { return jsEngine; } | 210 JsEnginePtr GetJsEngine() const { return jsEngine; } |
211 | |
212 /** | |
213 * Checks if this is the first run of the application. | |
214 * @return `true` if the application is running for the first time. | |
215 */ | |
75 bool IsFirstRun() const; | 216 bool IsFirstRun() const; |
217 | |
218 /** | |
219 * Retrieves a filter object from its text representation. | |
220 * @param text Text representation of the filter, | |
221 * see https://adblockplus.org/en/filters. | |
222 * @return New `Filter` instance. | |
223 */ | |
76 FilterPtr GetFilter(const std::string& text); | 224 FilterPtr GetFilter(const std::string& text); |
225 | |
226 /** | |
227 * Retrieves a subscription object for the supplied URL. | |
228 * @param url Subscription URL. | |
229 * @return New `Subscription` instance. | |
230 */ | |
77 SubscriptionPtr GetSubscription(const std::string& url); | 231 SubscriptionPtr GetSubscription(const std::string& url); |
232 | |
233 /** | |
234 * Retrieves the list of custom filters. | |
235 * @return List of custom filters. | |
236 */ | |
78 std::vector<FilterPtr> GetListedFilters() const; | 237 std::vector<FilterPtr> GetListedFilters() const; |
238 | |
239 /** | |
240 * Retrieves all subscriptions. | |
241 * @return List of subscriptions. | |
242 */ | |
79 std::vector<SubscriptionPtr> GetListedSubscriptions() const; | 243 std::vector<SubscriptionPtr> GetListedSubscriptions() const; |
244 | |
245 /** | |
246 * Retrieves all recommended subscriptions. | |
247 * @return List of recommended subscriptions. | |
248 */ | |
80 std::vector<SubscriptionPtr> FetchAvailableSubscriptions() const; | 249 std::vector<SubscriptionPtr> FetchAvailableSubscriptions() const; |
250 | |
251 /** | |
252 * Invokes the listener set via SetNotificationAvailableCallback() with the | |
253 * next notification to be shown. | |
254 * @param url URL to match notifications to (optional). | |
255 */ | |
256 void ShowNextNotification(const std::string& url = std::string()); | |
257 | |
258 /** | |
259 * Sets the callback invoked when a notification should be shown. | |
260 * @param callback Callback to invoke. | |
261 */ | |
262 void SetShowNotificationCallback(const ShowNotificationCallback& value); | |
263 | |
264 /** | |
265 * Removes the callback invoked when a notification should be shown. | |
266 */ | |
267 void RemoveShowNotificationCallback(); | |
268 | |
269 /** | |
270 * Checks if any active filter matches the supplied URL. | |
271 * @param url URL to match. | |
272 * @param contentType Content type of the requested resource. | |
273 * @param documentUrl URL of the document requesting the resource. | |
274 * Note that there will be more than one document if frames are | |
275 * involved, see | |
276 * Matches(const std::string&, const std::string&, const std::vector< std::string>&) const. | |
277 * @return Matching filter, or `null` if there was no match. | |
278 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. | |
279 */ | |
81 FilterPtr Matches(const std::string& url, | 280 FilterPtr Matches(const std::string& url, |
82 const std::string& contentType, | 281 ContentType contentType, |
83 const std::string& documentUrl) const; | 282 const std::string& documentUrl) const; |
283 | |
284 /** | |
285 * Checks if any active filter matches the supplied URL. | |
286 * @param url URL to match. | |
287 * @param contentType Content type of the requested resource. | |
288 * @param documentUrls Chain of documents requesting the resource, starting | |
289 * with the current resource's parent frame, ending with the | |
290 * top-level frame. | |
291 * If the application is not capable of identifying the frame | |
292 * structure, e.g. because it is a proxy, it can be approximated | |
293 * using `ReferrerMapping`. | |
294 * @return Matching filter, or a `null` if there was no match. | |
295 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. | |
296 */ | |
84 FilterPtr Matches(const std::string& url, | 297 FilterPtr Matches(const std::string& url, |
85 const std::string& contentType, | 298 ContentType contentType, |
86 const std::vector<std::string>& documentUrls) const; | 299 const std::vector<std::string>& documentUrls) const; |
300 | |
301 /** | |
302 * Retrieves CSS selectors for all element hiding filters active on the | |
303 * supplied domain. | |
304 * @param domain Domain to retrieve CSS selectors for. | |
305 * @return List of CSS selectors. | |
306 */ | |
87 std::vector<std::string> GetElementHidingSelectors(const std::string& domain ) const; | 307 std::vector<std::string> GetElementHidingSelectors(const std::string& domain ) const; |
308 | |
309 /** | |
310 * Retrieves a preference value. | |
311 * @param pref Preference name. | |
312 * @return Preference value, or `null` if it doesn't exist. | |
313 */ | |
88 JsValuePtr GetPref(const std::string& pref) const; | 314 JsValuePtr GetPref(const std::string& pref) const; |
315 | |
316 /** | |
317 * Sets a preference value. | |
318 * @param pref Preference name. | |
319 * @param value New value of the preference. | |
320 */ | |
89 void SetPref(const std::string& pref, JsValuePtr value); | 321 void SetPref(const std::string& pref, JsValuePtr value); |
322 | |
323 /** | |
324 * Extracts the host from a URL. | |
325 * @param url URL to extract the host from. | |
326 * @return Extracted host. | |
327 */ | |
90 std::string GetHostFromURL(const std::string& url); | 328 std::string GetHostFromURL(const std::string& url); |
91 void ForceUpdateCheck(UpdaterCallback callback); | 329 |
Wladimir Palant
2014/07/11 14:29:11
Actually, there is one more actual code change her
sergei
2015/01/06 13:51:39
Actually, when do we need a default value here?
R
Felix Dahlke
2015/08/04 11:21:49
Probably some subtle differences between std::func
| |
330 /** | |
331 * Sets the callback invoked when an application update becomes available. | |
332 * @param callback Callback to invoke. | |
333 */ | |
334 void SetUpdateAvailableCallback(UpdateAvailableCallback callback); | |
335 | |
336 /** | |
337 * Removes the callback invoked when an application update becomes | |
338 * available. | |
339 */ | |
340 void RemoveUpdateAvailableCallback(); | |
341 | |
342 /** | |
343 * Forces an immediate update check. | |
344 * `FilterEngine` will automatically check for updates in regular intervals, | |
345 * so applications should only call this when the user triggers an update | |
346 * check manually. | |
347 * @param callback Optional callback to invoke when the update check is | |
348 * finished. The string parameter will be empty when the update check | |
349 * succeeded, or contain an error message if it failed. | |
350 * Note that the callback will be invoked whether updates are | |
351 * available or not - to react to updates being available, use | |
352 * `FilterEngine::SetUpdateAvailableCallback()`. | |
353 */ | |
354 void ForceUpdateCheck(UpdateCheckDoneCallback callback); | |
355 | |
356 /** | |
357 * Sets the callback invoked when the filters change. | |
358 * @param callback Callback to invoke. | |
359 */ | |
92 void SetFilterChangeCallback(FilterChangeCallback callback); | 360 void SetFilterChangeCallback(FilterChangeCallback callback); |
361 | |
362 /** | |
363 * Removes the callback invoked when the filters change. | |
364 */ | |
93 void RemoveFilterChangeCallback(); | 365 void RemoveFilterChangeCallback(); |
366 | |
367 /** | |
368 * Compares two version strings in | |
369 * [Mozilla toolkit version format](https://developer.mozilla.org/en/docs/To olkit_version_format). | |
370 * @param v1 First version string. | |
371 * @param v2 Second version string. | |
372 * @return | |
373 * - `0` if `v1` and `v2` are identical. | |
374 * - A negative number if `v1` is less than `v2`. | |
375 * - A positive number if `v1` is greater than `v2`. | |
376 */ | |
377 int CompareVersions(const std::string& v1, const std::string& v2); | |
378 | |
379 /** | |
380 * Retrieves the `ContentType` for the supplied string. | |
381 * @param contentType Content type string. | |
382 * @return The `ContentType` for the string. | |
383 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. | |
384 */ | |
385 static ContentType StringToContentType(const std::string& contentType); | |
386 | |
387 /** | |
388 * Retrieves the string representation of the supplied `ContentType`. | |
389 * @param contentType `ContentType` value. | |
390 * @return The string representation of `contentType`. | |
391 * @throw `std::invalid_argument`, if an invalid `contentType` was supplied. | |
392 */ | |
393 static std::string ContentTypeToString(ContentType contentType); | |
94 | 394 |
95 private: | 395 private: |
96 JsEnginePtr jsEngine; | 396 JsEnginePtr jsEngine; |
97 bool initialized; | 397 bool initialized; |
98 bool firstRun; | 398 bool firstRun; |
99 int updateCheckId; | 399 int updateCheckId; |
400 static const std::map<ContentType, std::string> contentTypes; | |
100 | 401 |
101 void InitDone(JsValueList& params); | 402 void InitDone(JsValueList& params); |
102 FilterPtr CheckFilterMatch(const std::string& url, | 403 FilterPtr CheckFilterMatch(const std::string& url, |
103 const std::string& contentType, | 404 ContentType contentType, |
104 const std::string& documentUrl) const; | 405 const std::string& documentUrl) const; |
105 void UpdateCheckDone(const std::string& eventName, UpdaterCallback callback, JsValueList& params); | 406 void UpdateAvailable(UpdateAvailableCallback callback, JsValueList& params); |
407 void UpdateCheckDone(const std::string& eventName, | |
408 UpdateCheckDoneCallback callback, JsValueList& params); | |
106 void FilterChanged(FilterChangeCallback callback, JsValueList& params); | 409 void FilterChanged(FilterChangeCallback callback, JsValueList& params); |
410 void ShowNotification(const ShowNotificationCallback& callback, | |
411 const JsValueList& params); | |
107 }; | 412 }; |
108 } | 413 } |
109 | 414 |
110 #endif | 415 #endif |
LEFT | RIGHT |