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

Side by Side Diff: lib/filterStorage.js

Issue 29886685: Issue 6856 - Remove FilterStorage.moveSubscription (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Created Sept. 20, 2018, 7:33 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/synchronizer.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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 */ 73 */
74 firstRun: false, 74 firstRun: false,
75 75
76 /** 76 /**
77 * Map of properties listed in the filter storage file before the sections 77 * Map of properties listed in the filter storage file before the sections
78 * start. Right now this should be only the format version. 78 * start. Right now this should be only the format version.
79 */ 79 */
80 fileProperties: Object.create(null), 80 fileProperties: Object.create(null),
81 81
82 /** 82 /**
83 * List of filter subscriptions containing all filters 83 * Generator for filter subscriptions containing all filters
Manish Jethani 2018/09/21 09:21:58 We could just say "Yields filter subscriptions con
Jon Sonesen 2018/09/27 15:14:05 Done.
84 * @type {Subscription[]} 84 * @type {Subscription.Generator}
Manish Jethani 2018/09/21 09:21:58 This is a function now rather than a property, so
Jon Sonesen 2018/09/27 15:14:04 Done.
85 */ 85 */
86 subscriptions: [], 86 *subscriptions()
87 {
88 for (let subscription of this.knownSubscriptions.values())
Manish Jethani 2018/09/21 09:21:58 We could replace the body of this function with th
Jon Sonesen 2018/09/27 15:14:05 Done.
89 yield subscription;
90 },
91
92 subscriptionCount()
Manish Jethani 2018/09/21 09:21:58 This needs JSDoc. How about: Number of known s
Jon Sonesen 2018/09/27 15:14:04 Done.
93 {
94 return this.knownSubscriptions.size;
95 },
87 96
88 /** 97 /**
89 * Map of subscriptions already on the list, by their URL/identifier 98 * Map of subscriptions already on the list, by their URL/identifier
90 * @type {Map.<string,Subscription>} 99 * @type {Map.<string,Subscription>}
91 */ 100 */
92 knownSubscriptions: new Map(), 101 knownSubscriptions: new Map(),
93 102
94 /** 103 /**
95 * Finds the filter group that a filter should be added to by default. Will 104 * Finds the filter group that a filter should be added to by default. Will
96 * return null if this group doesn't exist yet. 105 * return null if this group doesn't exist yet.
97 * @param {Filter} filter 106 * @param {Filter} filter
98 * @return {?SpecialSubscription} 107 * @return {?SpecialSubscription}
99 */ 108 */
100 getGroupForFilter(filter) 109 getGroupForFilter(filter)
101 { 110 {
102 let generalSubscription = null; 111 let generalSubscription = null;
103 for (let subscription of FilterStorage.subscriptions) 112 for (let subscription of FilterStorage.subscriptions())
104 { 113 {
105 if (subscription instanceof SpecialSubscription && !subscription.disabled) 114 if (subscription instanceof SpecialSubscription && !subscription.disabled)
106 { 115 {
107 // Always prefer specialized subscriptions 116 // Always prefer specialized subscriptions
108 if (subscription.isDefaultFor(filter)) 117 if (subscription.isDefaultFor(filter))
109 return subscription; 118 return subscription;
110 119
111 // If this is a general subscription - store it as fallback 120 // If this is a general subscription - store it as fallback
112 if (!generalSubscription && 121 if (!generalSubscription &&
113 (!subscription.defaults || !subscription.defaults.length)) 122 (!subscription.defaults || !subscription.defaults.length))
114 { 123 {
115 generalSubscription = subscription; 124 generalSubscription = subscription;
116 } 125 }
117 } 126 }
118 } 127 }
119 return generalSubscription; 128 return generalSubscription;
120 }, 129 },
121 130
122 /** 131 /**
123 * Adds a filter subscription to the list 132 * Adds a filter subscription to the list
124 * @param {Subscription} subscription filter subscription to be added 133 * @param {Subscription} subscription filter subscription to be added
125 */ 134 */
126 addSubscription(subscription) 135 addSubscription(subscription)
127 { 136 {
128 if (FilterStorage.knownSubscriptions.has(subscription.url)) 137 if (FilterStorage.knownSubscriptions.has(subscription.url))
129 return; 138 return;
130 139
131 FilterStorage.subscriptions.push(subscription);
132 FilterStorage.knownSubscriptions.set(subscription.url, subscription); 140 FilterStorage.knownSubscriptions.set(subscription.url, subscription);
133 addSubscriptionFilters(subscription); 141 addSubscriptionFilters(subscription);
134 142
135 filterNotifier.emit("subscription.added", subscription); 143 filterNotifier.emit("subscription.added", subscription);
136 }, 144 },
137 145
138 /** 146 /**
139 * Removes a filter subscription from the list 147 * Removes a filter subscription from the list
140 * @param {Subscription} subscription filter subscription to be removed 148 * @param {Subscription} subscription filter subscription to be removed
141 */ 149 */
142 removeSubscription(subscription) 150 removeSubscription(subscription)
143 { 151 {
144 for (let i = 0; i < FilterStorage.subscriptions.length; i++) 152 if (!FilterStorage.knownSubscriptions.has(subscription.url))
145 { 153 return;
Manish Jethani 2018/09/21 09:21:59 It seems when we have a check like this we normall
Jon Sonesen 2018/09/27 15:14:04 Done.
146 if (FilterStorage.subscriptions[i].url == subscription.url) 154 removeSubscriptionFilters(subscription);
147 { 155 FilterStorage.knownSubscriptions.delete(subscription.url);
148 removeSubscriptionFilters(subscription);
149 156
150 FilterStorage.subscriptions.splice(i--, 1); 157 // This should be the last remaining reference to the Subscription
151 FilterStorage.knownSubscriptions.delete(subscription.url); 158 // object.
159 Subscription.knownSubscriptions.delete(subscription.url);
152 160
153 // This should be the last remaining reference to the Subscription 161 filterNotifier.emit("subscription.removed", subscription);
154 // object.
155 Subscription.knownSubscriptions.delete(subscription.url);
156
157 filterNotifier.emit("subscription.removed", subscription);
158 return;
159 }
160 }
161 },
162
163 /**
164 * Moves a subscription in the list to a new position.
165 * @param {Subscription} subscription filter subscription to be moved
166 * @param {Subscription} [insertBefore] filter subscription to insert before
167 * (if omitted the subscription will be put at the end of the list)
168 */
169 moveSubscription(subscription, insertBefore)
170 {
171 let currentPos = FilterStorage.subscriptions.indexOf(subscription);
172 if (currentPos < 0)
173 return;
174
175 let newPos = -1;
176 if (insertBefore)
177 newPos = FilterStorage.subscriptions.indexOf(insertBefore);
178
179 if (newPos < 0)
180 newPos = FilterStorage.subscriptions.length;
181
182 if (currentPos < newPos)
183 newPos--;
184 if (currentPos == newPos)
185 return;
186
187 FilterStorage.subscriptions.splice(currentPos, 1);
188 FilterStorage.subscriptions.splice(newPos, 0, subscription);
189 filterNotifier.emit("subscription.moved", subscription);
190 }, 162 },
191 163
192 /** 164 /**
193 * Replaces the list of filters in a subscription by a new list 165 * Replaces the list of filters in a subscription by a new list
194 * @param {Subscription} subscription filter subscription to be updated 166 * @param {Subscription} subscription filter subscription to be updated
195 * @param {Filter[]} filters new filter list 167 * @param {Filter[]} filters new filter list
196 */ 168 */
197 updateSubscriptionFilters(subscription, filters) 169 updateSubscriptionFilters(subscription, filters)
198 { 170 {
199 removeSubscriptionFilters(subscription); 171 removeSubscriptionFilters(subscription);
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 return line => 338 return line =>
367 { 339 {
368 parser.process(line); 340 parser.process(line);
369 if (line === null) 341 if (line === null)
370 { 342 {
371 let knownSubscriptions = new Map(); 343 let knownSubscriptions = new Map();
372 for (let subscription of parser.subscriptions) 344 for (let subscription of parser.subscriptions)
373 knownSubscriptions.set(subscription.url, subscription); 345 knownSubscriptions.set(subscription.url, subscription);
374 346
375 this.fileProperties = parser.fileProperties; 347 this.fileProperties = parser.fileProperties;
376 this.subscriptions = parser.subscriptions;
377 this.knownSubscriptions = knownSubscriptions; 348 this.knownSubscriptions = knownSubscriptions;
378 Filter.knownFilters = parser.knownFilters; 349 Filter.knownFilters = parser.knownFilters;
379 Subscription.knownSubscriptions = parser.knownSubscriptions; 350 Subscription.knownSubscriptions = parser.knownSubscriptions;
380 351
381 if (!silent) 352 if (!silent)
382 filterNotifier.emit("load"); 353 filterNotifier.emit("load");
383 } 354 }
384 }; 355 };
385 }, 356 },
386 357
387 /** 358 /**
388 * Loads all subscriptions from the disk. 359 * Loads all subscriptions from the disk.
389 * @return {Promise} promise resolved or rejected when loading is complete 360 * @return {Promise} promise resolved or rejected when loading is complete
390 */ 361 */
391 loadFromDisk() 362 loadFromDisk()
392 { 363 {
393 let tryBackup = backupIndex => 364 let tryBackup = backupIndex =>
394 { 365 {
395 return this.restoreBackup(backupIndex, true).then(() => 366 return this.restoreBackup(backupIndex, true).then(() =>
396 { 367 {
397 if (this.subscriptions.length == 0) 368 if (this.subscriptionCount == 0)
398 return tryBackup(backupIndex + 1); 369 return tryBackup(backupIndex + 1);
399 }).catch(error => 370 }).catch(error =>
400 { 371 {
401 // Give up 372 // Give up
402 }); 373 });
403 }; 374 };
404 375
405 return IO.statFile(this.sourceFile).then(statData => 376 return IO.statFile(this.sourceFile).then(statData =>
406 { 377 {
407 if (!statData.exists) 378 if (!statData.exists)
408 { 379 {
409 this.firstRun = true; 380 this.firstRun = true;
410 return; 381 return;
411 } 382 }
412 383
413 let parser = this.importData(true); 384 let parser = this.importData(true);
414 return IO.readFromFile(this.sourceFile, parser).then(() => 385 return IO.readFromFile(this.sourceFile, parser).then(() =>
415 { 386 {
416 parser(null); 387 parser(null);
417 if (this.subscriptions.length == 0) 388 if (this.subscriptionCount == 0)
418 { 389 {
419 // No filter subscriptions in the file, this isn't right. 390 // No filter subscriptions in the file, this isn't right.
420 throw new Error("No data in the file"); 391 throw new Error("No data in the file");
421 } 392 }
422 }); 393 });
423 }).catch(error => 394 }).catch(error =>
424 { 395 {
425 Cu.reportError(error); 396 Cu.reportError(error);
426 return tryBackup(1); 397 return tryBackup(1);
427 }).then(() => 398 }).then(() =>
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 return this.saveToDisk(); 432 return this.saveToDisk();
462 }); 433 });
463 }, 434 },
464 435
465 /** 436 /**
466 * Generator serializing filter data and yielding it line by line. 437 * Generator serializing filter data and yielding it line by line.
467 */ 438 */
468 *exportData() 439 *exportData()
469 { 440 {
470 // Do not persist external subscriptions 441 // Do not persist external subscriptions
471 let subscriptions = this.subscriptions.filter( 442 let subscriptions = [...this.subscriptions()].filter(
472 s => !(s instanceof ExternalSubscription) 443 s => !(s instanceof ExternalSubscription)
473 ); 444 );
474 445
475 yield "# Adblock Plus preferences"; 446 yield "# Adblock Plus preferences";
476 yield "version=" + formatVersion; 447 yield "version=" + formatVersion;
477 448
478 let saved = new Set(); 449 let saved = new Set();
479 let buf = []; 450 let buf = [];
480 451
481 // Save subscriptions 452 // Save subscriptions
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 * @param {Subscription} subscription filter subscription to be removed 642 * @param {Subscription} subscription filter subscription to be removed
672 */ 643 */
673 function removeSubscriptionFilters(subscription) 644 function removeSubscriptionFilters(subscription)
674 { 645 {
675 if (!FilterStorage.knownSubscriptions.has(subscription.url)) 646 if (!FilterStorage.knownSubscriptions.has(subscription.url))
676 return; 647 return;
677 648
678 for (let filter of subscription.filters) 649 for (let filter of subscription.filters)
679 filter.removeSubscription(subscription); 650 filter.removeSubscription(subscription);
680 } 651 }
OLDNEW
« no previous file with comments | « lib/filterListener.js ('k') | lib/synchronizer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld