bitwarden-estensione-browser/src/main/powerMonitor.main.ts

52 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-02-11 05:24:22 +01:00
import { powerMonitor } from 'electron';
import { ConstantsService } from 'jslib/services/constants.service';
import { MessagingService } from 'jslib/abstractions/messaging.service';
import { StorageService } from 'jslib/abstractions/storage.service';
2018-02-11 06:09:47 +01:00
// tslint:disable-next-line
const desktopIdle = require('desktop-idle');
const IdleLockSeconds = 5 * 60; // 5 minutes
const IdleCheckInterval = 30 * 1000; // 30 seconds
2018-02-11 05:24:22 +01:00
export class PowerMonitorMain {
2018-02-11 06:09:47 +01:00
private idle: boolean = false;
2018-02-11 05:24:22 +01:00
constructor(private storageService: StorageService, private messagingService: MessagingService) { }
init() {
// System sleep
powerMonitor.on('suspend', async () => {
2018-02-11 06:09:47 +01:00
const lockOption = await this.getLockOption();
2018-02-11 05:24:22 +01:00
if (lockOption === -3) {
this.messagingService.send('lockVault');
}
});
2018-02-11 06:09:47 +01:00
// 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.messagingService.send('lockVault');
}
}
this.idle = idle;
}, IdleCheckInterval);
2018-02-11 05:24:22 +01:00
// TODO: System locked
}
2018-02-11 06:09:47 +01:00
private async getLockOption(): Promise<number> {
return await this.storageService.get<number>(ConstantsService.lockOptionKey);
}
2018-02-11 05:24:22 +01:00
}