Handle cases where sync storage is not available. Fix #406.

This commit is contained in:
FelisCatus 2015-04-19 19:21:29 +08:00
parent 4ebcf74828
commit eb8c0b99d8
4 changed files with 29 additions and 6 deletions

View File

@ -138,11 +138,12 @@ actionForUrl = (url) ->
storage = new OmegaTargetCurrent.Storage(chrome.storage.local, 'local')
state = new OmegaTargetCurrent.BrowserStorage(localStorage, 'omega.local.')
syncStorage = new OmegaTargetCurrent.Storage(chrome.storage.sync, 'sync')
sync = new OmegaTargetCurrent.OptionsSync(syncStorage)
if localStorage['omega.local.syncOptions'] != '"sync"'
sync.enabled = false
sync.transformValue = OmegaTargetCurrent.Options.transformValueForSync
if chrome.storage.sync
syncStorage = new OmegaTargetCurrent.Storage(chrome.storage.sync, 'sync')
sync = new OmegaTargetCurrent.OptionsSync(syncStorage)
if localStorage['omega.local.syncOptions'] != '"sync"'
sync.enabled = false
sync.transformValue = OmegaTargetCurrent.Options.transformValueForSync
options = new OmegaTargetCurrent.Options(null, storage, state, Log, sync)
options.externalApi = new OmegaTargetCurrent.ExternalApi(options)

View File

@ -24,6 +24,10 @@ class ChromeStorage extends OmegaTarget.Storage
err = new OmegaTarget.Storage.RateLimitExceededError()
err.perMinute = true
err.sustained = 10
else if err.message.indexOf('is not available') >= 0
# This could happen if the storage area is not available. For example,
# some Chromium-based browsers disable access to the sync storage.
err = new OmegaTarget.Storage.StorageUnavailableError()
return Promise.reject(err)

View File

@ -89,7 +89,14 @@ class Options
else
@_state.set({'syncOptions': 'sync'})
@_syncWatchStop = @sync.watchAndPull(@_storage)
@sync.copyTo(@_storage).then =>
@sync.copyTo(@_storage).catch(Storage.StorageUnavailableError, =>
console.error('Warning: Sync storage is not available in this ' +
'browser! Disabling options sync.')
@_syncWatchStop?()
@_syncWatchStop = null
@sync = null
@_state.set({'syncOptions': 'unsupported'})
).then =>
@_storage.get(null)
@optionsLoaded = loadRaw.then((options) =>

View File

@ -19,6 +19,17 @@ class Storage
class QuotaExceededError extends Error
constructor: -> super
###*
# If this storage is not available for some reason, all operations should
# reject with an instance of StorageUnavailableError, when implemented in
# derived classes of Storage.
# This error is considered fatal and unrecoverable in the current environment.
# Further access to this storage should be avoided until restart.
###
@StorageUnavailableError:
class StorageUnavailableError extends Error
constructor: -> super
###*
# A set of operations to be performed on a Storage.
# @typedef WriteOperations