diff options
author | Sebastian <sebasjm@gmail.com> | 2023-04-18 10:47:53 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-04-18 10:47:53 -0300 |
commit | b34f3568e8c9dde73f11583a6b46ef1509990397 (patch) | |
tree | 2898223d8e67023dbfd2d338eb145bd06b737a71 /packages/taler-wallet-webextension/src/wxBackend.ts | |
parent | b1a0d034fc1be0824e1eac46661604558264beee (diff) | |
download | wallet-core-b34f3568e8c9dde73f11583a6b46ef1509990397.tar.xz |
check if the script should be injected
remove header listener
Diffstat (limited to 'packages/taler-wallet-webextension/src/wxBackend.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/wxBackend.ts | 222 |
1 files changed, 103 insertions, 119 deletions
diff --git a/packages/taler-wallet-webextension/src/wxBackend.ts b/packages/taler-wallet-webextension/src/wxBackend.ts index c50412053..bf91e8521 100644 --- a/packages/taler-wallet-webextension/src/wxBackend.ts +++ b/packages/taler-wallet-webextension/src/wxBackend.ts @@ -24,42 +24,39 @@ * Imports. */ import { - classifyTalerUri, - Logger, LogLevel, - setGlobalLogLevelFromString, - setLogLevelFromString, + Logger, TalerErrorCode, - TalerUriType, WalletDiagnostics, - makeErrorDetail, getErrorDetailFromException, - parseTalerUri, - TalerUriAction, + makeErrorDetail, + setGlobalLogLevelFromString, + setLogLevelFromString, } from "@gnu-taler/taler-util"; import { DbAccess, - deleteTalerDatabase, - exportDb, - importDb, OpenedPromise, - openPromise, - openTalerDatabase, SetTimeoutTimerAPI, Wallet, WalletOperations, WalletStoresV1, + deleteTalerDatabase, + exportDb, + importDb, + openPromise, + openTalerDatabase, } from "@gnu-taler/taler-wallet-core"; import { BrowserHttpLib } from "./browserHttpLib.js"; -import { platform } from "./platform/background.js"; import { MessageFromBackend, MessageFromFrontend, MessageResponse, } from "./platform/api.js"; +import { platform } from "./platform/background.js"; import { SynchronousCryptoWorkerFactory } from "./serviceWorkerCryptoWorkerFactory.js"; import { ServiceWorkerHttpLib } from "./serviceWorkerHttpLib.js"; -import { BackgroundOperations, ExtendedPermissionsResponse } from "./wxApi.js"; +import { ExtensionOperations } from "./taler-wallet-interaction-loader.js"; +import { BackgroundOperations } from "./wxApi.js"; /** * Currently active wallet instance. Might be unloaded and @@ -123,10 +120,11 @@ type BackendHandlerType = { ) => Promise<BackgroundOperations[Op]["response"]>; }; -async function containsHeaderListener(): Promise<ExtendedPermissionsResponse> { - const result = await platform.containsTalerHeaderListener(); - return { newValue: result }; -} +type ExtensionHandlerType = { + [Op in keyof ExtensionOperations]: ( + req: ExtensionOperations[Op]["request"], + ) => Promise<ExtensionOperations[Op]["response"]>; +}; async function resetDb(): Promise<void> { await deleteTalerDatabase(indexedDB as any); @@ -153,20 +151,6 @@ async function runGarbageCollector(): Promise<void> { logger.info("imported"); } -async function toggleHeaderListener( - newVal: boolean, -): Promise<ExtendedPermissionsResponse> { - logger.trace("new extended permissions value", newVal); - if (newVal) { - platform.registerTalerHeaderListener(parseTalerUriAndRedirect); - return { newValue: true }; - } - - const rem = await platform.getPermissionsApi().removeHostPermissions(); - logger.trace("permissions removed:", rem); - return { newValue: false }; -} - function freeze(time: number): Promise<void> { return new Promise((res, rej) => { setTimeout(res, time); @@ -177,14 +161,21 @@ async function sum(ns: Array<number>): Promise<number> { return ns.reduce((prev, cur) => prev + cur, 0); } +const extensionHandlers: ExtensionHandlerType = { + isInjectionEnabled, +}; + +async function isInjectionEnabled(): Promise<boolean> { + const settings = await platform.getSettingsFromStorage(); + return settings.injectTalerSupport === true; +} + const backendHandlers: BackendHandlerType = { freeze, sum, - containsHeaderListener, getDiagnostics, resetDb, runGarbageCollector, - toggleHeaderListener, setLoggingLevel, }; @@ -203,55 +194,85 @@ async function setLoggingLevel({ } } -async function dispatch<Op extends WalletOperations | BackgroundOperations>( - req: MessageFromFrontend<Op> & { id: string }, -): Promise<MessageResponse> { - if (req.channel === "background") { - const handler = backendHandlers[req.operation] as (req: any) => any; - if (!handler) { - return { - type: "error", - id: req.id, - operation: String(req.operation), - error: getErrorDetailFromException( - Error(`unknown background operation`), - ), - }; +async function dispatch< + Op extends WalletOperations | BackgroundOperations | ExtensionOperations, +>(req: MessageFromFrontend<Op> & { id: string }): Promise<MessageResponse> { + switch (req.channel) { + case "background": { + const handler = backendHandlers[req.operation] as (req: any) => any; + if (!handler) { + return { + type: "error", + id: req.id, + operation: String(req.operation), + error: getErrorDetailFromException( + Error(`unknown background operation`), + ), + }; + } + try { + const result = await handler(req.payload); + return { + type: "response", + id: req.id, + operation: String(req.operation), + result, + }; + } catch (er) { + return { + type: "error", + id: req.id, + error: getErrorDetailFromException(er), + operation: String(req.operation), + }; + } } - try { - const result = await handler(req.payload); - return { - type: "response", - id: req.id, - operation: String(req.operation), - result, - }; - } catch (er) { - return { - type: "error", - id: req.id, - error: getErrorDetailFromException(er), - operation: String(req.operation), - }; + case "extension": { + const handler = extensionHandlers[req.operation] as (req: any) => any; + if (!handler) { + return { + type: "error", + id: req.id, + operation: String(req.operation), + error: getErrorDetailFromException( + Error(`unknown extension operation`), + ), + }; + } + try { + const result = await handler(req.payload); + return { + type: "response", + id: req.id, + operation: String(req.operation), + result, + }; + } catch (er) { + return { + type: "error", + id: req.id, + error: getErrorDetailFromException(er), + operation: String(req.operation), + }; + } } - } + case "wallet": { + const w = currentWallet; + if (!w) { + return { + type: "error", + id: req.id, + operation: req.operation, + error: makeErrorDetail( + TalerErrorCode.WALLET_CORE_NOT_AVAILABLE, + {}, + "wallet core not available", + ), + }; + } - if (req.channel === "wallet") { - const w = currentWallet; - if (!w) { - return { - type: "error", - id: req.id, - operation: req.operation, - error: makeErrorDetail( - TalerErrorCode.WALLET_CORE_NOT_AVAILABLE, - {}, - "wallet core not available", - ), - }; + return await w.handleCoreApiRequest(req.operation, req.id, req.payload); } - - return await w.handleCoreApiRequest(req.operation, req.id, req.payload); } const anyReq = req as any; @@ -261,7 +282,7 @@ async function dispatch<Op extends WalletOperations | BackgroundOperations>( operation: String(anyReq.operation), error: getErrorDetailFromException( Error( - `unknown channel ${anyReq.channel}, should be "background" or "wallet"`, + `unknown channel ${anyReq.channel}, should be "background", "extension" or "wallet"`, ), ), }; @@ -330,23 +351,6 @@ async function reinitWallet(): Promise<void> { return walletInit.resolve(); } -function parseTalerUriAndRedirect(tabId: number, maybeTalerUri: string): void { - const talerUri = maybeTalerUri.startsWith("ext+") - ? maybeTalerUri.substring(4) - : maybeTalerUri; - const uri = parseTalerUri(talerUri); - if (!uri) { - logger.warn( - `Response with HTTP 402 the Taler header but could not classify ${talerUri}`, - ); - return; - } - return platform.redirectTabToWalletPage( - tabId, - `/taler-uri/${encodeURIComponent(talerUri)}`, - ); -} - /** * Main function to run for the WebExtension backend. * @@ -370,30 +374,10 @@ export async function wxMain(): Promise<void> { platform.registerAllIncomingConnections(); try { - platform.registerOnInstalled(() => { + await platform.registerOnInstalled(() => { platform.openWalletPage("/welcome"); - - // - try { - platform.registerTalerHeaderListener(parseTalerUriAndRedirect); - } catch (e) { - logger.error("could not register header listener", e); - } }); } catch (e) { console.error(e); } - - // On platforms that support it, also listen to external - // modification of permissions. - platform.getPermissionsApi().addPermissionsListener((perm, lastError) => { - if (lastError) { - logger.error( - `there was a problem trying to get permission ${perm}`, - lastError, - ); - return; - } - platform.registerTalerHeaderListener(parseTalerUriAndRedirect); - }); } |