diff options
author | Sebastian <sebasjm@gmail.com> | 2023-01-04 15:44:28 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-01-04 15:46:01 -0300 |
commit | 590cda1dd366cf3347654119f04c8a4529698b2f (patch) | |
tree | 878ef4517689fd0c71c34f5ad463b4d5f7a88dc1 /packages/taler-wallet-webextension/src/platform/chrome.ts | |
parent | f4e1e8e6e7ca6dcb9ac7d1c5f3b64130e99e325b (diff) | |
download | wallet-core-590cda1dd366cf3347654119f04c8a4529698b2f.tar.xz |
split foreground and background api
Diffstat (limited to 'packages/taler-wallet-webextension/src/platform/chrome.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/platform/chrome.ts | 68 |
1 files changed, 40 insertions, 28 deletions
diff --git a/packages/taler-wallet-webextension/src/platform/chrome.ts b/packages/taler-wallet-webextension/src/platform/chrome.ts index 744283913..e5efdec4e 100644 --- a/packages/taler-wallet-webextension/src/platform/chrome.ts +++ b/packages/taler-wallet-webextension/src/platform/chrome.ts @@ -15,23 +15,19 @@ */ import { - classifyTalerUri, - CoreApiResponse, - Logger, - TalerUriType, + classifyTalerUri, Logger, + TalerUriType } from "@gnu-taler/taler-util"; import { WalletOperations } from "@gnu-taler/taler-wallet-core"; import { BackgroundOperations } from "../wxApi.js"; import { - CrossBrowserPermissionsApi, - MessageFromBackend, + BackgroundPlatformAPI, CrossBrowserPermissionsApi, ForegroundPlatformAPI, MessageFromBackend, MessageFromFrontend, MessageResponse, - Permissions, - PlatformAPI, + Permissions } from "./api.js"; -const api: PlatformAPI = { +const api: BackgroundPlatformAPI & ForegroundPlatformAPI = { isFirefox, findTalerUriInActiveTab, findTalerUriInClipboard, @@ -310,16 +306,28 @@ function openWalletPageFromPopup(page: string): void { }); } -let i = 0; +let nextMessageIndex = 0; + +/** + * To be used by the foreground + * @param message + * @returns + */ async function sendMessageToBackground< Op extends WalletOperations | BackgroundOperations, >(message: MessageFromFrontend<Op>): Promise<MessageResponse> { - const messageWithId = { ...message, id: `id_${i++ % 1000}` }; + const messageWithId = { ...message, id: `id_${nextMessageIndex++ % 1000}` }; return new Promise<any>((resolve, reject) => { logger.trace("send operation to the wallet background", message); + let timedout = false + setTimeout(() => { + timedout = true + reject("timedout") + }, 2000); chrome.runtime.sendMessage(messageWithId, (backgroundResponse) => { + if (timedout) return false if (chrome.runtime.lastError) { reject(chrome.runtime.lastError.message); } else { @@ -331,6 +339,9 @@ async function sendMessageToBackground< }); } +/** + * To be used by the foreground + */ let notificationPort: chrome.runtime.Port | undefined; function listenToWalletBackground(listener: (m: any) => void): () => void { if (notificationPort === undefined) { @@ -347,6 +358,7 @@ function listenToWalletBackground(listener: (m: any) => void): () => void { const allPorts: chrome.runtime.Port[] = []; + function sendMessageToAllChannels(message: MessageFromBackend): void { for (const notif of allPorts) { // const message: MessageFromBackend = { type: msg.type }; @@ -566,26 +578,26 @@ function setAlertedIcon(): void { interface OffscreenCanvasRenderingContext2D extends CanvasState, - CanvasTransform, - CanvasCompositing, - CanvasImageSmoothing, - CanvasFillStrokeStyles, - CanvasShadowStyles, - CanvasFilters, - CanvasRect, - CanvasDrawPath, - CanvasUserInterface, - CanvasText, - CanvasDrawImage, - CanvasImageData, - CanvasPathDrawingStyles, - CanvasTextDrawingStyles, - CanvasPath { + CanvasTransform, + CanvasCompositing, + CanvasImageSmoothing, + CanvasFillStrokeStyles, + CanvasShadowStyles, + CanvasFilters, + CanvasRect, + CanvasDrawPath, + CanvasUserInterface, + CanvasText, + CanvasDrawImage, + CanvasImageData, + CanvasPathDrawingStyles, + CanvasTextDrawingStyles, + CanvasPath { readonly canvas: OffscreenCanvas; } declare const OffscreenCanvasRenderingContext2D: { prototype: OffscreenCanvasRenderingContext2D; - new (): OffscreenCanvasRenderingContext2D; + new(): OffscreenCanvasRenderingContext2D; }; interface OffscreenCanvas extends EventTarget { @@ -598,7 +610,7 @@ interface OffscreenCanvas extends EventTarget { } declare const OffscreenCanvas: { prototype: OffscreenCanvas; - new (width: number, height: number): OffscreenCanvas; + new(width: number, height: number): OffscreenCanvas; }; function createCanvas(size: number): OffscreenCanvas { |