| Left: | ||
| Right: |
| 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-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 "use strict"; | |
| 19 | |
| 18 const keyPrefix = "file:"; | 20 const keyPrefix = "file:"; |
| 19 | 21 |
| 20 function fileToKey(file) | 22 function fileToKey(file, chunk) |
| 21 { | 23 { |
| 22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); | 24 let key = keyPrefix; |
| 25 | |
| 26 if (file instanceof FakeFile) | |
| 27 key += file.path; | |
| 28 else | |
| 29 key += file.spec; | |
| 30 | |
| 31 if (typeof chunk != "undefined") | |
| 32 key += ":" + chunk; | |
| 33 | |
| 34 return key; | |
| 23 } | 35 } |
| 24 | 36 |
| 25 function loadFile(file, successCallback, errorCallback) | 37 function loadFile(file) |
| 26 { | 38 { |
| 27 let key = fileToKey(file); | 39 return new Promise((resolve, reject) => |
| 28 | |
| 29 ext.storage.get([key], function(items) | |
| 30 { | 40 { |
| 31 let entry = items[key]; | 41 let key = fileToKey(file); |
| 32 | 42 ext.storage.get([key], items => |
| 33 if (entry) | 43 { |
| 34 successCallback(entry); | 44 let entry = items[key]; |
| 35 else | 45 if (entry) |
| 36 errorCallback(new Error("File doesn't exist")); | 46 resolve(entry); |
| 47 else | |
| 48 reject(new Error("File doesn't exist")); | |
| 49 }); | |
| 37 }); | 50 }); |
| 38 } | 51 } |
| 39 | 52 |
| 40 function saveFile(file, data, callback) | |
| 41 { | |
| 42 ext.storage.set( | |
| 43 fileToKey(file), | |
| 44 { | |
| 45 content: data, | |
| 46 lastModified: Date.now() | |
| 47 }, | |
| 48 callback | |
| 49 ); | |
| 50 } | |
| 51 | |
| 52 exports.IO = | 53 exports.IO = |
| 53 { | 54 { |
| 54 resolveFilePath: function(path) | 55 resolveFilePath: function(path) |
| 55 { | 56 { |
| 56 return new FakeFile(path); | 57 return new FakeFile(path); |
| 57 }, | 58 }, |
| 58 | 59 |
| 59 readFromFile: function(file, listener, callback) | 60 readFromFile: function(file, listener, callback) |
| 60 { | 61 { |
| 61 function onLoaded(entry) | 62 loadFile(file).catch(() => null) |
|
Sebastian Noack
2016/04/01 10:49:44
The logic here is wrong. In the case of readFromFi
| |
| 63 .then(entry => | |
| 62 { | 64 { |
| 63 for (let line of entry.content) | 65 if (!entry) |
| 64 listener.process(line); | 66 return; |
| 67 if ("content" in entry) | |
| 68 { | |
| 69 for (let line of entry.content) | |
| 70 listener.process(line); | |
| 65 | 71 |
| 66 listener.process(null); | 72 listener.process(null); |
| 67 callback(null); | 73 callback(null); |
| 68 } | 74 } |
| 75 else | |
| 76 { | |
| 77 let keys = []; | |
| 78 for (let i = 0; i < entry.chunks; i++) | |
| 79 keys.push(fileToKey(file, i)); | |
| 69 | 80 |
| 70 loadFile(file, onLoaded, callback); | 81 ext.storage.get(keys, items => |
| 82 { | |
| 83 for (let key of keys) | |
| 84 for (let line of items[key]) | |
| 85 listener.process(line); | |
| 86 | |
| 87 listener.process(null); | |
| 88 callback(null); | |
| 89 }); | |
| 90 } | |
| 91 }); | |
| 71 }, | 92 }, |
| 72 | 93 |
|
Sebastian Noack
2016/04/01 10:49:44
Nit: You added trailing whitespaces to this line.
| |
| 73 writeToFile: function(file, data, callback) | 94 writeToFile: function(file, data, callback) |
| 74 { | 95 { |
| 75 saveFile(file, data, callback); | 96 let items = {}; |
| 76 }, | 97 let entry = items[fileToKey(file)] = {lastModified: Date.now()}; |
| 77 | 98 |
| 78 copyFile: function(fromFile, toFile, callback) | 99 if (typeof browser != "object") |
| 79 { | |
| 80 function onLoaded(entry) | |
| 81 { | 100 { |
| 82 saveFile(toFile, entry.content, callback); | 101 loadFile(file).catch(() => null) |
| 83 } | 102 .then(oldEntry => |
| 103 { | |
| 104 let quota = 1024 * 1024 / 2 - 1000; | |
| 105 let chunks = []; | |
| 106 let chunk = []; | |
| 107 let chunkSize = 0; | |
| 84 | 108 |
| 85 loadFile(fromFile, onLoaded, callback); | 109 for (let line of data) |
| 86 }, | 110 { |
| 111 if (line.length + chunkSize > quota) | |
| 112 { | |
| 113 chunks.push(chunk); | |
| 114 chunk = []; | |
| 115 chunkSize = 0; | |
| 116 } | |
| 87 | 117 |
| 88 renameFile: function(fromFile, newName, callback) | 118 chunk.push(line); |
| 89 { | 119 chunkSize += line.length; |
| 90 function onLoaded() | 120 } |
| 91 { | 121 chunks.push(chunk); |
| 92 ext.storage.remove(fileToKey(fromFile), function() | 122 |
| 93 { | 123 for (let i = 0; i < chunks.length; i++) |
| 94 ext.storage.set(keyPrefix + newName, entry, callback); | 124 items[fileToKey(file, i)] = chunks[i]; |
| 125 entry.chunks = chunks.length; | |
| 126 ext.storage.set(items, callback); | |
| 127 | |
| 128 if (oldEntry && "chunks" in oldEntry) | |
| 129 for (let i = entry.chunks; i < oldEntry.chunks; i++) | |
| 130 ext.storage.remove(fileToKey(file, i)); | |
| 95 }); | 131 }); |
| 96 } | 132 } |
| 97 | 133 else |
| 98 loadFile(fromFile, onLoaded, callback); | 134 { |
| 99 }, | 135 entry.content = data; |
| 100 | 136 ext.storage.set(items, callback); |
|
Sebastian Noack
2016/04/01 10:49:44
Nit: You added trailing whitespaces to this line.
| |
| 101 removeFile: function(file, callback) | 137 } |
| 102 { | |
| 103 ext.storage.remove(fileToKey(file), callback); | |
| 104 }, | 138 }, |
| 105 | 139 |
| 106 statFile: function(file, callback) | 140 statFile: function(file, callback) |
| 107 { | 141 { |
| 108 function onLoaded(entry) | 142 loadFile(file).catch((val) => null) |
| 143 .then(entry => | |
| 109 { | 144 { |
| 110 callback(null, { | 145 if (entry) |
| 111 exists: true, | 146 callback(null, { |
| 112 lastModified: entry.lastModified | 147 exists: true, |
| 113 }); | 148 lastModified: entry.lastModified |
| 114 } | 149 }); |
| 115 | 150 }); |
| 116 loadFile(file, onLoaded, callback); | 151 }, |
| 117 } | |
| 118 }; | 152 }; |
| OLD | NEW |