OLD | NEW |
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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 /** | 20 /** |
21 * @fileOverview FilterStorage class responsible for managing user's | 21 * @fileOverview FilterStorage class responsible for managing user's |
22 * subscriptions and filters. | 22 * subscriptions and filters. |
23 */ | 23 */ |
24 | 24 |
25 const {IO} = require("io"); | 25 const {IO} = require("io"); |
26 const {Prefs} = require("prefs"); | 26 const {Prefs} = require("prefs"); |
27 const {Filter, ActiveFilter} = require("./filterClasses"); | 27 const {Filter, ActiveFilter} = require("./filterClasses"); |
28 const {Subscription, SpecialSubscription, | 28 const {Subscription, SpecialSubscription, |
29 ExternalSubscription} = require("./subscriptionClasses"); | 29 ExternalSubscription} = require("./subscriptionClasses"); |
30 const {FilterNotifier} = require("./filterNotifier"); | 30 const {filterNotifier} = require("./filterNotifier"); |
31 const {INIParser} = require("./iniParser"); | 31 const {INIParser} = require("./iniParser"); |
32 | 32 |
33 /** | 33 /** |
34 * Version number of the filter storage file format. | 34 * Version number of the filter storage file format. |
35 * @type {number} | 35 * @type {number} |
36 */ | 36 */ |
37 let formatVersion = 5; | 37 let formatVersion = 5; |
38 | 38 |
39 /** | 39 /** |
40 * This class reads user's filters from disk, manages them in memory | 40 * This class reads user's filters from disk, manages them in memory |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 */ | 125 */ |
126 addSubscription(subscription) | 126 addSubscription(subscription) |
127 { | 127 { |
128 if (FilterStorage.knownSubscriptions.has(subscription.url)) | 128 if (FilterStorage.knownSubscriptions.has(subscription.url)) |
129 return; | 129 return; |
130 | 130 |
131 FilterStorage.subscriptions.push(subscription); | 131 FilterStorage.subscriptions.push(subscription); |
132 FilterStorage.knownSubscriptions.set(subscription.url, subscription); | 132 FilterStorage.knownSubscriptions.set(subscription.url, subscription); |
133 addSubscriptionFilters(subscription); | 133 addSubscriptionFilters(subscription); |
134 | 134 |
135 FilterNotifier.emit("subscription.added", subscription); | 135 filterNotifier.emit("subscription.added", subscription); |
136 }, | 136 }, |
137 | 137 |
138 /** | 138 /** |
139 * Removes a filter subscription from the list | 139 * Removes a filter subscription from the list |
140 * @param {Subscription} subscription filter subscription to be removed | 140 * @param {Subscription} subscription filter subscription to be removed |
141 */ | 141 */ |
142 removeSubscription(subscription) | 142 removeSubscription(subscription) |
143 { | 143 { |
144 for (let i = 0; i < FilterStorage.subscriptions.length; i++) | 144 for (let i = 0; i < FilterStorage.subscriptions.length; i++) |
145 { | 145 { |
146 if (FilterStorage.subscriptions[i].url == subscription.url) | 146 if (FilterStorage.subscriptions[i].url == subscription.url) |
147 { | 147 { |
148 removeSubscriptionFilters(subscription); | 148 removeSubscriptionFilters(subscription); |
149 | 149 |
150 FilterStorage.subscriptions.splice(i--, 1); | 150 FilterStorage.subscriptions.splice(i--, 1); |
151 FilterStorage.knownSubscriptions.delete(subscription.url); | 151 FilterStorage.knownSubscriptions.delete(subscription.url); |
152 | 152 |
153 // This should be the last remaining reference to the Subscription | 153 // This should be the last remaining reference to the Subscription |
154 // object. | 154 // object. |
155 Subscription.knownSubscriptions.delete(subscription.url); | 155 Subscription.knownSubscriptions.delete(subscription.url); |
156 | 156 |
157 FilterNotifier.emit("subscription.removed", subscription); | 157 filterNotifier.emit("subscription.removed", subscription); |
158 return; | 158 return; |
159 } | 159 } |
160 } | 160 } |
161 }, | 161 }, |
162 | 162 |
163 /** | 163 /** |
164 * Moves a subscription in the list to a new position. | 164 * Moves a subscription in the list to a new position. |
165 * @param {Subscription} subscription filter subscription to be moved | 165 * @param {Subscription} subscription filter subscription to be moved |
166 * @param {Subscription} [insertBefore] filter subscription to insert before | 166 * @param {Subscription} [insertBefore] filter subscription to insert before |
167 * (if omitted the subscription will be put at the end of the list) | 167 * (if omitted the subscription will be put at the end of the list) |
(...skipping 11 matching lines...) Expand all Loading... |
179 if (newPos < 0) | 179 if (newPos < 0) |
180 newPos = FilterStorage.subscriptions.length; | 180 newPos = FilterStorage.subscriptions.length; |
181 | 181 |
182 if (currentPos < newPos) | 182 if (currentPos < newPos) |
183 newPos--; | 183 newPos--; |
184 if (currentPos == newPos) | 184 if (currentPos == newPos) |
185 return; | 185 return; |
186 | 186 |
187 FilterStorage.subscriptions.splice(currentPos, 1); | 187 FilterStorage.subscriptions.splice(currentPos, 1); |
188 FilterStorage.subscriptions.splice(newPos, 0, subscription); | 188 FilterStorage.subscriptions.splice(newPos, 0, subscription); |
189 FilterNotifier.emit("subscription.moved", subscription); | 189 filterNotifier.emit("subscription.moved", subscription); |
190 }, | 190 }, |
191 | 191 |
192 /** | 192 /** |
193 * Replaces the list of filters in a subscription by a new list | 193 * Replaces the list of filters in a subscription by a new list |
194 * @param {Subscription} subscription filter subscription to be updated | 194 * @param {Subscription} subscription filter subscription to be updated |
195 * @param {Filter[]} filters new filter list | 195 * @param {Filter[]} filters new filter list |
196 */ | 196 */ |
197 updateSubscriptionFilters(subscription, filters) | 197 updateSubscriptionFilters(subscription, filters) |
198 { | 198 { |
199 removeSubscriptionFilters(subscription); | 199 removeSubscriptionFilters(subscription); |
200 subscription.oldFilters = subscription.filters; | 200 subscription.oldFilters = subscription.filters; |
201 subscription.filters = filters; | 201 subscription.filters = filters; |
202 addSubscriptionFilters(subscription); | 202 addSubscriptionFilters(subscription); |
203 FilterNotifier.emit("subscription.updated", subscription); | 203 filterNotifier.emit("subscription.updated", subscription); |
204 delete subscription.oldFilters; | 204 delete subscription.oldFilters; |
205 }, | 205 }, |
206 | 206 |
207 /** | 207 /** |
208 * Adds a user-defined filter to the list | 208 * Adds a user-defined filter to the list |
209 * @param {Filter} filter | 209 * @param {Filter} filter |
210 * @param {SpecialSubscription} [subscription] | 210 * @param {SpecialSubscription} [subscription] |
211 * particular group that the filter should be added to | 211 * particular group that the filter should be added to |
212 * @param {number} [position] | 212 * @param {number} [position] |
213 * position within the subscription at which the filter should be added | 213 * position within the subscription at which the filter should be added |
(...skipping 18 matching lines...) Expand all Loading... |
232 subscription = SpecialSubscription.createForFilter(filter); | 232 subscription = SpecialSubscription.createForFilter(filter); |
233 this.addSubscription(subscription); | 233 this.addSubscription(subscription); |
234 return; | 234 return; |
235 } | 235 } |
236 | 236 |
237 if (typeof position == "undefined") | 237 if (typeof position == "undefined") |
238 position = subscription.filters.length; | 238 position = subscription.filters.length; |
239 | 239 |
240 filter.subscriptions.add(subscription); | 240 filter.subscriptions.add(subscription); |
241 subscription.filters.splice(position, 0, filter); | 241 subscription.filters.splice(position, 0, filter); |
242 FilterNotifier.emit("filter.added", filter, subscription, position); | 242 filterNotifier.emit("filter.added", filter, subscription, position); |
243 }, | 243 }, |
244 | 244 |
245 /** | 245 /** |
246 * Removes a user-defined filter from the list | 246 * Removes a user-defined filter from the list |
247 * @param {Filter} filter | 247 * @param {Filter} filter |
248 * @param {SpecialSubscription} [subscription] a particular filter group that | 248 * @param {SpecialSubscription} [subscription] a particular filter group that |
249 * the filter should be removed from (if ommited will be removed from all | 249 * the filter should be removed from (if ommited will be removed from all |
250 * subscriptions) | 250 * subscriptions) |
251 * @param {number} [position] position inside the filter group at which the | 251 * @param {number} [position] position inside the filter group at which the |
252 * filter should be removed (if ommited all instances will be removed) | 252 * filter should be removed (if ommited all instances will be removed) |
(...skipping 22 matching lines...) Expand all Loading... |
275 positions.push(position); | 275 positions.push(position); |
276 | 276 |
277 for (let j = positions.length - 1; j >= 0; j--) | 277 for (let j = positions.length - 1; j >= 0; j--) |
278 { | 278 { |
279 let currentPosition = positions[j]; | 279 let currentPosition = positions[j]; |
280 if (currentSubscription.filters[currentPosition] == filter) | 280 if (currentSubscription.filters[currentPosition] == filter) |
281 { | 281 { |
282 currentSubscription.filters.splice(currentPosition, 1); | 282 currentSubscription.filters.splice(currentPosition, 1); |
283 if (currentSubscription.filters.indexOf(filter) < 0) | 283 if (currentSubscription.filters.indexOf(filter) < 0) |
284 filter.subscriptions.delete(currentSubscription); | 284 filter.subscriptions.delete(currentSubscription); |
285 FilterNotifier.emit("filter.removed", filter, currentSubscription, | 285 filterNotifier.emit("filter.removed", filter, currentSubscription, |
286 currentPosition); | 286 currentPosition); |
287 } | 287 } |
288 } | 288 } |
289 } | 289 } |
290 } | 290 } |
291 }, | 291 }, |
292 | 292 |
293 /** | 293 /** |
294 * Moves a user-defined filter to a new position | 294 * Moves a user-defined filter to a new position |
295 * @param {Filter} filter | 295 * @param {Filter} filter |
(...skipping 10 matching lines...) Expand all Loading... |
306 return; | 306 return; |
307 } | 307 } |
308 | 308 |
309 newPosition = Math.min(Math.max(newPosition, 0), | 309 newPosition = Math.min(Math.max(newPosition, 0), |
310 subscription.filters.length - 1); | 310 subscription.filters.length - 1); |
311 if (oldPosition == newPosition) | 311 if (oldPosition == newPosition) |
312 return; | 312 return; |
313 | 313 |
314 subscription.filters.splice(oldPosition, 1); | 314 subscription.filters.splice(oldPosition, 1); |
315 subscription.filters.splice(newPosition, 0, filter); | 315 subscription.filters.splice(newPosition, 0, filter); |
316 FilterNotifier.emit("filter.moved", filter, subscription, oldPosition, | 316 filterNotifier.emit("filter.moved", filter, subscription, oldPosition, |
317 newPosition); | 317 newPosition); |
318 }, | 318 }, |
319 | 319 |
320 /** | 320 /** |
321 * Increases the hit count for a filter by one | 321 * Increases the hit count for a filter by one |
322 * @param {Filter} filter | 322 * @param {Filter} filter |
323 */ | 323 */ |
324 increaseHitCount(filter) | 324 increaseHitCount(filter) |
325 { | 325 { |
326 if (!Prefs.savestats || !(filter instanceof ActiveFilter)) | 326 if (!Prefs.savestats || !(filter instanceof ActiveFilter)) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 for (let subscription of parser.subscriptions) | 373 for (let subscription of parser.subscriptions) |
374 knownSubscriptions.set(subscription.url, subscription); | 374 knownSubscriptions.set(subscription.url, subscription); |
375 | 375 |
376 this.fileProperties = parser.fileProperties; | 376 this.fileProperties = parser.fileProperties; |
377 this.subscriptions = parser.subscriptions; | 377 this.subscriptions = parser.subscriptions; |
378 this.knownSubscriptions = knownSubscriptions; | 378 this.knownSubscriptions = knownSubscriptions; |
379 Filter.knownFilters = parser.knownFilters; | 379 Filter.knownFilters = parser.knownFilters; |
380 Subscription.knownSubscriptions = parser.knownSubscriptions; | 380 Subscription.knownSubscriptions = parser.knownSubscriptions; |
381 | 381 |
382 if (!silent) | 382 if (!silent) |
383 FilterNotifier.emit("load"); | 383 filterNotifier.emit("load"); |
384 } | 384 } |
385 }; | 385 }; |
386 }, | 386 }, |
387 | 387 |
388 /** | 388 /** |
389 * Loads all subscriptions from the disk. | 389 * Loads all subscriptions from the disk. |
390 * @return {Promise} promise resolved or rejected when loading is complete | 390 * @return {Promise} promise resolved or rejected when loading is complete |
391 */ | 391 */ |
392 loadFromDisk() | 392 loadFromDisk() |
393 { | 393 { |
(...skipping 27 matching lines...) Expand all Loading... |
421 throw new Error("No data in the file"); | 421 throw new Error("No data in the file"); |
422 } | 422 } |
423 }); | 423 }); |
424 }).catch(error => | 424 }).catch(error => |
425 { | 425 { |
426 Cu.reportError(error); | 426 Cu.reportError(error); |
427 return tryBackup(1); | 427 return tryBackup(1); |
428 }).then(() => | 428 }).then(() => |
429 { | 429 { |
430 this.initialized = true; | 430 this.initialized = true; |
431 FilterNotifier.emit("load"); | 431 filterNotifier.emit("load"); |
432 }); | 432 }); |
433 }, | 433 }, |
434 | 434 |
435 /** | 435 /** |
436 * Constructs the file name for a patterns.ini backup. | 436 * Constructs the file name for a patterns.ini backup. |
437 * @param {number} backupIndex | 437 * @param {number} backupIndex |
438 * number of the backup file (1 being the most recent) | 438 * number of the backup file (1 being the most recent) |
439 * @return {string} backup file name | 439 * @return {string} backup file name |
440 */ | 440 */ |
441 getBackupName(backupIndex) | 441 getBackupName(backupIndex) |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 return renameBackup(Prefs.patternsbackups - 1); | 589 return renameBackup(Prefs.patternsbackups - 1); |
590 }).catch(error => | 590 }).catch(error => |
591 { | 591 { |
592 // Errors during backup creation shouldn't prevent writing filters. | 592 // Errors during backup creation shouldn't prevent writing filters. |
593 Cu.reportError(error); | 593 Cu.reportError(error); |
594 }).then(() => | 594 }).then(() => |
595 { | 595 { |
596 return IO.writeToFile(this.sourceFile, this.exportData()); | 596 return IO.writeToFile(this.sourceFile, this.exportData()); |
597 }).then(() => | 597 }).then(() => |
598 { | 598 { |
599 FilterNotifier.emit("save"); | 599 filterNotifier.emit("save"); |
600 }).catch(error => | 600 }).catch(error => |
601 { | 601 { |
602 // If saving failed, report error but continue - we still have to process | 602 // If saving failed, report error but continue - we still have to process |
603 // flags. | 603 // flags. |
604 Cu.reportError(error); | 604 Cu.reportError(error); |
605 }).then(() => | 605 }).then(() => |
606 { | 606 { |
607 this._saving = false; | 607 this._saving = false; |
608 if (this._needsSave) | 608 if (this._needsSave) |
609 { | 609 { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 * @param {Subscription} subscription filter subscription to be removed | 672 * @param {Subscription} subscription filter subscription to be removed |
673 */ | 673 */ |
674 function removeSubscriptionFilters(subscription) | 674 function removeSubscriptionFilters(subscription) |
675 { | 675 { |
676 if (!FilterStorage.knownSubscriptions.has(subscription.url)) | 676 if (!FilterStorage.knownSubscriptions.has(subscription.url)) |
677 return; | 677 return; |
678 | 678 |
679 for (let filter of subscription.filters) | 679 for (let filter of subscription.filters) |
680 filter.subscriptions.delete(subscription); | 680 filter.subscriptions.delete(subscription); |
681 } | 681 } |
OLD | NEW |