From 0411aa3c4ac488f5490ad9cc1bc2fef5866b70df Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 10 Dec 2020 12:11:03 +0100 Subject: [PATCH] Fix cache issue with Firefox Private browsing --- www/common/outer/cache-store.js | 103 +++++++++++++++++++++--------- www/common/sframe-common-outer.js | 2 + 2 files changed, 74 insertions(+), 31 deletions(-) diff --git a/www/common/outer/cache-store.js b/www/common/outer/cache-store.js index cd88de6f6..e55287d8c 100644 --- a/www/common/outer/cache-store.js +++ b/www/common/outer/cache-store.js @@ -3,30 +3,55 @@ 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 () {})); - cache.getItem(id, function (err, obj) { - if (err || !obj || !obj.c) { - return void cb(err || 'EINVAL'); - } - cb(null, obj.c); - obj.t = +new Date(); - cache.setItem(id, obj); + + 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'); + } + cb(null, obj.c); + obj.t = +new Date(); + cache.setItem(id, obj); + }); }); }; S.setBlobCache = function (id, u8, cb) { cb = Util.once(Util.mkAsync(cb || function () {})); - if (!u8) { return void cb('EINVAL'); } - cache.setItem(id, { - c: u8, - t: (+new Date()) // 't' represent the "lastAccess" of this cache (get or set) - }, function (err) { - cb(err); + + onReady.reg(function () { + if (!allowed) { return void cb('NOCACHE'); } + if (!u8) { return void cb('EINVAL'); } + cache.setItem(id, { + 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 S.getChannelCache = function (id, cb) { cb = Util.once(Util.mkAsync(cb || function () {})); - cache.getItem(id, function (err, obj) { - if (err || !obj || !Array.isArray(obj.c)) { - return void cb(err || 'EINVAL'); - } - cb(null, obj); - obj.t = +new Date(); - cache.setItem(id, obj); + + 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'); + } + cb(null, obj); + obj.t = +new Date(); + cache.setItem(id, obj); + }); }); }; @@ -64,27 +93,39 @@ define([ S.storeCache = function (id, validateKey, val, cb) { cb = Util.once(Util.mkAsync(cb || function () {})); - if (!Array.isArray(val) || !validateKey) { return void cb('EINVAL'); } - checkCheckpoints(val); - cache.setItem(id, { - k: validateKey, - c: val, - t: (+new Date()) // 't' represent the "lastAccess" of this cache (get or set) - }, function (err) { - cb(err); + + onReady.reg(function (allowed) { + if (!allowed) { return void cb('NOCACHE'); } + if (!Array.isArray(val) || !validateKey) { return void cb('EINVAL'); } + checkCheckpoints(val); + cache.setItem(id, { + k: validateKey, + c: val, + t: (+new Date()) // 't' represent the "lastAccess" of this cache (get or set) + }, function (err) { + cb(err); + }); }); }; S.clearChannel = function (id, cb) { 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) { 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; diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index a03f61730..44cbb3e3c 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -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}); }