1
0
mirror of https://github.com/bitwarden/browser synced 2024-12-25 17:32:46 +01:00

[PM-5584] Set up a stay alive method to allow service worker in manifest v3 to stay alive indefinitely (#8535)

This commit is contained in:
Cesar Gonzalez 2024-04-02 10:24:16 -05:00 committed by GitHub
parent 2e6d977ef1
commit b9771c1e42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,42 +1,35 @@
import { ConsoleLogService } from "@bitwarden/common/platform/services/console-log.service";
import MainBackground from "../background/main.background"; import MainBackground from "../background/main.background";
import { onAlarmListener } from "./alarms/on-alarm-listener";
import { registerAlarms } from "./alarms/register-alarms";
import { BrowserApi } from "./browser/browser-api"; import { BrowserApi } from "./browser/browser-api";
import {
contextMenusClickedListener,
onCommandListener,
onInstallListener,
runtimeMessageListener,
windowsOnFocusChangedListener,
tabsOnActivatedListener,
tabsOnReplacedListener,
tabsOnUpdatedListener,
} from "./listeners";
if (BrowserApi.isManifestVersion(3)) { const logService = new ConsoleLogService(false);
chrome.commands.onCommand.addListener(onCommandListener);
chrome.runtime.onInstalled.addListener(onInstallListener);
chrome.alarms.onAlarm.addListener(onAlarmListener);
registerAlarms();
chrome.windows.onFocusChanged.addListener(windowsOnFocusChangedListener);
chrome.tabs.onActivated.addListener(tabsOnActivatedListener);
chrome.tabs.onReplaced.addListener(tabsOnReplacedListener);
chrome.tabs.onUpdated.addListener(tabsOnUpdatedListener);
chrome.contextMenus.onClicked.addListener(contextMenusClickedListener);
BrowserApi.messageListener(
"runtime.background",
(message: { command: string }, sender, sendResponse) => {
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
runtimeMessageListener(message, sender);
},
);
} else {
const bitwardenMain = ((self as any).bitwardenMain = new MainBackground()); const bitwardenMain = ((self as any).bitwardenMain = new MainBackground());
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling. bitwardenMain
// eslint-disable-next-line @typescript-eslint/no-floating-promises .bootstrap()
bitwardenMain.bootstrap().then(() => { .then(() => {
// Finished bootstrapping // Finished bootstrapping
}); if (BrowserApi.isManifestVersion(3)) {
startHeartbeat().catch((error) => logService.error(error));
}
})
.catch((error) => logService.error(error));
/**
* Tracks when a service worker was last alive and extends the service worker
* lifetime by writing the current time to extension storage every 20 seconds.
*/
async function runHeartbeat() {
await chrome.storage.local.set({ "last-heartbeat": new Date().getTime() });
}
/**
* Starts the heartbeat interval which keeps the service worker alive.
*/
async function startHeartbeat() {
// Run the heartbeat once at service worker startup, then again every 20 seconds.
runHeartbeat()
.then(() => setInterval(runHeartbeat, 20 * 1000))
.catch((error) => logService.error(error));
} }