diff --git a/source/storage/persistEverything.js b/source/storage/persistEverything.js index acd354573..05860cdf2 100644 --- a/source/storage/persistEverything.js +++ b/source/storage/persistEverything.js @@ -2,6 +2,7 @@ import type { Store } from 'redux' import { debounce } from '../utils' +import safeLocalStorage from './safeLocalStorage' import type { State } from 'Types/State' import type { Action } from 'Types/ActionsTypes' @@ -12,12 +13,12 @@ const LOCAL_STORAGE_KEY = 'mycompanyinfrance::persisted-everything:v' + VERSION export function persistEverything(store: Store) { const listener = () => { const state = store.getState() - window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(state)) + safeLocalStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(state)) } store.subscribe(debounce(1000, listener)) } export function retrievePersistedState(): ?State { - const serializedState = window.localStorage.getItem(LOCAL_STORAGE_KEY) + const serializedState = safeLocalStorage.getItem(LOCAL_STORAGE_KEY) return serializedState ? JSON.parse(serializedState) : null } diff --git a/source/storage/persistSimulation.js b/source/storage/persistSimulation.js index 79c521537..d9f74a2aa 100644 --- a/source/storage/persistSimulation.js +++ b/source/storage/persistSimulation.js @@ -2,6 +2,7 @@ import type { Store } from 'redux' import { debounce } from '../utils' +import safeLocalStorage from './safeLocalStorage' import { deserialize, serialize } from './serializeSimulation' import type { State, SavedSimulation } from '../types/State' import type { Action } from 'Types/ActionsTypes' @@ -16,16 +17,16 @@ export function persistSimulation(store: Store) { if (!state.conversationStarted) { return } - window.localStorage.setItem(LOCAL_STORAGE_KEY, serialize(state)) + safeLocalStorage.setItem(LOCAL_STORAGE_KEY, serialize(state)) } store.subscribe(debounce(1000, listener)) } export function retrievePersistedSimulation(): ?SavedSimulation { - const serializedState = window.localStorage.getItem(LOCAL_STORAGE_KEY) + const serializedState = safeLocalStorage.getItem(LOCAL_STORAGE_KEY) return serializedState ? deserialize(serializedState) : null } export function deletePersistedSimulation(): void { - window.localStorage.removeItem(LOCAL_STORAGE_KEY) + safeLocalStorage.removeItem(LOCAL_STORAGE_KEY) } diff --git a/source/storage/safeLocalStorage.js b/source/storage/safeLocalStorage.js new file mode 100644 index 000000000..07731e815 --- /dev/null +++ b/source/storage/safeLocalStorage.js @@ -0,0 +1,38 @@ +export default { + removeItem: function(...args) { + try { + return window.localStorage.removeItem(...args) + } catch (error) { + if (error.name === 'SecurityError') { + console.warn( + '[localStorage] Unable to remove item due to security settings' + ) + } + return null + } + }, + getItem: function(...args) { + try { + return window.localStorage.getItem(...args) + } catch (error) { + if (error.name === 'SecurityError') { + console.warn( + '[localStorage] Unable to get item due to security settings' + ) + } + return null + } + }, + setItem: function(...args) { + try { + return window.localStorage.setItem(...args) + } catch (error) { + if (error.name === 'SecurityError') { + console.warn( + '[localStorage] Unable to set item due to security settings' + ) + } + return null + } + } +}