Fix cache issue with Firefox Private browsing

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

@ -3,30 +3,55 @@ define([
'/bower_components/localforage/dist/localforage.min.js', '/bower_components/localforage/dist/localforage.min.js',
], function (Util, localForage) { ], function (Util, localForage) {
var S = {}; 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({ var cache = localForage.createInstance({
driver: localForage.INDEXEDDB,
name: "cp_cache" name: "cp_cache"
}); });
S.getBlobCache = function (id, cb) { S.getBlobCache = function (id, cb) {
cb = Util.once(Util.mkAsync(cb || function () {})); cb = Util.once(Util.mkAsync(cb || function () {}));
cache.getItem(id, function (err, obj) {
if (err || !obj || !obj.c) { onReady.reg(function () {
return void cb(err || 'EINVAL'); if (!allowed) { return void cb('NOCACHE'); }
} cache.getItem(id, function (err, obj) {
cb(null, obj.c); if (err || !obj || !obj.c) {
obj.t = +new Date(); return void cb(err || 'EINVAL');
cache.setItem(id, obj); }
cb(null, obj.c);
obj.t = +new Date();
cache.setItem(id, obj);
});
}); });
}; };
S.setBlobCache = function (id, u8, cb) { S.setBlobCache = function (id, u8, cb) {
cb = Util.once(Util.mkAsync(cb || function () {})); cb = Util.once(Util.mkAsync(cb || function () {}));
if (!u8) { return void cb('EINVAL'); }
cache.setItem(id, { onReady.reg(function () {
c: u8, if (!allowed) { return void cb('NOCACHE'); }
t: (+new Date()) // 't' represent the "lastAccess" of this cache (get or set) if (!u8) { return void cb('EINVAL'); }
}, function (err) { cache.setItem(id, {
cb(err); c: u8,
t: (+new Date()) // 't' represent the "lastAccess" of this cache (get or set)
}, function (err) {
cb(err);
});
}); });
}; };
@ -34,13 +59,17 @@ define([
// returns array of messages // returns array of messages
S.getChannelCache = function (id, cb) { S.getChannelCache = function (id, cb) {
cb = Util.once(Util.mkAsync(cb || function () {})); cb = Util.once(Util.mkAsync(cb || function () {}));
cache.getItem(id, function (err, obj) {
if (err || !obj || !Array.isArray(obj.c)) { onReady.reg(function () {
return void cb(err || 'EINVAL'); if (!allowed) { return void cb('NOCACHE'); }
} cache.getItem(id, function (err, obj) {
cb(null, obj); if (err || !obj || !Array.isArray(obj.c)) {
obj.t = +new Date(); return void cb(err || 'EINVAL');
cache.setItem(id, obj); }
cb(null, obj);
obj.t = +new Date();
cache.setItem(id, obj);
});
}); });
}; };
@ -64,27 +93,39 @@ define([
S.storeCache = function (id, validateKey, val, cb) { S.storeCache = function (id, validateKey, val, cb) {
cb = Util.once(Util.mkAsync(cb || function () {})); cb = Util.once(Util.mkAsync(cb || function () {}));
if (!Array.isArray(val) || !validateKey) { return void cb('EINVAL'); }
checkCheckpoints(val); onReady.reg(function (allowed) {
cache.setItem(id, { if (!allowed) { return void cb('NOCACHE'); }
k: validateKey, if (!Array.isArray(val) || !validateKey) { return void cb('EINVAL'); }
c: val, checkCheckpoints(val);
t: (+new Date()) // 't' represent the "lastAccess" of this cache (get or set) cache.setItem(id, {
}, function (err) { k: validateKey,
cb(err); c: val,
t: (+new Date()) // 't' represent the "lastAccess" of this cache (get or set)
}, function (err) {
cb(err);
});
}); });
}; };
S.clearChannel = function (id, cb) { S.clearChannel = function (id, cb) {
cb = Util.once(Util.mkAsync(cb || function () {})); cb = Util.once(Util.mkAsync(cb || function () {}));
cache.removeItem(id, function () {
cb(); onReady.reg(function () {
if (!allowed) { return void cb('NOCACHE'); }
cache.removeItem(id, function () {
cb();
});
}); });
}; };
S.clear = function (cb) { S.clear = function (cb) {
cb = Util.once(Util.mkAsync(cb || function () {})); cb = Util.once(Util.mkAsync(cb || function () {}));
cache.clear(cb);
onReady.reg(function () {
if (!allowed) { return void cb('NOCACHE'); }
cache.clear(cb);
});
}; };
self.CryptPad_clearIndexedDB = S.clear; self.CryptPad_clearIndexedDB = S.clear;

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

Loading…
Cancel
Save