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: Update JSDoc Created Nov. 18, 2018, 2:27 a.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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 connectSubscriptionFilters(subscription); 168 connectSubscriptionFilters(subscription);
169 169
170 filterNotifier.emit("subscription.added", subscription); 170 filterNotifier.emit("subscription.added", subscription);
171
172 subscription.clearCaches();
173 } 171 }
174 172
175 /** 173 /**
176 * Removes a subscription from the storage. 174 * Removes a subscription from the storage.
177 * @param {Subscription} subscription The subscription to be removed. 175 * @param {Subscription} subscription The subscription to be removed.
178 */ 176 */
179 removeSubscription(subscription) 177 removeSubscription(subscription)
180 { 178 {
181 if (!this.knownSubscriptions.has(subscription.url)) 179 if (!this.knownSubscriptions.has(subscription.url))
182 return; 180 return;
(...skipping 19 matching lines...) Expand all
202 let oldFilters = [...subscription.filters()]; 200 let oldFilters = [...subscription.filters()];
203 disconnectSubscriptionFilters(subscription, oldFilters); 201 disconnectSubscriptionFilters(subscription, oldFilters);
204 subscription.clearFilters(); 202 subscription.clearFilters();
205 203
206 for (let filter of filters) 204 for (let filter of filters)
207 subscription.addFilter(filter); 205 subscription.addFilter(filter);
208 206
209 connectSubscriptionFilters(subscription, filters); 207 connectSubscriptionFilters(subscription, filters);
210 208
211 filterNotifier.emit("subscription.updated", subscription, oldFilters); 209 filterNotifier.emit("subscription.updated", subscription, oldFilters);
212
213 subscription.clearCaches();
214 } 210 }
215 211
216 /** 212 /**
217 * Adds a user-defined filter to the storage. 213 * Adds a user-defined filter to the storage.
218 * @param {Filter} filter 214 * @param {Filter} filter
219 * @param {?SpecialSubscription} [subscription] The subscription that the 215 * @param {?SpecialSubscription} [subscription] The subscription that the
220 * filter should be added to. 216 * filter should be added to.
221 * @param {number} [position] The position within the subscription at which 217 * @param {number} [position] The position within the subscription at which
222 * 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
223 * end of the subscription. 219 * end of the subscription.
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 let knownSubscriptions = new Map(); 380 let knownSubscriptions = new Map();
385 for (let subscription of parser.subscriptions) 381 for (let subscription of parser.subscriptions)
386 knownSubscriptions.set(subscription.url, subscription); 382 knownSubscriptions.set(subscription.url, subscription);
387 383
388 this.fileProperties = parser.fileProperties; 384 this.fileProperties = parser.fileProperties;
389 this.knownSubscriptions = knownSubscriptions; 385 this.knownSubscriptions = knownSubscriptions;
390 Filter.knownFilters = parser.knownFilters; 386 Filter.knownFilters = parser.knownFilters;
391 Subscription.knownSubscriptions = parser.knownSubscriptions; 387 Subscription.knownSubscriptions = parser.knownSubscriptions;
392 388
393 if (!silent) 389 if (!silent)
394 {
395 filterNotifier.emit("load"); 390 filterNotifier.emit("load");
396
397 // Clear any in-memory caches that may have been created during
398 // initialization.
399 clearSubscriptionCaches();
400 }
401 } 391 }
402 }; 392 };
403 } 393 }
404 394
405 /** 395 /**
406 * Loads all subscriptions from disk. 396 * Loads all subscriptions from disk.
407 * @returns {Promise} A promise resolved or rejected when loading is complete. 397 * @returns {Promise} A promise resolved or rejected when loading is complete.
408 */ 398 */
409 loadFromDisk() 399 loadFromDisk()
410 { 400 {
(...skipping 28 matching lines...) Expand all
439 } 429 }
440 }); 430 });
441 }).catch(error => 431 }).catch(error =>
442 { 432 {
443 Cu.reportError(error); 433 Cu.reportError(error);
444 return tryBackup(1); 434 return tryBackup(1);
445 }).then(() => 435 }).then(() =>
446 { 436 {
447 this.initialized = true; 437 this.initialized = true;
448 filterNotifier.emit("load"); 438 filterNotifier.emit("load");
449 clearSubscriptionCaches();
450 }); 439 });
451 } 440 }
452 441
453 /** 442 /**
454 * Constructs the file name for a <code>patterns.ini</code> backup. 443 * Constructs the file name for a <code>patterns.ini</code> backup.
455 * @param {number} backupIndex Number of the backup file (1 being the most 444 * @param {number} backupIndex Number of the backup file (1 being the most
456 * recent). 445 * recent).
457 * @returns {string} Backup file name. 446 * @returns {string} Backup file name.
458 */ 447 */
459 getBackupName(backupIndex) 448 getBackupName(backupIndex)
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 * subscription is disconnected from its own filters. 676 * subscription is disconnected from its own filters.
688 */ 677 */
689 function disconnectSubscriptionFilters(subscription, filters) 678 function disconnectSubscriptionFilters(subscription, filters)
690 { 679 {
691 if (!filterStorage.knownSubscriptions.has(subscription.url)) 680 if (!filterStorage.knownSubscriptions.has(subscription.url))
692 return; 681 return;
693 682
694 for (let filter of filters || subscription.filters()) 683 for (let filter of filters || subscription.filters())
695 filter.removeSubscription(subscription); 684 filter.removeSubscription(subscription);
696 } 685 }
697
698 /**
699 * Clears any in-memory caches held by subscriptions in the storage.
700 */
701 function clearSubscriptionCaches()
702 {
703 for (let subscription of filterStorage.subscriptions())
704 subscription.clearCaches();
705 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld