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 const keyPrefix = "file:"; | 18 const keyPrefix = "file:"; |
19 | 19 |
20 function fileToKey(file) | 20 function fileToKey(file) |
21 { | 21 { |
22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); | 22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); |
23 } | 23 } |
24 | 24 |
25 function loadFile(file, successCallback, errorCallback) | 25 function loadFile(file, successCallback, errorCallback) |
26 { | 26 { |
27 let key = fileToKey(file); | 27 let key = fileToKey(file); |
28 | 28 // Make sure we do not have subscriptions in localStorage from older |
kzar
2016/12/20 11:09:01
Nit: Mind adding this blank line back in?
Oleksandr
2016/12/20 14:36:50
Done.
| |
29 ext.storage.get([key], function(items) | 29 // versions first |
30 let entry = localStorage.getItem(key); | |
31 if (typeof entry == "string") | |
30 { | 32 { |
31 let entry = items[key]; | 33 try |
32 | 34 { |
33 if (entry) | 35 entry = JSON.parse(entry); |
34 successCallback(entry); | 36 } |
37 catch(err) | |
38 { | |
39 setTimeout(errorCallback(new Error("File is corrupted"))); | |
40 return; | |
41 } | |
42 setTimeout(successCallback(entry)); | |
43 return; | |
44 } | |
45 // Now try to read from IndexedDB | |
46 localforage.getItem(key, function(err, value) | |
47 { | |
48 if (err || !value) | |
49 errorCallback(new Error("File doesn't exist")); | |
35 else | 50 else |
36 errorCallback(new Error("File doesn't exist")); | 51 successCallback(value); |
37 }); | 52 }); |
38 } | 53 } |
39 | 54 |
40 function saveFile(file, data, callback) | 55 function saveFile(file, data, callback) |
41 { | 56 { |
42 ext.storage.set( | 57 var key = fileToKey(file); |
43 fileToKey(file), | 58 var entry = { |
44 { | 59 lastModified: Date.now(), |
kzar
2016/12/20 11:09:01
Nit: Mind putting these keys lastModified and cont
Oleksandr
2016/12/20 14:36:50
Done.
| |
45 content: Array.from(data), | 60 content: Array.from(data) |
46 lastModified: Date.now() | 61 }; |
47 }, | 62 |
48 callback | 63 localStorage.removeItem(key); |
49 ); | 64 localforage.setItem(key, entry, callback); |
50 } | 65 } |
51 | |
kzar
2016/12/20 11:09:01
Nit: Mind adding this blank line back in?
Oleksandr
2016/12/20 14:36:50
Done.
| |
52 exports.IO = | 66 exports.IO = |
53 { | 67 { |
54 resolveFilePath: function(path) | 68 resolveFilePath: function(path) |
55 { | 69 { |
56 return new FakeFile(path); | 70 return new FakeFile(path); |
57 }, | 71 }, |
58 | 72 |
59 readFromFile: function(file, listener, callback) | 73 readFromFile: function(file, listener, callback) |
60 { | 74 { |
61 function onLoaded(entry) | 75 function onLoaded(entry) |
62 { | 76 { |
63 for (let line of entry.content) | 77 if ("content" in entry) |
64 listener.process(line); | 78 { |
65 | 79 for (let line of entry.content) |
80 listener.process(line); | |
81 } | |
66 listener.process(null); | 82 listener.process(null); |
67 callback(null); | 83 callback(null); |
68 } | 84 } |
69 | 85 |
70 loadFile(file, onLoaded, callback); | 86 loadFile(file, onLoaded, callback); |
71 }, | 87 }, |
72 | 88 |
73 writeToFile: function(file, data, callback) | 89 writeToFile: function(file, data, callback) |
74 { | 90 { |
75 saveFile(file, data, callback); | 91 saveFile(file, data, callback); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 { | 125 { |
110 callback(null, { | 126 callback(null, { |
111 exists: true, | 127 exists: true, |
112 lastModified: entry.lastModified | 128 lastModified: entry.lastModified |
113 }); | 129 }); |
114 } | 130 } |
115 | 131 |
116 loadFile(file, onLoaded, callback); | 132 loadFile(file, onLoaded, callback); |
117 } | 133 } |
118 }; | 134 }; |
OLD | NEW |