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 function onLoaded(entry) |
62 { | 63 { |
63 for (let line of entry.content) | 64 if ("content" in entry) |
64 listener.process(line); | 65 { |
| 66 for (let line of entry.content) |
| 67 listener.process(line); |
65 | 68 |
66 listener.process(null); | 69 listener.process(null); |
67 callback(null); | 70 callback(null); |
| 71 } |
| 72 else |
| 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 } |
68 } | 88 } |
69 | 89 |
70 loadFile(file, onLoaded, callback); | 90 loadFile(file).then(onLoaded, callback); |
71 }, | 91 }, |
72 | 92 |
73 writeToFile: function(file, data, callback) | 93 writeToFile: function(file, data, callback) |
74 { | 94 { |
75 saveFile(file, data, callback); | 95 let items = {}; |
76 }, | 96 let entry = items[fileToKey(file)] = {lastModified: Date.now()}; |
77 | 97 |
78 copyFile: function(fromFile, toFile, callback) | 98 if (typeof browser == "object") |
79 { | |
80 function onLoaded(entry) | |
81 { | 99 { |
82 saveFile(toFile, entry.content, callback); | 100 loadFile(file).catch(() => null) |
83 } | 101 .then(oldEntry => |
| 102 { |
| 103 let quota = 1024 * 1024 / 2 - 1000; |
| 104 let chunks = []; |
| 105 let chunk = []; |
| 106 let chunkSize = 0; |
84 | 107 |
85 loadFile(fromFile, onLoaded, callback); | 108 for (let line of data) |
86 }, | 109 { |
| 110 if (line.length + chunkSize > quota) |
| 111 { |
| 112 chunks.push(chunk); |
| 113 chunk = []; |
| 114 chunkSize = 0; |
| 115 } |
87 | 116 |
88 renameFile: function(fromFile, newName, callback) | 117 chunk.push(line); |
89 { | 118 chunkSize += line.length; |
90 function onLoaded() | 119 } |
91 { | 120 chunks.push(chunk); |
92 ext.storage.remove(fileToKey(fromFile), function() | 121 |
93 { | 122 for (let i = 0; i < chunks.length; i++) |
94 ext.storage.set(keyPrefix + newName, entry, callback); | 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)); |
95 }); | 130 }); |
96 } | 131 } |
97 | 132 else |
98 loadFile(fromFile, onLoaded, callback); | 133 { |
99 }, | 134 entry.content = data; |
100 | 135 ext.storage.set(items, callback); |
101 removeFile: function(file, callback) | 136 } |
102 { | |
103 ext.storage.remove(fileToKey(file), callback); | |
104 }, | 137 }, |
105 | 138 |
106 statFile: function(file, callback) | 139 statFile: function(file, callback) |
107 { | 140 { |
108 function onLoaded(entry) | 141 function onLoaded(entry) |
109 { | 142 { |
110 callback(null, { | 143 callback(null, { |
111 exists: true, | 144 exists: true, |
112 lastModified: entry.lastModified | 145 lastModified: entry.lastModified |
113 }); | 146 }); |
114 } | 147 } |
115 | 148 |
116 loadFile(file, onLoaded, callback); | 149 loadFile(file).then(onLoaded, callback); |
117 } | 150 } |
118 }; | 151 }; |
OLD | NEW |