keep alive manifest v3
This commit is contained in:
parent
451c9884dc
commit
b239ae1029
@ -326,6 +326,27 @@ export interface RetryLoopOpts {
|
|||||||
stopWhenDone?: boolean;
|
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.
|
* Main retry loop of the wallet.
|
||||||
*
|
*
|
||||||
@ -336,6 +357,10 @@ async function runTaskLoop(
|
|||||||
opts: RetryLoopOpts = {},
|
opts: RetryLoopOpts = {},
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
for (let iteration = 0; !ws.stopped; iteration++) {
|
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);
|
const pending = await getPendingOperations(ws);
|
||||||
logger.trace(`pending operations: ${j2s(pending)}`);
|
logger.trace(`pending operations: ${j2s(pending)}`);
|
||||||
let numGivingLiveness = 0;
|
let numGivingLiveness = 0;
|
||||||
|
@ -26,7 +26,9 @@
|
|||||||
},
|
},
|
||||||
"commands": {
|
"commands": {
|
||||||
"_execute_browser_action": {
|
"_execute_browser_action": {
|
||||||
"suggested_key": "Alt+W"
|
"suggested_key": {
|
||||||
|
"default": "Alt+W"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"permissions": [
|
"permissions": [
|
||||||
|
@ -25,7 +25,9 @@
|
|||||||
],
|
],
|
||||||
"commands": {
|
"commands": {
|
||||||
"_execute_action": {
|
"_execute_action": {
|
||||||
"suggested_key": "Alt+W"
|
"suggested_key": {
|
||||||
|
"default": "Alt+W"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"optional_permissions": [
|
"optional_permissions": [
|
||||||
|
@ -56,6 +56,10 @@ export interface WalletVersion {
|
|||||||
* WebExtension APIs consistently.
|
* WebExtension APIs consistently.
|
||||||
*/
|
*/
|
||||||
export interface PlatformAPI {
|
export interface PlatformAPI {
|
||||||
|
/**
|
||||||
|
* Garantee that the
|
||||||
|
*/
|
||||||
|
keepAlive(cb: VoidFunction): void;
|
||||||
/**
|
/**
|
||||||
* FIXME: should not be needed
|
* FIXME: should not be needed
|
||||||
*
|
*
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
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";
|
import { CrossBrowserPermissionsApi, MessageFromBackend, Permissions, PlatformAPI } from "./api.js";
|
||||||
|
|
||||||
const api: PlatformAPI = {
|
const api: PlatformAPI = {
|
||||||
@ -38,10 +38,27 @@ const api: PlatformAPI = {
|
|||||||
sendMessageToWalletBackground,
|
sendMessageToWalletBackground,
|
||||||
useServiceWorkerAsBackgroundProcess,
|
useServiceWorkerAsBackgroundProcess,
|
||||||
containsTalerHeaderListener,
|
containsTalerHeaderListener,
|
||||||
|
keepAlive,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default api;
|
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 {
|
function isFirefox(): boolean {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ const frames = ["popup", "wallet"]
|
|||||||
|
|
||||||
const api: PlatformAPI = ({
|
const api: PlatformAPI = ({
|
||||||
isFirefox: () => false,
|
isFirefox: () => false,
|
||||||
|
keepAlive: (cb: VoidFunction) => cb(),
|
||||||
findTalerUriInActiveTab: async () => undefined,
|
findTalerUriInActiveTab: async () => undefined,
|
||||||
containsTalerHeaderListener: () => { return true },
|
containsTalerHeaderListener: () => { return true },
|
||||||
getPermissionsApi: () => ({
|
getPermissionsApi: () => ({
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -46,7 +46,6 @@ import { BrowserHttpLib } from "./browserHttpLib.js";
|
|||||||
import { MessageFromBackend, platform } from "./platform/api.js";
|
import { MessageFromBackend, platform } from "./platform/api.js";
|
||||||
import { SynchronousCryptoWorkerFactory } from "./serviceWorkerCryptoWorkerFactory.js";
|
import { SynchronousCryptoWorkerFactory } from "./serviceWorkerCryptoWorkerFactory.js";
|
||||||
import { ServiceWorkerHttpLib } from "./serviceWorkerHttpLib.js";
|
import { ServiceWorkerHttpLib } from "./serviceWorkerHttpLib.js";
|
||||||
import { ServiceWorkerTimerAPI } from "./serviceWorkerTimerAPI.js";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Currently active wallet instance. Might be unloaded and
|
* Currently active wallet instance. Might be unloaded and
|
||||||
@ -196,7 +195,7 @@ async function reinitWallet(): Promise<void> {
|
|||||||
if (platform.useServiceWorkerAsBackgroundProcess()) {
|
if (platform.useServiceWorkerAsBackgroundProcess()) {
|
||||||
httpLib = new ServiceWorkerHttpLib();
|
httpLib = new ServiceWorkerHttpLib();
|
||||||
cryptoWorker = new SynchronousCryptoWorkerFactory();
|
cryptoWorker = new SynchronousCryptoWorkerFactory();
|
||||||
timer = new ServiceWorkerTimerAPI();
|
timer = new SetTimeoutTimerAPI();
|
||||||
} else {
|
} else {
|
||||||
httpLib = new BrowserHttpLib();
|
httpLib = new BrowserHttpLib();
|
||||||
cryptoWorker = new BrowserCryptoWorkerFactory();
|
cryptoWorker = new BrowserCryptoWorkerFactory();
|
||||||
@ -216,9 +215,12 @@ async function reinitWallet(): Promise<void> {
|
|||||||
const message: MessageFromBackend = { type: x.type };
|
const message: MessageFromBackend = { type: x.type };
|
||||||
platform.sendMessageToAllChannels(message)
|
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.
|
// Useful for debugging in the background page.
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
(window as any).talerWallet = wallet;
|
(window as any).talerWallet = wallet;
|
||||||
|
Loading…
Reference in New Issue
Block a user