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

66 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-02-11 05:24:22 +01:00
import { powerMonitor } from 'electron';
import { isSnapStore } from 'jslib-electron/utils';
2018-07-26 22:50:05 +02:00
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 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 () => {
const options = await this.getVaultTimeoutOptions();
if (options[0] === -3) {
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 () => {
const options = await this.getVaultTimeoutOptions();
if (options[0] === -2) {
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 () => {
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;
}
const options = await this.getVaultTimeoutOptions();
if (options[0] === -4) {
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
private async getVaultTimeoutOptions(): Promise<[number, string]> {
[Account Switching] [Feature] Add the ability to maintain state for up to 5 accounts at once (#1079) * [refactor] Remove references to deprecated services * [feature] Implement account switching * [bug] Fix state handling for authentication dependent system menu items * [bug] Enable the account switcher to fucntion properly when switching to a locked accounts * [feature] Enable locking any account from the menu * [bug] Ensure the avatar instance used in the account switcher updates on account change * [style] Fix lint complaints * [bug] Ensure the logout command callback can handle any user in state * [style] Fix lint complaints * rollup * [style] Fix lint complaints * [bug] Don't clean up state until everything else is done on logout * [bug] Navigate to vault on a succesful account switch * [bug] Init the state service on start * [feature] Limit account switching to 5 account maximum * [bug] Resolve app lock state with 5 logged out accounts * [chore] Update account refrences to match recent jslib restructuring * [bug] Add missing awaits * [bug] Update app menu on logout * [bug] Hide the switcher if there are no authed accounts * [bug] Move authenticationStatus display information out of jslib * [bug] Remove unused active style from scss * [refactor] Rewrite the menu bar * [style] Fix lint complaints * [bug] Clean state of loggout out user after redirect * [bug] Redirect on logout if not explicity provided a userId that isn't active * [bug] Relocated several settings items to persistant storage * [bug] Correct account switcher styles on all themes * [chore] Include state migration service in services * [bug] Swap to next account on logout * [bug] Correct DI service * [bug] fix loginGuard deps in services.module * [chore] update jslib * [bug] Remove badly merged scss * [chore] update jslib * [review] Code review cleanup * [review] Code review cleanup Co-authored-by: Hinton <oscar@oscarhinton.com>
2021-12-15 23:32:00 +01:00
const timeout = await this.main.stateService.getVaultTimeout();
const action = await this.main.stateService.getVaultTimeoutAction();
return [timeout, action];
2018-02-11 06:09:47 +01:00
}
2018-02-11 05:24:22 +01:00
}