| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 * |
| (...skipping 27 matching lines...) Expand all Loading... |
| 41 }, errorCallback); | 41 }, errorCallback); |
| 42 }, | 42 }, |
| 43 | 43 |
| 44 lineBreak: "\n", | 44 lineBreak: "\n", |
| 45 | 45 |
| 46 resolveFilePath: function(path) | 46 resolveFilePath: function(path) |
| 47 { | 47 { |
| 48 return new FakeFile(path); | 48 return new FakeFile(path); |
| 49 }, | 49 }, |
| 50 | 50 |
| 51 readFromFile: function(file, decode, listener, callback, timeLineID) | 51 readFromFile: function(file, listener, callback, timeLineID) |
| 52 { | 52 { |
| 53 // Hack to allow importing old data | 53 // Hack to allow importing old data |
| 54 if (typeof file == "string") | 54 if (typeof file == "string") |
| 55 { | 55 { |
| 56 var Utils = require("utils").Utils; | 56 var Utils = require("utils").Utils; |
| 57 Utils.runAsync(function() | 57 Utils.runAsync(function() |
| 58 { | 58 { |
| 59 var lines = file.split(/[\r\n]+/); | 59 var lines = file.split(/[\r\n]+/); |
| 60 for (var i = 0; i < lines.length; i++) | 60 for (var i = 0; i < lines.length; i++) |
| 61 listener.process(lines[i]); | 61 listener.process(lines[i]); |
| 62 listener.process(null); | 62 listener.process(null); |
| 63 callback(null); | 63 callback(null); |
| 64 }.bind(this)); | 64 }.bind(this)); |
| 65 return; | 65 return; |
| 66 } | 66 } |
| 67 | 67 |
| 68 if ("spec" in file && /^defaults\b/.test(file.spec)) | |
| 69 { | |
| 70 // Code attempts to read the default patterns.ini, we don't have that. | |
| 71 // Make sure to execute first-run actions instead. | |
| 72 var Utils = require("utils").Utils; | |
| 73 Utils.runAsync(function() | |
| 74 { | |
| 75 if (localStorage.currentVersion) | |
| 76 seenDataCorruption = true; | |
| 77 delete localStorage.currentVersion; | |
| 78 callback(null); | |
| 79 }); | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 this._getFileEntry(file, false, function(fs, fileEntry) | 68 this._getFileEntry(file, false, function(fs, fileEntry) |
| 84 { | 69 { |
| 85 fileEntry.file(function(file) | 70 fileEntry.file(function(file) |
| 86 { | 71 { |
| 72 if (file.size == 0) |
| 73 { |
| 74 callback("File is empty"); |
| 75 return; |
| 76 } |
| 77 |
| 87 var reader = new FileReader(); | 78 var reader = new FileReader(); |
| 88 reader.onloadend = function() | 79 reader.onloadend = function() |
| 89 { | 80 { |
| 90 if (reader.error) | 81 if (reader.error) |
| 91 callback(reader.error); | 82 callback(reader.error); |
| 92 else | 83 else |
| 93 { | 84 { |
| 94 var lines = reader.result.split(/[\r\n]+/); | 85 var lines = reader.result.split(/[\r\n]+/); |
| 95 for (var i = 0; i < lines.length; i++) | 86 for (var i = 0; i < lines.length; i++) |
| 96 listener.process(lines[i]); | 87 listener.process(lines[i]); |
| 97 listener.process(null); | 88 listener.process(null); |
| 98 callback(null); | 89 callback(null); |
| 99 } | 90 } |
| 100 }; | 91 }; |
| 101 reader.readAsText(file); | 92 reader.readAsText(file); |
| 102 }, callback); | 93 }, callback); |
| 103 }, callback); | 94 }, callback); |
| 104 }, | 95 }, |
| 105 | 96 |
| 106 writeToFile: function(file, encode, data, callback, timeLineID) | 97 writeToFile: function(file, data, callback, timeLineID) |
| 107 { | 98 { |
| 108 this._getFileEntry(file, true, function(fs, fileEntry) | 99 this._getFileEntry(file, true, function(fs, fileEntry) |
| 109 { | 100 { |
| 110 fileEntry.createWriter(function(writer) | 101 fileEntry.createWriter(function(writer) |
| 111 { | 102 { |
| 112 var executeWriteOperation = function(op, nextOperation) | 103 var executeWriteOperation = function(op, nextOperation) |
| 113 { | 104 { |
| 114 writer.onwriteend = function() | 105 writer.onwriteend = function() |
| 115 { | 106 { |
| 116 if (writer.error) | 107 if (writer.error) |
| 117 callback(writer.error); | 108 callback(writer.error); |
| 118 else | 109 else |
| 119 nextOperation(); | 110 nextOperation(); |
| 120 }.bind(this); | 111 }.bind(this); |
| 121 | 112 |
| 122 op(); | 113 op(); |
| 123 }.bind(this); | 114 }.bind(this); |
| 124 | 115 |
| 125 executeWriteOperation(writer.truncate.bind(writer, 0), function() | 116 var blob; |
| 117 |
| 118 try |
| 126 { | 119 { |
| 127 var blob; | 120 blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type: "
text/plain"}); |
| 128 try | 121 } |
| 129 { | 122 catch (e) |
| 130 blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type:
"text/plain"}); | 123 { |
| 131 } | 124 if (!(e instanceof TypeError)) |
| 132 catch (e) | 125 throw e; |
| 133 { | |
| 134 if (!(e instanceof TypeError)) | |
| 135 throw e; | |
| 136 | 126 |
| 137 // Blob wasn't a constructor before Chrome 20 | 127 // Blob wasn't a constructor before Chrome 20 |
| 138 var builder = new (window.BlobBuilder || window.WebKitBlobBuilder); | 128 var builder = new (window.BlobBuilder || window.WebKitBlobBuilder); |
| 139 builder.append(data.join(this.lineBreak) + this.lineBreak); | 129 builder.append(data.join(this.lineBreak) + this.lineBreak); |
| 140 blob = builder.getBlob("text/plain"); | 130 blob = builder.getBlob("text/plain"); |
| 141 } | 131 } |
| 142 executeWriteOperation(writer.write.bind(writer, blob), callback.bind(n
ull, null)); | 132 executeWriteOperation(writer.write.bind(writer, blob), function() |
| 143 }.bind(this)); | 133 { |
| 134 executeWriteOperation(writer.truncate.bind(writer, writer.position), c
allback.bind(null, null)); |
| 135 }); |
| 144 }.bind(this), callback); | 136 }.bind(this), callback); |
| 145 }.bind(this), callback); | 137 }.bind(this), callback); |
| 146 }, | 138 }, |
| 147 | 139 |
| 148 copyFile: function(fromFile, toFile, callback) | 140 copyFile: function(fromFile, toFile, callback) |
| 149 { | 141 { |
| 150 // Simply combine read and write operations | 142 // Simply combine read and write operations |
| 151 var data = []; | 143 var data = []; |
| 152 this.readFromFile(fromFile, false, { | 144 this.readFromFile(fromFile, { |
| 153 process: function(line) | 145 process: function(line) |
| 154 { | 146 { |
| 155 if (line !== null) | 147 if (line !== null) |
| 156 data.push(line); | 148 data.push(line); |
| 157 } | 149 } |
| 158 }, function(e) | 150 }, function(e) |
| 159 { | 151 { |
| 160 if (e) | 152 if (e) |
| 161 callback(e); | 153 callback(e); |
| 162 else | 154 else |
| 163 this.writeToFile(toFile, false, data, callback); | 155 this.writeToFile(toFile, data, callback); |
| 164 }.bind(this)); | 156 }.bind(this)); |
| 165 }, | 157 }, |
| 166 | 158 |
| 167 renameFile: function(fromFile, newName, callback) | 159 renameFile: function(fromFile, newName, callback) |
| 168 { | 160 { |
| 169 this._getFileEntry(fromFile, false, function(fs, fileEntry) | 161 this._getFileEntry(fromFile, false, function(fs, fileEntry) |
| 170 { | 162 { |
| 171 fileEntry.moveTo(fs.root, newName, function() | 163 fileEntry.moveTo(fs.root, newName, function() |
| 172 { | 164 { |
| 173 callback(null); | 165 callback(null); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 194 var Utils = require("utils").Utils; | 186 var Utils = require("utils").Utils; |
| 195 Utils.runAsync(callback.bind(null, null, { | 187 Utils.runAsync(callback.bind(null, null, { |
| 196 exists: true, | 188 exists: true, |
| 197 isDirectory: false, | 189 isDirectory: false, |
| 198 isFile: true, | 190 isFile: true, |
| 199 lastModified: 0 | 191 lastModified: 0 |
| 200 })); | 192 })); |
| 201 return; | 193 return; |
| 202 } | 194 } |
| 203 | 195 |
| 204 this._getFileEntry(file, false, function(fs, fileEntry) | 196 // This needs to use Utils.runAsync(), otherwise FilterStorage might |
| 205 { | 197 // initialize too early - see #337. |
| 206 fileEntry.getMetadata(function(metadata) | 198 require("utils").Utils.runAsync(function() { |
| 199 this._getFileEntry(file, false, function(fs, fileEntry) |
| 207 { | 200 { |
| 208 callback(null, { | 201 fileEntry.getMetadata(function(metadata) |
| 209 exists: true, | 202 { |
| 210 isDirectory: fileEntry.isDirectory, | 203 callback(null, { |
| 211 isFile: fileEntry.isFile, | 204 exists: true, |
| 212 lastModified: metadata.modificationTime.getTime() | 205 isDirectory: fileEntry.isDirectory, |
| 213 }); | 206 isFile: fileEntry.isFile, |
| 207 lastModified: metadata.modificationTime.getTime() |
| 208 }); |
| 209 }, callback); |
| 214 }, callback); | 210 }, callback); |
| 215 }, callback); | 211 }.bind(this)); |
| 216 } | 212 } |
| 217 }; | 213 }; |
| OLD | NEW |