1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-21 18:53:29 +00:00
Files
browser/src/main/powerMonitor.main.ts
2019-03-19 16:12:26 -04:00

64 lines
1.9 KiB
TypeScript

import { powerMonitor } from 'electron';
import { ConstantsService } from 'jslib/services/constants.service';
import { isSnapStore } from 'jslib/electron/utils';
import { Main } from '../main';
// tslint:disable-next-line
const desktopIdle = require('desktop-idle');
const IdleLockSeconds = 5 * 60; // 5 minutes
const IdleCheckInterval = 30 * 1000; // 30 seconds
export class PowerMonitorMain {
private idle: boolean = false;
constructor(private main: Main) { }
init() {
// ref: https://github.com/electron/electron/issues/13767
if (!isSnapStore()) {
// System sleep
powerMonitor.on('suspend', async () => {
const lockOption = await this.getLockOption();
if (lockOption === -3) {
this.main.messagingService.send('lockVault');
}
});
}
if (process.platform !== 'linux') {
// System locked
powerMonitor.on('lock-screen', async () => {
const lockOption = await this.getLockOption();
if (lockOption === -2) {
this.main.messagingService.send('lockVault');
}
});
}
// System idle
global.setInterval(async () => {
const idleSeconds: number = desktopIdle.getIdleTime();
const idle = idleSeconds >= IdleLockSeconds;
if (idle) {
if (this.idle) {
return;
}
const lockOption = await this.getLockOption();
if (lockOption === -4) {
this.main.messagingService.send('lockVault');
}
}
this.idle = idle;
}, IdleCheckInterval);
}
private getLockOption(): Promise<number> {
return this.main.storageService.get<number>(ConstantsService.lockOptionKey);
}
}