Merge pull request #9951 from alexstrat/fix-chrome-storage

Fix chrome storage access scope
This commit is contained in:
Cheng Zhao 2017-07-24 14:01:15 +09:00 committed by GitHub
commit 25f168cecb
2 changed files with 103 additions and 60 deletions

View file

@ -173,7 +173,7 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
onMessage: chrome.runtime.onMessage onMessage: chrome.runtime.onMessage
} }
chrome.storage = require('./extensions/storage') chrome.storage = require('./extensions/storage').setup(extensionId)
chrome.pageAction = { chrome.pageAction = {
show () {}, show () {},

View file

@ -1,88 +1,131 @@
const getStorage = (storageType) => { const fs = require('fs')
const data = window.localStorage.getItem(`__chrome.storage.${storageType}__`) const path = require('path')
if (data != null) { const { remote } = require('electron')
return JSON.parse(data) const { app } = remote
} else {
return {} const getChromeStoragePath = (storageType, extensionId) => {
} return path.join(
app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`)
} }
const setStorage = (storageType, storage) => { const mkdirp = (dir, callback) => {
const json = JSON.stringify(storage) fs.mkdir(dir, (error) => {
window.localStorage.setItem(`__chrome.storage.${storageType}__`, json) if (error && error.code === 'ENOENT') {
} mkdirp(path.dirname(dir), (error) => {
if (!error) {
const scheduleCallback = (items, callback) => { mkdirp(dir, callback)
setTimeout(function () { }
callback(items) })
} else if (error && error.code === 'EEXIST') {
callback(null)
} else {
callback(error)
}
}) })
} }
const getStorageManager = (storageType) => { const readChromeStorageFile = (storageType, extensionId, cb) => {
const filePath = getChromeStoragePath(storageType, extensionId)
fs.readFile(filePath, 'utf8', (err, data) => {
if (err && err.code === 'ENOENT') {
return cb(null, null)
}
cb(err, data)
})
}
const writeChromeStorageFile = (storageType, extensionId, data, cb) => {
const filePath = getChromeStoragePath(storageType, extensionId)
mkdirp(path.dirname(filePath), err => {
if (err) { /* we just ignore the errors of mkdir or mkdirp */ }
fs.writeFile(filePath, data, cb)
})
}
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, cb) => {
const json = JSON.stringify(storage)
writeChromeStorageFile(storageType, extensionId, json, err => {
if (err) throw err
if (cb) cb()
})
}
const getStorageManager = (storageType, extensionId) => {
return { return {
get (keys, callback) { get (keys, callback) {
const storage = getStorage(storageType) getStorage(storageType, extensionId, storage => {
if (keys == null) return scheduleCallback(storage, callback) if (keys == null) return callback(storage)
let defaults = {} let defaults = {}
switch (typeof keys) { switch (typeof keys) {
case 'string': case 'string':
keys = [keys] keys = [keys]
break break
case 'object': case 'object':
if (!Array.isArray(keys)) { if (!Array.isArray(keys)) {
defaults = keys defaults = keys
keys = Object.keys(keys) keys = Object.keys(keys)
} }
break break
} }
if (keys.length === 0) return scheduleCallback({}, callback) if (keys.length === 0) return callback({})
let items = {} let items = {}
keys.forEach(function (key) { keys.forEach(function (key) {
var value = storage[key] var value = storage[key]
if (value == null) value = defaults[key] if (value == null) value = defaults[key]
items[key] = value items[key] = value
})
callback(items)
}) })
scheduleCallback(items, callback)
}, },
set (items, callback) { set (items, callback) {
const storage = getStorage(storageType) getStorage(storageType, extensionId, storage => {
Object.keys(items).forEach(function (name) {
storage[name] = items[name]
})
Object.keys(items).forEach(function (name) { setStorage(storageType, extensionId, storage, callback)
storage[name] = items[name]
}) })
setStorage(storageType, storage)
setTimeout(callback)
}, },
remove (keys, callback) { remove (keys, callback) {
const storage = getStorage(storageType) getStorage(storageType, extensionId, storage => {
if (!Array.isArray(keys)) {
keys = [keys]
}
keys.forEach(function (key) {
delete storage[key]
})
if (!Array.isArray(keys)) { setStorage(storageType, extensionId, storage, callback)
keys = [keys]
}
keys.forEach(function (key) {
delete storage[key]
}) })
setStorage(storageType, storage)
setTimeout(callback)
}, },
clear (callback) { clear (callback) {
setStorage(storageType, {}) setStorage(storageType, extensionId, {}, callback)
setTimeout(callback)
} }
} }
} }
module.exports = { module.exports = {
sync: getStorageManager('sync'), setup: extensionId => ({
local: getStorageManager('local') sync: getStorageManager('sync', extensionId),
local: getStorageManager('local', extensionId)
})
} }