blob: 4a970321cd7213a0f012a34401b458b3e9ace52b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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 {
constructor() {
this.isSupported = !!(extension.storage && extension.storage.sync)
this.isEnabled = true // TODO: get value from user settings
}
async fetch() {
return new Promise((resolve) => {
extension.storage && extension.storage.sync.get(KEYS_TO_SYNC, (data) => {
handleDisabledSyncAndResolve(resolve, data)
})
})
}
async sync(state) {
const dataToSync = KEYS_TO_SYNC.reduce((result, key) => {
result[key] = state.data[key]
return result
}, {})
return new Promise((resolve) => {
extension.storage && extension.storage.sync.set(dataToSync, () => {
handleDisabledSyncAndResolve(resolve)
})
})
}
}
|