Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: include/AdblockPlus/FilterEngine.h

Issue 5728380594946048: Issue 116 - Document the libadblockplus API (Closed)
Patch Set: Address all comments Created Sept. 1, 2014, 4:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 an Adblock Plus 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
48 /**
49 * Retrieves the type of this filter.
50 * @return Type of this filter.
51 */
42 Type GetType(); 52 Type GetType();
53
54 /**
55 * Checks whether this filter has been added to the list of custom filters.
56 * @return `true` if this filter has been added.
57 */
43 bool IsListed(); 58 bool IsListed();
59
60 /**
61 * Adds this filter to the list of custom filters.
62 */
44 void AddToList(); 63 void AddToList();
64
65 /**
66 * Removes this filter from the list of custom filters.
67 */
45 void RemoveFromList(); 68 void RemoveFromList();
69
46 bool operator==(const Filter& filter) const; 70 bool operator==(const Filter& filter) const;
47 71
72 /**
73 * Creates a wrapper for an existing JavaScript filter object.
74 * Normally you shouldn't call this directly, but use GetFilter() instead.
Wladimir Palant 2014/09/01 17:36:56 It's FilterEngine::GetFilter(), not Filter::GetFil
Felix Dahlke 2014/09/02 05:24:51 Done.
75 * @param value JavaScript filter object.
76 */
48 Filter(JsValuePtr value); 77 Filter(JsValuePtr value);
49 }; 78 };
50 79
80 /**
81 * Wrapper for a subscription object.
82 */
51 class Subscription : public JsValue, 83 class Subscription : public JsValue,
52 public std::tr1::enable_shared_from_this<Subscription> 84 public std::tr1::enable_shared_from_this<Subscription>
53 { 85 {
54 public: 86 public:
87 /**
88 * Checks if this subscription has been added to the list of subscriptions.
89 * @return `true` if this subscription has been added.
90 */
55 bool IsListed(); 91 bool IsListed();
92
93 /**
94 * Adds this subscription to the list of subscriptions.
95 */
56 void AddToList(); 96 void AddToList();
97
98 /**
99 * Removes this subscription from the list of subscriptions.
100 */
57 void RemoveFromList(); 101 void RemoveFromList();
102
103 /**
104 * Updates this subscription, i.e.\ retrieves the current filters from the
105 * subscription URL.
106 */
58 void UpdateFilters(); 107 void UpdateFilters();
108
109 /**
110 * Checks if this subscriptions is currently being updated.
111 * @return `true` if this subscriptions is currently being updated.
112 */
59 bool IsUpdating(); 113 bool IsUpdating();
114
60 bool operator==(const Subscription& subscription) const; 115 bool operator==(const Subscription& subscription) const;
61 116
117 /**
118 * Creates a wrapper for an existing JavaScript subscription object.
119 * Normally you shouldn't call this directly, but use GetSubscription()
Wladimir Palant 2014/09/01 17:36:56 It's FilterEngine::GetSubscription(), not Subscrip
Felix Dahlke 2014/09/02 05:24:51 Done.
120 * instead.
121 * @param value JavaScript subscription object.
122 */
62 Subscription(JsValuePtr value); 123 Subscription(JsValuePtr value);
63 }; 124 };
64 125
126 /**
127 * Shared smart pointer to a `Filter` instance.
128 */
65 typedef std::tr1::shared_ptr<Filter> FilterPtr; 129 typedef std::tr1::shared_ptr<Filter> FilterPtr;
130
131 /**
132 * Shared smart pointer to a `Subscription` instance.
133 */
66 typedef std::tr1::shared_ptr<Subscription> SubscriptionPtr; 134 typedef std::tr1::shared_ptr<Subscription> SubscriptionPtr;
67 135
136 /**
137 * Main component of Libadblockplus.
138 * It handles:
139 * - Filter management and matching.
140 * - Subscription management and synchronization.
141 * - Update checks for the application.
142 */
68 class FilterEngine 143 class FilterEngine
69 { 144 {
70 public: 145 public:
146 /**
147 * Callback type invoked when an update is available.
148 * The parameter is optional error message.
149 */
71 typedef std::tr1::function<void(const std::string&)> UpdaterCallback; 150 typedef std::tr1::function<void(const std::string&)> UpdaterCallback;
151
152 /**
153 * Callback type invoked when the filters change.
154 * The first parameter is the action event code (see
155 * [FilterNotifier.triggerListeners](https://adblockplus.org/jsdoc/adblockpl us/symbols/FilterNotifier.html#.triggerListeners)
156 * for the full list).
157 * The second parameter is the filter/subscription object affected, if any.
158 */
72 typedef std::tr1::function<void(const std::string&, const JsValuePtr)> Filte rChangeCallback; 159 typedef std::tr1::function<void(const std::string&, const JsValuePtr)> Filte rChangeCallback;
73 160
161 /**
162 * Constructor.
163 * @param jsEngine `JsEngine` instance used to run JavaScript code
164 * internally.
165 */
74 explicit FilterEngine(JsEnginePtr jsEngine); 166 explicit FilterEngine(JsEnginePtr jsEngine);
167
168 /**
169 * Retrieves the `JsEngine` instance associated with this `FilterEngine`
170 * instance.
171 */
75 JsEnginePtr GetJsEngine() const { return jsEngine; } 172 JsEnginePtr GetJsEngine() const { return jsEngine; }
173
174 /**
175 * Checks if this is the first run of the application.
176 * @return `true` if the application is running for the first time.
177 */
76 bool IsFirstRun() const; 178 bool IsFirstRun() const;
179
180 /**
181 * Retrieves a filter object from its text representation.
182 * @param text Text representation of the filter,
183 * see https://adblockplus.org/en/filters.
184 * @return New `Filter` instance.
185 */
77 FilterPtr GetFilter(const std::string& text); 186 FilterPtr GetFilter(const std::string& text);
187
188 /**
189 * Retrieves a subscription object for the supplied URL.
190 * @param url Subscription URL.
191 * @return New `Subscription` instance.
192 */
78 SubscriptionPtr GetSubscription(const std::string& url); 193 SubscriptionPtr GetSubscription(const std::string& url);
194
195 /**
196 * Retrieves the list of custom filters.
197 * @return List of custom filters.
198 */
79 std::vector<FilterPtr> GetListedFilters() const; 199 std::vector<FilterPtr> GetListedFilters() const;
200
201 /**
202 * Retrieves all subscriptions.
203 * @return List of subscriptions.
204 */
80 std::vector<SubscriptionPtr> GetListedSubscriptions() const; 205 std::vector<SubscriptionPtr> GetListedSubscriptions() const;
206
207 /**
208 * Retrieves all recommended subscriptions.
209 * @return List of recommended subscriptions.
210 */
81 std::vector<SubscriptionPtr> FetchAvailableSubscriptions() const; 211 std::vector<SubscriptionPtr> FetchAvailableSubscriptions() 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 documentUrl URL of the document requesting the resource.
230 * Note that there will be more than one document if frames are
231 * involved, see
232 * Matches(const std::string&, const std::string&, const std::vector< std::string>&) const.
233 * @return Matching filter, or `null` if there was no match.
234 */
82 FilterPtr Matches(const std::string& url, 235 FilterPtr Matches(const std::string& url,
83 const std::string& contentType, 236 const std::string& contentType,
84 const std::string& documentUrl) const; 237 const std::string& documentUrl) const;
238
239 /**
240 * Checks if any active filter matches the supplied URL.
241 * @param url URL to match.
242 * @param contentType Content type of the requested resource, possible
243 * values are:
244 * - OTHER
245 * - SCRIPT
246 * - IMAGE
247 * - STYLESHEET
248 * - OBJECT
249 * - SUBDOCUMENT
250 * - DOCUMENT
251 * - XMLHTTPREQUEST
252 * - OBJECT_SUBREQUEST
253 * - FONT
254 * - MEDIA
255 * @param documentUrls Chain of documents requesting the resource, starting
256 * with the current resource's parent frame, ending with the
257 * top-level frame.
258 * If the application is not capable of identifying the frame
259 * structure, e.g. because it is a proxy, it can be approximated
260 * using `ReferrerMapping`.
261 * @return Matching filter, or a `null` if there was no match.
262 */
85 FilterPtr Matches(const std::string& url, 263 FilterPtr Matches(const std::string& url,
86 const std::string& contentType, 264 const std::string& contentType,
87 const std::vector<std::string>& documentUrls) const; 265 const std::vector<std::string>& documentUrls) const;
266
267 /**
268 * Retrieves CSS selectors for all element hiding filters active on the
269 * supplied domain.
270 * @param domain Domain to retrieve CSS selectors for.
271 * @return List of CSS selectors.
272 */
88 std::vector<std::string> GetElementHidingSelectors(const std::string& domain ) const; 273 std::vector<std::string> GetElementHidingSelectors(const std::string& domain ) const;
274
275 /**
276 * Retrieves a preference value.
277 * @param pref Preference name.
278 * @return Preference value, or `null` if it doesn't exist.
279 */
89 JsValuePtr GetPref(const std::string& pref) const; 280 JsValuePtr GetPref(const std::string& pref) const;
281
282 /**
283 * Sets a preference value.
284 * @param pref Preference name.
285 * @param value New value of the preference.
286 */
90 void SetPref(const std::string& pref, JsValuePtr value); 287 void SetPref(const std::string& pref, JsValuePtr value);
288
289 /**
290 * Extracts the host from a URL.
291 * @param url URL to extract the host from.
292 * @return Extracted host.
293 */
91 std::string GetHostFromURL(const std::string& url); 294 std::string GetHostFromURL(const std::string& url);
295
296 /**
297 * Forces an immediate update check.
298 * `FilterEngine` will automatically check for updates in regular intervals,
299 * so applications should only call this when the user triggers an update
300 * check manually.
301 * @param callback Optional callback to invoke when the update check is
302 * finished. The string parameter will be empty when the update check
303 * succeeded, or contain an error message if it failed.
304 * Note that the callback will be invoked after every update check -
305 * to react to updates being available, register a callback for the
306 * "updateAvailable" event (see JsEngine::SetEventCallback()).
307 */
92 void ForceUpdateCheck(UpdaterCallback callback = 0); 308 void ForceUpdateCheck(UpdaterCallback callback = 0);
309
310 /**
311 * Sets the callback invoked when the filters change.
312 * @param callback Callback to invoke.
313 */
93 void SetFilterChangeCallback(FilterChangeCallback callback); 314 void SetFilterChangeCallback(FilterChangeCallback callback);
315
316 /**
317 * Removes the callback invoked when the filters change.
318 */
94 void RemoveFilterChangeCallback(); 319 void RemoveFilterChangeCallback();
320
321 /**
322 * Compares two version strings in
323 * [Mozilla toolkit version format](https://developer.mozilla.org/en/docs/To olkit_version_format).
324 * @param v1 First version string.
325 * @param v2 Second version string.
326 * @return
327 * - `0` if `v1` and `v2` are identical.
328 * - A negative number if `v1` is less than `v2`.
329 * - A positive number if `v1` is greater than `v2`.
330 */
95 int CompareVersions(const std::string& v1, const std::string& v2); 331 int CompareVersions(const std::string& v1, const std::string& v2);
96 332
97 private: 333 private:
98 JsEnginePtr jsEngine; 334 JsEnginePtr jsEngine;
99 bool initialized; 335 bool initialized;
100 bool firstRun; 336 bool firstRun;
101 int updateCheckId; 337 int updateCheckId;
102 338
103 void InitDone(JsValueList& params); 339 void InitDone(JsValueList& params);
104 FilterPtr CheckFilterMatch(const std::string& url, 340 FilterPtr CheckFilterMatch(const std::string& url,
105 const std::string& contentType, 341 const std::string& contentType,
106 const std::string& documentUrl) const; 342 const std::string& documentUrl) const;
107 void UpdateCheckDone(const std::string& eventName, UpdaterCallback callback, JsValueList& params); 343 void UpdateCheckDone(const std::string& eventName, UpdaterCallback callback, JsValueList& params);
108 void FilterChanged(FilterChangeCallback callback, JsValueList& params); 344 void FilterChanged(FilterChangeCallback callback, JsValueList& params);
109 }; 345 };
110 } 346 }
111 347
112 #endif 348 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld