Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 Loading... | |
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 * Generator for filter subscriptions containing all filters | 83 * Yields 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.Generator} | 84 * @yields {Subscription} |
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 { | 87 { |
88 for (let subscription of this.knownSubscriptions.values()) | 88 yield* 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; | 89 }, |
90 }, | 90 |
91 | 91 /** |
92 subscriptionCount() | 92 * Number of known subscriptions. |
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 * @type {number} | |
94 */ | |
95 get subscriptionCount() | |
93 { | 96 { |
94 return this.knownSubscriptions.size; | 97 return this.knownSubscriptions.size; |
95 }, | 98 }, |
96 | 99 |
97 /** | 100 /** |
98 * Map of subscriptions already on the list, by their URL/identifier | 101 * Map of subscriptions already on the list, by their URL/identifier |
99 * @type {Map.<string,Subscription>} | 102 * @type {Map.<string,Subscription>} |
100 */ | 103 */ |
101 knownSubscriptions: new Map(), | 104 knownSubscriptions: new Map(), |
102 | 105 |
103 /** | 106 /** |
104 * Finds the filter group that a filter should be added to by default. Will | 107 * Finds the filter group that a filter should be added to by default. Will |
105 * return null if this group doesn't exist yet. | 108 * return null if this group doesn't exist yet. |
106 * @param {Filter} filter | 109 * @param {Filter} filter |
107 * @return {?SpecialSubscription} | 110 * @return {?SpecialSubscription} |
108 */ | 111 */ |
109 getGroupForFilter(filter) | 112 getGroupForFilter(filter) |
110 { | 113 { |
111 let generalSubscription = null; | 114 let generalSubscription = null; |
112 for (let subscription of FilterStorage.subscriptions()) | 115 for (let subscription of FilterStorage.knownSubscriptions.values()) |
113 { | 116 { |
114 if (subscription instanceof SpecialSubscription && !subscription.disabled) | 117 if (subscription instanceof SpecialSubscription && !subscription.disabled) |
115 { | 118 { |
116 // Always prefer specialized subscriptions | 119 // Always prefer specialized subscriptions |
117 if (subscription.isDefaultFor(filter)) | 120 if (subscription.isDefaultFor(filter)) |
118 return subscription; | 121 return subscription; |
119 | 122 |
120 // If this is a general subscription - store it as fallback | 123 // If this is a general subscription - store it as fallback |
121 if (!generalSubscription && | 124 if (!generalSubscription && |
122 (!subscription.defaults || !subscription.defaults.length)) | 125 (!subscription.defaults || !subscription.defaults.length)) |
(...skipping 20 matching lines...) Expand all Loading... | |
143 filterNotifier.emit("subscription.added", subscription); | 146 filterNotifier.emit("subscription.added", subscription); |
144 }, | 147 }, |
145 | 148 |
146 /** | 149 /** |
147 * Removes a filter subscription from the list | 150 * Removes a filter subscription from the list |
148 * @param {Subscription} subscription filter subscription to be removed | 151 * @param {Subscription} subscription filter subscription to be removed |
149 */ | 152 */ |
150 removeSubscription(subscription) | 153 removeSubscription(subscription) |
151 { | 154 { |
152 if (!FilterStorage.knownSubscriptions.has(subscription.url)) | 155 if (!FilterStorage.knownSubscriptions.has(subscription.url)) |
153 return; | 156 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.
| |
157 | |
154 removeSubscriptionFilters(subscription); | 158 removeSubscriptionFilters(subscription); |
159 | |
155 FilterStorage.knownSubscriptions.delete(subscription.url); | 160 FilterStorage.knownSubscriptions.delete(subscription.url); |
156 | 161 |
157 // This should be the last remaining reference to the Subscription | 162 // This should be the last remaining reference to the Subscription |
158 // object. | 163 // object. |
159 Subscription.knownSubscriptions.delete(subscription.url); | 164 Subscription.knownSubscriptions.delete(subscription.url); |
160 | 165 |
161 filterNotifier.emit("subscription.removed", subscription); | 166 filterNotifier.emit("subscription.removed", subscription); |
162 }, | 167 }, |
163 | 168 |
164 /** | 169 /** |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 /** | 363 /** |
359 * Loads all subscriptions from the disk. | 364 * Loads all subscriptions from the disk. |
360 * @return {Promise} promise resolved or rejected when loading is complete | 365 * @return {Promise} promise resolved or rejected when loading is complete |
361 */ | 366 */ |
362 loadFromDisk() | 367 loadFromDisk() |
363 { | 368 { |
364 let tryBackup = backupIndex => | 369 let tryBackup = backupIndex => |
365 { | 370 { |
366 return this.restoreBackup(backupIndex, true).then(() => | 371 return this.restoreBackup(backupIndex, true).then(() => |
367 { | 372 { |
368 if (this.subscriptionCount == 0) | 373 if (this.knownSubscriptions.size == 0) |
369 return tryBackup(backupIndex + 1); | 374 return tryBackup(backupIndex + 1); |
370 }).catch(error => | 375 }).catch(error => |
371 { | 376 { |
372 // Give up | 377 // Give up |
373 }); | 378 }); |
374 }; | 379 }; |
375 | 380 |
376 return IO.statFile(this.sourceFile).then(statData => | 381 return IO.statFile(this.sourceFile).then(statData => |
377 { | 382 { |
378 if (!statData.exists) | 383 if (!statData.exists) |
379 { | 384 { |
380 this.firstRun = true; | 385 this.firstRun = true; |
381 return; | 386 return; |
382 } | 387 } |
383 | 388 |
384 let parser = this.importData(true); | 389 let parser = this.importData(true); |
385 return IO.readFromFile(this.sourceFile, parser).then(() => | 390 return IO.readFromFile(this.sourceFile, parser).then(() => |
386 { | 391 { |
387 parser(null); | 392 parser(null); |
388 if (this.subscriptionCount == 0) | 393 if (this.knownSubscriptions.size == 0) |
389 { | 394 { |
390 // No filter subscriptions in the file, this isn't right. | 395 // No filter subscriptions in the file, this isn't right. |
391 throw new Error("No data in the file"); | 396 throw new Error("No data in the file"); |
392 } | 397 } |
393 }); | 398 }); |
394 }).catch(error => | 399 }).catch(error => |
395 { | 400 { |
396 Cu.reportError(error); | 401 Cu.reportError(error); |
397 return tryBackup(1); | 402 return tryBackup(1); |
398 }).then(() => | 403 }).then(() => |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
432 return this.saveToDisk(); | 437 return this.saveToDisk(); |
433 }); | 438 }); |
434 }, | 439 }, |
435 | 440 |
436 /** | 441 /** |
437 * Generator serializing filter data and yielding it line by line. | 442 * Generator serializing filter data and yielding it line by line. |
438 */ | 443 */ |
439 *exportData() | 444 *exportData() |
440 { | 445 { |
441 // Do not persist external subscriptions | 446 // Do not persist external subscriptions |
442 let subscriptions = [...this.subscriptions()].filter( | 447 let subscriptions = []; |
443 s => !(s instanceof ExternalSubscription) | 448 for (let subscription of this.subscriptions()) |
444 ); | 449 { |
450 if (!(subscription instanceof ExternalSubscription)) | |
451 subscriptions.push(subscription); | |
452 } | |
445 | 453 |
446 yield "# Adblock Plus preferences"; | 454 yield "# Adblock Plus preferences"; |
447 yield "version=" + formatVersion; | 455 yield "version=" + formatVersion; |
448 | 456 |
449 let saved = new Set(); | 457 let saved = new Set(); |
450 let buf = []; | 458 let buf = []; |
451 | 459 |
452 // Save subscriptions | 460 // Save subscriptions |
453 for (let subscription of subscriptions) | 461 for (let subscription of subscriptions) |
454 { | 462 { |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
642 * @param {Subscription} subscription filter subscription to be removed | 650 * @param {Subscription} subscription filter subscription to be removed |
643 */ | 651 */ |
644 function removeSubscriptionFilters(subscription) | 652 function removeSubscriptionFilters(subscription) |
645 { | 653 { |
646 if (!FilterStorage.knownSubscriptions.has(subscription.url)) | 654 if (!FilterStorage.knownSubscriptions.has(subscription.url)) |
647 return; | 655 return; |
648 | 656 |
649 for (let filter of subscription.filters) | 657 for (let filter of subscription.filters) |
650 filter.removeSubscription(subscription); | 658 filter.removeSubscription(subscription); |
651 } | 659 } |
LEFT | RIGHT |