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

Side by Side Diff: chrome/content/ui/filters-backup.js

Issue 29398708: Issue 5051 - Make sure that custom backups don't rely on FilterStorage's I/O (Closed) Base URL: https://hg.adblockplus.org/adblockplus
Patch Set: Created March 30, 2017, 12:23 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 | « no previous file | dependencies » ('j') | dependencies » ('J')
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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 { 101 {
102 backups.reverse(); 102 backups.reverse();
103 for (let backup of backups) 103 for (let backup of backups)
104 { 104 {
105 let item = this.restoreTemplate.cloneNode(true); 105 let item = this.restoreTemplate.cloneNode(true);
106 let label = item.getAttribute("label"); 106 let label = item.getAttribute("label");
107 label = label.replace(/\?1\?/, Utils.formatTime(backup.lastModified)); 107 label = label.replace(/\?1\?/, Utils.formatTime(backup.lastModified));
108 item.setAttribute("label", label); 108 item.setAttribute("label", label);
109 item.addEventListener("command", function() 109 item.addEventListener("command", function()
110 { 110 {
111 Backup.restoreAllData(backup.file); 111 FilterStorage.restoreBackup(backup.index);
112 }, false); 112 }, false);
113 this.restoreInsertionPoint.parentNode.insertBefore(item, this.restoreIns ertionPoint.nextSibling); 113 this.restoreInsertionPoint.parentNode.insertBefore(item, this.restoreIns ertionPoint.nextSibling);
114 } 114 }
115 }); 115 });
116 }, 116 },
117 117
118 /** 118 /**
119 * Lets the user choose a file to restore filters from. 119 * Lets the user choose a file to restore filters from.
120 */ 120 */
121 restoreFromFile: function() 121 restoreFromFile: function()
(...skipping 12 matching lines...) Expand all
134 else 134 else
135 this.restoreCustomFilters(picker.file); 135 this.restoreCustomFilters(picker.file);
136 } 136 }
137 }, 137 },
138 138
139 /** 139 /**
140 * Restores patterns.ini from a file. 140 * Restores patterns.ini from a file.
141 */ 141 */
142 restoreAllData: function(/**nsIFile*/ file) 142 restoreAllData: function(/**nsIFile*/ file)
143 { 143 {
144 let stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(C i.nsIFileInputStream); 144 let sink = FilterStorage.importData();
145 stream.init(file, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, 0); 145 let lines = [];
146 stream.QueryInterface(Ci.nsILineInputStream); 146 IO.readFromFile(file, {
147 process(line)
148 {
149 if (line === null)
150 {
151 let match;
152 if (lines.length < 2 || lines[0] != "# Adblock Plus preferences" || !( match = /version=(\d+)/.exec(lines[1])))
153 {
154 Utils.alert(window, E("backupButton").getAttribute("_restoreError"), E("backupButton").getAttribute("_restoreDialogTitle"));
155 return;
156 }
147 157
148 let lines = []; 158 let warning = E("backupButton").getAttribute("_restoreCompleteWarning" );
149 let line = {value: null}; 159 let minVersion = parseInt(match[1], 10);
150 if (stream.readLine(line)) 160 if (minVersion > FilterStorage.formatVersion)
151 lines.push(line.value); 161 warning += "\n\n" + E("backupButton").getAttribute("_restoreVersionW arning");
152 if (stream.readLine(line))
153 lines.push(line.value);
154 stream.close();
155 162
156 let match; 163 if (!Utils.confirm(window, warning, E("backupButton").getAttribute("_r estoreDialogTitle")))
157 if (lines.length < 2 || lines[0] != "# Adblock Plus preferences" || !(match = /version=(\d+)/.exec(lines[1]))) 164 return;
165 }
166 else if (lines.length < 2)
167 lines.push(line);
168
169 sink(line);
170 }
171 }, error =>
158 { 172 {
159 Utils.alert(window, E("backupButton").getAttribute("_restoreError"), E("ba ckupButton").getAttribute("_restoreDialogTitle")); 173 if (error)
160 return; 174 alert(error);
161 } 175 else
162 176 FilterStorage.saveToDisk();
163 let warning = E("backupButton").getAttribute("_restoreCompleteWarning"); 177 });
164 let minVersion = parseInt(match[1], 10);
165 if (minVersion > FilterStorage.formatVersion)
166 warning += "\n\n" + E("backupButton").getAttribute("_restoreVersionWarning ");
167
168 if (!Utils.confirm(window, warning, E("backupButton").getAttribute("_restore DialogTitle")))
169 return;
170
171 FilterStorage.loadFromDisk(file);
172 }, 178 },
173 179
174 /** 180 /**
175 * Restores custom filters from a file. 181 * Restores custom filters from a file.
176 */ 182 */
177 restoreCustomFilters: function(/**nsIFile*/ file) 183 restoreCustomFilters: function(/**nsIFile*/ file)
178 { 184 {
179 IO.readFromFile(file, { 185 IO.readFromFile(file, {
180 seenHeader: false, 186 seenHeader: false,
181 subscription: null, 187 subscription: null,
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 else 283 else
278 this.backupCustomFilters(picker.file); 284 this.backupCustomFilters(picker.file);
279 } 285 }
280 }, 286 },
281 287
282 /** 288 /**
283 * Writes all patterns.ini data to a file. 289 * Writes all patterns.ini data to a file.
284 */ 290 */
285 backupAllData: function(/**nsIFile*/ file) 291 backupAllData: function(/**nsIFile*/ file)
286 { 292 {
287 FilterStorage.saveToDisk(file); 293 IO.writeToFile(file, FilterStorage.exportData(), error =>
294 {
295 if (error)
296 alert(error);
297 });
288 }, 298 },
289 299
290 /** 300 /**
291 * Writes user's custom filters to a file. 301 * Writes user's custom filters to a file.
292 */ 302 */
293 backupCustomFilters: function(/**nsIFile*/ file) 303 backupCustomFilters: function(/**nsIFile*/ file)
294 { 304 {
295 let subscriptions = FilterStorage.subscriptions.filter(s => s instanceof Spe cialSubscription); 305 let subscriptions = FilterStorage.subscriptions.filter(s => s instanceof Spe cialSubscription);
296 let minVersion = "2.0" 306 let minVersion = "2.0"
297 let list = []; 307 let list = [];
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 Utils.alert(window, E("backupButton").getAttribute("_backupError"), E("b ackupButton").getAttribute("_backupDialogTitle")); 358 Utils.alert(window, E("backupButton").getAttribute("_backupError"), E("b ackupButton").getAttribute("_backupDialogTitle"));
349 } 359 }
350 }); 360 });
351 } 361 }
352 }; 362 };
353 363
354 window.addEventListener("load", function() 364 window.addEventListener("load", function()
355 { 365 {
356 Backup.init(); 366 Backup.init();
357 }, false); 367 }, false);
OLDNEW
« no previous file with comments | « no previous file | dependencies » ('j') | dependencies » ('J')

Powered by Google App Engine
This is Rietveld