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 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 */ | 124 */ |
125 addSubscription(subscription) | 125 addSubscription(subscription) |
126 { | 126 { |
127 if (FilterStorage.knownSubscriptions.has(subscription.url)) | 127 if (FilterStorage.knownSubscriptions.has(subscription.url)) |
128 return; | 128 return; |
129 | 129 |
130 FilterStorage.subscriptions.push(subscription); | 130 FilterStorage.subscriptions.push(subscription); |
131 FilterStorage.knownSubscriptions.set(subscription.url, subscription); | 131 FilterStorage.knownSubscriptions.set(subscription.url, subscription); |
132 addSubscriptionFilters(subscription); | 132 addSubscriptionFilters(subscription); |
133 | 133 |
134 FilterNotifier.triggerListeners("subscription.added", subscription); | 134 FilterNotifier.emit("subscription.added", subscription); |
135 }, | 135 }, |
136 | 136 |
137 /** | 137 /** |
138 * Removes a filter subscription from the list | 138 * Removes a filter subscription from the list |
139 * @param {Subscription} subscription filter subscription to be removed | 139 * @param {Subscription} subscription filter subscription to be removed |
140 */ | 140 */ |
141 removeSubscription(subscription) | 141 removeSubscription(subscription) |
142 { | 142 { |
143 for (let i = 0; i < FilterStorage.subscriptions.length; i++) | 143 for (let i = 0; i < FilterStorage.subscriptions.length; i++) |
144 { | 144 { |
145 if (FilterStorage.subscriptions[i].url == subscription.url) | 145 if (FilterStorage.subscriptions[i].url == subscription.url) |
146 { | 146 { |
147 removeSubscriptionFilters(subscription); | 147 removeSubscriptionFilters(subscription); |
148 | 148 |
149 FilterStorage.subscriptions.splice(i--, 1); | 149 FilterStorage.subscriptions.splice(i--, 1); |
150 FilterStorage.knownSubscriptions.delete(subscription.url); | 150 FilterStorage.knownSubscriptions.delete(subscription.url); |
151 FilterNotifier.triggerListeners("subscription.removed", subscription); | 151 FilterNotifier.emit("subscription.removed", subscription); |
152 return; | 152 return; |
153 } | 153 } |
154 } | 154 } |
155 }, | 155 }, |
156 | 156 |
157 /** | 157 /** |
158 * Moves a subscription in the list to a new position. | 158 * Moves a subscription in the list to a new position. |
159 * @param {Subscription} subscription filter subscription to be moved | 159 * @param {Subscription} subscription filter subscription to be moved |
160 * @param {Subscription} [insertBefore] filter subscription to insert before | 160 * @param {Subscription} [insertBefore] filter subscription to insert before |
161 * (if omitted the subscription will be put at the end of the list) | 161 * (if omitted the subscription will be put at the end of the list) |
(...skipping 11 matching lines...) Expand all Loading... |
173 if (newPos < 0) | 173 if (newPos < 0) |
174 newPos = FilterStorage.subscriptions.length; | 174 newPos = FilterStorage.subscriptions.length; |
175 | 175 |
176 if (currentPos < newPos) | 176 if (currentPos < newPos) |
177 newPos--; | 177 newPos--; |
178 if (currentPos == newPos) | 178 if (currentPos == newPos) |
179 return; | 179 return; |
180 | 180 |
181 FilterStorage.subscriptions.splice(currentPos, 1); | 181 FilterStorage.subscriptions.splice(currentPos, 1); |
182 FilterStorage.subscriptions.splice(newPos, 0, subscription); | 182 FilterStorage.subscriptions.splice(newPos, 0, subscription); |
183 FilterNotifier.triggerListeners("subscription.moved", subscription); | 183 FilterNotifier.emit("subscription.moved", subscription); |
184 }, | 184 }, |
185 | 185 |
186 /** | 186 /** |
187 * Replaces the list of filters in a subscription by a new list | 187 * Replaces the list of filters in a subscription by a new list |
188 * @param {Subscription} subscription filter subscription to be updated | 188 * @param {Subscription} subscription filter subscription to be updated |
189 * @param {Filter[]} filters new filter list | 189 * @param {Filter[]} filters new filter list |
190 */ | 190 */ |
191 updateSubscriptionFilters(subscription, filters) | 191 updateSubscriptionFilters(subscription, filters) |
192 { | 192 { |
193 removeSubscriptionFilters(subscription); | 193 removeSubscriptionFilters(subscription); |
194 subscription.oldFilters = subscription.filters; | 194 subscription.oldFilters = subscription.filters; |
195 subscription.filters = filters; | 195 subscription.filters = filters; |
196 addSubscriptionFilters(subscription); | 196 addSubscriptionFilters(subscription); |
197 FilterNotifier.triggerListeners("subscription.updated", subscription); | 197 FilterNotifier.emit("subscription.updated", subscription); |
198 delete subscription.oldFilters; | 198 delete subscription.oldFilters; |
199 }, | 199 }, |
200 | 200 |
201 /** | 201 /** |
202 * Adds a user-defined filter to the list | 202 * Adds a user-defined filter to the list |
203 * @param {Filter} filter | 203 * @param {Filter} filter |
204 * @param {SpecialSubscription} [subscription] | 204 * @param {SpecialSubscription} [subscription] |
205 * particular group that the filter should be added to | 205 * particular group that the filter should be added to |
206 * @param {number} [position] | 206 * @param {number} [position] |
207 * position within the subscription at which the filter should be added | 207 * position within the subscription at which the filter should be added |
(...skipping 16 matching lines...) Expand all Loading... |
224 this.addSubscription(subscription); | 224 this.addSubscription(subscription); |
225 return; | 225 return; |
226 } | 226 } |
227 | 227 |
228 if (typeof position == "undefined") | 228 if (typeof position == "undefined") |
229 position = subscription.filters.length; | 229 position = subscription.filters.length; |
230 | 230 |
231 if (filter.subscriptions.indexOf(subscription) < 0) | 231 if (filter.subscriptions.indexOf(subscription) < 0) |
232 filter.subscriptions.push(subscription); | 232 filter.subscriptions.push(subscription); |
233 subscription.filters.splice(position, 0, filter); | 233 subscription.filters.splice(position, 0, filter); |
234 FilterNotifier.triggerListeners("filter.added", filter, subscription, | 234 FilterNotifier.emit("filter.added", filter, subscription, position); |
235 position); | |
236 }, | 235 }, |
237 | 236 |
238 /** | 237 /** |
239 * Removes a user-defined filter from the list | 238 * Removes a user-defined filter from the list |
240 * @param {Filter} filter | 239 * @param {Filter} filter |
241 * @param {SpecialSubscription} [subscription] a particular filter group that | 240 * @param {SpecialSubscription} [subscription] a particular filter group that |
242 * the filter should be removed from (if ommited will be removed from all | 241 * the filter should be removed from (if ommited will be removed from all |
243 * subscriptions) | 242 * subscriptions) |
244 * @param {number} [position] position inside the filter group at which the | 243 * @param {number} [position] position inside the filter group at which the |
245 * filter should be removed (if ommited all instances will be removed) | 244 * filter should be removed (if ommited all instances will be removed) |
(...skipping 27 matching lines...) Expand all Loading... |
273 let currentPosition = positions[j]; | 272 let currentPosition = positions[j]; |
274 if (currentSubscription.filters[currentPosition] == filter) | 273 if (currentSubscription.filters[currentPosition] == filter) |
275 { | 274 { |
276 currentSubscription.filters.splice(currentPosition, 1); | 275 currentSubscription.filters.splice(currentPosition, 1); |
277 if (currentSubscription.filters.indexOf(filter) < 0) | 276 if (currentSubscription.filters.indexOf(filter) < 0) |
278 { | 277 { |
279 let index = filter.subscriptions.indexOf(currentSubscription); | 278 let index = filter.subscriptions.indexOf(currentSubscription); |
280 if (index >= 0) | 279 if (index >= 0) |
281 filter.subscriptions.splice(index, 1); | 280 filter.subscriptions.splice(index, 1); |
282 } | 281 } |
283 FilterNotifier.triggerListeners( | 282 FilterNotifier.emit("filter.removed", filter, currentSubscription, |
284 "filter.removed", filter, currentSubscription, currentPosition | 283 currentPosition); |
285 ); | |
286 } | 284 } |
287 } | 285 } |
288 } | 286 } |
289 } | 287 } |
290 }, | 288 }, |
291 | 289 |
292 /** | 290 /** |
293 * Moves a user-defined filter to a new position | 291 * Moves a user-defined filter to a new position |
294 * @param {Filter} filter | 292 * @param {Filter} filter |
295 * @param {SpecialSubscription} subscription filter group where the filter is | 293 * @param {SpecialSubscription} subscription filter group where the filter is |
296 * located | 294 * located |
297 * @param {number} oldPosition current position of the filter | 295 * @param {number} oldPosition current position of the filter |
298 * @param {number} newPosition new position of the filter | 296 * @param {number} newPosition new position of the filter |
299 */ | 297 */ |
300 moveFilter(filter, subscription, oldPosition, newPosition) | 298 moveFilter(filter, subscription, oldPosition, newPosition) |
301 { | 299 { |
302 if (!(subscription instanceof SpecialSubscription) || | 300 if (!(subscription instanceof SpecialSubscription) || |
303 subscription.filters[oldPosition] != filter) | 301 subscription.filters[oldPosition] != filter) |
304 { | 302 { |
305 return; | 303 return; |
306 } | 304 } |
307 | 305 |
308 newPosition = Math.min(Math.max(newPosition, 0), | 306 newPosition = Math.min(Math.max(newPosition, 0), |
309 subscription.filters.length - 1); | 307 subscription.filters.length - 1); |
310 if (oldPosition == newPosition) | 308 if (oldPosition == newPosition) |
311 return; | 309 return; |
312 | 310 |
313 subscription.filters.splice(oldPosition, 1); | 311 subscription.filters.splice(oldPosition, 1); |
314 subscription.filters.splice(newPosition, 0, filter); | 312 subscription.filters.splice(newPosition, 0, filter); |
315 FilterNotifier.triggerListeners("filter.moved", filter, subscription, | 313 FilterNotifier.emit("filter.moved", filter, subscription, oldPosition, |
316 oldPosition, newPosition); | 314 newPosition); |
317 }, | 315 }, |
318 | 316 |
319 /** | 317 /** |
320 * Increases the hit count for a filter by one | 318 * Increases the hit count for a filter by one |
321 * @param {Filter} filter | 319 * @param {Filter} filter |
322 */ | 320 */ |
323 increaseHitCount(filter) | 321 increaseHitCount(filter) |
324 { | 322 { |
325 if (!Prefs.savestats || !(filter instanceof ActiveFilter)) | 323 if (!Prefs.savestats || !(filter instanceof ActiveFilter)) |
326 return; | 324 return; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 for (let subscription of parser.subscriptions) | 370 for (let subscription of parser.subscriptions) |
373 knownSubscriptions.set(subscription.url, subscription); | 371 knownSubscriptions.set(subscription.url, subscription); |
374 | 372 |
375 this.fileProperties = parser.fileProperties; | 373 this.fileProperties = parser.fileProperties; |
376 this.subscriptions = parser.subscriptions; | 374 this.subscriptions = parser.subscriptions; |
377 this.knownSubscriptions = knownSubscriptions; | 375 this.knownSubscriptions = knownSubscriptions; |
378 Filter.knownFilters = parser.knownFilters; | 376 Filter.knownFilters = parser.knownFilters; |
379 Subscription.knownSubscriptions = parser.knownSubscriptions; | 377 Subscription.knownSubscriptions = parser.knownSubscriptions; |
380 | 378 |
381 if (!silent) | 379 if (!silent) |
382 FilterNotifier.triggerListeners("load"); | 380 FilterNotifier.emit("load"); |
383 } | 381 } |
384 }; | 382 }; |
385 }, | 383 }, |
386 | 384 |
387 /** | 385 /** |
388 * Loads all subscriptions from the disk. | 386 * Loads all subscriptions from the disk. |
389 * @return {Promise} promise resolved or rejected when loading is complete | 387 * @return {Promise} promise resolved or rejected when loading is complete |
390 */ | 388 */ |
391 loadFromDisk() | 389 loadFromDisk() |
392 { | 390 { |
(...skipping 27 matching lines...) Expand all Loading... |
420 throw new Error("No data in the file"); | 418 throw new Error("No data in the file"); |
421 } | 419 } |
422 }); | 420 }); |
423 }).catch(error => | 421 }).catch(error => |
424 { | 422 { |
425 Cu.reportError(error); | 423 Cu.reportError(error); |
426 return tryBackup(1); | 424 return tryBackup(1); |
427 }).then(() => | 425 }).then(() => |
428 { | 426 { |
429 this.initialized = true; | 427 this.initialized = true; |
430 FilterNotifier.triggerListeners("load"); | 428 FilterNotifier.emit("load"); |
431 }); | 429 }); |
432 }, | 430 }, |
433 | 431 |
434 /** | 432 /** |
435 * Constructs the file name for a patterns.ini backup. | 433 * Constructs the file name for a patterns.ini backup. |
436 * @param {number} backupIndex | 434 * @param {number} backupIndex |
437 * number of the backup file (1 being the most recent) | 435 * number of the backup file (1 being the most recent) |
438 * @return {string} backup file name | 436 * @return {string} backup file name |
439 */ | 437 */ |
440 getBackupName(backupIndex) | 438 getBackupName(backupIndex) |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 return renameBackup(Prefs.patternsbackups - 1); | 586 return renameBackup(Prefs.patternsbackups - 1); |
589 }).catch(error => | 587 }).catch(error => |
590 { | 588 { |
591 // Errors during backup creation shouldn't prevent writing filters. | 589 // Errors during backup creation shouldn't prevent writing filters. |
592 Cu.reportError(error); | 590 Cu.reportError(error); |
593 }).then(() => | 591 }).then(() => |
594 { | 592 { |
595 return IO.writeToFile(this.sourceFile, this.exportData()); | 593 return IO.writeToFile(this.sourceFile, this.exportData()); |
596 }).then(() => | 594 }).then(() => |
597 { | 595 { |
598 FilterNotifier.triggerListeners("save"); | 596 FilterNotifier.emit("save"); |
599 }).catch(error => | 597 }).catch(error => |
600 { | 598 { |
601 // If saving failed, report error but continue - we still have to process | 599 // If saving failed, report error but continue - we still have to process |
602 // flags. | 600 // flags. |
603 Cu.reportError(error); | 601 Cu.reportError(error); |
604 }).then(() => | 602 }).then(() => |
605 { | 603 { |
606 this._saving = false; | 604 this._saving = false; |
607 if (this._needsSave) | 605 if (this._needsSave) |
608 { | 606 { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
773 else if (this.wantObj === false && val) | 771 else if (this.wantObj === false && val) |
774 this.curObj.push(val.replace(/\\\[/g, "[")); | 772 this.curObj.push(val.replace(/\\\[/g, "[")); |
775 } | 773 } |
776 finally | 774 finally |
777 { | 775 { |
778 Filter.knownFilters = origKnownFilters; | 776 Filter.knownFilters = origKnownFilters; |
779 Subscription.knownSubscriptions = origKnownSubscriptions; | 777 Subscription.knownSubscriptions = origKnownSubscriptions; |
780 } | 778 } |
781 } | 779 } |
782 }; | 780 }; |
OLD | NEW |