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

Side by Side Diff: lib/subscriptionClasses.js

Issue 29934588: Issue 7094 - Encapsulate management of subscription filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Reworked Created Nov. 3, 2018, 2:20 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/iniParser.js ('k') | lib/synchronizer.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Definition of Subscription class and its subclasses. 21 * @fileOverview Definition of Subscription class and its subclasses.
22 */ 22 */
23 23
24 const {ActiveFilter, BlockingFilter, 24 const {Filter, ActiveFilter, BlockingFilter,
25 WhitelistFilter, ElemHideBase} = require("./filterClasses"); 25 WhitelistFilter, ElemHideBase} = require("./filterClasses");
26 const {filterNotifier} = require("./filterNotifier"); 26 const {filterNotifier} = require("./filterNotifier");
27 const {extend} = require("./coreUtils"); 27 const {extend} = require("./coreUtils");
28 28
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 this._filterText = [];
40 this._filters = [];
40 if (title) 41 if (title)
41 this._title = title; 42 this._title = title;
42 Subscription.knownSubscriptions.set(url, this); 43 Subscription.knownSubscriptions.set(url, this);
43 } 44 }
44 exports.Subscription = Subscription; 45 exports.Subscription = Subscription;
45 46
46 Subscription.prototype = 47 Subscription.prototype =
47 { 48 {
48 /** 49 /**
49 * Download location of the subscription 50 * Download location of the subscription
50 * @type {string} 51 * @type {string}
51 */ 52 */
52 url: null, 53 url: null,
53 54
54 /** 55 /**
55 * Type of the subscription 56 * Type of the subscription
56 * @type {?string} 57 * @type {?string}
57 */ 58 */
58 type: null, 59 type: null,
59 60
60 /** 61 /**
61 * Filters contained in the filter subscription 62 * Filter text contained in the filter subscription.
62 * @type {Filter[]} 63 * @type {Array.<string>}
64 * @private
63 */ 65 */
64 filters: null, 66 _filterText: null,
67
68 /**
69 * Optional {@link Filter} objects corresponding to the subscription's filter
70 * text.
71 * @type {Array.<?Filter>}
72 * @private
73 */
74 _filters: null,
65 75
66 _title: null, 76 _title: null,
67 _fixedTitle: false, 77 _fixedTitle: false,
68 _disabled: false, 78 _disabled: false,
69 79
70 /** 80 /**
71 * Title of the filter subscription 81 * Title of the filter subscription
72 * @type {string} 82 * @type {string}
73 */ 83 */
74 get title() 84 get title()
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 if (value != this._disabled) 128 if (value != this._disabled)
119 { 129 {
120 let oldValue = this._disabled; 130 let oldValue = this._disabled;
121 this._disabled = value; 131 this._disabled = value;
122 filterNotifier.emit("subscription.disabled", this, value, oldValue); 132 filterNotifier.emit("subscription.disabled", this, value, oldValue);
123 } 133 }
124 return this._disabled; 134 return this._disabled;
125 }, 135 },
126 136
127 /** 137 /**
138 * The number of filters in the subscription.
139 * @type {number}
140 */
141 get filterCount()
142 {
143 return this._filters.length;
144 },
145
146 /**
147 * Yields the text for each filter in the subscription.
148 * @yields {string}
149 */
150 *filterText()
151 {
152 yield* this._filterText;
153 },
154
155 /**
156 * Yields the {@link Filter} object for each filter in the subscription.
157 * @yields {Filter}
158 */
159 *filters()
160 {
161 for (let i = 0; i < this._filters.length; i++)
162 yield this._filters[i] || Filter.fromText(this._filterText[i]);
163 },
164
165 /**
166 * Returns the {@link Filter} object at the given 0-based index.
167 * @param {number} index
168 * @returns {?Filter}
169 */
170 filterAt(index)
171 {
172 return this._filters[index] ||
173 (index < this._filters.length ?
174 Filter.fromText(this._filterText[index]) : null);
175 },
176
177 /**
178 * Returns the 0-based index of the given filter.
179 * @param {Filter} filter
180 * @param {number} [fromIndex] The index from which to start the search.
181 * @return {number}
182 */
183 searchFilter(filter, fromIndex = 0)
184 {
185 return this._filterText.indexOf(filter.text, fromIndex);
186 },
187
188 /**
189 * Removes all filters from the subscription.
190 */
191 clearFilters()
192 {
193 this._filterText = [];
194 this._filters = [];
195 },
196
197 /**
198 * Adds a filter to the subscription.
199 * @param {Filter} filter
200 * @param {boolean} [forceCache] If set to <code>true</code>, the
201 * {@link Filter} object is cached regardless of whether it's lightweight.
202 * By default lightweight objects are not cached but are recreated on
203 * demand.
204 */
205 addFilter(filter, forceCache = false)
206 {
207 this._filterText.push(filter.text);
208 this._filters.push(!forceCache && filter.lightweight ? null : filter);
209 },
210
211 /**
212 * Inserts a filter into the subscription.
213 * @param {Filter} filter
214 * @param {number} [index] The index at which to insert the filter.
215 */
216 insertFilterAt(filter, index)
217 {
218 this._filterText.splice(index, 0, filter.text);
219 this._filters.splice(index, 0, filter.lightweight ? null : filter);
220 },
221
222 /**
223 * Deletes a filter from the subscription.
224 * @param {number} [index] The index at which to delete the filter.
225 */
226 deleteFilterAt(index)
227 {
228 this._filterText.splice(index, 1);
229 this._filters.splice(index, 1);
230 },
231
232 /**
233 * Frees up any force-cached {@link Filter} objects.
234 * @see #addFilter
235 */
236 freeFilters()
237 {
238 for (let i = 0; i < this._filters.length; i++)
239 {
240 let filter = this._filters[i];
241 if (filter && filter.lightweight)
242 this._filters[i] = null;
243 }
244 },
245
246 /**
128 * Serializes the subscription for writing out on disk. 247 * Serializes the subscription for writing out on disk.
129 * @yields {string} 248 * @yields {string}
130 */ 249 */
131 *serialize() 250 *serialize()
132 { 251 {
133 let {url, type, _title, _fixedTitle, _disabled} = this; 252 let {url, type, _title, _fixedTitle, _disabled} = this;
134 253
135 yield "[Subscription]"; 254 yield "[Subscription]";
136 yield "url=" + url; 255 yield "url=" + url;
137 256
138 if (type) 257 if (type)
139 yield "type=" + type; 258 yield "type=" + type;
140 if (_title) 259 if (_title)
141 yield "title=" + _title; 260 yield "title=" + _title;
142 if (_fixedTitle) 261 if (_fixedTitle)
143 yield "fixedTitle=true"; 262 yield "fixedTitle=true";
144 if (_disabled) 263 if (_disabled)
145 yield "disabled=true"; 264 yield "disabled=true";
146 }, 265 },
147 266
148 *serializeFilters() 267 *serializeFilters()
149 { 268 {
150 let {filters} = this; 269 let {_filterText} = this;
151 270
152 yield "[Subscription filters]"; 271 yield "[Subscription filters]";
153 272
154 for (let filter of filters) 273 for (let text of _filterText)
155 yield filter.text.replace(/\[/g, "\\["); 274 yield text.replace(/\[/g, "\\[");
156 }, 275 },
157 276
158 toString() 277 toString()
159 { 278 {
160 return [...this.serialize()].join("\n"); 279 return [...this.serialize()].join("\n");
161 } 280 }
162 }; 281 };
163 282
164 /** 283 /**
165 * Cache for known filter subscriptions, maps URL to subscription objects. 284 * Cache for known filter subscriptions, maps URL to subscription objects.
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 445
327 /** 446 /**
328 * Creates a new user-defined filter group and adds the given filter to it. 447 * 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. 448 * This group will act as the default group for this filter type.
330 * @param {Filter} filter 449 * @param {Filter} filter
331 * @return {SpecialSubscription} 450 * @return {SpecialSubscription}
332 */ 451 */
333 SpecialSubscription.createForFilter = function(filter) 452 SpecialSubscription.createForFilter = function(filter)
334 { 453 {
335 let subscription = SpecialSubscription.create(); 454 let subscription = SpecialSubscription.create();
336 subscription.filters.push(filter); 455 subscription.addFilter(filter);
337 for (let [type, class_] of SpecialSubscription.defaultsMap) 456 for (let [type, class_] of SpecialSubscription.defaultsMap)
338 { 457 {
339 if (filter instanceof class_) 458 if (filter instanceof class_)
340 subscription.defaults = [type]; 459 subscription.defaults = [type];
341 } 460 }
342 if (!subscription.defaults) 461 if (!subscription.defaults)
343 subscription.defaults = ["blocking"]; 462 subscription.defaults = ["blocking"];
344 return subscription; 463 return subscription;
345 }; 464 };
346 465
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 if (errors) 701 if (errors)
583 yield "errors=" + errors; 702 yield "errors=" + errors;
584 if (version) 703 if (version)
585 yield "version=" + version; 704 yield "version=" + version;
586 if (requiredVersion) 705 if (requiredVersion)
587 yield "requiredVersion=" + requiredVersion; 706 yield "requiredVersion=" + requiredVersion;
588 if (downloadCount) 707 if (downloadCount)
589 yield "downloadCount=" + downloadCount; 708 yield "downloadCount=" + downloadCount;
590 } 709 }
591 }); 710 });
OLDNEW
« no previous file with comments | « lib/iniParser.js ('k') | lib/synchronizer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld