2017-07-07 01:06:53 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const { remote } = require('electron')
|
2017-07-10 21:56:44 +00:00
|
|
|
const { app } = remote
|
2017-07-07 01:06:53 +00:00
|
|
|
|
2017-07-07 01:13:19 +00:00
|
|
|
const getChromeStoragePath = (storageType, extensionId) => {
|
2017-07-07 01:06:53 +00:00
|
|
|
return path.join(
|
2017-07-07 01:13:19 +00:00
|
|
|
app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`)
|
2017-07-07 01:06:53 +00:00
|
|
|
}
|
2017-07-10 22:35:53 +00:00
|
|
|
|
2017-07-20 17:50:55 +00:00
|
|
|
const mkdirp = (dir, callback) => {
|
|
|
|
fs.mkdir(dir, (error) => {
|
|
|
|
if (error && error.code === 'ENOENT') {
|
|
|
|
mkdirp(path.dirname(dir), (error) => {
|
|
|
|
if (!error) {
|
|
|
|
mkdirp(dir, callback)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else if (error && error.code === 'EEXIST') {
|
|
|
|
callback(null)
|
|
|
|
} else {
|
|
|
|
callback(error)
|
2017-07-10 22:56:45 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-10 22:35:53 +00:00
|
|
|
const readChromeStorageFile = (storageType, extensionId, cb) => {
|
2017-07-07 01:13:19 +00:00
|
|
|
const filePath = getChromeStoragePath(storageType, extensionId)
|
2017-07-10 22:35:53 +00:00
|
|
|
fs.readFile(filePath, 'utf8', (err, data) => {
|
|
|
|
if (err && err.code === 'ENOENT') {
|
|
|
|
return cb(null, null)
|
|
|
|
}
|
|
|
|
cb(err, data)
|
|
|
|
})
|
2017-07-07 01:06:53 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 22:35:53 +00:00
|
|
|
const writeChromeStorageFile = (storageType, extensionId, data, cb) => {
|
2017-07-07 01:13:19 +00:00
|
|
|
const filePath = getChromeStoragePath(storageType, extensionId)
|
2017-07-07 01:06:53 +00:00
|
|
|
|
2017-07-20 17:50:55 +00:00
|
|
|
mkdirp(path.dirname(filePath), err => {
|
|
|
|
if (err) { /* we just ignore the errors of mkdir or mkdirp */ }
|
2017-07-10 22:35:53 +00:00
|
|
|
fs.writeFile(filePath, data, cb)
|
|
|
|
})
|
2016-06-09 21:14:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 22:35:53 +00:00
|
|
|
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({})
|
|
|
|
}
|
|
|
|
})
|
2016-06-09 21:14:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 22:35:53 +00:00
|
|
|
const setStorage = (storageType, extensionId, storage, cb) => {
|
|
|
|
const json = JSON.stringify(storage)
|
|
|
|
writeChromeStorageFile(storageType, extensionId, json, err => {
|
|
|
|
if (err) throw err
|
|
|
|
if (cb) cb()
|
2016-06-28 20:02:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-10 22:35:53 +00:00
|
|
|
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 callback({})
|
|
|
|
|
|
|
|
let items = {}
|
|
|
|
keys.forEach(function (key) {
|
|
|
|
var value = storage[key]
|
|
|
|
if (value == null) value = defaults[key]
|
|
|
|
items[key] = value
|
|
|
|
})
|
|
|
|
callback(items)
|
2016-06-09 21:14:14 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
set (items, callback) {
|
2017-07-10 22:35:53 +00:00
|
|
|
getStorage(storageType, extensionId, storage => {
|
|
|
|
Object.keys(items).forEach(function (name) {
|
|
|
|
storage[name] = items[name]
|
|
|
|
})
|
2016-06-09 21:14:14 +00:00
|
|
|
|
2017-07-10 22:35:53 +00:00
|
|
|
setStorage(storageType, extensionId, storage, callback)
|
2016-06-09 21:14:14 +00:00
|
|
|
})
|
2016-11-10 03:34:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
remove (keys, callback) {
|
2017-07-10 22:35:53 +00:00
|
|
|
getStorage(storageType, extensionId, storage => {
|
|
|
|
if (!Array.isArray(keys)) {
|
|
|
|
keys = [keys]
|
|
|
|
}
|
|
|
|
keys.forEach(function (key) {
|
|
|
|
delete storage[key]
|
|
|
|
})
|
2016-11-10 03:34:46 +00:00
|
|
|
|
2017-07-10 22:35:53 +00:00
|
|
|
setStorage(storageType, extensionId, storage, callback)
|
2016-11-10 03:34:46 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
clear (callback) {
|
2017-07-10 22:35:53 +00:00
|
|
|
setStorage(storageType, extensionId, {}, callback)
|
2016-06-09 21:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|