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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 }; | 150 }; |
151 } | 151 } |
152 | 152 |
153 function getObjectStore(dbInstance, storeName) | 153 function getObjectStore(dbInstance, storeName) |
154 { | 154 { |
155 return dbInstance | 155 return dbInstance |
156 .transaction([storeName], IDBTransaction.READ_WRITE) | 156 .transaction([storeName], IDBTransaction.READ_WRITE) |
157 .objectStore(storeName); | 157 .objectStore(storeName); |
158 } | 158 } |
159 | 159 |
| 160 function reestablishConnection(dbInstance, retries = 10) |
| 161 { |
| 162 dbInstance.close(); |
| 163 db = openDB(dbConfig); |
| 164 |
| 165 return db.catch(err => |
| 166 { |
| 167 if (!retries) |
| 168 throw err; |
| 169 |
| 170 return reestablishConnection(dbInstance, --retries); |
| 171 }); |
| 172 } |
| 173 |
160 function getFile(fileName, dbInstance, storeName) | 174 function getFile(fileName, dbInstance, storeName) |
161 { | 175 { |
| 176 return getFromIndexedDB(fileToKey(fileName), dbInstance, storeName) |
| 177 .then(indexedDBResult => |
| 178 { |
| 179 if (!indexedDBResult) |
| 180 { |
| 181 const {IndexedDBBackup} = require("./indexedDBBackup"); |
| 182 |
| 183 return IndexedDBBackup.getBackupData() |
| 184 .then(backupData => |
| 185 saveFile( |
| 186 { |
| 187 fileName: fileToKey(fileName), |
| 188 content: backupData.content, |
| 189 lastModified: backupData.lastModified |
| 190 }, |
| 191 dbInstance, |
| 192 storeName).then(() => backupData) |
| 193 ); |
| 194 } |
| 195 return indexedDBResult; |
| 196 }); |
| 197 } |
| 198 |
| 199 function getFromIndexedDB(fileName, dbInstance, storeName) |
| 200 { |
162 return new Promise((resolve, reject) => | 201 return new Promise((resolve, reject) => |
163 { | 202 { |
164 let store = getObjectStore(dbInstance, storeName); | 203 let store = getObjectStore(dbInstance, storeName); |
165 let req = store.get(fileToKey(fileName)); | 204 let req = store.get(fileName); |
166 | 205 |
167 req.onsuccess = event => | 206 req.onsuccess = event => resolve(event.currentTarget.result); |
168 { | 207 req.onerror = event => reject(event.target.error); |
169 let {result} = event.currentTarget; | 208 }) |
170 | 209 .catch(error => |
171 if (result) | 210 { |
172 resolve(result); | 211 if (error.name == "UnknownError") |
173 else | 212 return reestablishConnection(dbInstance).then(() => undefined); |
174 reject({type: "NoSuchFile"}); | |
175 }; | |
176 req.onerror = reject; | |
177 }); | 213 }); |
178 } | 214 } |
179 | 215 |
180 function saveFile(data, dbInstance, storeName) | 216 function saveFile(data, dbInstance, storeName) |
181 { | 217 { |
182 return new Promise((resolve, reject) => | 218 return new Promise((resolve, reject) => |
183 { | 219 { |
184 let store = getObjectStore(dbInstance, storeName); | 220 let store = getObjectStore(dbInstance, storeName); |
185 let req = store.put(data); | 221 let req = store.put(data); |
186 | 222 |
187 req.onsuccess = resolve; | 223 req.onsuccess = resolve; |
188 req.onerror = reject; | 224 req.onerror = event => reject(event.target.error); |
| 225 }) |
| 226 .catch(error => |
| 227 { |
| 228 if (error.name == "UnknownError") |
| 229 { |
| 230 return reestablishConnection(dbInstance).then(newDbInstance => |
| 231 saveFile(data, newDbInstance, storeName) |
| 232 ); |
| 233 } |
189 }); | 234 }); |
190 } | 235 } |
191 | 236 |
192 function deleteFile(fileName, dbInstance, storeName) | 237 function deleteFile(fileName, dbInstance, storeName) |
193 { | 238 { |
194 return new Promise((resolve, reject) => | 239 return new Promise((resolve, reject) => |
195 { | 240 { |
196 let store = getObjectStore(dbInstance, storeName); | 241 let store = getObjectStore(dbInstance, storeName); |
197 let req = store.delete(fileToKey(fileName)); | 242 let req = store.delete(fileToKey(fileName)); |
198 | 243 |
199 req.onsuccess = resolve; | 244 req.onsuccess = resolve; |
200 req.onerror = reject; | 245 req.onerror = event => reject(event.target.error); |
| 246 }) |
| 247 .catch(error => |
| 248 { |
| 249 if (error.name == "UnknownError") |
| 250 return reestablishConnection(dbInstance); |
201 }); | 251 }); |
202 } | 252 } |
203 | 253 |
204 exports.IO = | 254 exports.IO = |
205 { | 255 { |
206 /** | 256 /** |
207 * Writes text lines to a file. | 257 * Writes text lines to a file. |
208 * @param {string} fileName | 258 * @param {string} fileName |
209 * Name of the file to be written | 259 * Name of the file to be written |
210 * @param {Iterable.<string>} data | 260 * @param {Iterable.<string>} data |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 fileName: fileToKey(newName), | 344 fileName: fileToKey(newName), |
295 content: fileData.content, | 345 content: fileData.content, |
296 lastModified: fileData.lastModified | 346 lastModified: fileData.lastModified |
297 }, | 347 }, |
298 dbInstance, | 348 dbInstance, |
299 dbConfig.storeName)) | 349 dbConfig.storeName)) |
300 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); | 350 .then(() => deleteFile(fromFile, dbInstance, dbConfig.storeName)))); |
301 } | 351 } |
302 }; | 352 }; |
303 | 353 |
OLD | NEW |