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) {
if (window.cryptpadCache) { return void window.cryptpadCache.put(k, v, cb); }
setTimeout(function () {
try {
localStorage['LESS_CACHE|' + key + '|' + k] = v;
} catch (err) {
console.error(err);
}
if (cb) { cb(); }
});
};

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

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

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

Loading…
Cancel
Save