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

Side by Side Diff: lib/filterStorage.js

Issue 29934588: Issue 7094 - Encapsulate management of subscription filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Simplify patch Created Nov. 17, 2018, 9:25 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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
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.
197 */ 197 */
198 updateSubscriptionFilters(subscription, filters) 198 updateSubscriptionFilters(subscription, filters)
199 { 199 {
200 disconnectSubscriptionFilters(subscription); 200 disconnectSubscriptionFilters(subscription);
201 let oldFilters = subscription.filters; 201
202 subscription.filters = filters; 202 let oldFilters = [...subscription.filters()];
203 connectSubscriptionFilters(subscription); 203 subscription.clearFilters();
204 for (let filter of filters)
205 subscription.addFilter(filter);
206
207 connectSubscriptionFilters(subscription, filters);
Manish Jethani 2018/11/17 21:41:09 The reason we're passing the filters array to the
204 filterNotifier.emit("subscription.updated", subscription, oldFilters); 208 filterNotifier.emit("subscription.updated", subscription, oldFilters);
205 } 209 }
206 210
207 /** 211 /**
208 * Adds a user-defined filter to the storage. 212 * Adds a user-defined filter to the storage.
209 * @param {Filter} filter 213 * @param {Filter} filter
210 * @param {?SpecialSubscription} [subscription] The subscription that the 214 * @param {?SpecialSubscription} [subscription] The subscription that the
211 * filter should be added to. 215 * filter should be added to.
212 * @param {number} [position] The position within the subscription at which 216 * @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 217 * the filter should be added. If not specified, the filter is added at the
(...skipping 15 matching lines...) Expand all
229 } 233 }
230 if (!subscription) 234 if (!subscription)
231 { 235 {
232 // No group for this filter exists, create one 236 // No group for this filter exists, create one
233 subscription = SpecialSubscription.createForFilter(filter); 237 subscription = SpecialSubscription.createForFilter(filter);
234 this.addSubscription(subscription); 238 this.addSubscription(subscription);
235 return; 239 return;
236 } 240 }
237 241
238 if (typeof position == "undefined") 242 if (typeof position == "undefined")
239 position = subscription.filters.length; 243 position = subscription.filterCount;
240 244
241 filter.addSubscription(subscription); 245 filter.addSubscription(subscription);
242 subscription.filters.splice(position, 0, filter); 246 subscription.insertFilterAt(filter, position);
243 filterNotifier.emit("filter.added", filter, subscription, position); 247 filterNotifier.emit("filter.added", filter, subscription, position);
244 } 248 }
245 249
246 /** 250 /**
247 * Removes a user-defined filter from the storage. 251 * Removes a user-defined filter from the storage.
248 * @param {Filter} filter 252 * @param {Filter} filter
249 * @param {?SpecialSubscription} [subscription] The subscription that the 253 * @param {?SpecialSubscription} [subscription] The subscription that the
250 * filter should be removed from. If not specified, the filter will be 254 * filter should be removed from. If not specified, the filter will be
251 * removed from all subscriptions. 255 * removed from all subscriptions.
252 * @param {number} [position] The position within the subscription at which 256 * @param {number} [position] The position within the subscription at which
253 * the filter should be removed. If not specified, all instances of the 257 * the filter should be removed. If not specified, all instances of the
254 * filter will be removed. 258 * filter will be removed.
255 */ 259 */
256 removeFilter(filter, subscription, position) 260 removeFilter(filter, subscription, position)
257 { 261 {
258 let subscriptions = ( 262 let subscriptions = (
Manish Jethani 2018/11/17 21:41:09 Note: Both removeFilter() and moveFilter() can pro
259 subscription ? [subscription] : filter.subscriptions() 263 subscription ? [subscription] : filter.subscriptions()
260 ); 264 );
261 for (let currentSubscription of subscriptions) 265 for (let currentSubscription of subscriptions)
262 { 266 {
263 if (currentSubscription instanceof SpecialSubscription) 267 if (currentSubscription instanceof SpecialSubscription)
264 { 268 {
265 let positions = []; 269 let positions = [];
266 if (typeof position == "undefined") 270 if (typeof position == "undefined")
267 { 271 {
268 let index = -1; 272 let index = -1;
269 do 273 do
270 { 274 {
271 index = currentSubscription.filters.indexOf(filter, index + 1); 275 index = currentSubscription.searchFilter(filter, index + 1);
272 if (index >= 0) 276 if (index >= 0)
273 positions.push(index); 277 positions.push(index);
274 } while (index >= 0); 278 } while (index >= 0);
275 } 279 }
276 else 280 else
277 positions.push(position); 281 positions.push(position);
278 282
279 for (let j = positions.length - 1; j >= 0; j--) 283 for (let j = positions.length - 1; j >= 0; j--)
280 { 284 {
281 let currentPosition = positions[j]; 285 let currentPosition = positions[j];
282 if (currentSubscription.filters[currentPosition] == filter) 286 let currentFilter = currentSubscription.filterAt(currentPosition);
287 if (currentFilter && currentFilter.text == filter.text)
283 { 288 {
284 currentSubscription.filters.splice(currentPosition, 1); 289 currentSubscription.deleteFilterAt(currentPosition);
285 if (currentSubscription.filters.indexOf(filter) < 0) 290 if (currentSubscription.searchFilter(filter) < 0)
286 filter.removeSubscription(currentSubscription); 291 filter.removeSubscription(currentSubscription);
287 filterNotifier.emit("filter.removed", filter, currentSubscription, 292 filterNotifier.emit("filter.removed", filter, currentSubscription,
288 currentPosition); 293 currentPosition);
289 } 294 }
290 } 295 }
291 } 296 }
292 } 297 }
293 } 298 }
294 299
295 /** 300 /**
296 * Moves a user-defined filter to a new position. 301 * Moves a user-defined filter to a new position.
297 * @param {Filter} filter 302 * @param {Filter} filter
298 * @param {SpecialSubscription} subscription The subscription where the 303 * @param {SpecialSubscription} subscription The subscription where the
299 * filter is located. 304 * filter is located.
300 * @param {number} oldPosition The current position of the filter. 305 * @param {number} oldPosition The current position of the filter.
301 * @param {number} newPosition The new position of the filter. 306 * @param {number} newPosition The new position of the filter.
302 */ 307 */
303 moveFilter(filter, subscription, oldPosition, newPosition) 308 moveFilter(filter, subscription, oldPosition, newPosition)
304 { 309 {
305 if (!(subscription instanceof SpecialSubscription) || 310 if (!(subscription instanceof SpecialSubscription))
306 subscription.filters[oldPosition] != filter)
307 {
308 return; 311 return;
309 } 312
313 let currentFilter = subscription.filterAt(oldPosition);
314 if (!currentFilter || currentFilter.text != filter.text)
315 return;
310 316
311 newPosition = Math.min(Math.max(newPosition, 0), 317 newPosition = Math.min(Math.max(newPosition, 0),
312 subscription.filters.length - 1); 318 subscription.filterCount - 1);
313 if (oldPosition == newPosition) 319 if (oldPosition == newPosition)
314 return; 320 return;
315 321
316 subscription.filters.splice(oldPosition, 1); 322 subscription.deleteFilterAt(oldPosition);
317 subscription.filters.splice(newPosition, 0, filter); 323 subscription.insertFilterAt(filter, newPosition);
318 filterNotifier.emit("filter.moved", filter, subscription, oldPosition, 324 filterNotifier.emit("filter.moved", filter, subscription, oldPosition,
319 newPosition); 325 newPosition);
320 } 326 }
321 327
322 /** 328 /**
323 * Increases the hit count for a filter by one. 329 * Increases the hit count for a filter by one.
324 * @param {Filter} filter 330 * @param {Filter} filter
325 */ 331 */
326 increaseHitCount(filter) 332 increaseHitCount(filter)
327 { 333 {
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 * @yields {string} 475 * @yields {string}
470 */ 476 */
471 *exportData() 477 *exportData()
472 { 478 {
473 // Do not persist external subscriptions 479 // Do not persist external subscriptions
474 let subscriptions = []; 480 let subscriptions = [];
475 for (let subscription of this.subscriptions()) 481 for (let subscription of this.subscriptions())
476 { 482 {
477 if (!(subscription instanceof ExternalSubscription) && 483 if (!(subscription instanceof ExternalSubscription) &&
478 !(subscription instanceof SpecialSubscription && 484 !(subscription instanceof SpecialSubscription &&
479 subscription.filters.length == 0)) 485 subscription.filterCount == 0))
480 { 486 {
481 subscriptions.push(subscription); 487 subscriptions.push(subscription);
482 } 488 }
483 } 489 }
484 490
485 yield "# Adblock Plus preferences"; 491 yield "# Adblock Plus preferences";
486 yield "version=" + this.formatVersion; 492 yield "version=" + this.formatVersion;
487 493
488 let saved = new Set(); 494 let saved = new Set();
489 495
490 // Save subscriptions 496 // Save subscriptions
491 for (let subscription of subscriptions) 497 for (let subscription of subscriptions)
492 { 498 {
493 yield* subscription.serialize(); 499 yield* subscription.serialize();
494 yield* subscription.serializeFilters(); 500 yield* subscription.serializeFilters();
495 } 501 }
496 502
497 // Save filter data 503 // Save filter data
498 for (let subscription of subscriptions) 504 for (let subscription of subscriptions)
499 { 505 {
500 for (let filter of subscription.filters) 506 for (let filter of subscription.filters())
501 { 507 {
502 if (!saved.has(filter.text)) 508 if (!saved.has(filter.text))
503 { 509 {
504 yield* filter.serialize(); 510 yield* filter.serialize();
505 saved.add(filter.text); 511 saved.add(filter.text);
506 } 512 }
507 } 513 }
508 } 514 }
509 } 515 }
510 516
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 * back to disk. 646 * back to disk.
641 */ 647 */
642 let filterStorage = new FilterStorage(); 648 let filterStorage = new FilterStorage();
643 649
644 exports.filterStorage = filterStorage; 650 exports.filterStorage = filterStorage;
645 651
646 /** 652 /**
647 * Connects a subscription to its filters without any notifications. 653 * Connects a subscription to its filters without any notifications.
648 * @param {Subscription} subscription The subscription that should be 654 * @param {Subscription} subscription The subscription that should be
649 * connected to its filters. 655 * connected to its filters.
656 * @param {?Array.<Filter>} [filters] A list of filters to which the
657 * subscription should be connected. If this is not given, the subscription
658 * is connected to its own filters.
650 */ 659 */
651 function connectSubscriptionFilters(subscription) 660 function connectSubscriptionFilters(subscription, filters)
652 { 661 {
653 if (!filterStorage.knownSubscriptions.has(subscription.url)) 662 if (!filterStorage.knownSubscriptions.has(subscription.url))
654 return; 663 return;
655 664
656 for (let filter of subscription.filters) 665 for (let filter of filters || subscription.filters())
657 filter.addSubscription(subscription); 666 filter.addSubscription(subscription);
658 } 667 }
659 668
660 /** 669 /**
661 * Disconnects a subscription from its filters without any notifications. 670 * Disconnects a subscription from its filters without any notifications.
662 * @param {Subscription} subscription The subscription that should be 671 * @param {Subscription} subscription The subscription that should be
663 * disconnected from its filters. 672 * disconnected from its filters.
673 * @param {?Array.<Filter>} [filters] A list of filters from which the
674 * subscription should be disconnected. If this is not given, the
675 * subscription is disconnected from its own filters.
664 */ 676 */
665 function disconnectSubscriptionFilters(subscription) 677 function disconnectSubscriptionFilters(subscription, filters)
666 { 678 {
667 if (!filterStorage.knownSubscriptions.has(subscription.url)) 679 if (!filterStorage.knownSubscriptions.has(subscription.url))
668 return; 680 return;
669 681
670 for (let filter of subscription.filters) 682 for (let filter of filters || subscription.filters())
671 filter.removeSubscription(subscription); 683 filter.removeSubscription(subscription);
672 } 684 }
OLDNEW
« no previous file with comments | « lib/filterListener.js ('k') | lib/iniParser.js » ('j') | lib/subscriptionClasses.js » ('J')

Powered by Google App Engine
This is Rietveld