Skip to content

Commit

Permalink
handle situation where storage.sync is disabled in certain versions o…
Browse files Browse the repository at this point in the history
…f firefox
  • Loading branch information
heyellieday committed Jan 3, 2018
1 parent a76324f commit 7184db7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 2 additions & 3 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 17 additions & 2 deletions app/scripts/lib/extension-store.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
})
})
}
}

0 comments on commit 7184db7

Please sign in to comment.