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) |
21 { | 23 { |
22 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); | 24 return keyPrefix + (file instanceof FakeFile ? file.path : file.spec); |
23 } | 25 } |
24 | 26 |
25 function loadFile(file, successCallback, errorCallback) | 27 function loadFile(file) |
26 { | 28 { |
27 let key = fileToKey(file); | 29 return new Promise((resolve, reject) => |
28 | |
29 ext.storage.get([key], function(items) | |
30 { | 30 { |
31 let entry = items[key]; | 31 let key = fileToKey(file); |
32 | 32 ext.storage.get([key], items => |
33 if (entry) | 33 { |
34 successCallback(entry); | 34 let entry = items[key]; |
35 else | 35 if (!entry) |
36 errorCallback(new Error("File doesn't exist")); | 36 { |
37 // Check in localStorage | |
38 try | |
39 { | |
40 entry = JSON.parse(window.localStorage.getItem(key)); | |
41 } | |
42 catch(err) | |
43 { | |
44 entry = null; | |
kzar
2016/08/22 15:38:32
Nit: `entry = null` seems redundant since we alrea
Oleksandr
2016/08/31 21:54:35
Done.
| |
45 } | |
46 } | |
47 if (entry) | |
48 resolve(entry); | |
49 else | |
50 { | |
kzar
2016/08/22 15:38:32
Nit: Please remove the braces for the else clause.
Oleksandr
2016/08/31 21:54:35
Done.
| |
51 reject(new Error("File doesn't exist")); | |
52 } | |
53 }); | |
37 }); | 54 }); |
38 } | 55 } |
39 | 56 |
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 = | 57 exports.IO = |
53 { | 58 { |
54 resolveFilePath: function(path) | 59 resolveFilePath: function(path) |
55 { | 60 { |
56 return new FakeFile(path); | 61 return new FakeFile(path); |
57 }, | 62 }, |
58 | 63 |
59 readFromFile: function(file, listener, callback) | 64 readFromFile: function(file, listener, callback) |
60 { | 65 { |
61 function onLoaded(entry) | 66 function onLoaded(entry) |
62 { | 67 { |
63 for (let line of entry.content) | 68 if ("content" in entry) |
64 listener.process(line); | 69 { |
65 | 70 if ("compressed" in entry) |
66 listener.process(null); | 71 entry.content = JSON.parse(LZString.decompressFromUTF16(entry.content) ); |
72 for (let line of entry.content) | |
73 listener.process(line); | |
74 } | |
67 callback(null); | 75 callback(null); |
68 } | 76 } |
69 | 77 |
70 loadFile(file, onLoaded, callback); | 78 loadFile(file).then(onLoaded, callback); |
71 }, | 79 }, |
72 | 80 |
73 writeToFile: function(file, data, callback) | 81 writeToFile: function(file, data, callback) |
74 { | 82 { |
75 saveFile(file, data, callback); | 83 let entry = {}; |
76 }, | 84 var key = fileToKey(file); |
kzar
2016/08/22 15:38:32
You seem to use let and var inconsistently through
Oleksandr
2016/08/31 21:54:35
Done.
| |
77 | 85 |
78 copyFile: function(fromFile, toFile, callback) | 86 // Edge cannot write files larger than 1Mb (500k string in UTF-16). |
kzar
2016/08/22 15:38:32
copyFile and renameFile have been removed?
Oleksandr
2016/08/31 21:54:35
Yes.
kzar
2016/09/01 12:57:37
But aren't they needed by adblockpluscore/lib/file
Oleksandr
2016/09/02 04:12:26
I don't think they are. Looks like we are not doin
kzar
2016/09/02 08:57:49
Ah Ok, fair enough.
| |
79 { | 87 // However it does not always raise an exception when trying to do so. |
80 function onLoaded(entry) | 88 if (typeof browser == "undefined") |
kzar
2016/08/22 15:38:32
Since this code is for the adblockplusedge reposit
Sebastian Noack
2016/08/22 16:49:19
As discussed on IRC, this should go into a bookmar
Oleksandr
2016/08/31 21:54:36
Acknowledged.
| |
81 { | 89 { |
82 saveFile(toFile, entry.content, callback); | 90 entry[key] = {lastModified: Date.now(), |
91 content: data}; | |
92 ext.storage.set(entry, callback); | |
83 } | 93 } |
84 | 94 else |
85 loadFile(fromFile, onLoaded, callback); | |
86 }, | |
87 | |
88 renameFile: function(fromFile, newName, callback) | |
89 { | |
90 function onLoaded() | |
91 { | 95 { |
92 ext.storage.remove(fileToKey(fromFile), function() | 96 // Compress and fallback to localStorage |
93 { | 97 var dataString = JSON.stringify(data); |
kzar
2016/08/22 15:38:32
Perhaps do the JSON serialization and compression
Oleksandr
2016/08/31 21:54:35
Done.
| |
94 ext.storage.set(keyPrefix + newName, entry, callback); | 98 var processedData = LZString.compressToUTF16(dataString); |
95 }); | 99 chrome.storage.local.remove(key); |
kzar
2016/08/22 15:38:32
Did you mean to use chrome.storage instead of ext.
Oleksandr
2016/08/31 21:54:36
Done.
| |
100 entry[key] = {lastModified: Date.now(), | |
101 content: processedData, | |
102 compressed: true}; | |
103 window.localStorage.setItem(key, JSON.stringify(entry[key])); | |
104 callback(); | |
96 } | 105 } |
97 | |
98 loadFile(fromFile, onLoaded, callback); | |
99 }, | |
100 | |
101 removeFile: function(file, callback) | |
102 { | |
103 ext.storage.remove(fileToKey(file), callback); | |
104 }, | 106 }, |
105 | 107 |
106 statFile: function(file, callback) | 108 statFile: function(file, callback) |
107 { | 109 { |
108 function onLoaded(entry) | 110 function onLoaded(entry) |
109 { | 111 { |
110 callback(null, { | 112 callback(null, { |
111 exists: true, | 113 exists: true, |
112 lastModified: entry.lastModified | 114 lastModified: entry.lastModified |
113 }); | 115 }); |
114 } | 116 } |
115 | 117 |
116 loadFile(file, onLoaded, callback); | 118 loadFile(file).then(onLoaded, callback); |
117 } | 119 } |
118 }; | 120 }; |
OLD | NEW |