diff options
author | Sebastian <sebasjm@gmail.com> | 2022-05-06 17:29:42 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-05-06 17:29:52 -0300 |
commit | 37690dd22c1c4cd63f3fa4b1d600b3da1cbae152 (patch) | |
tree | ca5760dc2e3e1640ce8c1a21fdca2e43765fedff /packages/taler-wallet-webextension | |
parent | 9641299d6cb82cbc60dd9f5863a76c751dcdfacd (diff) |
handling service worker waiting phase
Diffstat (limited to 'packages/taler-wallet-webextension')
-rw-r--r-- | packages/taler-wallet-webextension/manifest-v3.json | 2 | ||||
-rwxr-xr-x | packages/taler-wallet-webextension/pack.sh | 4 | ||||
-rw-r--r-- | packages/taler-wallet-webextension/package.json | 2 | ||||
-rw-r--r-- | packages/taler-wallet-webextension/service_worker.js | 11 | ||||
-rw-r--r-- | packages/taler-wallet-webextension/src/platform/chrome.ts | 23 | ||||
-rw-r--r-- | packages/taler-wallet-webextension/src/wxBackend.ts | 115 |
6 files changed, 83 insertions, 74 deletions
diff --git a/packages/taler-wallet-webextension/manifest-v3.json b/packages/taler-wallet-webextension/manifest-v3.json index 21c6b3373..3443b3d1d 100644 --- a/packages/taler-wallet-webextension/manifest-v3.json +++ b/packages/taler-wallet-webextension/manifest-v3.json @@ -54,6 +54,6 @@ "default_popup": "static/popup.html" }, "background": { - "service_worker": "dist/background.js" + "service_worker": "service_worker.js" } }
\ No newline at end of file diff --git a/packages/taler-wallet-webextension/pack.sh b/packages/taler-wallet-webextension/pack.sh index 4fb419991..504b5443f 100755 --- a/packages/taler-wallet-webextension/pack.sh +++ b/packages/taler-wallet-webextension/pack.sh @@ -32,8 +32,8 @@ zipfile="taler-wallet-webextension-${vers_manifest}.zip" TEMP_DIR=$(mktemp -d) jq '. | .name = "GNU Taler Wallet" ' manifest-v3.json > $TEMP_DIR/manifest.json -cp -r dist static $TEMP_DIR -(cd $TEMP_DIR && zip -q -r "$zipfile" dist static manifest.json) +cp -r dist static service_worker.js $TEMP_DIR +(cd $TEMP_DIR && zip -q -r "$zipfile" dist static manifest.json service_worker.js) mkdir -p extension/v3 mv "$TEMP_DIR/$zipfile" ./extension/v3/ rm -rf $TEMP_DIR diff --git a/packages/taler-wallet-webextension/package.json b/packages/taler-wallet-webextension/package.json index 07495a274..cd4510daf 100644 --- a/packages/taler-wallet-webextension/package.json +++ b/packages/taler-wallet-webextension/package.json @@ -9,7 +9,7 @@ "private": false, "scripts": { "clean": "rimraf dist lib tsconfig.tsbuildinfo", - "test": "mocha --enable-source-maps 'dist/**/*.test.js'", + "test": "pnpm compile && mocha --enable-source-maps 'dist/**/*.test.js'", "test:coverage": "nyc pnpm test", "compile": "tsc && ./build-fast-with-linaria.mjs", "dev": "./dev.mjs", diff --git a/packages/taler-wallet-webextension/service_worker.js b/packages/taler-wallet-webextension/service_worker.js new file mode 100644 index 000000000..38064e245 --- /dev/null +++ b/packages/taler-wallet-webextension/service_worker.js @@ -0,0 +1,11 @@ +/* eslint-disable no-undef */ +/** + * Wrapper to catch any initialization error and show it in the logs + */ +try { + importScripts("dist/background.js"); + self.skipWaiting(); + console.log("SERVICE WORKER init: ok"); +} catch (e) { + console.error("SERVICE WORKER failed:", e); +} diff --git a/packages/taler-wallet-webextension/src/platform/chrome.ts b/packages/taler-wallet-webextension/src/platform/chrome.ts index ef8904b77..e097baaa1 100644 --- a/packages/taler-wallet-webextension/src/platform/chrome.ts +++ b/packages/taler-wallet-webextension/src/platform/chrome.ts @@ -117,7 +117,7 @@ export async function removeHostPermissions(): Promise<boolean> { if ("webRequest" in chrome) { chrome.webRequest.handlerBehaviorChanged(() => { if (chrome.runtime.lastError) { - console.error(JSON.stringify(chrome.runtime.lastError)); + logger.error(JSON.stringify(chrome.runtime.lastError)); } }); } @@ -138,7 +138,6 @@ export async function removeHostPermissions(): Promise<boolean> { } function addPermissionsListener(callback: (p: Permissions, lastError?: string) => void): void { - console.log("addPermissionListener is not supported for Firefox"); chrome.permissions.onAdded.addListener((perm: Permissions) => { const lastError = chrome.runtime.lastError?.message; callback(perm, lastError) @@ -182,7 +181,7 @@ function openWalletURIFromPopup(talerUri: string): void { url = chrome.runtime.getURL(`static/wallet.html#/cta/refund?talerRefundUri=${talerUri}`); break; default: - console.warn( + logger.warn( "Response with HTTP 402 has Taler header, but header value is not a taler:// URI.", ); return; @@ -211,6 +210,7 @@ function openWalletPageFromPopup(page: string): void { async function sendMessageToWalletBackground(operation: string, payload: any): Promise<any> { return new Promise<any>((resolve, reject) => { + logger.trace("send operation to the wallet background", operation) chrome.runtime.sendMessage({ operation, payload, id: "(none)" }, (resp) => { if (chrome.runtime.lastError) { reject(chrome.runtime.lastError.message) @@ -245,7 +245,7 @@ function sendMessageToAllChannels(message: MessageFromBackend): void { try { notif.postMessage(message); } catch (e) { - console.error(e); + logger.error("error posting a message", e); } } } @@ -275,7 +275,7 @@ function registerReloadOnNewVersion(): void { // Explicitly unload the extension page as soon as an update is available, // so the update gets installed as soon as possible. chrome.runtime.onUpdateAvailable.addListener((details) => { - console.log("update available:", details); + logger.info("update available:", details); chrome.runtime.reload(); }); @@ -286,7 +286,7 @@ function redirectTabToWalletPage( page: string, ): void { const url = chrome.runtime.getURL(`/static/wallet.html#${page}`); - console.log("redirecting tabId: ", tabId, " to: ", url); + logger.trace("redirecting tabId: ", tabId, " to: ", url); chrome.tabs.update(tabId, { url }); } @@ -301,13 +301,13 @@ function getWalletVersion(): WalletVersion { } function registerTalerHeaderListener(callback: (tabId: number, url: string) => void): void { - console.log("setting up header listener"); + logger.trace("setting up header listener"); function headerListener( details: chrome.webRequest.WebResponseHeadersDetails, ): void { if (chrome.runtime.lastError) { - console.error(JSON.stringify(chrome.runtime.lastError)); + logger.error(JSON.stringify(chrome.runtime.lastError)); return; } if ( @@ -350,7 +350,7 @@ function registerTalerHeaderListener(callback: (tabId: number, url: string) => v //notify the browser about this change, this operation is expensive chrome?.webRequest?.handlerBehaviorChanged(() => { if (chrome.runtime.lastError) { - console.error(JSON.stringify(chrome.runtime.lastError)); + logger.error(JSON.stringify(chrome.runtime.lastError)); } }); }); @@ -473,7 +473,6 @@ async function registerIconChangeOnTalerContent(): Promise<void> { logger.info("tab updated", tabId, info); if (info.status !== "complete") return; const uri = await findTalerUriInTab(tabId); - console.log("urio", uri) if (uri) { setAlertedIcon() } else { @@ -483,9 +482,7 @@ async function registerIconChangeOnTalerContent(): Promise<void> { }); chrome.tabs.onActivated.addListener(async ({ tabId }: chrome.tabs.TabActiveInfo) => { if (tabId < 0) return; - logger.info("tab activated", tabId); const uri = await findTalerUriInTab(tabId); - console.log("urio", uri) if (uri) { setAlertedIcon() } else { @@ -565,7 +562,7 @@ async function findTalerUriInTab(tabId: number): Promise<string | undefined> { }, (result) => { if (chrome.runtime.lastError) { - console.error(JSON.stringify(chrome.runtime.lastError)); + logger.error(JSON.stringify(chrome.runtime.lastError)); resolve(undefined); return; } diff --git a/packages/taler-wallet-webextension/src/wxBackend.ts b/packages/taler-wallet-webextension/src/wxBackend.ts index f5483e807..532b28622 100644 --- a/packages/taler-wallet-webextension/src/wxBackend.ts +++ b/packages/taler-wallet-webextension/src/wxBackend.ts @@ -121,69 +121,70 @@ async function dispatch( }; }; - switch (req.operation) { - case "wxGetDiagnostics": { - r = wrapResponse(await getDiagnostics()); - break; - } - case "reset-db": { - await deleteTalerDatabase(indexedDB as any); - r = wrapResponse(await reinitWallet()); - break; - } - case "run-gc": { - logger.info("gc") - const dump = await exportDb(currentDatabase!.idbHandle()); - await deleteTalerDatabase(indexedDB as any); - logger.info("cleaned") - await reinitWallet(); - logger.info("init") - await importDb(currentDatabase!.idbHandle(), dump) - logger.info("imported") - r = wrapResponse({ result: true }); - break; - } - case "containsHeaderListener": { - const res = await platform.containsTalerHeaderListener(); - r = wrapResponse({ newValue: res }); - break; - } - case "toggleHeaderListener": { - const newVal = req.payload.value; - logger.trace("new extended permissions value", newVal); - if (newVal) { - platform.registerTalerHeaderListener(parseTalerUriAndRedirect); - r = wrapResponse({ newValue: true }); - } else { - const rem = await platform.getPermissionsApi().removeHostPermissions(); - logger.trace("permissions removed:", rem); - r = wrapResponse({ newVal: false }); + try { + switch (req.operation) { + case "wxGetDiagnostics": { + r = wrapResponse(await getDiagnostics()); + break; } - break; - } - default: { - const w = currentWallet; - if (!w) { - r = { - type: "error", - id: req.id, - operation: req.operation, - error: makeErrorDetail( - TalerErrorCode.WALLET_CORE_NOT_AVAILABLE, - {}, - "wallet core not available", - ), - }; + case "reset-db": { + await deleteTalerDatabase(indexedDB as any); + r = wrapResponse(await reinitWallet()); + break; + } + case "run-gc": { + logger.info("gc") + const dump = await exportDb(currentDatabase!.idbHandle()); + await deleteTalerDatabase(indexedDB as any); + logger.info("cleaned") + await reinitWallet(); + logger.info("init") + await importDb(currentDatabase!.idbHandle(), dump) + logger.info("imported") + r = wrapResponse({ result: true }); + break; + } + case "containsHeaderListener": { + const res = await platform.containsTalerHeaderListener(); + r = wrapResponse({ newValue: res }); + break; + } + case "toggleHeaderListener": { + const newVal = req.payload.value; + logger.trace("new extended permissions value", newVal); + if (newVal) { + platform.registerTalerHeaderListener(parseTalerUriAndRedirect); + r = wrapResponse({ newValue: true }); + } else { + const rem = await platform.getPermissionsApi().removeHostPermissions(); + logger.trace("permissions removed:", rem); + r = wrapResponse({ newVal: false }); + } + break; + } + default: { + const w = currentWallet; + if (!w) { + r = { + type: "error", + id: req.id, + operation: req.operation, + error: makeErrorDetail( + TalerErrorCode.WALLET_CORE_NOT_AVAILABLE, + {}, + "wallet core not available", + ), + }; + break; + } + r = await w.handleCoreApiRequest(req.operation, req.id, req.payload); break; } - r = await w.handleCoreApiRequest(req.operation, req.id, req.payload); - break; } - } - try { sendResponse(r); } catch (e) { + logger.error(`Error sending operation: ${req.operation}`, e) // might fail if tab disconnected } } @@ -231,7 +232,7 @@ async function reinitWallet(): Promise<void> { }); platform.keepAlive(() => { - wallet.runTaskLoop().catch((e) => { + return wallet.runTaskLoop().catch((e) => { logger.error("error during wallet task loop", e); }); }) |