diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/platform')
6 files changed, 161 insertions, 99 deletions
diff --git a/packages/taler-wallet-webextension/src/platform/api.ts b/packages/taler-wallet-webextension/src/platform/api.ts index 7df190303..cd09f6438 100644 --- a/packages/taler-wallet-webextension/src/platform/api.ts +++ b/packages/taler-wallet-webextension/src/platform/api.ts @@ -85,7 +85,7 @@ export interface WalletWebExVersion { * Compatibility helpers needed for browsers that don't implement * WebExtension APIs consistently. */ -export interface PlatformAPI { +export interface BackgroundPlatformAPI { /** * Guarantee that the service workers don't die */ @@ -97,61 +97,35 @@ export interface PlatformAPI { */ isFirefox(): boolean; - /** - * Permission API for checking and add a listener - */ - getPermissionsApi(): CrossBrowserPermissionsApi; - - /** - * Backend API - * - * Register a callback to be called when the wallet is ready to start - * @param callback - */ - notifyWhenAppIsReady(callback: () => void): void; + registerOnInstalled(callback: () => void): void; /** - * Popup API - * - * Used when an TalerURI is found and open up from the popup UI. - * Closes the popup and open the URI into the wallet UI. * - * @param talerUri + * Check if background process run as service worker. This is used from the + * wallet use different http api and crypto worker. */ - openWalletURIFromPopup(talerUri: string): void; - + useServiceWorkerAsBackgroundProcess(): boolean; /** - * Backend API * * Open a page into the wallet UI * @param page */ openWalletPage(page: string): void; - /** - * Popup API * - * Open a page into the wallet UI and closed the popup - * @param page - */ - openWalletPageFromPopup(page: string): void; - - /** - * Backend API - * - * When a tab has been detected to have a Taler action the background process - * can use this function to redirect the tab to the wallet UI - * - * @param tabId - * @param page + * Register a callback to be called when the wallet is ready to start + * @param callback */ - redirectTabToWalletPage(tabId: number, page: string): void; + notifyWhenAppIsReady(callback: () => void): void; /** * Get the wallet version from manifest */ getWalletWebExVersion(): WalletWebExVersion; - + /** + * Frontend API + */ + containsTalerHeaderListener(): boolean; /** * Backend API */ @@ -166,22 +140,75 @@ export interface PlatformAPI { registerTalerHeaderListener( onHeader: (tabId: number, url: string) => void, ): void; + /** - * Frontend API + * Permission API for checking and add a listener */ - containsTalerHeaderListener(): boolean; + getPermissionsApi(): CrossBrowserPermissionsApi; + /** + * Used by the wallet backend to send notification about new information + * @param message + */ + sendMessageToAllChannels(message: MessageFromBackend): void; + /** * Backend API + * + * When a tab has been detected to have a Taler action the background process + * can use this function to redirect the tab to the wallet UI + * + * @param tabId + * @param page */ - registerOnInstalled(callback: () => void): void; + redirectTabToWalletPage(tabId: number, page: string): void; + /** + * Use by the wallet backend to receive operations from frontend (popup & wallet) + * and send a response back. + * + * @param onNewMessage + */ + listenToAllChannels( + notifyNewMessage: <Op extends WalletOperations | BackgroundOperations>( + message: MessageFromFrontend<Op> & { id: string }, + ) => Promise<MessageResponse>, + ): void; +} +export interface ForegroundPlatformAPI { + /** + * FIXME: should not be needed + * + * check if the platform is firefox + */ + isFirefox(): boolean; /** - * Backend API + * Permission API for checking and add a listener + */ + getPermissionsApi(): CrossBrowserPermissionsApi; + + /** + * Popup API * - * Check if background process run as service worker. This is used from the - * wallet use different http api and crypto worker. + * Used when an TalerURI is found and open up from the popup UI. + * Closes the popup and open the URI into the wallet UI. + * + * @param talerUri */ - useServiceWorkerAsBackgroundProcess(): boolean; + openWalletURIFromPopup(talerUri: string): void; + + + /** + * Popup API + * + * Open a page into the wallet UI and closed the popup + * @param page + */ + openWalletPageFromPopup(page: string): void; + + /** + * Get the wallet version from manifest + */ + getWalletWebExVersion(): WalletWebExVersion; /** * Popup API @@ -222,26 +249,4 @@ export interface PlatformAPI { listener: (message: MessageFromBackend) => void, ): () => void; - /** - * Use by the wallet backend to receive operations from frontend (popup & wallet) - * and send a response back. - * - * @param onNewMessage - */ - listenToAllChannels( - notifyNewMessage: <Op extends WalletOperations | BackgroundOperations>( - message: MessageFromFrontend<Op> & { id: string }, - ) => Promise<MessageResponse>, - ): void; - - /** - * Used by the wallet backend to send notification about new information - * @param message - */ - sendMessageToAllChannels(message: MessageFromBackend): void; -} - -export let platform: PlatformAPI = undefined as any; -export function setupPlatform(impl: PlatformAPI): void { - platform = impl; } diff --git a/packages/taler-wallet-webextension/src/platform/background.ts b/packages/taler-wallet-webextension/src/platform/background.ts new file mode 100644 index 000000000..9f3764c25 --- /dev/null +++ b/packages/taler-wallet-webextension/src/platform/background.ts @@ -0,0 +1,22 @@ +/* + This file is part of GNU Taler + (C) 2022 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +import { BackgroundPlatformAPI } from "./api.js"; + +export let platform: BackgroundPlatformAPI = undefined as any; +export function setupPlatform(impl: BackgroundPlatformAPI): void { + platform = impl; +} 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 { diff --git a/packages/taler-wallet-webextension/src/platform/dev.ts b/packages/taler-wallet-webextension/src/platform/dev.ts index df40b29e7..d57072c80 100644 --- a/packages/taler-wallet-webextension/src/platform/dev.ts +++ b/packages/taler-wallet-webextension/src/platform/dev.ts @@ -18,15 +18,16 @@ import { CoreApiResponse } from "@gnu-taler/taler-util"; import { WalletOperations } from "@gnu-taler/taler-wallet-core"; import { BackgroundOperations } from "../wxApi.js"; import { + BackgroundPlatformAPI, + ForegroundPlatformAPI, MessageFromBackend, MessageFromFrontend, MessageResponse, - PlatformAPI, } from "./api.js"; const frames = ["popup", "wallet"]; -const api: PlatformAPI = { +const api: BackgroundPlatformAPI & ForegroundPlatformAPI = { isFirefox: () => false, keepAlive: (cb: VoidFunction) => cb(), findTalerUriInActiveTab: async () => undefined, diff --git a/packages/taler-wallet-webextension/src/platform/firefox.ts b/packages/taler-wallet-webextension/src/platform/firefox.ts index 943168956..a36859a0b 100644 --- a/packages/taler-wallet-webextension/src/platform/firefox.ts +++ b/packages/taler-wallet-webextension/src/platform/firefox.ts @@ -14,7 +14,7 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { CrossBrowserPermissionsApi, Permissions, PlatformAPI } from "./api.js"; +import { BackgroundPlatformAPI, CrossBrowserPermissionsApi, ForegroundPlatformAPI, Permissions } from "./api.js"; import chromePlatform, { containsHostPermissions as chromeHostContains, removeHostPermissions as chromeHostRemove, @@ -24,7 +24,7 @@ import chromePlatform, { requestClipboardPermissions as chromeClipRequest, } from "./chrome.js"; -const api: PlatformAPI = { +const api: BackgroundPlatformAPI & ForegroundPlatformAPI = { ...chromePlatform, isFirefox, getPermissionsApi, diff --git a/packages/taler-wallet-webextension/src/platform/foreground.ts b/packages/taler-wallet-webextension/src/platform/foreground.ts new file mode 100644 index 000000000..ae8dc8a95 --- /dev/null +++ b/packages/taler-wallet-webextension/src/platform/foreground.ts @@ -0,0 +1,22 @@ +/* + This file is part of GNU Taler + (C) 2022 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +import { ForegroundPlatformAPI } from "./api.js"; + +export let platform: ForegroundPlatformAPI = undefined as any; +export function setupPlatform(impl: ForegroundPlatformAPI): void { + platform = impl; +} |