guard against QuotaExceeded DOMExceptions when writing to localStorage

pull/1/head
ansuz 3 years ago
parent 6ab6e94802
commit b35c3e44ad

@ -37,7 +37,11 @@ define([
var cachePut = function (k, v, cb) { var cachePut = function (k, v, cb) {
if (window.cryptpadCache) { return void window.cryptpadCache.put(k, v, cb); } if (window.cryptpadCache) { return void window.cryptpadCache.put(k, v, cb); }
setTimeout(function () { setTimeout(function () {
localStorage['LESS_CACHE|' + key + '|' + k] = v; try {
localStorage['LESS_CACHE|' + key + '|' + k] = v;
} catch (err) {
console.error(err);
}
if (cb) { cb(); } if (cb) { cb(); }
}); });
}; };

@ -8,6 +8,14 @@ define([
], function (Constants, Hash, Cache, localForage, AppConfig, Util) { ], function (Constants, Hash, Cache, localForage, AppConfig, Util) {
var LocalStore = {}; var LocalStore = {};
var safeSet = function (key, val) {
try {
localStorage.setItem(key, val);
} catch (err) {
console.error(err);
}
};
LocalStore.setThumbnail = function (key, value, cb) { LocalStore.setThumbnail = function (key, value, cb) {
localForage.setItem(key, value, cb); localForage.setItem(key, value, cb);
}; };
@ -21,7 +29,7 @@ define([
LocalStore.setFSHash = function (hash) { LocalStore.setFSHash = function (hash) {
var sHash = Hash.serializeHash(hash); var sHash = Hash.serializeHash(hash);
localStorage[Constants.fileHashKey] = sHash; safeSet(Constants.fileHashKey, sHash);
}; };
LocalStore.getFSHash = function () { LocalStore.getFSHash = function () {
var hash = localStorage[Constants.fileHashKey]; var hash = localStorage[Constants.fileHashKey];
@ -33,7 +41,7 @@ define([
if (hash) { if (hash) {
var sHash = Hash.serializeHash(hash); var sHash = Hash.serializeHash(hash);
if (sHash !== hash) { localStorage[Constants.fileHashKey] = sHash; } if (sHash !== hash) { safeSet(Constants.fileHashKey, sHash); }
} }
return hash; return hash;
@ -49,7 +57,7 @@ define([
if (hash) { if (hash) {
var sHash = Hash.serializeHash(hash); var sHash = Hash.serializeHash(hash);
if (sHash !== hash) { localStorage[Constants.userHashKey] = sHash; } if (sHash !== hash) { safeSet(Constants.userHashKey, sHash); }
} }
return hash; return hash;
@ -57,7 +65,7 @@ define([
LocalStore.setUserHash = function (hash) { LocalStore.setUserHash = function (hash) {
var sHash = Hash.serializeHash(hash); var sHash = Hash.serializeHash(hash);
localStorage[Constants.userHashKey] = sHash; safeSet(Constants.userHashKey, sHash);
}; };
LocalStore.getBlockHash = function () { LocalStore.getBlockHash = function () {
@ -65,7 +73,7 @@ define([
}; };
LocalStore.setBlockHash = function (hash) { LocalStore.setBlockHash = function (hash) {
localStorage[Constants.blockHashKey] = hash; safeSet(Constants.blockHashKey, hash);
}; };
LocalStore.getAccountName = function () { LocalStore.getAccountName = function () {
@ -87,7 +95,7 @@ define([
}; };
LocalStore.setDriveRedirectPreference = function (bool) { LocalStore.setDriveRedirectPreference = function (bool) {
localStorage.setItem(Constants.redirectToDriveKey, Boolean(bool)); safeSet(Constants.redirectToDriveKey, Boolean(bool));
}; };
LocalStore.getPremium = function () { LocalStore.getPremium = function () {
@ -96,15 +104,15 @@ define([
} catch (err) { return; } } catch (err) { return; }
}; };
LocalStore.setPremium = function (bool) { LocalStore.setPremium = function (bool) {
localStorage.setItem(Constants.isPremiumKey, Boolean(bool)); safeSet(Constants.isPremiumKey, Boolean(bool));
}; };
LocalStore.login = function (hash, name, cb) { LocalStore.login = function (hash, name, cb) {
if (!hash) { throw new Error('expected a user hash'); } if (!hash) { throw new Error('expected a user hash'); }
if (!name) { throw new Error('expected a user name'); } if (!name) { throw new Error('expected a user name'); }
hash = Hash.serializeHash(hash); hash = Hash.serializeHash(hash);
localStorage.setItem(Constants.userHashKey, hash); safeSet(Constants.userHashKey, hash);
localStorage.setItem(Constants.userNameKey, name); safeSet(Constants.userNameKey, name);
if (cb) { cb(); } if (cb) { cb(); }
}; };
var logoutHandlers = []; var logoutHandlers = [];

@ -251,7 +251,11 @@ define([
sframeChan.on('EV_CACHE_PUT', function (x) { sframeChan.on('EV_CACHE_PUT', function (x) {
Object.keys(x).forEach(function (k) { Object.keys(x).forEach(function (k) {
localStorage['CRYPTPAD_CACHE|' + k] = x[k]; try {
localStorage['CRYPTPAD_CACHE|' + k] = x[k];
} catch (err) {
console.error(err);
}
}); });
}); });
sframeChan.on('EV_LOCALSTORE_PUT', function (x) { sframeChan.on('EV_LOCALSTORE_PUT', function (x) {
@ -260,7 +264,11 @@ define([
delete localStorage['CRYPTPAD_STORE|' + k]; delete localStorage['CRYPTPAD_STORE|' + k];
return; return;
} }
localStorage['CRYPTPAD_STORE|' + k] = x[k]; try {
localStorage['CRYPTPAD_STORE|' + k] = x[k];
} catch (err) {
console.error(err);
}
}); });
}); });
@ -1859,7 +1867,11 @@ define([
if (isChrome && getChromeVersion() === 68) { if (isChrome && getChromeVersion() === 68) {
sframeChan.whenReg('EV_CHROME_68', function () { sframeChan.whenReg('EV_CHROME_68', function () {
sframeChan.event("EV_CHROME_68"); sframeChan.event("EV_CHROME_68");
localStorage.CryptPad_chrome68 = "1"; try {
localStorage.CryptPad_chrome68 = "1";
} catch (err) {
console.error(err);
}
}); });
} }
} }

@ -125,7 +125,11 @@ define([
sframeChan.on('EV_CACHE_PUT', function (x) { sframeChan.on('EV_CACHE_PUT', function (x) {
Object.keys(x).forEach(function (k) { Object.keys(x).forEach(function (k) {
localStorage['CRYPTPAD_CACHE|' + k] = x[k]; try {
localStorage['CRYPTPAD_CACHE|' + k] = x[k];
} catch (err) {
console.error(err);
}
}); });
}); });
sframeChan.on('EV_LOCALSTORE_PUT', function (x) { sframeChan.on('EV_LOCALSTORE_PUT', function (x) {
@ -134,7 +138,11 @@ define([
delete localStorage['CRYPTPAD_STORE|' + k]; delete localStorage['CRYPTPAD_STORE|' + k];
return; return;
} }
localStorage['CRYPTPAD_STORE|' + k] = x[k]; try {
localStorage['CRYPTPAD_STORE|' + k] = x[k];
} catch (err) {
console.error(err);
}
}); });
}); });

Loading…
Cancel
Save