1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-24 21:41:33 +01:00
bitwarden-browser/apps/desktop/src/main/power-monitor.main.ts
Bernd Schoolmann 5cf29a655b
[PM-9149] Enable "Timeout on System Lock" on Linux Desktop (#9645)
* Enable system lock detection on linux

* Fix order of vault timeout options

* Port to new plit core / napi desktop native crates

* Make unimplemented implementation panic for on_lock

* Remove unecessary String::from

* Update cargo lock

* Extract generation of vault timeout options
2024-07-25 17:09:03 +02:00

68 lines
1.8 KiB
TypeScript

import { ipcMain, powerMonitor } from "electron";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessageSender } from "@bitwarden/common/platform/messaging";
import { powermonitors } from "@bitwarden/desktop-napi";
import { isSnapStore } from "../utils";
// tslint:disable-next-line
const IdleLockSeconds = 5 * 60; // 5 minutes
const IdleCheckInterval = 30 * 1000; // 30 seconds
export class PowerMonitorMain {
private idle = false;
constructor(
private messagingService: MessageSender,
private logService: LogService,
) {}
init() {
// ref: https://github.com/electron/electron/issues/13767
if (!isSnapStore()) {
// System sleep
powerMonitor.on("suspend", () => {
this.messagingService.send("systemSuspended");
});
}
if (process.platform !== "linux") {
// System locked
powerMonitor.on("lock-screen", () => {
this.messagingService.send("systemLocked");
});
} else {
powermonitors
.onLock(() => {
this.messagingService.send("systemLocked");
})
.catch((error) => {
this.logService.error("Error setting up lock monitor", { error });
});
}
ipcMain.handle("powermonitor.isLockMonitorAvailable", async (_event: any, _message: any) => {
if (process.platform !== "linux") {
return true;
} else {
return await powermonitors.isLockMonitorAvailable();
}
});
// System idle
global.setInterval(() => {
const idleSeconds: number = powerMonitor.getSystemIdleTime();
const idle = idleSeconds >= IdleLockSeconds;
if (idle) {
if (this.idle) {
return;
}
this.messagingService.send("systemIdle");
}
this.idle = idle;
}, IdleCheckInterval);
}
}