| 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"; | 18 "use strict"; |
| 19 | 19 |
| 20 const keyPrefix = "file:"; | 20 const keyPrefix = "file:"; |
| 21 | 21 |
| 22 function fileToKey(file, chunk) | 22 function fileToKey(file, chunk) |
| 23 { | 23 { |
| 24 let key = keyPrefix; | 24 let key = keyPrefix; |
| 25 | 25 |
| 26 if (typeof file == "string") |
| 27 return file; |
| 28 |
| 26 if (file instanceof FakeFile) | 29 if (file instanceof FakeFile) |
| 27 key += file.path; | 30 key += file.path; |
| 28 else | 31 else |
| 29 key += file.spec; | 32 key += file.spec; |
| 30 | 33 |
| 31 if (typeof chunk != "undefined") | |
| 32 key += ":" + chunk; | |
| 33 | |
| 34 return key; | 34 return key; |
| 35 } | 35 } |
| 36 | 36 |
| 37 function loadFile(file) | 37 function loadFile(file) |
| 38 { | 38 { |
| 39 return new Promise((resolve, reject) => | 39 return new Promise((resolve, reject) => |
| 40 { | 40 { |
| 41 let key = fileToKey(file); | 41 let key = fileToKey(file); |
| 42 ext.storage.get([key], items => | 42 ext.storage.get([key], items => |
| 43 { | 43 { |
| 44 let entry = items[key]; | 44 let entry = items[key]; |
| 45 if (entry && entry.isSplit) |
| 46 { |
| 47 // Load both left and right parts and combine them |
| 48 return Promise.all([loadFile(key + "_l"), loadFile(key + "_r")]). |
| 49 then(values => |
| 50 { |
| 51 entry.content = values[0].content.concat(values[1].
content); |
| 52 resolve(entry); |
| 53 }).catch(() => |
| 54 { |
| 55 reject(new Error("File doesn't exist")); |
| 56 }); |
| 57 } |
| 45 if (entry) | 58 if (entry) |
| 46 resolve(entry); | 59 resolve(entry); |
| 47 else | 60 else |
| 48 reject(new Error("File doesn't exist")); | 61 reject(new Error("File doesn't exist")); |
| 49 }); | 62 }); |
| 50 }); | 63 }); |
| 51 } | 64 } |
| 52 | 65 |
| 53 exports.IO = | 66 exports.IO = |
| 54 { | 67 { |
| 55 resolveFilePath: function(path) | 68 resolveFilePath: function(path) |
| 56 { | 69 { |
| 57 return new FakeFile(path); | 70 return new FakeFile(path); |
| 58 }, | 71 }, |
| 59 | 72 |
| 60 readFromFile: function(file, listener, callback) | 73 readFromFile: function(file, listener, callback) |
| 61 { | 74 { |
| 62 function onLoaded(entry) | 75 function onLoaded(entry) |
| 63 { | 76 { |
| 64 if ("content" in entry) | 77 if ("content" in entry) |
| 65 { | 78 { |
| 66 for (let line of entry.content) | 79 for (let line of entry.content) |
| 67 listener.process(line); | 80 listener.process(line); |
| 68 | |
| 69 listener.process(null); | |
| 70 callback(null); | |
| 71 } | 81 } |
| 72 else | 82 callback(null); |
| 73 { | |
| 74 let keys = []; | |
| 75 for (let i = 0; i < entry.chunks; i++) | |
| 76 keys.push(fileToKey(file, i)); | |
| 77 | |
| 78 ext.storage.get(keys, items => | |
| 79 { | |
| 80 for (let key of keys) | |
| 81 for (let line of items[key]) | |
| 82 listener.process(line); | |
| 83 | |
| 84 listener.process(null); | |
| 85 callback(null); | |
| 86 }); | |
| 87 } | |
| 88 } | 83 } |
| 89 | 84 |
| 90 loadFile(file).then(onLoaded, callback); | 85 loadFile(file).then(onLoaded, callback); |
| 91 }, | 86 }, |
| 92 | 87 |
| 93 writeToFile: function(file, data, callback) | 88 writeToFile: function(file, data, callback) |
| 94 { | 89 { |
| 95 let items = {}; | 90 let entry = {}; |
| 96 let entry = items[fileToKey(file)] = {lastModified: Date.now()}; | 91 var key = fileToKey(file); |
| 97 | 92 entry[key] = {lastModified: Date.now(), content: data}; |
| 98 if (typeof browser == "object") | 93 try |
| 99 { | 94 { |
| 100 loadFile(file).catch(() => null) | 95 ext.storage.set(entry, callback); |
| 101 .then(oldEntry => | |
| 102 { | |
| 103 let quota = 1024 * 1024 / 2 - 1000; | |
| 104 let chunks = []; | |
| 105 let chunk = []; | |
| 106 let chunkSize = 0; | |
| 107 | |
| 108 for (let line of data) | |
| 109 { | |
| 110 if (line.length + chunkSize > quota) | |
| 111 { | |
| 112 chunks.push(chunk); | |
| 113 chunk = []; | |
| 114 chunkSize = 0; | |
| 115 } | |
| 116 | |
| 117 chunk.push(line); | |
| 118 chunkSize += line.length; | |
| 119 } | |
| 120 chunks.push(chunk); | |
| 121 | |
| 122 for (let i = 0; i < chunks.length; i++) | |
| 123 items[fileToKey(file, i)] = chunks[i]; | |
| 124 entry.chunks = chunks.length; | |
| 125 ext.storage.set(items, callback); | |
| 126 | |
| 127 if (oldEntry && "chunks" in oldEntry) | |
| 128 for (let i = entry.chunks; i < oldEntry.chunks; i++) | |
| 129 ext.storage.remove(fileToKey(file, i)); | |
| 130 }); | |
| 131 } | 96 } |
| 132 else | 97 catch(e) |
| 133 { | 98 { |
| 134 entry.content = data; | 99 // Edge throws an exception when trying to write files larger than 1Mb. |
| 135 ext.storage.set(items, callback); | 100 // Split the file in halfs and write them. |
| 101 var leftPart = data.slice(0, data.length / 2); |
| 102 var rightPart = data.slice(data.length / 2, data.length); |
| 103 exports.IO.writeToFile(key + "_l", leftPart); |
| 104 exports.IO.writeToFile(key + "_r", rightPart); |
| 105 entry[key].content = null; |
| 106 entry[key].isSplit = true; |
| 107 ext.storage.set(entry, callback); |
| 136 } | 108 } |
| 137 }, | 109 }, |
| 138 | 110 |
| 139 statFile: function(file, callback) | 111 statFile: function(file, callback) |
| 140 { | 112 { |
| 141 function onLoaded(entry) | 113 function onLoaded(entry) |
| 142 { | 114 { |
| 143 callback(null, { | 115 callback(null, { |
| 144 exists: true, | 116 exists: true, |
| 145 lastModified: entry.lastModified | 117 lastModified: entry.lastModified |
| 146 }); | 118 }); |
| 147 } | 119 } |
| 148 | 120 |
| 149 loadFile(file).then(onLoaded, callback); | 121 loadFile(file).then(onLoaded, callback); |
| 150 } | 122 } |
| 151 }; | 123 }; |
| OLD | NEW |