From aac8414a8982d0fc2502df1f78399a234c6dfb25 Mon Sep 17 00:00:00 2001 From: Johan Girod Date: Thu, 11 Oct 2018 15:18:31 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20l'erreur=20'unsafe=20operation'=20lors=20?= =?UTF-8?q?de=20l'acc=C3=A8s=20au=20localstorage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/storage/persistEverything.js | 5 ++-- source/storage/persistSimulation.js | 7 +++--- source/storage/safeLocalStorage.js | 38 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 source/storage/safeLocalStorage.js 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 + } + } +}