DB reset must be async

This commit is contained in:
Florian Dold 2022-02-03 14:36:37 +01:00
parent 14d18065f9
commit 5844a3065c
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 55 additions and 42 deletions

View File

@ -168,6 +168,12 @@ export async function openTalerDatabase(
return new DbAccess(mainDbHandle, WalletStoresV1);
}
export function deleteTalerDatabase(idbFactory: IDBFactory): void {
idbFactory.deleteDatabase(TALER_DB_NAME);
export async function deleteTalerDatabase(
idbFactory: IDBFactory,
): Promise<void> {
return new Promise((resolve, reject) => {
const req = idbFactory.deleteDatabase(TALER_DB_NAME);
req.onerror = () => reject(req.error);
req.onsuccess = () => resolve();
});
}

View File

@ -30,12 +30,17 @@ import {
NotificationType,
TalerErrorCode,
TalerUriType,
WalletDiagnostics
WalletDiagnostics,
} from "@gnu-taler/taler-util";
import {
DbAccess, deleteTalerDatabase, makeErrorDetails, OpenedPromise,
DbAccess,
deleteTalerDatabase,
makeErrorDetails,
OpenedPromise,
openPromise,
openTalerDatabase, Wallet, WalletStoresV1
openTalerDatabase,
Wallet,
WalletStoresV1,
} from "@gnu-taler/taler-wallet-core";
import { BrowserCryptoWorkerFactory } from "./browserCryptoWorkerFactory";
import { BrowserHttpLib } from "./browserHttpLib";
@ -55,7 +60,7 @@ let currentWallet: Wallet | undefined;
let currentDatabase: DbAccess<typeof WalletStoresV1> | undefined;
/**
* Last version if an outdated DB, if applicable.
* Last version of an outdated DB, if applicable.
*/
let outdatedDbVersion: number | undefined;
@ -128,9 +133,12 @@ async function dispatch(
}
case "wxGetExtendedPermissions": {
const res = await new Promise((resolve, reject) => {
getPermissionsApi().contains(getReadRequestPermissions(), (result: boolean) => {
getPermissionsApi().contains(
getReadRequestPermissions(),
(result: boolean) => {
resolve(result);
});
},
);
});
r = wrapResponse({ newValue: res });
break;
@ -257,19 +265,15 @@ async function reinitWallet(): Promise<void> {
let cryptoWorker;
if (chrome.runtime.getManifest().manifest_version === 3) {
httpLib = new ServiceWorkerHttpLib()
httpLib = new ServiceWorkerHttpLib();
cryptoWorker = new SynchronousCryptoWorkerFactory();
} else {
httpLib = new BrowserHttpLib()
cryptoWorker = new BrowserCryptoWorkerFactory()
httpLib = new BrowserHttpLib();
cryptoWorker = new BrowserCryptoWorkerFactory();
}
console.log("setting wallet");
const wallet = await Wallet.create(
currentDatabase,
httpLib,
cryptoWorker,
);
const wallet = await Wallet.create(currentDatabase, httpLib, cryptoWorker);
try {
await wallet.handleCoreApiRequest("initWallet", "native-init", {});
} catch (e) {
@ -402,7 +406,9 @@ function setupHeaderListener(): void {
// }
console.log("setting up header listener");
// Handlers for catching HTTP requests
getPermissionsApi().contains(getReadRequestPermissions(), (result: boolean) => {
getPermissionsApi().contains(
getReadRequestPermissions(),
(result: boolean) => {
if (
"webRequest" in chrome &&
"onHeadersReceived" in chrome.webRequest &&
@ -415,7 +421,7 @@ function setupHeaderListener(): void {
chrome.webRequest.onHeadersReceived.addListener(
headerListener,
{ urls: ["<all_urls>"] },
["responseHeaders"]
["responseHeaders"],
);
}
if ("webRequest" in chrome) {
@ -425,7 +431,8 @@ function setupHeaderListener(): void {
}
});
}
});
},
);
}
/**
@ -447,7 +454,7 @@ export async function wxMain(): Promise<void> {
chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
afterWalletIsInitialized.then(() => {
dispatch(req, sender, sendResponse);
})
});
return true;
});