1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

handle situation where storage.sync is disabled in certain versions of firefox

This commit is contained in:
Ellie Day 2018-01-02 21:31:17 -08:00
parent a76324f6d3
commit 7184db7632
2 changed files with 19 additions and 5 deletions

View File

@ -47,11 +47,10 @@ async function initialize () {
async function loadStateFromPersistence () {
// migrations
const migrator = new Migrator({ migrations })
// fetch from extension store
const extensionData = await extensionStore.fetch() // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError)
// read from disk
let versionedData = diskStore.getState() || migrator.generateInitialState(firstTimeState)
// merge extension and versioned data
// fetch from extension store and merge in data
const extensionData = await extensionStore.fetch() // TODO: handle possible exceptions (https://developer.chrome.com/apps/runtime#property-lastError)
versionedData = { ...versionedData, ...extensionData }
// migrate data
versionedData = await migrator.migrateData(versionedData)

View File

@ -1,11 +1,24 @@
const extension = require('extensionizer')
const KEYS_TO_SYNC = ['KeyringController', 'PreferencesController']
const FIREFOX_SYNC_DISABLED_MESSAGE = 'Please set webextensions.storage.sync.enabled to true in about:config'
const handleDisabledSyncAndResolve = (resolve, toResolve) => {
// Firefox 52 has sync available on extension.storage, but it is disabled by default
const lastError = extension.runtime.lastError
if (lastError && lastError.message.includes(FIREFOX_SYNC_DISABLED_MESSAGE)) {
resolve({})
} else {
resolve(toResolve)
}
}
module.exports = class ExtensionStore {
async fetch() {
return new Promise((resolve) => {
extension.storage.sync.get(KEYS_TO_SYNC, data => resolve(data))
extension.storage.sync.get(KEYS_TO_SYNC, (data) => {
handleDisabledSyncAndResolve(resolve, data)
})
})
}
async sync(state) {
@ -14,7 +27,9 @@ module.exports = class ExtensionStore {
return result
}, {})
return new Promise((resolve) => {
extension.storage.sync.set(dataToSync, () => resolve())
extension.storage.sync.set(dataToSync, () => {
handleDisabledSyncAndResolve(resolve)
})
})
}
}