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

Delta Between Two Patch Sets: lib/filterStorage.js

Issue 29912619: Issue 6891, 6741 - Rename FilterStorage to filterStorage (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Oct. 16, 2018, 9:34 p.m.
Right Patch Set: Minor update Created Oct. 16, 2018, 10:02 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/filterListener.js ('k') | lib/synchronizer.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 16 matching lines...) Expand all
27 const {Filter, ActiveFilter} = require("./filterClasses"); 27 const {Filter, ActiveFilter} = require("./filterClasses");
28 const {Subscription, SpecialSubscription, 28 const {Subscription, SpecialSubscription,
29 ExternalSubscription} = require("./subscriptionClasses"); 29 ExternalSubscription} = require("./subscriptionClasses");
30 const {filterNotifier} = require("./filterNotifier"); 30 const {filterNotifier} = require("./filterNotifier");
31 const {INIParser} = require("./iniParser"); 31 const {INIParser} = require("./iniParser");
32 32
33 /** 33 /**
34 * Version number of the filter storage file format. 34 * Version number of the filter storage file format.
35 * @type {number} 35 * @type {number}
36 */ 36 */
37 const FORMAT_VERSION = 5; 37 const FORMAT_VERSION = 5;
Manish Jethani 2018/10/16 21:59:32 Using `const` and accordingly using ALL_CAPS
38 38
39 /** 39 /**
40 * {@link filterStorage} implementation. 40 * {@link filterStorage} implementation.
41 */ 41 */
42 class FilterStorage 42 class FilterStorage
43 { 43 {
44 /** 44 /**
45 * @hideconstructor 45 * @hideconstructor
Manish Jethani 2018/10/16 21:59:31 Singleton, so hide constructor.
46 */ 46 */
47 constructor() 47 constructor()
48 { 48 {
49 /** 49 /**
50 * Will be set to true after the initial {@link FilterStorage#loadFromDisk} 50 * Will be set to true after the initial {@link FilterStorage#loadFromDisk}
51 * call completes. 51 * call completes.
52 * @type {boolean} 52 * @type {boolean}
53 */ 53 */
54 this.initialized = false; 54 this.initialized = false;
55 55
(...skipping 14 matching lines...) Expand all
70 /** 70 /**
71 * Map of subscriptions already on the list, by their URL/identifier. 71 * Map of subscriptions already on the list, by their URL/identifier.
72 * @type {Map.<string,Subscription>} 72 * @type {Map.<string,Subscription>}
73 */ 73 */
74 this.knownSubscriptions = new Map(); 74 this.knownSubscriptions = new Map();
75 75
76 /** 76 /**
77 * Will be set to true if {@link FilterStorage#saveToDisk} is running 77 * Will be set to true if {@link FilterStorage#saveToDisk} is running
78 * (reentrance protection). 78 * (reentrance protection).
79 * @type {boolean} 79 * @type {boolean}
80 * @private 80 * @private
Manish Jethani 2018/10/16 21:59:32 Marked @private
81 */ 81 */
82 this._saving = false; 82 this._saving = false;
83 83
84 /** 84 /**
85 * Will be set to true if a {@link FilterStorage#saveToDisk} call arrives 85 * Will be set to true if a {@link FilterStorage#saveToDisk} call arrives
86 * while {@link FilterStorage#saveToDisk} is already running (delayed 86 * while {@link FilterStorage#saveToDisk} is already running (delayed
87 * execution). 87 * execution).
88 * @type {boolean} 88 * @type {boolean}
89 * @private 89 * @private
90 */ 90 */
91 this._needsSave = false; 91 this._needsSave = false;
92 } 92 }
93 93
94 /** 94 /**
95 * The version number of the <code>patterns.ini</code> format used. 95 * The version number of the <code>patterns.ini</code> format used.
96 * @type {number} 96 * @type {number}
97 */ 97 */
98 get formatVersion() 98 get formatVersion()
99 { 99 {
100 return FORMAT_VERSION; 100 return FORMAT_VERSION;
101 } 101 }
102 102
103 /** 103 /**
104 * The file containing the subscriptions. 104 * The file containing the subscriptions.
Manish Jethani 2018/10/16 21:59:32 Fixed some inconsistencies in the documentation.
105 * @type {string} 105 * @type {string}
106 */ 106 */
107 get sourceFile() 107 get sourceFile()
108 { 108 {
109 return "patterns.ini"; 109 return "patterns.ini";
110 } 110 }
111 111
112 /** 112 /**
113 * Yields all subscriptions in the storage. 113 * Yields all subscriptions in the storage.
114 * @yields {Subscription} 114 * @yields {Subscription}
115 */ 115 */
116 *subscriptions() 116 *subscriptions()
117 { 117 {
118 yield* this.knownSubscriptions.values(); 118 yield* this.knownSubscriptions.values();
119 } 119 }
120 120
121 /** 121 /**
122 * The number of subscriptions in the storage. 122 * The number of subscriptions in the storage.
123 * @type {number} 123 * @type {number}
124 */ 124 */
125 get subscriptionCount() 125 get subscriptionCount()
126 { 126 {
127 return this.knownSubscriptions.size; 127 return this.knownSubscriptions.size;
128 } 128 }
129 129
130 /** 130 /**
131 * Finds the filter group that a filter should be added to by default. Will 131 * Finds the filter group that a filter should be added to by default. Will
132 * return <code>null</code> if this group doesn't exist yet. 132 * return <code>null</code> if this group doesn't exist yet.
133 * @param {Filter} filter 133 * @param {Filter} filter
134 * @returns {?SpecialSubscription} 134 * @returns {?SpecialSubscription}
Manish Jethani 2018/10/16 21:59:32 s/@return/@returns/
135 */ 135 */
136 getGroupForFilter(filter) 136 getGroupForFilter(filter)
137 { 137 {
138 let generalSubscription = null; 138 let generalSubscription = null;
139 for (let subscription of this.knownSubscriptions.values()) 139 for (let subscription of this.knownSubscriptions.values())
140 { 140 {
141 if (subscription instanceof SpecialSubscription && !subscription.disabled) 141 if (subscription instanceof SpecialSubscription && !subscription.disabled)
142 { 142 {
143 // Always prefer specialized subscriptions 143 // Always prefer specialized subscriptions
144 if (subscription.isDefaultFor(filter)) 144 if (subscription.isDefaultFor(filter))
(...skipping 13 matching lines...) Expand all
158 /** 158 /**
159 * Adds a subscription to the storage. 159 * Adds a subscription to the storage.
160 * @param {Subscription} subscription The subscription to be added. 160 * @param {Subscription} subscription The subscription to be added.
161 */ 161 */
162 addSubscription(subscription) 162 addSubscription(subscription)
163 { 163 {
164 if (this.knownSubscriptions.has(subscription.url)) 164 if (this.knownSubscriptions.has(subscription.url))
165 return; 165 return;
166 166
167 this.knownSubscriptions.set(subscription.url, subscription); 167 this.knownSubscriptions.set(subscription.url, subscription);
168 addSubscriptionFilters(subscription); 168 connectSubscriptionFilters(subscription);
169 169
170 filterNotifier.emit("subscription.added", subscription); 170 filterNotifier.emit("subscription.added", subscription);
171 } 171 }
172 172
173 /** 173 /**
174 * Removes a subscription from the storage. 174 * Removes a subscription from the storage.
175 * @param {Subscription} subscription The subscription to be removed. 175 * @param {Subscription} subscription The subscription to be removed.
176 */ 176 */
177 removeSubscription(subscription) 177 removeSubscription(subscription)
178 { 178 {
179 if (!this.knownSubscriptions.has(subscription.url)) 179 if (!this.knownSubscriptions.has(subscription.url))
180 return; 180 return;
181 181
182 removeSubscriptionFilters(subscription); 182 disconnectSubscriptionFilters(subscription);
183 183
184 this.knownSubscriptions.delete(subscription.url); 184 this.knownSubscriptions.delete(subscription.url);
185 185
186 // This should be the last remaining reference to the Subscription 186 // This should be the last remaining reference to the Subscription
187 // object. 187 // object.
188 Subscription.knownSubscriptions.delete(subscription.url); 188 Subscription.knownSubscriptions.delete(subscription.url);
189 189
190 filterNotifier.emit("subscription.removed", subscription); 190 filterNotifier.emit("subscription.removed", subscription);
191 } 191 }
192 192
193 /** 193 /**
194 * Replaces the list of filters in a subscription with a new list. 194 * Replaces the list of filters in a subscription with a new list.
195 * @param {Subscription} subscription The subscription to be updated. 195 * @param {Subscription} subscription The subscription to be updated.
196 * @param {Array.<Filter>} filters The new list of filters. 196 * @param {Array.<Filter>} filters The new list of filters.
Manish Jethani 2018/10/16 21:59:32 s/Filter[]/Array.<Filter>/
197 */ 197 */
198 updateSubscriptionFilters(subscription, filters) 198 updateSubscriptionFilters(subscription, filters)
199 { 199 {
200 removeSubscriptionFilters(subscription); 200 disconnectSubscriptionFilters(subscription);
201 let oldFilters = subscription.filters; 201 let oldFilters = subscription.filters;
202 subscription.filters = filters; 202 subscription.filters = filters;
203 addSubscriptionFilters(subscription); 203 connectSubscriptionFilters(subscription);
204 filterNotifier.emit("subscription.updated", subscription, oldFilters); 204 filterNotifier.emit("subscription.updated", subscription, oldFilters);
205 } 205 }
206 206
207 /** 207 /**
208 * Adds a user-defined filter to the storage. 208 * Adds a user-defined filter to the storage.
209 * @param {Filter} filter 209 * @param {Filter} filter
210 * @param {?SpecialSubscription} [subscription] The subscription that the 210 * @param {?SpecialSubscription} [subscription] The subscription that the
211 * filter should be added to. 211 * filter should be added to.
212 * @param {number} [position] The position within the subscription at which 212 * @param {number} [position] The position within the subscription at which
213 * the filter should be added. If not specified, the filter is added at the 213 * the filter should be added. If not specified, the filter is added at the
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 { 327 {
328 if (!Prefs.savestats || !(filter instanceof ActiveFilter)) 328 if (!Prefs.savestats || !(filter instanceof ActiveFilter))
329 return; 329 return;
330 330
331 filter.hitCount++; 331 filter.hitCount++;
332 filter.lastHit = Date.now(); 332 filter.lastHit = Date.now();
333 } 333 }
334 334
335 /** 335 /**
336 * Resets hit count for some filters. 336 * Resets hit count for some filters.
337 * @param {?Array.<Filter>} filters The filters to be reset. If 337 * @param {?Array.<Filter>} [filters] The filters to be reset. If not
Manish Jethani 2018/10/16 21:59:32 Added ? before the type to indicate that the value
338 * <code>null</code>, all filters will be reset. 338 * specified, all filters will be reset.
339 */ 339 */
340 resetHitCounts(filters) 340 resetHitCounts(filters)
341 { 341 {
342 if (!filters) 342 if (!filters)
343 filters = Filter.knownFilters.values(); 343 filters = Filter.knownFilters.values();
344 for (let filter of filters) 344 for (let filter of filters)
345 { 345 {
346 filter.hitCount = 0; 346 filter.hitCount = 0;
347 filter.lastHit = 0; 347 filter.lastHit = 0;
348 } 348 }
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 650
651 /** 651 /**
652 * Reads the user's filters from disk, manages them in memory, and writes them 652 * Reads the user's filters from disk, manages them in memory, and writes them
653 * back to disk. 653 * back to disk.
654 */ 654 */
655 let filterStorage = new FilterStorage(); 655 let filterStorage = new FilterStorage();
656 656
657 exports.filterStorage = filterStorage; 657 exports.filterStorage = filterStorage;
658 658
659 /** 659 /**
660 * Connects a subscription to its filters without any notifications. 660 * Connects a subscription to its filters without any notifications.
Manish Jethani 2018/10/16 21:59:31 s/joins/connects/
661 * @param {Subscription} subscription The subscription that should be 661 * @param {Subscription} subscription The subscription that should be
662 * connected to its filters. 662 * connected to its filters.
663 */ 663 */
664 function addSubscriptionFilters(subscription) 664 function connectSubscriptionFilters(subscription)
665 { 665 {
666 if (!filterStorage.knownSubscriptions.has(subscription.url)) 666 if (!filterStorage.knownSubscriptions.has(subscription.url))
667 return; 667 return;
668 668
669 for (let filter of subscription.filters) 669 for (let filter of subscription.filters)
670 filter.addSubscription(subscription); 670 filter.addSubscription(subscription);
671 } 671 }
672 672
673 /** 673 /**
674 * Disconnects a subscription from its filters without any notifications. 674 * Disconnects a subscription from its filters without any notifications.
Manish Jethani 2018/10/16 21:59:32 s/removes/disconnects/
675 * @param {Subscription} subscription The subscription that should be 675 * @param {Subscription} subscription The subscription that should be
676 * disconnected from its filters. 676 * disconnected from its filters.
677 */ 677 */
678 function removeSubscriptionFilters(subscription) 678 function disconnectSubscriptionFilters(subscription)
679 { 679 {
680 if (!filterStorage.knownSubscriptions.has(subscription.url)) 680 if (!filterStorage.knownSubscriptions.has(subscription.url))
681 return; 681 return;
682 682
683 for (let filter of subscription.filters) 683 for (let filter of subscription.filters)
684 filter.removeSubscription(subscription); 684 filter.removeSubscription(subscription);
685 } 685 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld