keep alive manifest v3

This commit is contained in:
Sebastian 2022-04-28 13:26:29 -03:00
parent 451c9884dc
commit b239ae1029
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1
8 changed files with 61 additions and 78 deletions

View File

@ -326,6 +326,27 @@ export interface RetryLoopOpts {
stopWhenDone?: boolean;
}
/**
* This iteration hint will keep incrementing while the taskLoop iteration.
* If the hint does not match the current iteration it means that another
* promises is also working or has done some work before.
*/
let iterationHint = 0
function thereIsAnotherPromiseWorking(iteration: number): boolean {
if (iterationHint > iteration) {
logger.trace(`some other promise is or has done some progress`);
iterationHint = iteration;
//we know that another promise did some work but don't know if still active
//so we take ownership and do work
} else if (iterationHint < iteration) {
//another promise take ownership that means that our time has come to an end
return true
}
// increment the hint to match the next loop
iterationHint++
return false
}
/**
* Main retry loop of the wallet.
*
@ -336,6 +357,10 @@ async function runTaskLoop(
opts: RetryLoopOpts = {},
): Promise<void> {
for (let iteration = 0; !ws.stopped; iteration++) {
if (thereIsAnotherPromiseWorking(iteration)) {
logger.trace(`another promise is working, we just need one`);
return;
}
const pending = await getPendingOperations(ws);
logger.trace(`pending operations: ${j2s(pending)}`);
let numGivingLiveness = 0;

View File

@ -26,7 +26,9 @@
},
"commands": {
"_execute_browser_action": {
"suggested_key": "Alt+W"
"suggested_key": {
"default": "Alt+W"
}
}
},
"permissions": [

View File

@ -25,7 +25,9 @@
],
"commands": {
"_execute_action": {
"suggested_key": "Alt+W"
"suggested_key": {
"default": "Alt+W"
}
}
},
"optional_permissions": [

View File

@ -56,6 +56,10 @@ export interface WalletVersion {
* WebExtension APIs consistently.
*/
export interface PlatformAPI {
/**
* Garantee that the
*/
keepAlive(cb: VoidFunction): void;
/**
* FIXME: should not be needed
*

View File

@ -15,7 +15,7 @@
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
import { classifyTalerUri, CoreApiResponse, TalerUriType } from "@gnu-taler/taler-util";
import { classifyTalerUri, CoreApiResponse, Logger, TalerUriType } from "@gnu-taler/taler-util";
import { CrossBrowserPermissionsApi, MessageFromBackend, Permissions, PlatformAPI } from "./api.js";
const api: PlatformAPI = {
@ -38,10 +38,27 @@ const api: PlatformAPI = {
sendMessageToWalletBackground,
useServiceWorkerAsBackgroundProcess,
containsTalerHeaderListener,
keepAlive,
}
export default api;
const logger = new Logger("chrome.ts");
function keepAlive(callback: any): void {
if (chrome.runtime && chrome.runtime.getManifest().manifest_version === 3) {
chrome.alarms.create("wallet-worker", { periodInMinutes: 1 })
chrome.alarms.onAlarm.addListener((a) => {
logger.trace(`kee p alive alarm: ${a.name}`)
callback()
})
} else {
callback();
}
}
function isFirefox(): boolean {
return false;
}

View File

@ -21,6 +21,7 @@ const frames = ["popup", "wallet"]
const api: PlatformAPI = ({
isFirefox: () => false,
keepAlive: (cb: VoidFunction) => cb(),
findTalerUriInActiveTab: async () => undefined,
containsTalerHeaderListener: () => { return true },
getPermissionsApi: () => ({

View File

@ -1,70 +0,0 @@
/*
This file is part of GNU Taler
(C) 2020 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/>
*/
/**
* Imports.
*/
import { Logger } from "@gnu-taler/taler-util";
import { timer, TimerAPI, TimerHandle } from "@gnu-taler/taler-wallet-core";
const logger = new Logger("ServiceWorkerTimerGroup.ts");
/**
* Implementation of [[TimerAPI]] using alarm API
*/
export class ServiceWorkerTimerAPI implements TimerAPI {
/**
* Call a function every time the delay given in milliseconds passes.
*/
every(delayMs: number, callback: VoidFunction): TimerHandle {
const seconds = delayMs / 1000;
const periodInMinutes = Math.round(seconds < 61 ? 1 : seconds / 60);
logger.info(`creating a alarm every ${periodInMinutes} min, intended delay was ${delayMs}`)
// chrome.alarms.create("wallet-worker", { periodInMinutes })
// chrome.alarms.onAlarm.addListener((a) => {
// logger.info(`alarm called, every: ${a.name}`)
// callback()
// })
const p = timer.every(delayMs, callback)
return p;
}
/**
* Call a function after the delay given in milliseconds passes.
*/
after(delayMs: number, callback: VoidFunction): TimerHandle {
const seconds = delayMs / 1000;
const delayInMinutes = Math.round(seconds < 61 ? 1 : seconds / 60);
logger.info(`creating a alarm after ${delayInMinutes} min, intended delay was ${delayMs}`)
chrome.alarms.create("wallet-worker", { delayInMinutes })
// chrome.alarms.onAlarm.addListener((a) => {
// logger.info(`alarm called, after: ${a.name}`)
// callback();
// })
const p = timer.after(delayMs, callback);
return p
}
}

View File

@ -46,7 +46,6 @@ import { BrowserHttpLib } from "./browserHttpLib.js";
import { MessageFromBackend, platform } from "./platform/api.js";
import { SynchronousCryptoWorkerFactory } from "./serviceWorkerCryptoWorkerFactory.js";
import { ServiceWorkerHttpLib } from "./serviceWorkerHttpLib.js";
import { ServiceWorkerTimerAPI } from "./serviceWorkerTimerAPI.js";
/**
* Currently active wallet instance. Might be unloaded and
@ -196,7 +195,7 @@ async function reinitWallet(): Promise<void> {
if (platform.useServiceWorkerAsBackgroundProcess()) {
httpLib = new ServiceWorkerHttpLib();
cryptoWorker = new SynchronousCryptoWorkerFactory();
timer = new ServiceWorkerTimerAPI();
timer = new SetTimeoutTimerAPI();
} else {
httpLib = new BrowserHttpLib();
cryptoWorker = new BrowserCryptoWorkerFactory();
@ -216,9 +215,12 @@ async function reinitWallet(): Promise<void> {
const message: MessageFromBackend = { type: x.type };
platform.sendMessageToAllChannels(message)
});
wallet.runTaskLoop().catch((e) => {
logger.error("error during wallet task loop", e);
});
platform.keepAlive(() => {
wallet.runTaskLoop().catch((e) => {
logger.error("error during wallet task loop", e);
});
})
// Useful for debugging in the background page.
if (typeof window !== "undefined") {
(window as any).talerWallet = wallet;