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

Delta Between Two Patch Sets: lib/filterStorage.js

Issue 29934588: Issue 7094 - Encapsulate management of subscription filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Nov. 2, 2018, 10:34 p.m.
Right Patch Set: Remove hasFilter and related code Created Nov. 18, 2018, 10:21 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
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 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 201 disconnectSubscriptionFilters(subscription, oldFilters);
202 let oldFilters = subscription.filters; 202 subscription.clearFilters();
203 subscription.filters = []; 203
204 for (let filter of filters) 204 for (let filter of filters)
205 subscription.filters.push(filter.text); 205 subscription.addFilter(filter);
206 206
207 connectSubscriptionFilters(subscription, filters); 207 connectSubscriptionFilters(subscription, filters);
208
208 filterNotifier.emit("subscription.updated", subscription, oldFilters); 209 filterNotifier.emit("subscription.updated", subscription, oldFilters);
209 } 210 }
210 211
211 /** 212 /**
212 * Adds a user-defined filter to the storage. 213 * Adds a user-defined filter to the storage.
213 * @param {Filter} filter 214 * @param {Filter} filter
214 * @param {?SpecialSubscription} [subscription] The subscription that the 215 * @param {?SpecialSubscription} [subscription] The subscription that the
215 * filter should be added to. 216 * filter should be added to.
216 * @param {number} [position] The position within the subscription at which 217 * @param {number} [position] The position within the subscription at which
217 * 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
233 } 234 }
234 if (!subscription) 235 if (!subscription)
235 { 236 {
236 // No group for this filter exists, create one 237 // No group for this filter exists, create one
237 subscription = SpecialSubscription.createForFilter(filter); 238 subscription = SpecialSubscription.createForFilter(filter);
238 this.addSubscription(subscription); 239 this.addSubscription(subscription);
239 return; 240 return;
240 } 241 }
241 242
242 if (typeof position == "undefined") 243 if (typeof position == "undefined")
243 position = subscription.filters.length; 244 position = subscription.filterCount;
244 245
245 filter.addSubscription(subscription); 246 filter.addSubscription(subscription);
246 subscription.filters.splice(position, 0, filter.text); 247 subscription.insertFilterAt(filter, position);
247 filterNotifier.emit("filter.added", filter, subscription, position); 248 filterNotifier.emit("filter.added", filter, subscription, position);
248 } 249 }
249 250
250 /** 251 /**
251 * Removes a user-defined filter from the storage. 252 * Removes a user-defined filter from the storage.
252 * @param {Filter} filter 253 * @param {Filter} filter
253 * @param {?SpecialSubscription} [subscription] The subscription that the 254 * @param {?SpecialSubscription} [subscription] The subscription that the
254 * 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
255 * removed from all subscriptions. 256 * removed from all subscriptions.
256 * @param {number} [position] The position within the subscription at which 257 * @param {number} [position] The position within the subscription at which
257 * 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
258 * filter will be removed. 259 * filter will be removed.
259 */ 260 */
260 removeFilter(filter, subscription, position) 261 removeFilter(filter, subscription, position)
261 { 262 {
262 let subscriptions = ( 263 let subscriptions = (
263 subscription ? [subscription] : filter.subscriptions() 264 subscription ? [subscription] : filter.subscriptions()
264 ); 265 );
265 for (let currentSubscription of subscriptions) 266 for (let currentSubscription of subscriptions)
266 { 267 {
267 if (currentSubscription instanceof SpecialSubscription) 268 if (currentSubscription instanceof SpecialSubscription)
268 { 269 {
269 let positions = []; 270 let positions = [];
270 if (typeof position == "undefined") 271 if (typeof position == "undefined")
271 { 272 {
272 let index = -1; 273 let index = -1;
273 do 274 do
274 { 275 {
275 index = currentSubscription.filters.indexOf(filter.text, index + 1); 276 index = currentSubscription.searchFilter(filter, index + 1);
276 if (index >= 0) 277 if (index >= 0)
277 positions.push(index); 278 positions.push(index);
278 } while (index >= 0); 279 } while (index >= 0);
279 } 280 }
280 else 281 else
281 positions.push(position); 282 positions.push(position);
282 283
283 for (let j = positions.length - 1; j >= 0; j--) 284 for (let j = positions.length - 1; j >= 0; j--)
284 { 285 {
285 let currentPosition = positions[j]; 286 let currentPosition = positions[j];
286 if (currentSubscription.filters[currentPosition] == filter.text) 287 let currentFilter = currentSubscription.filterAt(currentPosition);
288 if (currentFilter && currentFilter.text == filter.text)
287 { 289 {
288 currentSubscription.filters.splice(currentPosition, 1); 290 currentSubscription.deleteFilterAt(currentPosition);
289 if (currentSubscription.filters.indexOf(filter.text) < 0) 291 if (currentSubscription.searchFilter(filter) < 0)
290 filter.removeSubscription(currentSubscription); 292 filter.removeSubscription(currentSubscription);
291 filterNotifier.emit("filter.removed", filter, currentSubscription, 293 filterNotifier.emit("filter.removed", filter, currentSubscription,
292 currentPosition); 294 currentPosition);
293 } 295 }
294 } 296 }
295 } 297 }
296 } 298 }
297 } 299 }
298 300
299 /** 301 /**
300 * Moves a user-defined filter to a new position. 302 * Moves a user-defined filter to a new position.
301 * @param {Filter} filter 303 * @param {Filter} filter
302 * @param {SpecialSubscription} subscription The subscription where the 304 * @param {SpecialSubscription} subscription The subscription where the
303 * filter is located. 305 * filter is located.
304 * @param {number} oldPosition The current position of the filter. 306 * @param {number} oldPosition The current position of the filter.
305 * @param {number} newPosition The new position of the filter. 307 * @param {number} newPosition The new position of the filter.
306 */ 308 */
307 moveFilter(filter, subscription, oldPosition, newPosition) 309 moveFilter(filter, subscription, oldPosition, newPosition)
308 { 310 {
309 if (!(subscription instanceof SpecialSubscription) || 311 if (!(subscription instanceof SpecialSubscription))
310 subscription.filters[oldPosition] != filter.text) 312 return;
311 { 313
312 return; 314 let currentFilter = subscription.filterAt(oldPosition);
313 } 315 if (!currentFilter || currentFilter.text != filter.text)
316 return;
314 317
315 newPosition = Math.min(Math.max(newPosition, 0), 318 newPosition = Math.min(Math.max(newPosition, 0),
316 subscription.filters.length - 1); 319 subscription.filterCount - 1);
317 if (oldPosition == newPosition) 320 if (oldPosition == newPosition)
318 return; 321 return;
319 322
320 subscription.filters.splice(oldPosition, 1); 323 subscription.deleteFilterAt(oldPosition);
321 subscription.filters.splice(newPosition, 0, filter.text); 324 subscription.insertFilterAt(filter, newPosition);
322 filterNotifier.emit("filter.moved", filter, subscription, oldPosition, 325 filterNotifier.emit("filter.moved", filter, subscription, oldPosition,
323 newPosition); 326 newPosition);
324 } 327 }
325 328
326 /** 329 /**
327 * Increases the hit count for a filter by one. 330 * Increases the hit count for a filter by one.
328 * @param {Filter} filter 331 * @param {Filter} filter
329 */ 332 */
330 increaseHitCount(filter) 333 increaseHitCount(filter)
331 { 334 {
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 * @yields {string} 476 * @yields {string}
474 */ 477 */
475 *exportData() 478 *exportData()
476 { 479 {
477 // Do not persist external subscriptions 480 // Do not persist external subscriptions
478 let subscriptions = []; 481 let subscriptions = [];
479 for (let subscription of this.subscriptions()) 482 for (let subscription of this.subscriptions())
480 { 483 {
481 if (!(subscription instanceof ExternalSubscription) && 484 if (!(subscription instanceof ExternalSubscription) &&
482 !(subscription instanceof SpecialSubscription && 485 !(subscription instanceof SpecialSubscription &&
483 subscription.filters.length == 0)) 486 subscription.filterCount == 0))
484 { 487 {
485 subscriptions.push(subscription); 488 subscriptions.push(subscription);
486 } 489 }
487 } 490 }
488 491
489 yield "# Adblock Plus preferences"; 492 yield "# Adblock Plus preferences";
490 yield "version=" + this.formatVersion; 493 yield "version=" + this.formatVersion;
491 494
492 let saved = new Set(); 495 let saved = new Set();
493 496
494 // Save subscriptions 497 // Save subscriptions
495 for (let subscription of subscriptions) 498 for (let subscription of subscriptions)
496 { 499 {
497 yield* subscription.serialize(); 500 yield* subscription.serialize();
498 yield* subscription.serializeFilters(); 501 yield* subscription.serializeFilters();
499 } 502 }
500 503
501 // Save filter data 504 // Save filter data
502 for (let subscription of subscriptions) 505 for (let subscription of subscriptions)
503 { 506 {
504 for (let text of subscription.filters) 507 for (let filter of subscription.filters())
505 { 508 {
506 if (!saved.has(text)) 509 if (!saved.has(filter.text))
507 { 510 {
508 yield* Filter.fromText(text).serialize(); 511 yield* filter.serialize();
509 saved.add(text); 512 saved.add(filter.text);
510 } 513 }
511 } 514 }
512 } 515 }
513 } 516 }
514 517
515 /** 518 /**
516 * Saves all subscriptions back to disk. 519 * Saves all subscriptions back to disk.
517 * @returns {Promise} A promise resolved or rejected when saving is complete. 520 * @returns {Promise} A promise resolved or rejected when saving is complete.
518 */ 521 */
519 saveToDisk() 522 saveToDisk()
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 * connected to its filters. 656 * connected to its filters.
654 * @param {?Array.<Filter>} [filters] A list of filters to which the 657 * @param {?Array.<Filter>} [filters] A list of filters to which the
655 * subscription should be connected. If this is not given, the subscription 658 * subscription should be connected. If this is not given, the subscription
656 * is connected to its own filters. 659 * is connected to its own filters.
657 */ 660 */
658 function connectSubscriptionFilters(subscription, filters) 661 function connectSubscriptionFilters(subscription, filters)
659 { 662 {
660 if (!filterStorage.knownSubscriptions.has(subscription.url)) 663 if (!filterStorage.knownSubscriptions.has(subscription.url))
661 return; 664 return;
662 665
663 if (filters) 666 for (let filter of filters || subscription.filters())
664 { 667 filter.addSubscription(subscription);
665 for (let filter of filters)
666 filter.addSubscription(subscription);
667 }
668 else
669 {
670 for (let text of subscription.filters)
671 Filter.fromText(text).addSubscription(subscription);
672 }
673 } 668 }
674 669
675 /** 670 /**
676 * Disconnects a subscription from its filters without any notifications. 671 * Disconnects a subscription from its filters without any notifications.
677 * @param {Subscription} subscription The subscription that should be 672 * @param {Subscription} subscription The subscription that should be
678 * disconnected from its filters. 673 * disconnected from its filters.
679 * @param {?Array.<Filter>} [filters] A list of filters from which the 674 * @param {?Array.<Filter>} [filters] A list of filters from which the
680 * subscription should be disconnected. If this is not given, the 675 * subscription should be disconnected. If this is not given, the
681 * subscription is disconnected from its own filters. 676 * subscription is disconnected from its own filters.
682 */ 677 */
683 function disconnectSubscriptionFilters(subscription, filters) 678 function disconnectSubscriptionFilters(subscription, filters)
684 { 679 {
685 if (!filterStorage.knownSubscriptions.has(subscription.url)) 680 if (!filterStorage.knownSubscriptions.has(subscription.url))
686 return; 681 return;
687 682
688 if (filters) 683 for (let filter of filters || subscription.filters())
689 { 684 filter.removeSubscription(subscription);
690 for (let filter of filters)
691 filter.removeSubscription(subscription);
692 }
693 else
694 {
695 for (let text of subscription.filters)
696 Filter.fromText(text).removeSubscription(subscription);
697 }
698 } 685 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld