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

Delta Between Two Patch Sets: lib/filterStorage.js

Issue 29375915: Issue 4878 - Start using ESLint for adblockpluscore (Closed)
Left Patch Set: Stop using commonjs, fix other problems Created Feb. 21, 2017, 5:12 a.m.
Right Patch Set: Removed unused imports Created March 15, 2017, 3:11 a.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
« no previous file with change/comment | « lib/filterNotifier.js ('k') | lib/matcher.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 /* globals FileUtils, Services */
19
20 "use strict"; 18 "use strict";
21 19
22 /** 20 /**
23 * @fileOverview FilterStorage class responsible for managing user's 21 * @fileOverview FilterStorage class responsible for managing user's
24 * subscriptions and filters. 22 * subscriptions and filters.
25 */ 23 */
26 24
27 Cu.import("resource://gre/modules/Services.jsm"); 25 const {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", {});
28 Cu.import("resource://gre/modules/FileUtils.jsm"); 26 const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
29 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 27
30 28 const {IO} = require("io");
31 let {IO} = require("io"); 29 const {Prefs} = require("prefs");
32 let {Prefs} = require("prefs"); 30 const {Filter, ActiveFilter} = require("filterClasses");
33 let {Filter, ActiveFilter} = require("filterClasses"); 31 const {Subscription, SpecialSubscription,
34 let {Subscription, SpecialSubscription, 32 ExternalSubscription} = require("subscriptionClasses");
35 ExternalSubscription} = require("subscriptionClasses"); 33 const {FilterNotifier} = require("filterNotifier");
36 let {FilterNotifier} = require("filterNotifier"); 34 const {Utils} = require("utils");
37 let {Utils} = require("utils");
38 35
39 /** 36 /**
40 * Version number of the filter storage file format. 37 * Version number of the filter storage file format.
41 * @type {Integer} 38 * @type {number}
42 */ 39 */
43 let formatVersion = 4; 40 let formatVersion = 4;
44 41
45 /** 42 /**
46 * This class reads user's filters from disk, manages them in memory 43 * This class reads user's filters from disk, manages them in memory
47 * and writes them back. 44 * and writes them back.
48 * @class 45 * @class
49 */ 46 */
50 let FilterStorage = exports.FilterStorage = 47 let FilterStorage = exports.FilterStorage =
51 { 48 {
52 /** 49 /**
53 * Version number of the patterns.ini format used. 50 * Version number of the patterns.ini format used.
54 * @type {Integer} 51 * @type {number}
55 */ 52 */
56 get formatVersion() 53 get formatVersion()
57 { 54 {
58 return formatVersion; 55 return formatVersion;
59 }, 56 },
60 57
61 /** 58 /**
62 * File that the filter list has been loaded from and should be saved to 59 * File that the filter list has been loaded from and should be saved to
63 * @type {nsIFile} 60 * @type {nsIFile}
64 */ 61 */
(...skipping 11 matching lines...) Expand all
76 // Place the file in the data dir 73 // Place the file in the data dir
77 file = IO.resolveFilePath(Prefs.data_directory); 74 file = IO.resolveFilePath(Prefs.data_directory);
78 if (file) 75 if (file)
79 file.append("patterns.ini"); 76 file.append("patterns.ini");
80 } 77 }
81 if (!file) 78 if (!file)
82 { 79 {
83 // Data directory pref misconfigured? Try the default value 80 // Data directory pref misconfigured? Try the default value
84 try 81 try
85 { 82 {
86 file = IO.resolveFilePath( 83 let dir = Services.prefs.getDefaultBranch("extensions.adblockplus.")
87 Services.prefs.getDefaultBranch("extensions.adblockplus." 84 .getCharPref("data_directory");
88 ).getCharPref("data_directory")); 85 file = IO.resolveFilePath(dir);
89 if (file) 86 if (file)
90 file.append("patterns.ini"); 87 file.append("patterns.ini");
91 } 88 }
92 catch (e) {} 89 catch (e) {}
93 } 90 }
94 91
95 if (!file) 92 if (!file)
96 { 93 {
97 Cu.reportError("Adblock Plus: Failed to resolve filter file location " + 94 Cu.reportError("Adblock Plus: Failed to resolve filter file location " +
98 "from extensions.adblockplus.patternsfile preference"); 95 "from extensions.adblockplus.patternsfile preference");
99 } 96 }
100 97
101 // Property is configurable because of the test suite. 98 // Property is configurable because of the test suite.
102 Object.defineProperty(this, "sourceFile", 99 Object.defineProperty(this, "sourceFile",
103 {value: file, configurable: true}); 100 {value: file, configurable: true});
104 return file; 101 return file;
105 }, 102 },
106 103
107 /** 104 /**
108 * Will be set to true if no patterns.ini file exists. 105 * Will be set to true if no patterns.ini file exists.
109 * @type {Boolean} 106 * @type {boolean}
110 */ 107 */
111 firstRun: false, 108 firstRun: false,
112 109
113 /** 110 /**
114 * Map of properties listed in the filter storage file before the sections 111 * Map of properties listed in the filter storage file before the sections
115 * start. Right now this should be only the format version. 112 * start. Right now this should be only the format version.
116 */ 113 */
117 fileProperties: Object.create(null), 114 fileProperties: Object.create(null),
118 115
119 /** 116 /**
120 * List of filter subscriptions containing all filters 117 * List of filter subscriptions containing all filters
121 * @type {Subscription[]} 118 * @type {Subscription[]}
122 */ 119 */
123 subscriptions: [], 120 subscriptions: [],
124 121
125 /** 122 /**
126 * Map of subscriptions already on the list, by their URL/identifier 123 * Map of subscriptions already on the list, by their URL/identifier
127 * @type {Object} 124 * @type {Object}
128 */ 125 */
129 knownSubscriptions: Object.create(null), 126 knownSubscriptions: Object.create(null),
130 127
131 /** 128 /**
132 * Finds the filter group that a filter should be added to by default. Will 129 * Finds the filter group that a filter should be added to by default. Will
133 * return null if this group doesn't exist yet. 130 * return null if this group doesn't exist yet.
134 * @param {Filter} filter 131 * @param {Filter} filter
135 * @return {SpecialSubscription|null} 132 * @return {?SpecialSubscription}
136 */ 133 */
137 getGroupForFilter(filter) 134 getGroupForFilter(filter)
138 { 135 {
139 let generalSubscription = null; 136 let generalSubscription = null;
140 for (let subscription of FilterStorage.subscriptions) 137 for (let subscription of FilterStorage.subscriptions)
141 { 138 {
142 if (subscription instanceof SpecialSubscription && !subscription.disabled) 139 if (subscription instanceof SpecialSubscription && !subscription.disabled)
143 { 140 {
144 // Always prefer specialized subscriptions 141 // Always prefer specialized subscriptions
145 if (subscription.isDefaultFor(filter)) 142 if (subscription.isDefaultFor(filter))
146 return subscription; 143 return subscription;
147 144
148 // If this is a general subscription - store it as fallback 145 // If this is a general subscription - store it as fallback
149 if (!generalSubscription && 146 if (!generalSubscription &&
150 (!subscription.defaults || !subscription.defaults.length)) 147 (!subscription.defaults || !subscription.defaults.length))
148 {
151 generalSubscription = subscription; 149 generalSubscription = subscription;
150 }
152 } 151 }
153 } 152 }
154 return generalSubscription; 153 return generalSubscription;
155 }, 154 },
156 155
157 /** 156 /**
158 * Adds a filter subscription to the list 157 * Adds a filter subscription to the list
159 * @param {Subscription} subscription filter subscription to be added 158 * @param {Subscription} subscription filter subscription to be added
160 * @param {Boolean} silent if true, no listeners will be triggered 159 * @param {boolean} silent if true, no listeners will be triggered
161 * (to be used when filter list is reloaded) 160 * (to be used when filter list is reloaded)
162 */ 161 */
163 addSubscription(subscription, silent) 162 addSubscription(subscription, silent)
164 { 163 {
165 if (subscription.url in FilterStorage.knownSubscriptions) 164 if (subscription.url in FilterStorage.knownSubscriptions)
166 return; 165 return;
167 166
168 FilterStorage.subscriptions.push(subscription); 167 FilterStorage.subscriptions.push(subscription);
169 FilterStorage.knownSubscriptions[subscription.url] = subscription; 168 FilterStorage.knownSubscriptions[subscription.url] = subscription;
170 addSubscriptionFilters(subscription); 169 addSubscriptionFilters(subscription);
171 170
172 if (!silent) 171 if (!silent)
173 FilterNotifier.triggerListeners("subscription.added", subscription); 172 FilterNotifier.triggerListeners("subscription.added", subscription);
174 }, 173 },
175 174
176 /** 175 /**
177 * Removes a filter subscription from the list 176 * Removes a filter subscription from the list
178 * @param {Subscription} subscription filter subscription to be removed 177 * @param {Subscription} subscription filter subscription to be removed
179 * @param {Boolean} silent if true, no listeners will be triggered 178 * @param {boolean} silent if true, no listeners will be triggered
180 * (to be used when filter list is reloaded) 179 * (to be used when filter list is reloaded)
181 */ 180 */
182 removeSubscription(subscription, silent) 181 removeSubscription(subscription, silent)
183 { 182 {
184 for (let i = 0; i < FilterStorage.subscriptions.length; i++) 183 for (let i = 0; i < FilterStorage.subscriptions.length; i++)
185 { 184 {
186 if (FilterStorage.subscriptions[i].url == subscription.url) 185 if (FilterStorage.subscriptions[i].url == subscription.url)
187 { 186 {
188 removeSubscriptionFilters(subscription); 187 removeSubscriptionFilters(subscription);
189 188
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 subscription.oldFilters = subscription.filters; 235 subscription.oldFilters = subscription.filters;
237 subscription.filters = filters; 236 subscription.filters = filters;
238 addSubscriptionFilters(subscription); 237 addSubscriptionFilters(subscription);
239 FilterNotifier.triggerListeners("subscription.updated", subscription); 238 FilterNotifier.triggerListeners("subscription.updated", subscription);
240 delete subscription.oldFilters; 239 delete subscription.oldFilters;
241 }, 240 },
242 241
243 /** 242 /**
244 * Adds a user-defined filter to the list 243 * Adds a user-defined filter to the list
245 * @param {Filter} filter 244 * @param {Filter} filter
246 * @param {SpecialSubscription} [subscription] particular group that the 245 * @param {SpecialSubscription} [subscription]
247 * filter should be added to 246 * particular group that the filter should be added to
248 * @param {Integer} [position] position within the subscription at which 247 * @param {number} [position]
249 * the filter should be added 248 * position within the subscription at which the filter should be added
250 * @param {Boolean} silent if true, no listeners will be triggered (to 249 * @param {boolean} silent
251 * be used when filter list is reloaded) 250 * if true, no listeners will be triggered (to be used when filter list is
251 * reloaded)
252 */ 252 */
253 addFilter(filter, subscription, position, silent) 253 addFilter(filter, subscription, position, silent)
254 { 254 {
255 if (!subscription) 255 if (!subscription)
256 { 256 {
257 if (filter.subscriptions.some(s => s instanceof SpecialSubscription && 257 if (filter.subscriptions.some(s => s instanceof SpecialSubscription &&
258 !s.disabled)) 258 !s.disabled))
259 {
259 return; // No need to add 260 return; // No need to add
261 }
260 subscription = FilterStorage.getGroupForFilter(filter); 262 subscription = FilterStorage.getGroupForFilter(filter);
261 } 263 }
262 if (!subscription) 264 if (!subscription)
263 { 265 {
264 // No group for this filter exists, create one 266 // No group for this filter exists, create one
265 subscription = SpecialSubscription.createForFilter(filter); 267 subscription = SpecialSubscription.createForFilter(filter);
266 this.addSubscription(subscription); 268 this.addSubscription(subscription);
267 return; 269 return;
268 } 270 }
269 271
270 if (typeof position == "undefined") 272 if (typeof position == "undefined")
271 position = subscription.filters.length; 273 position = subscription.filters.length;
272 274
273 if (filter.subscriptions.indexOf(subscription) < 0) 275 if (filter.subscriptions.indexOf(subscription) < 0)
274 filter.subscriptions.push(subscription); 276 filter.subscriptions.push(subscription);
275 subscription.filters.splice(position, 0, filter); 277 subscription.filters.splice(position, 0, filter);
276 if (!silent) 278 if (!silent)
277 { 279 {
278 FilterNotifier.triggerListeners("filter.added", filter, subscription, 280 FilterNotifier.triggerListeners("filter.added", filter, subscription,
279 position); 281 position);
280 } 282 }
281 }, 283 },
282 284
283 /** 285 /**
284 * Removes a user-defined filter from the list 286 * Removes a user-defined filter from the list
285 * @param {Filter} filter 287 * @param {Filter} filter
286 * @param {SpecialSubscription} [subscription] a particular filter group that 288 * @param {SpecialSubscription} [subscription] a particular filter group that
287 * the filter should be removed from (if ommited will be removed from all 289 * the filter should be removed from (if ommited will be removed from all
288 * subscriptions) 290 * subscriptions)
289 * @param {Integer} [position] position inside the filter group at which the 291 * @param {number} [position] position inside the filter group at which the
290 * filter should be removed (if ommited all instances will be removed) 292 * filter should be removed (if ommited all instances will be removed)
291 */ 293 */
292 removeFilter(filter, subscription, position) 294 removeFilter(filter, subscription, position)
293 { 295 {
294 let subscriptions = ( 296 let subscriptions = (
295 subscription ? [subscription] : filter.subscriptions.slice() 297 subscription ? [subscription] : filter.subscriptions.slice()
296 ); 298 );
297 for (let i = 0; i < subscriptions.length; i++) 299 for (let i = 0; i < subscriptions.length; i++)
298 { 300 {
299 let currentSubscription = subscriptions[i]; 301 let currentSubscription = subscriptions[i];
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 } 334 }
333 } 335 }
334 } 336 }
335 }, 337 },
336 338
337 /** 339 /**
338 * Moves a user-defined filter to a new position 340 * Moves a user-defined filter to a new position
339 * @param {Filter} filter 341 * @param {Filter} filter
340 * @param {SpecialSubscription} subscription filter group where the filter is 342 * @param {SpecialSubscription} subscription filter group where the filter is
341 * located 343 * located
342 * @param {Integer} oldPosition current position of the filter 344 * @param {number} oldPosition current position of the filter
343 * @param {Integer} newPosition new position of the filter 345 * @param {number} newPosition new position of the filter
344 */ 346 */
345 moveFilter(filter, subscription, oldPosition, newPosition) 347 moveFilter(filter, subscription, oldPosition, newPosition)
346 { 348 {
347 if (!(subscription instanceof SpecialSubscription) || 349 if (!(subscription instanceof SpecialSubscription) ||
348 subscription.filters[oldPosition] != filter) 350 subscription.filters[oldPosition] != filter)
349 return; 351 {
352 return;
353 }
350 354
351 newPosition = Math.min(Math.max(newPosition, 0), 355 newPosition = Math.min(Math.max(newPosition, 0),
352 subscription.filters.length - 1); 356 subscription.filters.length - 1);
353 if (oldPosition == newPosition) 357 if (oldPosition == newPosition)
354 return; 358 return;
355 359
356 subscription.filters.splice(oldPosition, 1); 360 subscription.filters.splice(oldPosition, 1);
357 subscription.filters.splice(newPosition, 0, filter); 361 subscription.filters.splice(newPosition, 0, filter);
358 FilterNotifier.triggerListeners("filter.moved", filter, subscription, 362 FilterNotifier.triggerListeners("filter.moved", filter, subscription,
359 oldPosition, newPosition); 363 oldPosition, newPosition);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 * Loads all subscriptions from the disk 402 * Loads all subscriptions from the disk
399 * @param {nsIFile} [sourceFile] File to read from 403 * @param {nsIFile} [sourceFile] File to read from
400 */ 404 */
401 loadFromDisk(sourceFile) 405 loadFromDisk(sourceFile)
402 { 406 {
403 if (this._loading) 407 if (this._loading)
404 return; 408 return;
405 409
406 this._loading = true; 410 this._loading = true;
407 411
408 let readFile = function(currentSourceFile, backupIndex) 412 let readFile = (currentSourceFile, backupIndex) =>
409 { 413 {
410 let parser = new INIParser(); 414 let parser = new INIParser();
411 IO.readFromFile(currentSourceFile, parser, readFromFileException => 415 IO.readFromFile(currentSourceFile, parser, readFromFileException =>
412 { 416 {
413 if (!readFromFileException && parser.subscriptions.length == 0) 417 if (!readFromFileException && parser.subscriptions.length == 0)
414 { 418 {
415 // No filter subscriptions in the file, this isn't right. 419 // No filter subscriptions in the file, this isn't right.
416 readFromFileException = new Error("No data in the file"); 420 readFromFileException = new Error("No data in the file");
417 } 421 }
418 422
(...skipping 20 matching lines...) Expand all
439 if (!statFileException && statData.exists) 443 if (!statFileException && statData.exists)
440 readFile(currentSourceFile, backupIndex); 444 readFile(currentSourceFile, backupIndex);
441 else 445 else
442 doneReading(parser); 446 doneReading(parser);
443 }); 447 });
444 return; 448 return;
445 } 449 }
446 } 450 }
447 doneReading(parser); 451 doneReading(parser);
448 }); 452 });
449 }.bind(this); 453 };
450 454
451 let doneReading = function(parser) 455 let doneReading = parser =>
452 { 456 {
453 // Old special groups might have been converted, remove them if 457 // Old special groups might have been converted, remove them if
454 // they are empty 458 // they are empty
455 let specialMap = {"~il~": true, "~wl~": true, "~fl~": true, "~eh~": true}; 459 let specialMap = {"~il~": true, "~wl~": true, "~fl~": true, "~eh~": true};
456 let knownSubscriptions = Object.create(null); 460 let knownSubscriptions = Object.create(null);
457 for (let i = 0; i < parser.subscriptions.length; i++) 461 for (let i = 0; i < parser.subscriptions.length; i++)
458 { 462 {
459 let subscription = parser.subscriptions[i]; 463 let subscription = parser.subscriptions[i];
460 if (subscription instanceof SpecialSubscription && 464 if (subscription instanceof SpecialSubscription &&
461 subscription.filters.length == 0 && subscription.url in specialMap) 465 subscription.filters.length == 0 && subscription.url in specialMap)
466 {
462 parser.subscriptions.splice(i--, 1); 467 parser.subscriptions.splice(i--, 1);
468 }
463 else 469 else
464 knownSubscriptions[subscription.url] = subscription; 470 knownSubscriptions[subscription.url] = subscription;
465 } 471 }
466 472
467 this.fileProperties = parser.fileProperties; 473 this.fileProperties = parser.fileProperties;
468 this.subscriptions = parser.subscriptions; 474 this.subscriptions = parser.subscriptions;
469 this.knownSubscriptions = knownSubscriptions; 475 this.knownSubscriptions = knownSubscriptions;
470 Filter.knownFilters = parser.knownFilters; 476 Filter.knownFilters = parser.knownFilters;
471 Subscription.knownSubscriptions = parser.knownSubscriptions; 477 Subscription.knownSubscriptions = parser.knownSubscriptions;
472 478
473 if (parser.userFilters) 479 if (parser.userFilters)
474 { 480 {
475 for (let i = 0; i < parser.userFilters.length; i++) 481 for (let i = 0; i < parser.userFilters.length; i++)
476 { 482 {
477 let filter = Filter.fromText(parser.userFilters[i]); 483 let filter = Filter.fromText(parser.userFilters[i]);
478 this.addFilter(filter, null, undefined, true); 484 this.addFilter(filter, null, undefined, true);
479 } 485 }
480 } 486 }
481 487
482 this._loading = false; 488 this._loading = false;
483 FilterNotifier.triggerListeners("load"); 489 FilterNotifier.triggerListeners("load");
484 490
485 if (sourceFile != this.sourceFile) 491 if (sourceFile != this.sourceFile)
486 this.saveToDisk(); 492 this.saveToDisk();
487 }.bind(this); 493 };
488 494
489 let explicitFile; 495 let explicitFile;
490 if (sourceFile) 496 if (sourceFile)
491 { 497 {
492 explicitFile = true; 498 explicitFile = true;
493 readFile(sourceFile, 0); 499 readFile(sourceFile, 0);
494 } 500 }
495 else 501 else
496 { 502 {
497 explicitFile = false; 503 explicitFile = false;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 subscription.serializeFilters(buf); 562 subscription.serializeFilters(buf);
557 } 563 }
558 for (let k = 0; k < buf.length; k++) 564 for (let k = 0; k < buf.length; k++)
559 yield buf[k]; 565 yield buf[k];
560 buf.splice(0); 566 buf.splice(0);
561 } 567 }
562 }, 568 },
563 569
564 /** 570 /**
565 * Will be set to true if saveToDisk() is running (reentrance protection). 571 * Will be set to true if saveToDisk() is running (reentrance protection).
566 * @type {Boolean} 572 * @type {boolean}
567 */ 573 */
568 _saving: false, 574 _saving: false,
569 575
570 /** 576 /**
571 * Will be set to true if a saveToDisk() call arrives while saveToDisk() is 577 * Will be set to true if a saveToDisk() call arrives while saveToDisk() is
572 * already running (delayed execution). 578 * already running (delayed execution).
573 * @type {Boolean} 579 * @type {boolean}
574 */ 580 */
575 _needsSave: false, 581 _needsSave: false,
576 582
577 /** 583 /**
578 * Saves all subscriptions back to disk 584 * Saves all subscriptions back to disk
579 * @param {nsIFile} [targetFile] File to be written 585 * @param {nsIFile} [targetFile] File to be written
580 */ 586 */
581 saveToDisk(targetFile) 587 saveToDisk(targetFile)
582 { 588 {
583 let explicitFile = true; 589 let explicitFile = true;
(...skipping 12 matching lines...) Expand all
596 } 602 }
597 603
598 // Make sure the file's parent directory exists 604 // Make sure the file's parent directory exists
599 try 605 try
600 { 606 {
601 targetFile.parent.create(Ci.nsIFile.DIRECTORY_TYPE, 607 targetFile.parent.create(Ci.nsIFile.DIRECTORY_TYPE,
602 FileUtils.PERMS_DIRECTORY); 608 FileUtils.PERMS_DIRECTORY);
603 } 609 }
604 catch (e) {} 610 catch (e) {}
605 611
606 let writeFilters = function() 612 let writeFilters = () =>
607 { 613 {
608 IO.writeToFile(targetFile, this._generateFilterData(subscriptions), e => 614 IO.writeToFile(targetFile, this._generateFilterData(subscriptions), e =>
609 { 615 {
610 if (!explicitFile) 616 if (!explicitFile)
611 this._saving = false; 617 this._saving = false;
612 618
613 if (e) 619 if (e)
614 Cu.reportError(e); 620 Cu.reportError(e);
615 621
616 if (!explicitFile && this._needsSave) 622 if (!explicitFile && this._needsSave)
617 { 623 {
618 this._needsSave = false; 624 this._needsSave = false;
619 this.saveToDisk(); 625 this.saveToDisk();
620 } 626 }
621 else 627 else
622 FilterNotifier.triggerListeners("save"); 628 FilterNotifier.triggerListeners("save");
623 }); 629 });
624 }.bind(this); 630 };
625 631
626 let checkBackupRequired = function(callbackNotRequired, callbackRequired) 632 let checkBackupRequired = (callbackNotRequired, callbackRequired) =>
627 { 633 {
628 if (explicitFile || Prefs.patternsbackups <= 0) 634 if (explicitFile || Prefs.patternsbackups <= 0)
629 callbackNotRequired(); 635 callbackNotRequired();
630 else 636 else
631 { 637 {
632 IO.statFile(targetFile, (statFileException, statData) => 638 IO.statFile(targetFile, (statFileException, statData) =>
633 { 639 {
634 if (statFileException || !statData.exists) 640 if (statFileException || !statData.exists)
635 callbackNotRequired(); 641 callbackNotRequired();
636 else 642 else
637 { 643 {
638 let [, part1, part2] = /^(.*)(\.\w+)$/.exec(targetFile.leafName) || 644 let [, part1, part2] = /^(.*)(\.\w+)$/.exec(targetFile.leafName) ||
639 [null, targetFile.leafName, ""]; 645 [null, targetFile.leafName, ""];
640 let newestBackup = targetFile.clone(); 646 let newestBackup = targetFile.clone();
641 newestBackup.leafName = part1 + "-backup1" + part2; 647 newestBackup.leafName = part1 + "-backup1" + part2;
642 IO.statFile( 648 IO.statFile(
643 newestBackup, 649 newestBackup,
644 (statBackupFileException, statBackupData) => 650 (statBackupFileException, statBackupData) =>
645 { 651 {
646 if (!statBackupFileException && (!statBackupData.exists || 652 if (!statBackupFileException && (!statBackupData.exists ||
647 (Date.now() - statBackupData.lastModified) / 653 (Date.now() - statBackupData.lastModified) /
648 3600000 >= Prefs.patternsbackupinterval)) 654 3600000 >= Prefs.patternsbackupinterval))
655 {
649 callbackRequired(part1, part2); 656 callbackRequired(part1, part2);
657 }
650 else 658 else
651 callbackNotRequired(); 659 callbackNotRequired();
652 } 660 }
653 ); 661 );
654 } 662 }
655 }); 663 });
656 } 664 }
657 }; 665 };
658 666
659 let removeLastBackup = function(part1, part2) 667 let removeLastBackup = (part1, part2) =>
660 { 668 {
661 let file = targetFile.clone(); 669 let file = targetFile.clone();
662 file.leafName = part1 + "-backup" + Prefs.patternsbackups + part2; 670 file.leafName = part1 + "-backup" + Prefs.patternsbackups + part2;
663 IO.removeFile( 671 IO.removeFile(
664 file, e => renameBackup(part1, part2, Prefs.patternsbackups - 1) 672 file, e => renameBackup(part1, part2, Prefs.patternsbackups - 1)
665 ); 673 );
666 }; 674 };
667 675
668 let renameBackup = function(part1, part2, index) 676 let renameBackup = (part1, part2, index) =>
669 { 677 {
670 if (index > 0) 678 if (index > 0)
671 { 679 {
672 let fromFile = targetFile.clone(); 680 let fromFile = targetFile.clone();
673 fromFile.leafName = part1 + "-backup" + index + part2; 681 fromFile.leafName = part1 + "-backup" + index + part2;
674 682
675 let toName = part1 + "-backup" + (index + 1) + part2; 683 let toName = part1 + "-backup" + (index + 1) + part2;
676 684
677 IO.renameFile(fromFile, toName, e => renameBackup(part1, part2, 685 IO.renameFile(fromFile, toName, e => renameBackup(part1, part2,
678 index - 1)); 686 index - 1));
(...skipping 16 matching lines...) Expand all
695 703
696 checkBackupRequired(writeFilters, removeLastBackup); 704 checkBackupRequired(writeFilters, removeLastBackup);
697 }, 705 },
698 706
699 /** 707 /**
700 * Returns the list of existing backup files. 708 * Returns the list of existing backup files.
701 * @return {nsIFile[]} 709 * @return {nsIFile[]}
702 */ 710 */
703 getBackupFiles() 711 getBackupFiles()
704 { 712 {
705 /* eslint-disable no-warning-comments */
706 // TODO: This method should be asynchronous
707 /* eslint-enable no-warning-comments */
708 let result = []; 713 let result = [];
709 714
710 let [, part1, part2] = /^(.*)(\.\w+)$/.exec( 715 let [, part1, part2] = /^(.*)(\.\w+)$/.exec(
711 FilterStorage.sourceFile.leafName 716 FilterStorage.sourceFile.leafName
712 ) || [null, FilterStorage.sourceFile.leafName, ""]; 717 ) || [null, FilterStorage.sourceFile.leafName, ""];
713 for (let i = 1; ; i++) 718 for (let i = 1; ; i++)
714 { 719 {
715 let file = FilterStorage.sourceFile.clone(); 720 let file = FilterStorage.sourceFile.clone();
716 file.leafName = part1 + "-backup" + i + part2; 721 file.leafName = part1 + "-backup" + i + part2;
717 if (file.exists()) 722 if (file.exists())
718 result.push(file); 723 result.push(file);
719 else 724 else
720 break; 725 break;
721 } 726 }
722 return result; 727 return result;
723 } 728 }
724 }; 729 };
725 730
726 /** 731 /**
727 * Joins subscription's filters to the subscription without any notifications. 732 * Joins subscription's filters to the subscription without any notifications.
728 * @param {Subscription} subscription filter subscription that should be 733 * @param {Subscription} subscription
729 * connected to its filters 734 * filter subscription that should be connected to its filters
730 */ 735 */
731 function addSubscriptionFilters(subscription) 736 function addSubscriptionFilters(subscription)
732 { 737 {
733 if (!(subscription.url in FilterStorage.knownSubscriptions)) 738 if (!(subscription.url in FilterStorage.knownSubscriptions))
734 return; 739 return;
735 740
736 for (let filter of subscription.filters) 741 for (let filter of subscription.filters)
737 filter.subscriptions.push(subscription); 742 filter.subscriptions.push(subscription);
738 } 743 }
739 744
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 Subscription.knownSubscriptions = origKnownSubscriptions; 865 Subscription.knownSubscriptions = origKnownSubscriptions;
861 } 866 }
862 867
863 // Allow events to be processed every now and then. 868 // Allow events to be processed every now and then.
864 // Note: IO.readFromFile() will deal with the potential reentrance here. 869 // Note: IO.readFromFile() will deal with the potential reentrance here.
865 this.linesProcessed++; 870 this.linesProcessed++;
866 if (this.linesProcessed % 1000 == 0) 871 if (this.linesProcessed % 1000 == 0)
867 return Utils.yield(); 872 return Utils.yield();
868 } 873 }
869 }; 874 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld