2018-02-11 05:24:22 +01:00
|
|
|
import { powerMonitor } from 'electron';
|
|
|
|
|
|
|
|
import { ConstantsService } from 'jslib/services/constants.service';
|
|
|
|
|
2018-07-26 22:50:05 +02:00
|
|
|
import { isSnapStore } from 'jslib/electron/utils';
|
|
|
|
|
2018-02-14 05:38:18 +01:00
|
|
|
import { Main } from '../main';
|
2018-02-11 05:24:22 +01:00
|
|
|
|
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-14 05:38:18 +01:00
|
|
|
constructor(private main: Main) { }
|
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
|
|
|
|
powerMonitor.on('suspend', async () => {
|
2020-04-01 17:18:36 +02:00
|
|
|
const options = await this.getVaultTimeoutOptions();
|
|
|
|
if (options[0] === -3) {
|
2020-05-28 22:21:21 +02:00
|
|
|
options[1] === 'logOut' ? this.main.messagingService.send('logout', { expired: false }) :
|
|
|
|
this.main.messagingService.send('lockVault');
|
2018-07-26 22:50:05 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-02-11 05:24:22 +01:00
|
|
|
|
2019-03-19 21:12:26 +01:00
|
|
|
if (process.platform !== 'linux') {
|
|
|
|
// System locked
|
|
|
|
powerMonitor.on('lock-screen', async () => {
|
2020-04-01 17:18:36 +02:00
|
|
|
const options = await this.getVaultTimeoutOptions();
|
|
|
|
if (options[0] === -2) {
|
2020-05-28 22:21:21 +02:00
|
|
|
options[1] === 'logOut' ? this.main.messagingService.send('logout', { expired: false }) :
|
|
|
|
this.main.messagingService.send('lockVault');
|
2019-03-19 21:12:26 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-01 17:18:36 +02:00
|
|
|
const options = await this.getVaultTimeoutOptions();
|
|
|
|
if (options[0] === -4) {
|
2020-05-28 22:21:21 +02:00
|
|
|
options[1] === 'logOut' ? this.main.messagingService.send('logout', { expired: false }) :
|
|
|
|
this.main.messagingService.send('lockVault');
|
2018-02-11 06:09:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.idle = idle;
|
|
|
|
}, IdleCheckInterval);
|
2018-02-11 05:24:22 +01:00
|
|
|
}
|
2018-02-11 06:09:47 +01:00
|
|
|
|
2020-04-01 17:18:36 +02:00
|
|
|
private async getVaultTimeoutOptions(): Promise<[number, string]> {
|
|
|
|
const timeout = await this.main.storageService.get<number>(ConstantsService.vaultTimeoutKey);
|
|
|
|
const action = await this.main.storageService.get<string>(ConstantsService.vaultTimeoutActionKey);
|
|
|
|
return [timeout, action];
|
2018-02-11 06:09:47 +01:00
|
|
|
}
|
2018-02-11 05:24:22 +01:00
|
|
|
}
|