2019-03-25 17:43:53 +00:00
|
|
|
/*
|
2021-04-06 12:26:50 +01: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-06-24 10:14:42 +01:00
|
|
|
import { IndexedDBStore, IndexedDBCryptoStore } from "matrix-js-sdk/src/matrix";
|
2021-09-21 17:48:09 +02:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2024-05-02 16:19:55 -06:00
|
|
|
import { getIDBFactory } from "./StorageAccess";
|
2024-02-01 16:42:14 +01:00
|
|
|
|
2019-03-25 17:43:53 +00:00
|
|
|
const localStorage = window.localStorage;
|
|
|
|
|
|
|
|
|
|
// The JS SDK will add a prefix of "matrix-js-sdk:" to the sync store name.
|
|
|
|
|
const SYNC_STORE_NAME = "riot-web-sync";
|
2024-02-01 16:42:14 +01:00
|
|
|
const LEGACY_CRYPTO_STORE_NAME = "matrix-js-sdk:crypto";
|
|
|
|
|
const RUST_CRYPTO_STORE_NAME = "matrix-js-sdk::matrix-sdk-crypto";
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
function log(msg: string): void {
|
2021-09-21 17:48:09 +02:00
|
|
|
logger.log(`StorageManager: ${msg}`);
|
2019-03-25 17:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 17:39:56 +01:00
|
|
|
function error(msg: string, ...args: any[]): void {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error(`StorageManager: ${msg}`, ...args);
|
2019-03-25 17:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export function tryPersistStorage(): void {
|
2020-02-20 00:38:08 +00:00
|
|
|
if (navigator.storage && navigator.storage.persist) {
|
|
|
|
|
navigator.storage.persist().then((persistent) => {
|
2021-09-21 17:48:09 +02: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 12:04:09 +01:00
|
|
|
document.requestStorageAccess().then(
|
2021-09-21 17:48:09 +02:00
|
|
|
() => logger.log("StorageManager: Persistent?", true),
|
|
|
|
|
() => logger.log("StorageManager: Persistent?", false),
|
2020-03-25 12:04:09 +01:00
|
|
|
);
|
2020-02-20 00:38:08 +00:00
|
|
|
} else {
|
2021-09-21 17:48:09 +02:00
|
|
|
logger.log("StorageManager: Persistence unsupported");
|
2020-02-20 00:38:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export async function checkConsistency(): Promise<{
|
|
|
|
|
healthy: boolean;
|
|
|
|
|
cryptoInited: boolean;
|
|
|
|
|
dataInCryptoStore: boolean;
|
|
|
|
|
dataInLocalStorage: boolean;
|
|
|
|
|
}> {
|
2019-03-25 17:43:53 +00:00
|
|
|
log("Checking storage consistency");
|
|
|
|
|
log(`Local storage supported? ${!!localStorage}`);
|
2024-05-02 16:19:55 -06:00
|
|
|
log(`IndexedDB supported? ${!!getIDBFactory()}`);
|
2019-03-25 17:43:53 +00:00
|
|
|
|
|
|
|
|
let dataInLocalStorage = false;
|
|
|
|
|
let dataInCryptoStore = false;
|
2019-05-15 13:47:48 +01: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 13:47:48 +01:00
|
|
|
|
2021-04-06 12:26:50 +01:00
|
|
|
cryptoInited = !!localStorage.getItem("mx_crypto_initialised");
|
2019-05-15 13:47:48 +01: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");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 16:19:55 -06:00
|
|
|
if (getIDBFactory() && 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");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 16:19:55 -06:00
|
|
|
if (getIDBFactory()) {
|
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-05-15 13:47:48 +01:00
|
|
|
if (dataInLocalStorage && cryptoInited && !dataInCryptoStore) {
|
2019-03-25 17:43:53 +00:00
|
|
|
healthy = false;
|
|
|
|
|
error(
|
2019-05-15 13:47:48 +01: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!",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (healthy) {
|
|
|
|
|
log("Storage consistency checks passed");
|
|
|
|
|
} else {
|
|
|
|
|
error("Storage consistency checks failed");
|
|
|
|
|
}
|
2019-03-28 16:22:17 +00:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
dataInLocalStorage,
|
|
|
|
|
dataInCryptoStore,
|
2019-05-15 13:47:48 +01: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
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
interface StoreCheck {
|
|
|
|
|
exists: boolean;
|
|
|
|
|
healthy: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function checkSyncStore(): Promise<StoreCheck> {
|
2019-03-28 12:40:38 +00:00
|
|
|
let exists = false;
|
|
|
|
|
try {
|
2024-05-02 16:19:55 -06:00
|
|
|
exists = await IndexedDBStore.exists(getIDBFactory()!, SYNC_STORE_NAME);
|
2019-03-28 12:40:38 +00:00
|
|
|
log(`Sync store using IndexedDB contains data? ${exists}`);
|
|
|
|
|
return { exists, healthy: true };
|
|
|
|
|
} catch (e) {
|
|
|
|
|
error("Sync store using IndexedDB inaccessible", e);
|
|
|
|
|
}
|
|
|
|
|
log("Sync store using memory only");
|
|
|
|
|
return { exists, healthy: false };
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
async function checkCryptoStore(): Promise<StoreCheck> {
|
2024-06-24 10:14:42 +01:00
|
|
|
// check first if there is a rust crypto store
|
|
|
|
|
try {
|
|
|
|
|
const rustDbExists = await IndexedDBCryptoStore.exists(getIDBFactory()!, RUST_CRYPTO_STORE_NAME);
|
|
|
|
|
log(`Rust Crypto store using IndexedDB contains data? ${rustDbExists}`);
|
2024-02-05 18:57:12 +01:00
|
|
|
|
2024-06-24 10:14:42 +01:00
|
|
|
if (rustDbExists) {
|
|
|
|
|
// There was an existing rust database, so consider it healthy.
|
|
|
|
|
return { exists: true, healthy: true };
|
|
|
|
|
} else {
|
|
|
|
|
// No rust store, so let's check if there is a legacy store not yet migrated.
|
|
|
|
|
try {
|
|
|
|
|
const legacyIdbExists = await IndexedDBCryptoStore.existsAndIsNotMigrated(
|
|
|
|
|
getIDBFactory()!,
|
|
|
|
|
LEGACY_CRYPTO_STORE_NAME,
|
|
|
|
|
);
|
|
|
|
|
log(`Legacy Crypto store using IndexedDB contains non migrated data? ${legacyIdbExists}`);
|
|
|
|
|
return { exists: legacyIdbExists, healthy: true };
|
|
|
|
|
} catch (e) {
|
|
|
|
|
error("Legacy crypto store using IndexedDB inaccessible", e);
|
2024-02-05 18:57:12 +01:00
|
|
|
}
|
2024-06-24 10:14:42 +01:00
|
|
|
|
|
|
|
|
// No need to check local storage or memory as rust stack doesn't support them.
|
|
|
|
|
// Given that rust stack requires indexeddb, set healthy to false.
|
2024-02-05 18:57:12 +01:00
|
|
|
return { exists: false, healthy: false };
|
|
|
|
|
}
|
2024-06-24 10:14:42 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
error("Rust crypto store using IndexedDB inaccessible", e);
|
|
|
|
|
return { exists: false, healthy: false };
|
2019-03-28 12:27:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-04 11:59:53 +01:00
|
|
|
|
2019-05-15 13:47:48 +01: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 14:00:37 +01:00
|
|
|
*
|
2022-05-10 18:09:31 +01:00
|
|
|
* @param {boolean} cryptoInited True if crypto has been set up
|
2019-05-15 13:47:48 +01:00
|
|
|
*/
|
2023-01-12 13:25:14 +00:00
|
|
|
export function setCryptoInitialised(cryptoInited: boolean): void {
|
2022-05-10 18:09:31 +01:00
|
|
|
localStorage.setItem("mx_crypto_initialised", String(cryptoInited));
|
2019-05-15 13:47:48 +01:00
|
|
|
}
|