electron/lib/renderer/extensions/storage.js

116 lines
2.8 KiB
JavaScript
Raw Normal View History

const fs = require('fs')
const path = require('path')
const { remote } = require('electron')
const { app } = remote;
2017-07-07 01:13:19 +00:00
const getChromeStoragePath = (storageType, extensionId) => {
return path.join(
2017-07-07 01:13:19 +00:00
app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`)
}
2017-07-07 01:13:19 +00:00
const readChromeStorageFile = (storageType, extensionId) => {
const filePath = getChromeStoragePath(storageType, extensionId)
if(!fs.existsSync(filePath)) return null
return fs.readFileSync(filePath, 'utf8')
}
2017-07-07 01:13:19 +00:00
const writeChromeStorageFile = (storageType, extensionId, data) => {
const filePath = getChromeStoragePath(storageType, extensionId)
try {
fs.mkdirSync(path.dirname(filePath))
} catch (error) {
// Ignore error
}
return fs.writeFileSync(filePath, data)
}
2017-07-07 01:13:19 +00:00
const getStorage = (storageType, extensionId) => {
const data = readChromeStorageFile(storageType, extensionId)
2016-06-09 21:14:14 +00:00
if (data != null) {
return JSON.parse(data)
} else {
return {}
}
}
2017-07-07 01:13:19 +00:00
const setStorage = (storageType, extensionId, storage) => {
2016-06-09 21:14:14 +00:00
const json = JSON.stringify(storage)
2017-07-07 01:13:19 +00:00
const data = writeChromeStorageFile(storageType, extensionId, json)
2016-06-09 21:14:14 +00:00
}
2016-06-28 20:02:03 +00:00
const scheduleCallback = (items, callback) => {
setTimeout(function () {
callback(items)
})
}
2017-07-07 01:13:19 +00:00
const getStorageManager = (storageType, extensionId) => {
2016-06-28 09:02:25 +00:00
return {
2016-06-09 21:14:14 +00:00
get (keys, callback) {
2017-07-07 01:13:19 +00:00
const storage = getStorage(storageType, extensionId)
2016-06-28 20:02:03 +00:00
if (keys == null) return scheduleCallback(storage, callback)
2016-06-09 23:26:42 +00:00
let defaults = {}
switch (typeof keys) {
case 'string':
keys = [keys]
break
case 'object':
if (!Array.isArray(keys)) {
defaults = keys
keys = Object.keys(keys)
}
2016-06-09 23:39:24 +00:00
break
2016-06-09 23:26:42 +00:00
}
2016-06-28 20:02:03 +00:00
if (keys.length === 0) return scheduleCallback({}, callback)
2016-06-09 21:14:14 +00:00
let items = {}
keys.forEach(function (key) {
2016-06-09 23:26:42 +00:00
var value = storage[key]
if (value == null) value = defaults[key]
items[key] = value
2016-06-09 21:14:14 +00:00
})
2016-06-28 20:02:03 +00:00
scheduleCallback(items, callback)
2016-06-09 21:14:14 +00:00
},
set (items, callback) {
2017-07-07 01:13:19 +00:00
const storage = getStorage(storageType, extensionId)
2016-06-09 21:14:14 +00:00
Object.keys(items).forEach(function (name) {
storage[name] = items[name]
})
2017-07-07 01:13:19 +00:00
setStorage(storageType, extensionId, storage)
2016-06-09 21:14:14 +00:00
setTimeout(callback)
},
remove (keys, callback) {
2017-07-07 01:13:19 +00:00
const storage = getStorage(storageType, extensionId)
if (!Array.isArray(keys)) {
keys = [keys]
}
keys.forEach(function (key) {
delete storage[key]
})
2017-07-07 01:13:19 +00:00
setStorage(storageType, extensionId, storage)
setTimeout(callback)
},
clear (callback) {
2017-07-07 01:13:19 +00:00
setStorage(storageType, extensionId, {})
2016-06-09 21:14:14 +00:00
setTimeout(callback)
}
}
}
2016-06-28 09:02:25 +00:00
module.exports = {
2017-07-07 01:13:19 +00:00
setup: extensionId => ({
sync: getStorageManager('sync', extensionId),
local: getStorageManager('local', extensionId)
})
2016-06-28 09:02:25 +00:00
}