| 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, successCallback, errorCallback) |
| 26 { | 38 { |
| 27 let key = fileToKey(file); | 39 let key = fileToKey(file); |
| 28 | 40 |
| 29 ext.storage.get([key], function(items) | 41 ext.storage.get([key], function(items) |
| 30 { | 42 { |
| 31 let entry = items[key]; | 43 let entry = items[key]; |
| 32 | 44 |
| 33 if (entry) | 45 if (entry) |
| 34 successCallback(entry); | 46 successCallback(entry); |
| 35 else | 47 else |
| 36 errorCallback(new Error("File doesn't exist")); | 48 errorCallback(new Error("File doesn't exist")); |
| 37 }); | 49 }); |
| 38 } | 50 } |
| 39 | 51 |
| 40 function saveFile(file, data, callback) | 52 // Make sure we don't leak chunks when saving smaller files in Edge |
| 41 { | 53 ext.storage.onChanged.addListener(function(changes, namespace) { |
| 42 ext.storage.set( | 54 if (namespace != 'local') |
| 43 fileToKey(file), | 55 return; |
| 44 { | 56 |
| 45 content: data, | 57 for (key in changes) { |
| 46 lastModified: Date.now() | 58 var oldValue = changes[key].oldValue; |
| 47 }, | 59 var newValue = changes[key].newValue; |
| 48 callback | 60 if (typeof oldValue == "object" && |
| 49 ); | 61 typeof oldValue["chunks"] == "number") |
| 50 } | 62 { |
| 63 for (let i = newValue.chunks; i < oldValue.chunks; ++i) |
| 64 ext.storage.remove(key + ":" + i); |
| 65 } |
| 66 } |
| 67 }); |
| 68 |
| 51 | 69 |
| 52 exports.IO = | 70 exports.IO = |
| 53 { | 71 { |
| 54 resolveFilePath: function(path) | 72 resolveFilePath: function(path) |
| 55 { | 73 { |
| 56 return new FakeFile(path); | 74 return new FakeFile(path); |
| 57 }, | 75 }, |
| 58 | 76 |
| 59 readFromFile: function(file, listener, callback) | 77 readFromFile: function(file, listener, callback) |
| 60 { | 78 { |
| 61 function onLoaded(entry) | 79 function onLoaded(entry) |
| 62 { | 80 { |
| 63 for (let line of entry.content) | 81 if ("content" in entry) |
| 64 listener.process(line); | 82 { |
| 83 for (let line of entry.content) |
| 84 listener.process(line); |
| 65 | 85 |
| 66 listener.process(null); | 86 listener.process(null); |
| 67 callback(null); | 87 callback(null); |
| 88 } |
| 89 else |
| 90 { |
| 91 let keys = []; |
| 92 for (let i = 0; i < entry.chunks; i++) |
| 93 keys.push(fileToKey(file, i)); |
| 94 |
| 95 ext.storage.get(keys, items => |
| 96 { |
| 97 for (let key of keys) |
| 98 for (let line of items[key]) |
| 99 listener.process(line); |
| 100 |
| 101 listener.process(null); |
| 102 callback(null); |
| 103 }); |
| 104 } |
| 68 } | 105 } |
| 69 | 106 |
| 70 loadFile(file, onLoaded, callback); | 107 loadFile(file, onLoaded, callback); |
| 71 }, | 108 }, |
| 72 | 109 |
| 73 writeToFile: function(file, data, callback) | 110 writeToFile: function(file, data, callback) |
| 74 { | 111 { |
| 75 saveFile(file, data, callback); | 112 let key = fileToKey(file); |
| 76 }, | 113 let items = {}; |
| 114 items[key] = {lastModified: Date.now()}; |
| 77 | 115 |
| 78 copyFile: function(fromFile, toFile, callback) | 116 // Edge currently has a limitation of how much data can be stored |
| 79 { | 117 // using chrome.storage.local.set. The current limit is 1Mb per operation. |
| 80 function onLoaded(entry) | 118 // Edge uses 'browser' namespace instead of 'chrome'. |
| 119 if (typeof browser != "object") |
| 81 { | 120 { |
| 82 saveFile(toFile, entry.content, callback); | 121 // In Edge strings are stored in UTF-16 format, so 2 bytes per char |
| 122 // We subtract 1000 bytes for keys and array overhead |
| 123 let quota = 1024 * 1024 / 2 - 1000; |
| 124 let chunks = []; |
| 125 let chunk = []; |
| 126 let chunkSize = 0; |
| 127 |
| 128 for (let line of data) |
| 129 { |
| 130 if (line.length + chunkSize > quota) |
| 131 { |
| 132 chunks.push(chunk); |
| 133 chunk = []; |
| 134 chunkSize = 0; |
| 135 } |
| 136 |
| 137 chunk.push(line); |
| 138 chunkSize += line.length; |
| 139 } |
| 140 chunks.push(chunk); |
| 141 |
| 142 if (chunks.length > 1) |
| 143 { |
| 144 for (let i = 0; i < chunks.length; i++) |
| 145 items[fileToKey(file, i)] = chunks[i]; |
| 146 items[key].chunks = chunks.length; |
| 147 ext.storage.set(items, callback); |
| 148 return; |
| 149 } |
| 83 } | 150 } |
| 84 | 151 |
| 85 loadFile(fromFile, onLoaded, callback); | 152 items[key].content = data; |
| 86 }, | 153 ext.storage.set(items, callback); |
| 87 | |
| 88 renameFile: function(fromFile, newName, callback) | |
| 89 { | |
| 90 function onLoaded() | |
| 91 { | |
| 92 ext.storage.remove(fileToKey(fromFile), function() | |
| 93 { | |
| 94 ext.storage.set(keyPrefix + newName, entry, callback); | |
| 95 }); | |
| 96 } | |
| 97 | |
| 98 loadFile(fromFile, onLoaded, callback); | |
| 99 }, | |
| 100 | |
| 101 removeFile: function(file, callback) | |
| 102 { | |
| 103 ext.storage.remove(fileToKey(file), callback); | |
| 104 }, | 154 }, |
| 105 | 155 |
| 106 statFile: function(file, callback) | 156 statFile: function(file, callback) |
| 107 { | 157 { |
| 108 function onLoaded(entry) | 158 function onLoaded(entry) |
| 109 { | 159 { |
| 110 callback(null, { | 160 callback(null, { |
| 111 exists: true, | 161 exists: true, |
| 112 lastModified: entry.lastModified | 162 lastModified: entry.lastModified |
| 113 }); | 163 }); |
| 114 } | 164 } |
| 115 | 165 |
| 116 loadFile(file, onLoaded, callback); | 166 loadFile(file, onLoaded, callback); |
| 117 } | 167 }, |
| 118 }; | 168 }; |
| OLD | NEW |