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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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(fileName) | 22 function fileToKey(fileName) |
23 { | 23 { |
24 return keyPrefix + fileName; | 24 return keyPrefix + fileName; |
25 } | 25 } |
26 | 26 |
27 function loadFile(fileName) | 27 function loadFile(fileName) |
28 { | 28 { |
29 return new Promise((resolve, reject) => | 29 let key = fileToKey(fileName); |
30 return browser.storage.local.get(key).then(items => | |
30 { | 31 { |
31 let key = fileToKey(fileName); | 32 let entry = items[key]; |
32 | 33 if (entry) |
33 ext.storage.get(key, items => | 34 return entry; |
34 { | 35 throw {type: "NoSuchFile"}; |
kzar
2018/04/24 20:38:56
Don't we normally throw an Error instance? (I get
Sebastian Noack
2018/04/24 21:12:55
Yeah, I had a similar WTF moment when I converted
kzar
2018/04/25 10:33:39
Acknowledged.
| |
35 let entry = items[key]; | |
36 | |
37 if (entry) | |
38 resolve(entry); | |
39 else | |
40 reject({type: "NoSuchFile"}); | |
41 }); | |
42 }); | 36 }); |
43 } | 37 } |
44 | 38 |
45 function saveFile(fileName, data) | 39 function saveFile(fileName, data) |
46 { | 40 { |
47 return new Promise((resolve, reject) => | 41 return browser.storage.local.set({ |
48 { | 42 [fileToKey(fileName)]: { |
49 ext.storage.set( | 43 content: Array.from(data), |
50 fileToKey(fileName), | 44 lastModified: Date.now() |
51 { | 45 } |
52 content: Array.from(data), | |
53 lastModified: Date.now() | |
54 }, | |
55 resolve | |
56 ); | |
57 }); | 46 }); |
58 } | 47 } |
59 | 48 |
60 function removeFile(fileName) | |
61 { | |
62 return new Promise((resolve, reject) => | |
63 { | |
64 ext.storage.remove(fileToKey(fileName), () => | |
65 { | |
66 if (browser.runtime.lastError) | |
67 reject(browser.runtime.lastError.message); | |
68 else | |
69 resolve(); | |
70 }); | |
71 }); | |
72 } | |
73 | |
74 exports.IO = | 49 exports.IO = |
75 { | 50 { |
76 /** | 51 /** |
77 * Reads text lines from a file. | 52 * Reads text lines from a file. |
78 * @param {string} fileName | 53 * @param {string} fileName |
79 * Name of the file to be read | 54 * Name of the file to be read |
80 * @param {TextSink} listener | 55 * @param {TextSink} listener |
81 * Function that will be called for each line in the file | 56 * Function that will be called for each line in the file |
82 * @return {Promise} | 57 * @return {Promise} |
83 * Promise to be resolved or rejected once the operation is completed | 58 * Promise to be resolved or rejected once the operation is completed |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 * Renames a file. | 99 * Renames a file. |
125 * @param {string} fromFile | 100 * @param {string} fromFile |
126 * Name of the file to be renamed | 101 * Name of the file to be renamed |
127 * @param {string} newName | 102 * @param {string} newName |
128 * New file name, will be overwritten if exists | 103 * New file name, will be overwritten if exists |
129 * @return {Promise} | 104 * @return {Promise} |
130 * Promise to be resolved or rejected once the operation is completed | 105 * Promise to be resolved or rejected once the operation is completed |
131 */ | 106 */ |
132 renameFile(fromFile, newName) | 107 renameFile(fromFile, newName) |
133 { | 108 { |
134 return loadFile(fromFile).then(entry => | 109 return loadFile(fromFile) |
135 { | 110 .then(entry => browser.storage.local.set({[fileToKey(newName)]: entry})) |
136 return new Promise((resolve, reject) => | 111 .then(() => this.removeFile(fromFile)); |
137 { | |
138 ext.storage.set(fileToKey(newName), entry, () => | |
139 { | |
140 if (browser.runtime.lastError) | |
141 reject(browser.runtime.lastError.message); | |
142 else | |
143 resolve(); | |
144 }); | |
145 }); | |
146 }).then(() => removeFile(fromFile)); | |
147 }, | 112 }, |
148 | 113 |
149 /** | 114 /** |
150 * Removes a file. | 115 * Removes a file. |
151 * @param {string} fileName | 116 * @param {string} fileName |
152 * Name of the file to be removed | 117 * Name of the file to be removed |
153 * @return {Promise} | 118 * @return {Promise} |
154 * Promise to be resolved or rejected once the operation is completed | 119 * Promise to be resolved or rejected once the operation is completed |
155 */ | 120 */ |
156 removeFile(fileName) | 121 removeFile(fileName) |
157 { | 122 { |
158 return removeFile(fileName); | 123 return browser.storage.local.remove(fileToKey(fileName)); |
159 }, | 124 }, |
160 | 125 |
161 /** | 126 /** |
162 * Retrieves file metadata. | 127 * Retrieves file metadata. |
163 * @param {string} fileName | 128 * @param {string} fileName |
164 * Name of the file to be looked up | 129 * Name of the file to be looked up |
165 * @return {Promise.<StatData>} | 130 * @return {Promise.<StatData>} |
166 * Promise to be resolved with file metadata once the operation is | 131 * Promise to be resolved with file metadata once the operation is |
167 * completed | 132 * completed |
168 */ | 133 */ |
169 statFile(fileName) | 134 statFile(fileName) |
170 { | 135 { |
171 return loadFile(fileName).then(entry => | 136 return loadFile(fileName).then(entry => |
172 { | 137 { |
173 return { | 138 return { |
174 exists: true, | 139 exists: true, |
175 lastModified: entry.lastModified | 140 lastModified: entry.lastModified |
176 }; | 141 }; |
177 }).catch(error => | 142 }).catch(error => |
178 { | 143 { |
179 if (error.type == "NoSuchFile") | 144 if (error.type == "NoSuchFile") |
180 return {exists: false}; | 145 return {exists: false}; |
181 throw error; | 146 throw error; |
182 }); | 147 }); |
183 } | 148 } |
184 }; | 149 }; |
OLD | NEW |