diff --git a/lib/renderer/extensions/storage.js b/lib/renderer/extensions/storage.js index 1453cb97fadd..57672b8a2832 100644 --- a/lib/renderer/extensions/storage.js +++ b/lib/renderer/extensions/storage.js @@ -7,102 +7,102 @@ const getChromeStoragePath = (storageType, extensionId) => { return path.join( app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`) } -const readChromeStorageFile = (storageType, extensionId) => { + +const readChromeStorageFile = (storageType, extensionId, cb) => { const filePath = getChromeStoragePath(storageType, extensionId) - if (!fs.existsSync(filePath)) return null - return fs.readFileSync(filePath, 'utf8') + fs.readFile(filePath, 'utf8', (err, data) => { + if (err && err.code === 'ENOENT') { + return cb(null, null) + } + cb(err, data) + }) } -const writeChromeStorageFile = (storageType, extensionId, data) => { +const writeChromeStorageFile = (storageType, extensionId, data, cb) => { const filePath = getChromeStoragePath(storageType, extensionId) - try { - fs.mkdirSync(path.dirname(filePath)) - } catch (error) { - // Ignore error - } - return fs.writeFileSync(filePath, data) + + fs.mkdir(path.dirname(filePath), err => { + if (err) { /* we just ignore the errors of mkdir */ } + fs.writeFile(filePath, data, cb) + }) } -const getStorage = (storageType, extensionId) => { - const data = readChromeStorageFile(storageType, extensionId) - if (data != null) { - return JSON.parse(data) - } else { - return {} - } +const getStorage = (storageType, extensionId, cb) => { + readChromeStorageFile(storageType, extensionId, (err, data) => { + if (err) throw err + if (!cb) throw new TypeError('No callback provided') + + if (data !== null) { + cb(JSON.parse(data)) + } else { + cb({}) + } + }) } -const setStorage = (storageType, extensionId, storage) => { +const setStorage = (storageType, extensionId, storage, cb) => { const json = JSON.stringify(storage) - writeChromeStorageFile(storageType, extensionId, json) -} - -const scheduleCallback = (items, callback) => { - setTimeout(function () { - callback(items) + writeChromeStorageFile(storageType, extensionId, json, err => { + if (err) throw err + if (cb) cb() }) } const getStorageManager = (storageType, extensionId) => { return { get (keys, callback) { - const storage = getStorage(storageType, extensionId) - if (keys == null) return scheduleCallback(storage, callback) + getStorage(storageType, extensionId, storage => { + if (keys == null) return callback(storage) - let defaults = {} - switch (typeof keys) { - case 'string': - keys = [keys] - break - case 'object': - if (!Array.isArray(keys)) { - defaults = keys - keys = Object.keys(keys) - } - break - } - if (keys.length === 0) return scheduleCallback({}, callback) + let defaults = {} + switch (typeof keys) { + case 'string': + keys = [keys] + break + case 'object': + if (!Array.isArray(keys)) { + defaults = keys + keys = Object.keys(keys) + } + break + } + if (keys.length === 0) return callback({}) - let items = {} - keys.forEach(function (key) { - var value = storage[key] - if (value == null) value = defaults[key] - items[key] = value + let items = {} + keys.forEach(function (key) { + var value = storage[key] + if (value == null) value = defaults[key] + items[key] = value + }) + callback(items) }) - scheduleCallback(items, callback) }, set (items, callback) { - const storage = getStorage(storageType, extensionId) + getStorage(storageType, extensionId, storage => { + Object.keys(items).forEach(function (name) { + storage[name] = items[name] + }) - Object.keys(items).forEach(function (name) { - storage[name] = items[name] + setStorage(storageType, extensionId, storage, callback) }) - - setStorage(storageType, extensionId, storage) - - setTimeout(callback) }, remove (keys, callback) { - const storage = getStorage(storageType, extensionId) + getStorage(storageType, extensionId, storage => { + if (!Array.isArray(keys)) { + keys = [keys] + } + keys.forEach(function (key) { + delete storage[key] + }) - if (!Array.isArray(keys)) { - keys = [keys] - } - keys.forEach(function (key) { - delete storage[key] + setStorage(storageType, extensionId, storage, callback) }) - - setStorage(storageType, extensionId, storage) - - setTimeout(callback) }, clear (callback) { - setStorage(storageType, extensionId, {}) - - setTimeout(callback) + setStorage(storageType, extensionId, {}, callback) } } }