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

Side by Side Diff: lib/filterStorage.js

Issue 30013628: Issue 7029 - Remove subscriptions property of Filter object (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Feb. 24, 2019, 1:30 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 <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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 /** 103 /**
104 * The file containing the subscriptions. 104 * The file containing the subscriptions.
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 subscriptions in the storage.
114 * @param {?string} [filterText] The filter text for which to look. If
115 * specified, the function yields only those subscriptions that contain the
116 * given filter text. By default the function yields all subscriptions.
114 * @yields {Subscription} 117 * @yields {Subscription}
115 */ 118 */
116 *subscriptions() 119 *subscriptions(filterText = null)
117 { 120 {
118 yield* this.knownSubscriptions.values(); 121 if (filterText == null)
122 {
123 yield* this.knownSubscriptions.values();
124 }
125 else
126 {
127 for (let subscription of this.knownSubscriptions.values())
128 {
129 if (subscription.hasFilterText(filterText))
130 yield subscription;
131 }
132 }
119 } 133 }
120 134
121 /** 135 /**
122 * The number of subscriptions in the storage. 136 * The number of subscriptions in the storage.
123 * @type {number} 137 * @type {number}
124 */ 138 */
125 get subscriptionCount() 139 get subscriptionCount()
126 { 140 {
127 return this.knownSubscriptions.size; 141 return this.knownSubscriptions.size;
128 } 142 }
(...skipping 29 matching lines...) Expand all
158 /** 172 /**
159 * Adds a subscription to the storage. 173 * Adds a subscription to the storage.
160 * @param {Subscription} subscription The subscription to be added. 174 * @param {Subscription} subscription The subscription to be added.
161 */ 175 */
162 addSubscription(subscription) 176 addSubscription(subscription)
163 { 177 {
164 if (this.knownSubscriptions.has(subscription.url)) 178 if (this.knownSubscriptions.has(subscription.url))
165 return; 179 return;
166 180
167 this.knownSubscriptions.set(subscription.url, subscription); 181 this.knownSubscriptions.set(subscription.url, subscription);
168 connectSubscriptionFilters(subscription);
169 182
170 filterNotifier.emit("subscription.added", subscription); 183 filterNotifier.emit("subscription.added", subscription);
171 } 184 }
172 185
173 /** 186 /**
174 * Removes a subscription from the storage. 187 * Removes a subscription from the storage.
175 * @param {Subscription} subscription The subscription to be removed. 188 * @param {Subscription} subscription The subscription to be removed.
176 */ 189 */
177 removeSubscription(subscription) 190 removeSubscription(subscription)
178 { 191 {
179 if (!this.knownSubscriptions.has(subscription.url)) 192 if (!this.knownSubscriptions.has(subscription.url))
180 return; 193 return;
181 194
182 disconnectSubscriptionFilters(subscription);
183
184 this.knownSubscriptions.delete(subscription.url); 195 this.knownSubscriptions.delete(subscription.url);
185 196
186 // This should be the last remaining reference to the Subscription 197 // This should be the last remaining reference to the Subscription
187 // object. 198 // object.
188 Subscription.knownSubscriptions.delete(subscription.url); 199 Subscription.knownSubscriptions.delete(subscription.url);
189 200
190 filterNotifier.emit("subscription.removed", subscription); 201 filterNotifier.emit("subscription.removed", subscription);
191 } 202 }
192 203
193 /** 204 /**
194 * Replaces the list of filters in a subscription with a new list. 205 * Replaces the list of filters in a subscription with a new list.
195 * @param {Subscription} subscription The subscription to be updated. 206 * @param {Subscription} subscription The subscription to be updated.
196 * @param {Array.<string>} filterText The new filter text. 207 * @param {Array.<string>} filterText The new filter text.
197 */ 208 */
198 updateSubscriptionFilters(subscription, filterText) 209 updateSubscriptionFilters(subscription, filterText)
199 { 210 {
200 let oldFilterText = [...subscription.filterText()]; 211 let oldFilterText = [...subscription.filterText()];
201 disconnectSubscriptionFilters(subscription, oldFilterText);
Manish Jethani 2019/02/26 12:29:57 You might think that looking up the subscriptions
hub 2019/03/08 21:36:19 my default config has 6. Add one language, that ma
Manish Jethani 2019/03/30 20:50:35 Each user-defined filter is not a special subscrip
202 subscription.clearFilters(); 212 subscription.clearFilters();
203 213
204 for (let text of filterText) 214 for (let text of filterText)
205 subscription.addFilterText(text); 215 subscription.addFilterText(text);
206 216
207 connectSubscriptionFilters(subscription, filterText);
208
209 filterNotifier.emit("subscription.updated", subscription, oldFilterText); 217 filterNotifier.emit("subscription.updated", subscription, oldFilterText);
210 } 218 }
211 219
212 /** 220 /**
213 * Adds a user-defined filter to the storage. 221 * Adds a user-defined filter to the storage.
214 * @param {Filter} filter 222 * @param {Filter} filter
215 * @param {?SpecialSubscription} [subscription] The subscription that the 223 * @param {?SpecialSubscription} [subscription] The subscription that the
216 * filter should be added to. 224 * filter should be added to.
217 * @param {number} [position] The position within the subscription at which 225 * @param {number} [position] The position within the subscription at which
218 * the filter should be added. If not specified, the filter is added at the 226 * the filter should be added. If not specified, the filter is added at the
219 * end of the subscription. 227 * end of the subscription.
220 */ 228 */
221 addFilter(filter, subscription, position) 229 addFilter(filter, subscription, position)
222 { 230 {
223 if (!subscription) 231 if (!subscription)
224 { 232 {
225 for (let currentSubscription of filter.subscriptions()) 233 for (let currentSubscription of this.subscriptions(filter.text))
226 { 234 {
227 if (currentSubscription instanceof SpecialSubscription && 235 if (currentSubscription instanceof SpecialSubscription &&
228 !currentSubscription.disabled) 236 !currentSubscription.disabled)
229 { 237 {
230 return; // No need to add 238 return; // No need to add
231 } 239 }
232 } 240 }
233 subscription = this.getGroupForFilter(filter); 241 subscription = this.getGroupForFilter(filter);
234 } 242 }
235 if (!subscription) 243 if (!subscription)
236 { 244 {
237 // No group for this filter exists, create one 245 // No group for this filter exists, create one
238 subscription = SpecialSubscription.createForFilter(filter); 246 subscription = SpecialSubscription.createForFilter(filter);
239 this.addSubscription(subscription); 247 this.addSubscription(subscription);
240 return; 248 return;
241 } 249 }
242 250
243 if (typeof position == "undefined") 251 if (typeof position == "undefined")
244 position = subscription.filterCount; 252 position = subscription.filterCount;
245 253
246 filter.addSubscription(subscription);
247 subscription.insertFilterAt(filter, position); 254 subscription.insertFilterAt(filter, position);
248 filterNotifier.emit("filter.added", filter, subscription, position); 255 filterNotifier.emit("filter.added", filter, subscription, position);
249 } 256 }
250 257
251 /** 258 /**
252 * Removes a user-defined filter from the storage. 259 * Removes a user-defined filter from the storage.
253 * @param {Filter} filter 260 * @param {Filter} filter
254 * @param {?SpecialSubscription} [subscription] The subscription that the 261 * @param {?SpecialSubscription} [subscription] The subscription that the
255 * filter should be removed from. If not specified, the filter will be 262 * filter should be removed from. If not specified, the filter will be
256 * removed from all subscriptions. 263 * removed from all subscriptions.
257 * @param {number} [position] The position within the subscription at which 264 * @param {number} [position] The position within the subscription at which
258 * the filter should be removed. If not specified, all instances of the 265 * the filter should be removed. If not specified, all instances of the
259 * filter will be removed. 266 * filter will be removed.
260 */ 267 */
261 removeFilter(filter, subscription, position) 268 removeFilter(filter, subscription, position)
262 { 269 {
263 let subscriptions = ( 270 let subscriptions = (
264 subscription ? [subscription] : filter.subscriptions() 271 subscription ? [subscription] : this.subscriptions(filter.text)
265 ); 272 );
266 for (let currentSubscription of subscriptions) 273 for (let currentSubscription of subscriptions)
267 { 274 {
268 if (currentSubscription instanceof SpecialSubscription) 275 if (currentSubscription instanceof SpecialSubscription)
269 { 276 {
270 let positions = []; 277 let positions = [];
271 if (typeof position == "undefined") 278 if (typeof position == "undefined")
272 { 279 {
273 let index = -1; 280 let index = -1;
274 do 281 do
275 { 282 {
276 index = currentSubscription.searchFilter(filter, index + 1); 283 index = currentSubscription.findFilterIndex(filter, index + 1);
277 if (index >= 0) 284 if (index >= 0)
278 positions.push(index); 285 positions.push(index);
279 } while (index >= 0); 286 } while (index >= 0);
280 } 287 }
281 else 288 else
282 positions.push(position); 289 positions.push(position);
283 290
284 for (let j = positions.length - 1; j >= 0; j--) 291 for (let j = positions.length - 1; j >= 0; j--)
285 { 292 {
286 let currentPosition = positions[j]; 293 let currentPosition = positions[j];
287 let currentFilterText = 294 let currentFilterText =
288 currentSubscription.filterTextAt(currentPosition); 295 currentSubscription.filterTextAt(currentPosition);
289 if (currentFilterText && currentFilterText == filter.text) 296 if (currentFilterText && currentFilterText == filter.text)
290 { 297 {
291 currentSubscription.deleteFilterAt(currentPosition); 298 currentSubscription.deleteFilterAt(currentPosition);
292 if (currentSubscription.searchFilter(filter) < 0)
293 filter.removeSubscription(currentSubscription);
294 filterNotifier.emit("filter.removed", filter, currentSubscription, 299 filterNotifier.emit("filter.removed", filter, currentSubscription,
295 currentPosition); 300 currentPosition);
296 } 301 }
297 } 302 }
298 } 303 }
299 } 304 }
300 } 305 }
301 306
302 /** 307 /**
303 * Moves a user-defined filter to a new position. 308 * Moves a user-defined filter to a new position.
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 } 648 }
644 } 649 }
645 650
646 /** 651 /**
647 * 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
648 * back to disk. 653 * back to disk.
649 */ 654 */
650 let filterStorage = new FilterStorage(); 655 let filterStorage = new FilterStorage();
651 656
652 exports.filterStorage = filterStorage; 657 exports.filterStorage = filterStorage;
653
654 /**
655 * Connects a subscription to its filters without any notifications.
656 * @param {Subscription} subscription The subscription that should be
657 * connected to its filters.
658 * @param {?Array.<string>} [filterText] A list of filters (in text form) to
659 * which the subscription should be connected. If this is not given, the
660 * subscription is connected to its own filters.
661 */
662 function connectSubscriptionFilters(subscription, filterText)
663 {
664 if (!filterStorage.knownSubscriptions.has(subscription.url))
665 return;
666
667 for (let text of filterText || subscription.filterText())
668 Filter.fromText(text).addSubscription(subscription);
669 }
670
671 /**
672 * Disconnects a subscription from its filters without any notifications.
673 * @param {Subscription} subscription The subscription that should be
674 * disconnected from its filters.
675 * @param {?Array.<string>} [filterText] A list of filters (in text form) from
676 * which the subscription should be disconnected. If this is not given, the
677 * subscription is disconnected from its own filters.
678 */
679 function disconnectSubscriptionFilters(subscription, filterText)
680 {
681 if (!filterStorage.knownSubscriptions.has(subscription.url))
682 return;
683
684 for (let text of filterText || subscription.filterText())
685 Filter.fromText(text).removeSubscription(subscription);
686 }
OLDNEW

Powered by Google App Engine
This is Rietveld