2024-07-25 17:09:03 +02:00
|
|
|
import { ipcMain, powerMonitor } from "electron";
|
2018-02-11 05:24:22 +01:00
|
|
|
|
2024-07-25 17:09:03 +02:00
|
|
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
2024-04-19 21:02:40 +02:00
|
|
|
import { MessageSender } from "@bitwarden/common/platform/messaging";
|
2024-07-25 17:09:03 +02:00
|
|
|
import { powermonitors } from "@bitwarden/desktop-napi";
|
2024-04-19 21:02:40 +02:00
|
|
|
|
2022-12-02 12:45:09 +01:00
|
|
|
import { isSnapStore } from "../utils";
|
2018-02-11 05:24:22 +01:00
|
|
|
|
2018-02-11 06:09:47 +01:00
|
|
|
// tslint:disable-next-line
|
|
|
|
const IdleLockSeconds = 5 * 60; // 5 minutes
|
|
|
|
const IdleCheckInterval = 30 * 1000; // 30 seconds
|
|
|
|
|
2018-02-11 05:24:22 +01:00
|
|
|
export class PowerMonitorMain {
|
2022-02-24 20:50:19 +01:00
|
|
|
private idle = false;
|
2018-02-11 06:09:47 +01:00
|
|
|
|
2024-07-25 17:09:03 +02:00
|
|
|
constructor(
|
|
|
|
private messagingService: MessageSender,
|
|
|
|
private logService: LogService,
|
|
|
|
) {}
|
2018-02-11 05:24:22 +01:00
|
|
|
|
|
|
|
init() {
|
2018-07-26 22:50:05 +02:00
|
|
|
// ref: https://github.com/electron/electron/issues/13767
|
|
|
|
if (!isSnapStore()) {
|
|
|
|
// System sleep
|
2022-02-01 17:31:30 +01:00
|
|
|
powerMonitor.on("suspend", () => {
|
2023-03-10 21:32:26 +01:00
|
|
|
this.messagingService.send("systemSuspended");
|
2021-12-20 15:47:17 +01:00
|
|
|
});
|
|
|
|
}
|
2018-02-11 05:24:22 +01:00
|
|
|
|
2019-03-19 21:12:26 +01:00
|
|
|
if (process.platform !== "linux") {
|
|
|
|
// System locked
|
2022-02-01 17:31:30 +01:00
|
|
|
powerMonitor.on("lock-screen", () => {
|
2023-03-10 21:32:26 +01:00
|
|
|
this.messagingService.send("systemLocked");
|
2021-12-20 15:47:17 +01:00
|
|
|
});
|
2024-07-25 17:09:03 +02:00
|
|
|
} else {
|
|
|
|
powermonitors
|
|
|
|
.onLock(() => {
|
|
|
|
this.messagingService.send("systemLocked");
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.logService.error("Error setting up lock monitor", { error });
|
|
|
|
});
|
2021-12-20 15:47:17 +01:00
|
|
|
}
|
2024-07-25 17:09:03 +02:00
|
|
|
ipcMain.handle("powermonitor.isLockMonitorAvailable", async (_event: any, _message: any) => {
|
|
|
|
if (process.platform !== "linux") {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return await powermonitors.isLockMonitorAvailable();
|
|
|
|
}
|
|
|
|
});
|
2019-03-19 21:12:26 +01:00
|
|
|
|
2018-02-11 06:09:47 +01:00
|
|
|
// System idle
|
2022-02-01 17:31:30 +01:00
|
|
|
global.setInterval(() => {
|
2021-02-03 18:47:44 +01:00
|
|
|
const idleSeconds: number = powerMonitor.getSystemIdleTime();
|
2018-02-11 06:09:47 +01:00
|
|
|
const idle = idleSeconds >= IdleLockSeconds;
|
|
|
|
if (idle) {
|
|
|
|
if (this.idle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-10 21:32:26 +01:00
|
|
|
this.messagingService.send("systemIdle");
|
2018-02-11 06:09:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.idle = idle;
|
|
|
|
}, IdleCheckInterval);
|
2018-02-11 05:24:22 +01:00
|
|
|
}
|
|
|
|
}
|