2018-09-22 12:28:50 +00:00
|
|
|
'use strict'
|
|
|
|
|
2019-02-10 18:38:14 +00:00
|
|
|
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
|
2017-07-07 01:06:53 +00:00
|
|
|
|
2019-02-10 18:38:14 +00:00
|
|
|
const getStorage = (storageType, extensionId, callback) => {
|
|
|
|
if (typeof callback !== 'function') throw new TypeError('No callback provided')
|
2017-07-07 01:06:53 +00:00
|
|
|
|
2019-02-10 18:38:14 +00:00
|
|
|
ipcRendererUtils.invoke('CHROME_STORAGE_READ', storageType, extensionId)
|
|
|
|
.then(data => {
|
|
|
|
if (data !== null) {
|
|
|
|
callback(JSON.parse(data))
|
|
|
|
} else {
|
|
|
|
// Disabled due to false positive in StandardJS
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
|
|
|
callback({})
|
|
|
|
}
|
|
|
|
})
|
2016-06-09 21:14:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 18:38:14 +00:00
|
|
|
const setStorage = (storageType, extensionId, storage, callback) => {
|
2017-07-10 22:35:53 +00:00
|
|
|
const json = JSON.stringify(storage)
|
2019-02-10 18:38:14 +00:00
|
|
|
ipcRendererUtils.invoke('CHROME_STORAGE_WRITE', storageType, extensionId, json)
|
|
|
|
.then(() => {
|
|
|
|
if (callback) callback()
|
|
|
|
})
|
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
|
|
|
|
}
|
2017-11-23 21:53:03 +00:00
|
|
|
|
|
|
|
// Disabled due to false positive in StandardJS
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
2017-07-10 22:35:53 +00:00
|
|
|
if (keys.length === 0) return callback({})
|
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
const items = {}
|
2017-07-10 22:35:53 +00:00
|
|
|
keys.forEach(function (key) {
|
2018-09-28 23:17:00 +00:00
|
|
|
let value = storage[key]
|
2017-07-10 22:35:53 +00:00
|
|
|
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
|
|
|
}
|