From 38c9f87ae471d340fd45b96bb42afb937373324a Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 29 May 2024 10:13:20 -0300 Subject: prepare #8767 --- .../src/components/WalletActivity.tsx | 6 +- .../src/hooks/useIsOnline.ts | 20 ++- .../src/platform/chrome.ts | 30 ++++- .../taler-wallet-webextension/src/platform/dev.ts | 20 +-- .../taler-wallet-webextension/src/wxBackend.ts | 150 +++++++++++---------- 5 files changed, 127 insertions(+), 99 deletions(-) (limited to 'packages') diff --git a/packages/taler-wallet-webextension/src/components/WalletActivity.tsx b/packages/taler-wallet-webextension/src/components/WalletActivity.tsx index a77a69fa6..f29d0b0f7 100644 --- a/packages/taler-wallet-webextension/src/components/WalletActivity.tsx +++ b/packages/taler-wallet-webextension/src/components/WalletActivity.tsx @@ -22,7 +22,7 @@ import { TalerErrorDetail, TaskProgressNotification, WalletNotification, - assertUnreachable + assertUnreachable, } from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { useTranslationContext } from "@gnu-taler/web-util/browser"; @@ -83,7 +83,9 @@ export function WalletActivity(): VNode { cursor: "pointer", }} > - click here to open + + Click here to open the wallet activity tab. + ); diff --git a/packages/taler-wallet-webextension/src/hooks/useIsOnline.ts b/packages/taler-wallet-webextension/src/hooks/useIsOnline.ts index 8d26bf3b6..719aa2f96 100644 --- a/packages/taler-wallet-webextension/src/hooks/useIsOnline.ts +++ b/packages/taler-wallet-webextension/src/hooks/useIsOnline.ts @@ -1,7 +1,21 @@ -import { codecForBoolean } from "@gnu-taler/taler-util"; -import { buildStorageKey, useMemoryStorage } from "@gnu-taler/web-util/browser"; -import { platform } from "../platform/foreground.js"; +/* + 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 + */ +import { useMemoryStorage } from "@gnu-taler/web-util/browser"; import { useEffect } from "preact/hooks"; +import { platform } from "../platform/foreground.js"; export function useIsOnline(): boolean { const { value, update } = useMemoryStorage("online", true); diff --git a/packages/taler-wallet-webextension/src/platform/chrome.ts b/packages/taler-wallet-webextension/src/platform/chrome.ts index e63040f5c..056351e3f 100644 --- a/packages/taler-wallet-webextension/src/platform/chrome.ts +++ b/packages/taler-wallet-webextension/src/platform/chrome.ts @@ -732,15 +732,35 @@ function listenNetworkConnectionState( function notifyOnline() { notify("on"); } - notify(window.navigator.onLine ? "on" : "off"); - window.addEventListener("offline", notifyOffline); - window.addEventListener("online", notifyOnline); + function notifyChange() { + if (nav.onLine) { + notifyOnline(); + } else { + notifyOnline(); + } + } + notify(navigator.onLine ? "on" : "off"); + + const nav: any = navigator; + if (typeof nav.connection !== "undefined") { + nav.connection.addEventListener("change", notifyChange); + } + if (typeof window !== "undefined") { + window.addEventListener("offline", notifyOffline); + window.addEventListener("online", notifyOnline); + } return () => { - window.removeEventListener("offline", notifyOffline); - window.removeEventListener("online", notifyOnline); + if (typeof nav.connection !== "undefined") { + nav.connection.removeEventListener("change", notifyChange); + } + if (typeof window !== "undefined") { + window.removeEventListener("offline", notifyOffline); + window.removeEventListener("online", notifyOnline); + } }; } + function runningOnPrivateMode(): boolean { return chrome.extension.inIncognitoContext; } diff --git a/packages/taler-wallet-webextension/src/platform/dev.ts b/packages/taler-wallet-webextension/src/platform/dev.ts index d6e743147..b53e8f3c4 100644 --- a/packages/taler-wallet-webextension/src/platform/dev.ts +++ b/packages/taler-wallet-webextension/src/platform/dev.ts @@ -35,11 +35,11 @@ const api: BackgroundPlatformAPI & ForegroundPlatformAPI = { keepAlive: (cb: VoidFunction) => cb(), findTalerUriInActiveTab: async () => undefined, findTalerUriInClipboard: async () => undefined, - listenNetworkConnectionState, + listenNetworkConnectionState: () => () => undefined, openNewURLFromPopup: () => undefined, triggerWalletEvent: () => undefined, setAlertedIcon: () => undefined, - setNormalIcon : () => undefined, + setNormalIcon: () => undefined, getPermissionsApi: () => ({ containsClipboardPermissions: async () => true, removeClipboardPermissions: async () => false, @@ -200,19 +200,3 @@ interface IframeMessageCommand { export default api; -function listenNetworkConnectionState( - notify: (state: "on" | "off") => void, -): () => void { - function notifyOffline() { - notify("off"); - } - function notifyOnline() { - notify("on"); - } - window.addEventListener("offline", notifyOffline); - window.addEventListener("online", notifyOnline); - return () => { - window.removeEventListener("offline", notifyOffline); - window.removeEventListener("online", notifyOnline); - }; -} diff --git a/packages/taler-wallet-webextension/src/wxBackend.ts b/packages/taler-wallet-webextension/src/wxBackend.ts index 5fa255f5d..a0b9f2908 100644 --- a/packages/taler-wallet-webextension/src/wxBackend.ts +++ b/packages/taler-wallet-webextension/src/wxBackend.ts @@ -39,7 +39,7 @@ import { makeErrorDetail, openPromise, setGlobalLogLevelFromString, - setLogLevelFromString + setLogLevelFromString, } from "@gnu-taler/taler-util"; import { HttpRequestLibrary } from "@gnu-taler/taler-util/http"; import { @@ -92,7 +92,7 @@ async function resetDb(): Promise { export type WalletActivityTrack = { id: number; - events: (WalletNotification & {when: AbsoluteTime})[]; + events: (WalletNotification & { when: AbsoluteTime })[]; start: AbsoluteTime; type: NotificationType; end: AbsoluteTime; @@ -107,130 +107,138 @@ function getUniqueId(): number { //FIXME: maybe circular buffer const activity: WalletActivityTrack[] = []; -function addNewWalletActivityNotification(list: WalletActivityTrack[], n: WalletNotification) { - const start = AbsoluteTime.now(); - const ev = {...n, when:start}; - switch (n.type) { +function convertWalletActivityNotification( + knownEvents: WalletActivityTrack[], + event: WalletNotification & { + when: AbsoluteTime; + }, +): WalletActivityTrack | undefined { + switch (event.type) { case NotificationType.BalanceChange: { - const groupId = `${n.type}:${n.hintTransactionId}`; - const found = list.find((a)=>a.groupId === groupId) + const groupId = `${event.type}:${event.hintTransactionId}`; + const found = knownEvents.find((a) => a.groupId === groupId); if (found) { - found.end = start; - found.events.unshift(ev) - return; + found.end = event.when; + found.events.unshift(event); + return found; } - list.push({ + return { id: getUniqueId(), - type: n.type, - start, + type: event.type, + start: event.when, end: AbsoluteTime.never(), - events: [ev], + events: [event], groupId, - }); - return; + }; } case NotificationType.BackupOperationError: { const groupId = ""; - list.push({ + return { id: getUniqueId(), - type: n.type, - start, + type: event.type, + start: event.when, end: AbsoluteTime.never(), - events: [ev], + events: [event], groupId, - }); - return; + }; } case NotificationType.TransactionStateTransition: { - const groupId = `${n.type}:${n.transactionId}`; - const found = list.find((a)=>a.groupId === groupId) + const groupId = `${event.type}:${event.transactionId}`; + const found = knownEvents.find((a) => a.groupId === groupId); if (found) { - found.end = start; - found.events.unshift(ev) - return; + found.end = event.when; + found.events.unshift(event); + return found; } - list.push({ + return { id: getUniqueId(), - type: n.type, - start, + type: event.type, + start: event.when, end: AbsoluteTime.never(), - events: [ev], + events: [event], groupId, - }); - return; + }; } case NotificationType.WithdrawalOperationTransition: { - return; + return undefined; } case NotificationType.ExchangeStateTransition: { - const groupId = `${n.type}:${n.exchangeBaseUrl}`; - const found = list.find((a)=>a.groupId === groupId) + const groupId = `${event.type}:${event.exchangeBaseUrl}`; + const found = knownEvents.find((a) => a.groupId === groupId); if (found) { - found.end = start; - found.events.unshift(ev) - return; + found.end = event.when; + found.events.unshift(event); + return found; } - list.push({ + return { id: getUniqueId(), - type: n.type, - start, + type: event.type, + start: event.when, end: AbsoluteTime.never(), - events: [ev], + events: [event], groupId, - }); - return; + }; } case NotificationType.Idle: { const groupId = ""; - list.push({ + return({ id: getUniqueId(), - type: n.type, - start, + type: event.type, + start: event.when, end: AbsoluteTime.never(), - events: [ev], + events: [event], groupId, }); - return; } case NotificationType.TaskObservabilityEvent: { - const groupId = `${n.type}:${n.taskId}`; - const found = list.find((a)=>a.groupId === groupId) + const groupId = `${event.type}:${event.taskId}`; + const found = knownEvents.find((a) => a.groupId === groupId); if (found) { - found.end = start; - found.events.unshift(ev) - return; + found.end = event.when; + found.events.unshift(event); + return found; } - list.push({ + return({ id: getUniqueId(), - type: n.type, - start, + type: event.type, + start: event.when, end: AbsoluteTime.never(), - events: [ev], + events: [event], groupId, }); - return; } case NotificationType.RequestObservabilityEvent: { - const groupId = `${n.type}:${n.operation}:${n.requestId}`; - const found = list.find((a)=>a.groupId === groupId) + const groupId = `${event.type}:${event.operation}:${event.requestId}`; + const found = knownEvents.find((a) => a.groupId === groupId); if (found) { - found.end = start; - found.events.unshift(ev) - return; + found.end = event.when; + found.events.unshift(event); + return found; } - list.push({ + return({ id: getUniqueId(), - type: n.type, - start, + type: event.type, + start: event.when, end: AbsoluteTime.never(), - events: [ev], + events: [event], groupId, }); - return; } } } +function addNewWalletActivityNotification( + list: WalletActivityTrack[], + n: WalletNotification, +) { + const start = AbsoluteTime.now(); + const ev = { ...n, when: start }; + const activity = convertWalletActivityNotification(list, ev); + if (activity) { + list.unshift(activity); // insert at start + } +} + async function getNotifications({ filter, }: { -- cgit v1.2.3