OLD | NEW |
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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 18 matching lines...) Expand all Loading... |
29 /** | 29 /** |
30 * Abstract base class for filter subscriptions | 30 * Abstract base class for filter subscriptions |
31 * | 31 * |
32 * @param {string} url download location of the subscription | 32 * @param {string} url download location of the subscription |
33 * @param {string} [title] title of the filter subscription | 33 * @param {string} [title] title of the filter subscription |
34 * @constructor | 34 * @constructor |
35 */ | 35 */ |
36 function Subscription(url, title) | 36 function Subscription(url, title) |
37 { | 37 { |
38 this.url = url; | 38 this.url = url; |
39 this.filters = []; | 39 |
| 40 this._filterText = []; |
| 41 this._filters = []; |
| 42 |
| 43 this._filterTextSet = new Set(); |
| 44 |
40 if (title) | 45 if (title) |
41 this._title = title; | 46 this._title = title; |
| 47 |
42 Subscription.knownSubscriptions.set(url, this); | 48 Subscription.knownSubscriptions.set(url, this); |
43 } | 49 } |
44 exports.Subscription = Subscription; | 50 exports.Subscription = Subscription; |
45 | 51 |
46 Subscription.prototype = | 52 Subscription.prototype = |
47 { | 53 { |
48 /** | 54 /** |
49 * Download location of the subscription | 55 * Download location of the subscription |
50 * @type {string} | 56 * @type {string} |
51 */ | 57 */ |
52 url: null, | 58 url: null, |
53 | 59 |
54 /** | 60 /** |
55 * Type of the subscription | 61 * Type of the subscription |
56 * @type {?string} | 62 * @type {?string} |
57 */ | 63 */ |
58 type: null, | 64 type: null, |
59 | 65 |
60 /** | 66 /** |
61 * Filters contained in the filter subscription | 67 * Filter text contained in the filter subscription. |
62 * @type {Filter[]} | 68 * @type {Array.<string>} |
| 69 * @private |
63 */ | 70 */ |
64 filters: null, | 71 _filterText: null, |
| 72 |
| 73 /** |
| 74 * {@link Filter} objects corresponding to the subscription's filter text. |
| 75 * @type {Array.<Filter>} |
| 76 * @private |
| 77 */ |
| 78 _filters: null, |
| 79 |
| 80 /** |
| 81 * Set of filter text contained in the filter subscription, used for faster |
| 82 * lookup. |
| 83 * @type {Set.<string>} |
| 84 * @private |
| 85 */ |
| 86 _filterTextSet: null, |
65 | 87 |
66 _title: null, | 88 _title: null, |
67 _fixedTitle: false, | 89 _fixedTitle: false, |
68 _disabled: false, | 90 _disabled: false, |
69 | 91 |
70 /** | 92 /** |
71 * Title of the filter subscription | 93 * Title of the filter subscription |
72 * @type {string} | 94 * @type {string} |
73 */ | 95 */ |
74 get title() | 96 get title() |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 if (value != this._disabled) | 140 if (value != this._disabled) |
119 { | 141 { |
120 let oldValue = this._disabled; | 142 let oldValue = this._disabled; |
121 this._disabled = value; | 143 this._disabled = value; |
122 filterNotifier.emit("subscription.disabled", this, value, oldValue); | 144 filterNotifier.emit("subscription.disabled", this, value, oldValue); |
123 } | 145 } |
124 return this._disabled; | 146 return this._disabled; |
125 }, | 147 }, |
126 | 148 |
127 /** | 149 /** |
| 150 * The number of filters in the subscription. |
| 151 * @type {number} |
| 152 */ |
| 153 get filterCount() |
| 154 { |
| 155 return this._filters.length; |
| 156 }, |
| 157 |
| 158 /** |
| 159 * Yields the text for each filter in the subscription. |
| 160 * @yields {string} |
| 161 */ |
| 162 *filterText() |
| 163 { |
| 164 yield* this._filterText; |
| 165 }, |
| 166 |
| 167 /** |
| 168 * Yields the {@link Filter} object for each filter in the subscription. |
| 169 * @yields {Filter} |
| 170 */ |
| 171 *filters() |
| 172 { |
| 173 yield* this._filters; |
| 174 }, |
| 175 |
| 176 /** |
| 177 * Returns the {@link Filter} object at the given 0-based index. |
| 178 * @param {number} index |
| 179 * @returns {?Filter} |
| 180 */ |
| 181 filterAt(index) |
| 182 { |
| 183 return this._filters[index] || null; |
| 184 }, |
| 185 |
| 186 /** |
| 187 * Returns the 0-based index of the given filter. |
| 188 * @param {Filter} filter |
| 189 * @param {number} [fromIndex] The index from which to start the search. |
| 190 * @return {number} |
| 191 */ |
| 192 searchFilter(filter, fromIndex = 0) |
| 193 { |
| 194 return this._filterText.indexOf(filter.text, fromIndex); |
| 195 }, |
| 196 |
| 197 /** |
| 198 * Checks whether the subscription contains the given filter. |
| 199 * @param {Filter} filter |
| 200 * @return {boolean} |
| 201 */ |
| 202 hasFilter(filter) |
| 203 { |
| 204 return this._filterTextSet.has(filter.text); |
| 205 }, |
| 206 |
| 207 /** |
| 208 * Removes all filters from the subscription. |
| 209 */ |
| 210 clearFilters() |
| 211 { |
| 212 this._filterText = []; |
| 213 this._filters = []; |
| 214 |
| 215 this._filterTextSet.clear(); |
| 216 }, |
| 217 |
| 218 /** |
| 219 * Adds a filter to the subscription. |
| 220 * @param {Filter} filter |
| 221 */ |
| 222 addFilter(filter) |
| 223 { |
| 224 this._filterText.push(filter.text); |
| 225 this._filters.push(filter); |
| 226 |
| 227 this._filterTextSet.add(filter.text); |
| 228 }, |
| 229 |
| 230 /** |
| 231 * Inserts a filter into the subscription. |
| 232 * @param {Filter} filter |
| 233 * @param {number} index The index at which to insert the filter. |
| 234 */ |
| 235 insertFilterAt(filter, index) |
| 236 { |
| 237 this._filterText.splice(index, 0, filter.text); |
| 238 this._filters.splice(index, 0, filter); |
| 239 |
| 240 this._filterTextSet.add(filter.text); |
| 241 }, |
| 242 |
| 243 /** |
| 244 * Deletes a filter from the subscription. |
| 245 * @param {number} index The index at which to delete the filter. |
| 246 */ |
| 247 deleteFilterAt(index) |
| 248 { |
| 249 // Ignore index if out of bounds on the negative side, for consistency. |
| 250 if (index < 0) |
| 251 return; |
| 252 |
| 253 let items = this._filterText.splice(index, 1); |
| 254 |
| 255 this._filters.splice(index, 1); |
| 256 |
| 257 // Performance note: A subscription can contain the same filter multiple |
| 258 // times. We can only delete the text from the set if all occurrences of |
| 259 // the filter have been removed. This makes deletion an expensive |
| 260 // operation. It is only needed when a user-defined filter is removed, |
| 261 // which is relatively rare. In comparison, filters must be looked up in |
| 262 // subscriptions a lot more often; therefore, it makes sense to maintain |
| 263 // the set at this cost. An alternative here would be to use a map instead |
| 264 // of a set, with the value of each entry being the reference count, but |
| 265 // this would make addition more expensive thus slowing down the loading of |
| 266 // the initial subscriptions. |
| 267 if (items.length > 0 && this._filterText.indexOf(items[0]) == -1) |
| 268 this._filterTextSet.delete(items[0]); |
| 269 }, |
| 270 |
| 271 /** |
128 * Serializes the subscription for writing out on disk. | 272 * Serializes the subscription for writing out on disk. |
129 * @yields {string} | 273 * @yields {string} |
130 */ | 274 */ |
131 *serialize() | 275 *serialize() |
132 { | 276 { |
133 let {url, type, _title, _fixedTitle, _disabled} = this; | 277 let {url, type, _title, _fixedTitle, _disabled} = this; |
134 | 278 |
135 yield "[Subscription]"; | 279 yield "[Subscription]"; |
136 yield "url=" + url; | 280 yield "url=" + url; |
137 | 281 |
138 if (type) | 282 if (type) |
139 yield "type=" + type; | 283 yield "type=" + type; |
140 if (_title) | 284 if (_title) |
141 yield "title=" + _title; | 285 yield "title=" + _title; |
142 if (_fixedTitle) | 286 if (_fixedTitle) |
143 yield "fixedTitle=true"; | 287 yield "fixedTitle=true"; |
144 if (_disabled) | 288 if (_disabled) |
145 yield "disabled=true"; | 289 yield "disabled=true"; |
146 }, | 290 }, |
147 | 291 |
148 *serializeFilters() | 292 *serializeFilters() |
149 { | 293 { |
150 let {filters} = this; | 294 let {_filterText} = this; |
151 | 295 |
152 yield "[Subscription filters]"; | 296 yield "[Subscription filters]"; |
153 | 297 |
154 for (let filter of filters) | 298 for (let text of _filterText) |
155 yield filter.text.replace(/\[/g, "\\["); | 299 yield text.replace(/\[/g, "\\["); |
156 }, | 300 }, |
157 | 301 |
158 toString() | 302 toString() |
159 { | 303 { |
160 return [...this.serialize()].join("\n"); | 304 return [...this.serialize()].join("\n"); |
161 } | 305 } |
162 }; | 306 }; |
163 | 307 |
164 /** | 308 /** |
165 * Cache for known filter subscriptions, maps URL to subscription objects. | 309 * Cache for known filter subscriptions, maps URL to subscription objects. |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 | 470 |
327 /** | 471 /** |
328 * Creates a new user-defined filter group and adds the given filter to it. | 472 * Creates a new user-defined filter group and adds the given filter to it. |
329 * This group will act as the default group for this filter type. | 473 * This group will act as the default group for this filter type. |
330 * @param {Filter} filter | 474 * @param {Filter} filter |
331 * @return {SpecialSubscription} | 475 * @return {SpecialSubscription} |
332 */ | 476 */ |
333 SpecialSubscription.createForFilter = function(filter) | 477 SpecialSubscription.createForFilter = function(filter) |
334 { | 478 { |
335 let subscription = SpecialSubscription.create(); | 479 let subscription = SpecialSubscription.create(); |
336 subscription.filters.push(filter); | 480 subscription.addFilter(filter); |
337 for (let [type, class_] of SpecialSubscription.defaultsMap) | 481 for (let [type, class_] of SpecialSubscription.defaultsMap) |
338 { | 482 { |
339 if (filter instanceof class_) | 483 if (filter instanceof class_) |
340 subscription.defaults = [type]; | 484 subscription.defaults = [type]; |
341 } | 485 } |
342 if (!subscription.defaults) | 486 if (!subscription.defaults) |
343 subscription.defaults = ["blocking"]; | 487 subscription.defaults = ["blocking"]; |
344 return subscription; | 488 return subscription; |
345 }; | 489 }; |
346 | 490 |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 if (errors) | 726 if (errors) |
583 yield "errors=" + errors; | 727 yield "errors=" + errors; |
584 if (version) | 728 if (version) |
585 yield "version=" + version; | 729 yield "version=" + version; |
586 if (requiredVersion) | 730 if (requiredVersion) |
587 yield "requiredVersion=" + requiredVersion; | 731 yield "requiredVersion=" + requiredVersion; |
588 if (downloadCount) | 732 if (downloadCount) |
589 yield "downloadCount=" + downloadCount; | 733 yield "downloadCount=" + downloadCount; |
590 } | 734 } |
591 }); | 735 }); |
OLD | NEW |