From a7a922680de13d81a817a4ba3c147551367b656b Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 20 Oct 2020 17:02:34 +0200 Subject: [PATCH] request persisted storage when logging in/restoring a session --- src/matrix/storage/idb/StorageFactory.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/matrix/storage/idb/StorageFactory.js b/src/matrix/storage/idb/StorageFactory.js index 9234ba7a..b4411469 100644 --- a/src/matrix/storage/idb/StorageFactory.js +++ b/src/matrix/storage/idb/StorageFactory.js @@ -22,6 +22,21 @@ import { schema } from "./schema.js"; const sessionName = sessionId => `hydrogen_session_${sessionId}`; const openDatabaseWithSessionId = sessionId => openDatabase(sessionName(sessionId), createStores, schema.length); +async function requestPersistedStorage() { + if (navigator?.storage?.persist) { + return await navigator.storage.persist(); + } else if (document.requestStorageAccess) { + try { + await document.requestStorageAccess(); + return true; + } catch (err) { + return false; + } + } else { + return false; + } +} + export class StorageFactory { constructor(serviceWorkerHandler) { this._serviceWorkerHandler = serviceWorkerHandler; @@ -29,6 +44,12 @@ export class StorageFactory { async create(sessionId) { await this._serviceWorkerHandler?.preventConcurrentSessionAccess(sessionId); + requestPersistedStorage().then(persisted => { + // Firefox lies here though, and returns true even if the user denied the request + if (!persisted) { + console.warn("no persisted storage, database can be evicted by browser"); + } + }); const db = await openDatabaseWithSessionId(sessionId); return new Storage(db); }