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

Powered by Google App Engine
This is Rietveld