2019-03-25 17:43:53 +00:00
|
|
|
/*
|
2021-04-06 11:26:50 +00:00
|
|
|
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
|
2019-03-25 17:43:53 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { LocalStorageCryptoStore } from 'matrix-js-sdk/src/crypto/store/localStorage-crypto-store';
|
|
|
|
import { IndexedDBStore } from "matrix-js-sdk/src/store/indexeddb";
|
|
|
|
import { IndexedDBCryptoStore } from "matrix-js-sdk/src/crypto/store/indexeddb-crypto-store";
|
2021-09-21 15:48:09 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
2021-10-22 22:23:32 +00:00
|
|
|
import Analytics from '../Analytics';
|
|
|
|
|
2019-03-25 17:43:53 +00:00
|
|
|
const localStorage = window.localStorage;
|
|
|
|
|
|
|
|
// just *accessing* indexedDB throws an exception in firefox with
|
|
|
|
// indexeddb disabled.
|
2022-02-16 19:32:38 +00:00
|
|
|
let indexedDB;
|
2019-03-25 17:43:53 +00:00
|
|
|
try {
|
|
|
|
indexedDB = window.indexedDB;
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
// The JS SDK will add a prefix of "matrix-js-sdk:" to the sync store name.
|
|
|
|
const SYNC_STORE_NAME = "riot-web-sync";
|
|
|
|
const CRYPTO_STORE_NAME = "matrix-js-sdk:crypto";
|
|
|
|
|
2021-04-26 14:30:34 +00:00
|
|
|
function log(msg: string) {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`StorageManager: ${msg}`);
|
2019-03-25 17:43:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 14:30:34 +00:00
|
|
|
function error(msg: string, ...args: string[]) {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error(`StorageManager: ${msg}`, ...args);
|
2019-03-25 17:43:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 14:30:34 +00:00
|
|
|
function track(action: string) {
|
2019-03-26 16:36:12 +00:00
|
|
|
Analytics.trackEvent("StorageManager", action);
|
|
|
|
}
|
|
|
|
|
2020-02-20 00:38:08 +00:00
|
|
|
export function tryPersistStorage() {
|
|
|
|
if (navigator.storage && navigator.storage.persist) {
|
|
|
|
navigator.storage.persist().then(persistent => {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log("StorageManager: Persistent?", persistent);
|
2020-02-20 00:38:08 +00:00
|
|
|
});
|
2020-03-25 17:56:35 +00:00
|
|
|
} else if (document.requestStorageAccess) { // Safari
|
2020-03-25 11:04:09 +00:00
|
|
|
document.requestStorageAccess().then(
|
2021-09-21 15:48:09 +00:00
|
|
|
() => logger.log("StorageManager: Persistent?", true),
|
|
|
|
() => logger.log("StorageManager: Persistent?", false),
|
2020-03-25 11:04:09 +00:00
|
|
|
);
|
2020-02-20 00:38:08 +00:00
|
|
|
} else {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log("StorageManager: Persistence unsupported");
|
2020-02-20 00:38:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 17:43:53 +00:00
|
|
|
export async function checkConsistency() {
|
|
|
|
log("Checking storage consistency");
|
|
|
|
log(`Local storage supported? ${!!localStorage}`);
|
|
|
|
log(`IndexedDB supported? ${!!indexedDB}`);
|
|
|
|
|
|
|
|
let dataInLocalStorage = false;
|
|
|
|
let dataInCryptoStore = false;
|
2019-05-15 12:47:48 +00:00
|
|
|
let cryptoInited = false;
|
2019-03-25 17:43:53 +00:00
|
|
|
let healthy = true;
|
|
|
|
|
|
|
|
if (localStorage) {
|
|
|
|
dataInLocalStorage = localStorage.length > 0;
|
|
|
|
log(`Local storage contains data? ${dataInLocalStorage}`);
|
2019-05-15 12:47:48 +00:00
|
|
|
|
2021-04-06 11:26:50 +00:00
|
|
|
cryptoInited = !!localStorage.getItem("mx_crypto_initialised");
|
2019-05-15 12:47:48 +00:00
|
|
|
log(`Crypto initialised? ${cryptoInited}`);
|
2019-03-25 17:43:53 +00:00
|
|
|
} else {
|
|
|
|
healthy = false;
|
|
|
|
error("Local storage cannot be used on this browser");
|
2019-03-26 16:36:12 +00:00
|
|
|
track("Local storage disabled");
|
2019-03-25 17:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (indexedDB && localStorage) {
|
2019-03-28 12:40:38 +00:00
|
|
|
const results = await checkSyncStore();
|
|
|
|
if (!results.healthy) {
|
2019-03-27 15:48:38 +00:00
|
|
|
healthy = false;
|
|
|
|
}
|
2019-03-25 17:43:53 +00:00
|
|
|
} else {
|
|
|
|
healthy = false;
|
|
|
|
error("Sync store cannot be used on this browser");
|
2019-03-26 16:36:12 +00:00
|
|
|
track("Sync store disabled");
|
2019-03-25 17:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (indexedDB) {
|
2019-03-28 12:27:33 +00:00
|
|
|
const results = await checkCryptoStore();
|
|
|
|
dataInCryptoStore = results.exists;
|
|
|
|
if (!results.healthy) {
|
2019-03-27 15:48:38 +00:00
|
|
|
healthy = false;
|
|
|
|
}
|
2019-03-25 17:43:53 +00:00
|
|
|
} else {
|
|
|
|
healthy = false;
|
|
|
|
error("Crypto store cannot be used on this browser");
|
2019-03-26 16:36:12 +00:00
|
|
|
track("Crypto store disabled");
|
2019-03-25 17:43:53 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 12:47:48 +00:00
|
|
|
if (dataInLocalStorage && cryptoInited && !dataInCryptoStore) {
|
2019-03-25 17:43:53 +00:00
|
|
|
healthy = false;
|
|
|
|
error(
|
2019-05-15 12:47:48 +00:00
|
|
|
"Data exists in local storage and crypto is marked as initialised " +
|
|
|
|
" but no data found in crypto store. " +
|
2019-03-25 17:43:53 +00:00
|
|
|
"IndexedDB storage has likely been evicted by the browser!",
|
|
|
|
);
|
2019-03-26 16:36:12 +00:00
|
|
|
track("Crypto store evicted");
|
2019-03-25 17:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (healthy) {
|
|
|
|
log("Storage consistency checks passed");
|
2019-03-26 16:36:12 +00:00
|
|
|
track("Consistency checks passed");
|
2019-03-25 17:43:53 +00:00
|
|
|
} else {
|
|
|
|
error("Storage consistency checks failed");
|
2019-03-26 16:36:12 +00:00
|
|
|
track("Consistency checks failed");
|
2019-03-25 17:43:53 +00:00
|
|
|
}
|
2019-03-28 16:22:17 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
dataInLocalStorage,
|
|
|
|
dataInCryptoStore,
|
2019-05-15 12:47:48 +00:00
|
|
|
cryptoInited,
|
2019-03-28 16:22:17 +00:00
|
|
|
healthy,
|
|
|
|
};
|
2019-03-25 17:43:53 +00:00
|
|
|
}
|
2019-03-28 12:27:33 +00:00
|
|
|
|
2019-03-28 12:40:38 +00:00
|
|
|
async function checkSyncStore() {
|
|
|
|
let exists = false;
|
|
|
|
try {
|
2021-03-19 02:50:34 +00:00
|
|
|
exists = await IndexedDBStore.exists(
|
2019-03-28 12:40:38 +00:00
|
|
|
indexedDB, SYNC_STORE_NAME,
|
|
|
|
);
|
|
|
|
log(`Sync store using IndexedDB contains data? ${exists}`);
|
|
|
|
return { exists, healthy: true };
|
|
|
|
} catch (e) {
|
|
|
|
error("Sync store using IndexedDB inaccessible", e);
|
|
|
|
track("Sync store using IndexedDB inaccessible");
|
|
|
|
}
|
|
|
|
log("Sync store using memory only");
|
|
|
|
return { exists, healthy: false };
|
|
|
|
}
|
|
|
|
|
2019-03-28 12:27:33 +00:00
|
|
|
async function checkCryptoStore() {
|
|
|
|
let exists = false;
|
|
|
|
try {
|
2021-03-19 02:50:34 +00:00
|
|
|
exists = await IndexedDBCryptoStore.exists(
|
2019-03-28 12:27:33 +00:00
|
|
|
indexedDB, CRYPTO_STORE_NAME,
|
|
|
|
);
|
|
|
|
log(`Crypto store using IndexedDB contains data? ${exists}`);
|
|
|
|
return { exists, healthy: true };
|
|
|
|
} catch (e) {
|
|
|
|
error("Crypto store using IndexedDB inaccessible", e);
|
|
|
|
track("Crypto store using IndexedDB inaccessible");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
exists = await LocalStorageCryptoStore.exists(localStorage);
|
|
|
|
log(`Crypto store using local storage contains data? ${exists}`);
|
|
|
|
return { exists, healthy: true };
|
|
|
|
} catch (e) {
|
|
|
|
error("Crypto store using local storage inaccessible", e);
|
|
|
|
track("Crypto store using local storage inaccessible");
|
|
|
|
}
|
2019-03-28 12:40:38 +00:00
|
|
|
log("Crypto store using memory only");
|
2019-03-28 12:27:33 +00:00
|
|
|
return { exists, healthy: false };
|
|
|
|
}
|
2019-04-04 10:59:53 +00:00
|
|
|
|
|
|
|
export function trackStores(client) {
|
|
|
|
if (client.store && client.store.on) {
|
|
|
|
client.store.on("degraded", () => {
|
|
|
|
track("Sync store using IndexedDB degraded to memory");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 12:47:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether crypto has ever been successfully
|
|
|
|
* initialised on this client.
|
|
|
|
* StorageManager uses this to determine whether indexeddb
|
|
|
|
* has been wiped by the browser: this flag is saved to localStorage
|
|
|
|
* and if it is true and not crypto data is found, an error is
|
|
|
|
* presented to the user.
|
2019-05-15 13:00:37 +00:00
|
|
|
*
|
|
|
|
* @param {bool} cryptoInited True if crypto has been set up
|
2019-05-15 12:47:48 +00:00
|
|
|
*/
|
|
|
|
export function setCryptoInitialised(cryptoInited) {
|
|
|
|
localStorage.setItem("mx_crypto_initialised", cryptoInited);
|
|
|
|
}
|
2020-12-11 02:52:18 +00:00
|
|
|
|
|
|
|
/* Simple wrapper functions around IndexedDB.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 19:32:38 +00:00
|
|
|
let idb = null;
|
2020-12-11 02:52:18 +00:00
|
|
|
|
|
|
|
async function idbInit(): Promise<void> {
|
|
|
|
if (!indexedDB) {
|
|
|
|
throw new Error("IndexedDB not available");
|
|
|
|
}
|
|
|
|
idb = await new Promise((resolve, reject) => {
|
|
|
|
const request = indexedDB.open("matrix-react-sdk", 1);
|
|
|
|
request.onerror = reject;
|
|
|
|
request.onsuccess = (event) => { resolve(request.result); };
|
|
|
|
request.onupgradeneeded = (event) => {
|
|
|
|
const db = request.result;
|
|
|
|
db.createObjectStore("pickleKey");
|
|
|
|
db.createObjectStore("account");
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function idbLoad(
|
|
|
|
table: string,
|
|
|
|
key: string | string[],
|
|
|
|
): Promise<any> {
|
|
|
|
if (!idb) {
|
|
|
|
await idbInit();
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const txn = idb.transaction([table], "readonly");
|
|
|
|
txn.onerror = reject;
|
|
|
|
|
|
|
|
const objectStore = txn.objectStore(table);
|
|
|
|
const request = objectStore.get(key);
|
|
|
|
request.onerror = reject;
|
|
|
|
request.onsuccess = (event) => { resolve(request.result); };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function idbSave(
|
|
|
|
table: string,
|
|
|
|
key: string | string[],
|
|
|
|
data: any,
|
|
|
|
): Promise<void> {
|
|
|
|
if (!idb) {
|
|
|
|
await idbInit();
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const txn = idb.transaction([table], "readwrite");
|
|
|
|
txn.onerror = reject;
|
|
|
|
|
|
|
|
const objectStore = txn.objectStore(table);
|
|
|
|
const request = objectStore.put(data, key);
|
|
|
|
request.onerror = reject;
|
|
|
|
request.onsuccess = (event) => { resolve(); };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function idbDelete(
|
|
|
|
table: string,
|
|
|
|
key: string | string[],
|
|
|
|
): Promise<void> {
|
|
|
|
if (!idb) {
|
|
|
|
await idbInit();
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const txn = idb.transaction([table], "readwrite");
|
|
|
|
txn.onerror = reject;
|
|
|
|
|
|
|
|
const objectStore = txn.objectStore(table);
|
|
|
|
const request = objectStore.delete(key);
|
|
|
|
request.onerror = reject;
|
|
|
|
request.onsuccess = (event) => { resolve(); };
|
|
|
|
});
|
|
|
|
}
|