From 5844a3065c8de9a676a19d13d8cf455f67b4ca10 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 3 Feb 2022 14:36:37 +0100 Subject: [PATCH] DB reset must be async --- packages/taler-wallet-core/src/db-utils.ts | 10 ++- .../src/wxBackend.ts | 87 ++++++++++--------- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/packages/taler-wallet-core/src/db-utils.ts b/packages/taler-wallet-core/src/db-utils.ts index 336c7de27..af337d573 100644 --- a/packages/taler-wallet-core/src/db-utils.ts +++ b/packages/taler-wallet-core/src/db-utils.ts @@ -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 { + return new Promise((resolve, reject) => { + const req = idbFactory.deleteDatabase(TALER_DB_NAME); + req.onerror = () => reject(req.error); + req.onsuccess = () => resolve(); + }); } diff --git a/packages/taler-wallet-webextension/src/wxBackend.ts b/packages/taler-wallet-webextension/src/wxBackend.ts index 085d7045e..a242e8a08 100644 --- a/packages/taler-wallet-webextension/src/wxBackend.ts +++ b/packages/taler-wallet-webextension/src/wxBackend.ts @@ -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 | 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) => { - resolve(result); - }); + getPermissionsApi().contains( + getReadRequestPermissions(), + (result: boolean) => { + resolve(result); + }, + ); }); r = wrapResponse({ newValue: res }); break; @@ -257,19 +265,15 @@ async function reinitWallet(): Promise { 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,30 +406,33 @@ function setupHeaderListener(): void { // } console.log("setting up header listener"); // Handlers for catching HTTP requests - getPermissionsApi().contains(getReadRequestPermissions(), (result: boolean) => { - if ( - "webRequest" in chrome && - "onHeadersReceived" in chrome.webRequest && - chrome.webRequest.onHeadersReceived.hasListener(headerListener) - ) { - chrome.webRequest.onHeadersReceived.removeListener(headerListener); - } - if (result) { - console.log("actually adding listener"); - chrome.webRequest.onHeadersReceived.addListener( - headerListener, - { urls: [""] }, - ["responseHeaders"] - ); - } - if ("webRequest" in chrome) { - chrome.webRequest.handlerBehaviorChanged(() => { - if (chrome.runtime.lastError) { - console.error(chrome.runtime.lastError); - } - }); - } - }); + getPermissionsApi().contains( + getReadRequestPermissions(), + (result: boolean) => { + if ( + "webRequest" in chrome && + "onHeadersReceived" in chrome.webRequest && + chrome.webRequest.onHeadersReceived.hasListener(headerListener) + ) { + chrome.webRequest.onHeadersReceived.removeListener(headerListener); + } + if (result) { + console.log("actually adding listener"); + chrome.webRequest.onHeadersReceived.addListener( + headerListener, + { urls: [""] }, + ["responseHeaders"], + ); + } + if ("webRequest" in chrome) { + chrome.webRequest.handlerBehaviorChanged(() => { + if (chrome.runtime.lastError) { + console.error(chrome.runtime.lastError); + } + }); + } + }, + ); } /** @@ -447,7 +454,7 @@ export async function wxMain(): Promise { chrome.runtime.onMessage.addListener((req, sender, sendResponse) => { afterWalletIsInitialized.then(() => { dispatch(req, sender, sendResponse); - }) + }); return true; });