Fix cache issue with Firefox Private browsing

pull/1/head
yflory 4 years ago
parent ed7becbb54
commit 0411aa3c4a

@ -3,13 +3,33 @@ define([
'/bower_components/localforage/dist/localforage.min.js',
], function (Util, localForage) {
var S = {};
var onReady = Util.mkEvent(true);
// Check if indexedDB is allowed
var allowed = false;
try {
let request = indexedDB.open('mydatabase', 1);
request.onsuccess = function () {
allowed = true;
onReady.fire();
};
request.onerror = function () {
onReady.fire();
};
} catch (e) {
onReady.fire();
}
var cache = localForage.createInstance({
driver: localForage.INDEXEDDB,
name: "cp_cache"
});
S.getBlobCache = function (id, cb) {
cb = Util.once(Util.mkAsync(cb || function () {}));
onReady.reg(function () {
if (!allowed) { return void cb('NOCACHE'); }
cache.getItem(id, function (err, obj) {
if (err || !obj || !obj.c) {
return void cb(err || 'EINVAL');
@ -18,9 +38,13 @@ define([
obj.t = +new Date();
cache.setItem(id, obj);
});
});
};
S.setBlobCache = function (id, u8, cb) {
cb = Util.once(Util.mkAsync(cb || function () {}));
onReady.reg(function () {
if (!allowed) { return void cb('NOCACHE'); }
if (!u8) { return void cb('EINVAL'); }
cache.setItem(id, {
c: u8,
@ -28,12 +52,16 @@ define([
}, function (err) {
cb(err);
});
});
};
// id: channel ID or blob ID
// returns array of messages
S.getChannelCache = function (id, cb) {
cb = Util.once(Util.mkAsync(cb || function () {}));
onReady.reg(function () {
if (!allowed) { return void cb('NOCACHE'); }
cache.getItem(id, function (err, obj) {
if (err || !obj || !Array.isArray(obj.c)) {
return void cb(err || 'EINVAL');
@ -42,6 +70,7 @@ define([
obj.t = +new Date();
cache.setItem(id, obj);
});
});
};
// Keep the last two checkpoint + any checkpoint that may exist in the last 100 messages
@ -64,6 +93,9 @@ define([
S.storeCache = function (id, validateKey, val, cb) {
cb = Util.once(Util.mkAsync(cb || function () {}));
onReady.reg(function (allowed) {
if (!allowed) { return void cb('NOCACHE'); }
if (!Array.isArray(val) || !validateKey) { return void cb('EINVAL'); }
checkCheckpoints(val);
cache.setItem(id, {
@ -73,18 +105,27 @@ define([
}, function (err) {
cb(err);
});
});
};
S.clearChannel = function (id, cb) {
cb = Util.once(Util.mkAsync(cb || function () {}));
onReady.reg(function () {
if (!allowed) { return void cb('NOCACHE'); }
cache.removeItem(id, function () {
cb();
});
});
};
S.clear = function (cb) {
cb = Util.once(Util.mkAsync(cb || function () {}));
onReady.reg(function () {
if (!allowed) { return void cb('NOCACHE'); }
cache.clear(cb);
});
};
self.CryptPad_clearIndexedDB = S.clear;

@ -681,12 +681,14 @@ define([
});
sframeChan.on('Q_GET_BLOB_CACHE', function (data, cb) {
if (!Utils.Cache) { return void cb({error: 'NOCACHE'}); }
Utils.Cache.getBlobCache(data.id, function (err, obj) {
if (err) { return void cb({error: err}); }
cb(obj);
});
});
sframeChan.on('Q_SET_BLOB_CACHE', function (data, cb) {
if (!Utils.Cache) { return void cb({error: 'NOCACHE'}); }
if (!data || !data.u8 || typeof(data.u8) !== "object") { return void cb({error: 'EINVAL'}); }
Utils.Cache.setBlobCache(data.id, data.u8, function (err) {
if (err) { return void cb({error: err}); }

Loading…
Cancel
Save