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 |
(...skipping 12 matching lines...) Expand all Loading... | |
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 return new Promise((resolve, reject) => |
30 { | 30 { |
31 let key = fileToKey(fileName); | 31 let key = fileToKey(fileName); |
32 | 32 |
33 ext.storage.get(key, items => | 33 // Make sure we do not have subscriptions in localStorage from older |
Sebastian Noack
2017/08/24 12:11:46
We use IndexedDB for quite a while, now. So I gues
Oleksandr
2017/08/24 14:43:58
Fair enough.
| |
34 // versions first | |
35 let entry = localStorage.getItem(key); | |
36 if (typeof entry == "string") | |
34 { | 37 { |
35 let entry = items[key]; | 38 try |
36 | 39 { |
37 if (entry) | 40 entry = JSON.parse(entry); |
38 resolve(entry); | 41 } |
42 catch (err) | |
43 { | |
44 reject({type: "NoSuchFile"}); | |
45 return; | |
46 } | |
47 resolve(entry); | |
48 return; | |
49 } | |
50 // Now try to read from IndexedDB | |
51 localforage.getItem(key, (err, value) => | |
52 { | |
53 if (err || !value) | |
Sebastian Noack
2017/08/24 12:11:46
Any particular reason, we check for `!value`? It t
Oleksandr
2017/08/24 14:43:58
Based on https://localforage.github.io/localForage
Sebastian Noack
2017/08/24 14:56:27
Based on that information we might want to explici
Oleksandr
2017/08/24 16:53:29
Done.
| |
54 reject({type: "NoSuchFile"}); | |
39 else | 55 else |
40 reject({type: "NoSuchFile"}); | 56 resolve(value); |
41 }); | 57 }); |
42 }); | 58 }); |
43 } | 59 } |
44 | 60 |
45 function saveFile(fileName, data) | 61 function saveFile(fileName, data) |
46 { | 62 { |
47 return new Promise((resolve, reject) => | 63 let key = fileToKey(fileName); |
48 { | 64 localStorage.removeItem(key); |
49 ext.storage.set( | 65 return localforage.setItem(key, { |
50 fileToKey(fileName), | 66 content: Array.from(data), |
51 { | 67 lastModified: Date.now() |
52 content: Array.from(data), | |
53 lastModified: Date.now() | |
54 }, | |
55 resolve | |
56 ); | |
57 }); | 68 }); |
58 } | 69 } |
59 | 70 |
60 function removeFile(fileName) | 71 function removeFile(fileName) |
61 { | 72 { |
62 return new Promise((resolve, reject) => | 73 return new Promise((resolve, reject) => |
63 { | 74 { |
64 ext.storage.remove(fileToKey(fileName), () => | 75 ext.storage.remove(fileToKey(fileName), () => |
65 { | 76 { |
66 if (chrome.runtime.lastError) | 77 if (chrome.runtime.lastError) |
(...skipping 12 matching lines...) Expand all Loading... | |
79 * Name of the file to be read | 90 * Name of the file to be read |
80 * @param {TextSink} listener | 91 * @param {TextSink} listener |
81 * Function that will be called for each line in the file | 92 * Function that will be called for each line in the file |
82 * @return {Promise} | 93 * @return {Promise} |
83 * Promise to be resolved or rejected once the operation is completed | 94 * Promise to be resolved or rejected once the operation is completed |
84 */ | 95 */ |
85 readFromFile(fileName, listener) | 96 readFromFile(fileName, listener) |
86 { | 97 { |
87 return loadFile(fileName).then(entry => | 98 return loadFile(fileName).then(entry => |
88 { | 99 { |
89 for (let line of entry.content) | 100 if ("content" in entry) |
Sebastian Noack
2017/08/24 12:11:46
Why is this check necessary?
Oleksandr
2017/08/24 14:43:58
If content is undefined (for whatever reason) than
Sebastian Noack
2017/08/24 14:56:27
This theoretical scenario isn't handled in upstrea
Oleksandr
2017/08/24 16:53:29
Removed the check.
| |
90 listener(line); | 101 { |
102 for (let line of entry.content) | |
103 listener(line); | |
104 } | |
91 }); | 105 }); |
92 }, | 106 }, |
93 | 107 |
94 /** | 108 /** |
95 * Writes text lines to a file. | 109 * Writes text lines to a file. |
96 * @param {string} fileName | 110 * @param {string} fileName |
97 * Name of the file to be written | 111 * Name of the file to be written |
98 * @param {Iterable.<string>} data | 112 * @param {Iterable.<string>} data |
99 * An array-like or iterable object containing the lines (without line | 113 * An array-like or iterable object containing the lines (without line |
100 * endings) | 114 * endings) |
(...skipping 25 matching lines...) Expand all Loading... | |
126 * Name of the file to be renamed | 140 * Name of the file to be renamed |
127 * @param {string} newName | 141 * @param {string} newName |
128 * New file name, will be overwritten if exists | 142 * New file name, will be overwritten if exists |
129 * @return {Promise} | 143 * @return {Promise} |
130 * Promise to be resolved or rejected once the operation is completed | 144 * Promise to be resolved or rejected once the operation is completed |
131 */ | 145 */ |
132 renameFile(fromFile, newName) | 146 renameFile(fromFile, newName) |
133 { | 147 { |
134 return loadFile(fromFile).then(entry => | 148 return loadFile(fromFile).then(entry => |
135 { | 149 { |
136 return new Promise((resolve, reject) => | 150 return saveFile(fileToKey(newName), entry); |
137 { | |
138 ext.storage.set(fileToKey(newName), entry, () => | |
139 { | |
140 if (chrome.runtime.lastError) | |
141 reject(chrome.runtime.lastError.message); | |
142 else | |
143 resolve(); | |
144 }); | |
145 }); | |
146 }).then(() => removeFile(fromFile)); | 151 }).then(() => removeFile(fromFile)); |
147 }, | 152 }, |
148 | 153 |
149 /** | 154 /** |
150 * Removes a file. | 155 * Removes a file. |
151 * @param {string} fileName | 156 * @param {string} fileName |
152 * Name of the file to be removed | 157 * Name of the file to be removed |
153 * @return {Promise} | 158 * @return {Promise} |
154 * Promise to be resolved or rejected once the operation is completed | 159 * Promise to be resolved or rejected once the operation is completed |
155 */ | 160 */ |
(...skipping 19 matching lines...) Expand all Loading... | |
175 lastModified: entry.lastModified | 180 lastModified: entry.lastModified |
176 }; | 181 }; |
177 }).catch(error => | 182 }).catch(error => |
178 { | 183 { |
179 if (error.type == "NoSuchFile") | 184 if (error.type == "NoSuchFile") |
180 return {exists: false}; | 185 return {exists: false}; |
181 throw error; | 186 throw error; |
182 }); | 187 }); |
183 } | 188 } |
184 }; | 189 }; |
OLD | NEW |