diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 97b4fc32..5f7bd040 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -53,6 +53,12 @@ const BroadcasterSubscriptionId = "AppComponent"; const IdleTimeout = 60000 * 10; // 10 minutes const SyncInterval = 6 * 60 * 60 * 1000; // 6 hours +const systemTimeoutOptions = { + onLock: -2, + onSuspend: -3, + onIdle: -4, +}; + @Component({ selector: "app-root", styles: [], @@ -339,6 +345,12 @@ export class AppComponent implements OnInit { this.router.navigate(["vault"]); } break; + case "systemSuspended": + await this.checkForSystemTimeout(systemTimeoutOptions.onSuspend); + case "systemLocked": + await this.checkForSystemTimeout(systemTimeoutOptions.onLock); + case "systemIdle": + await this.checkForSystemTimeout(systemTimeoutOptions.onIdle); } }); }); @@ -583,4 +595,24 @@ export class AppComponent implements OnInit { } await this.systemService.startProcessReload(); } + + private async checkForSystemTimeout(timeout: number): Promise { + for (const userId in this.stateService.accounts.getValue()) { + if (userId == null) { + continue; + } + const options = await this.getVaultTimeoutOptions(userId); + if (options[0] === timeout) { + options[1] === "logOut" + ? this.logOut(false, userId) + : await this.vaultTimeoutService.lock(true, userId); + } + } + } + + private async getVaultTimeoutOptions(userId: string): Promise<[number, string]> { + const timeout = await this.stateService.getVaultTimeout({ userId: userId }); + const action = await this.stateService.getVaultTimeoutAction({ userId: userId }); + return [timeout, action]; + } } diff --git a/src/main/powerMonitor.main.ts b/src/main/powerMonitor.main.ts index da2d6f4d..23976c82 100644 --- a/src/main/powerMonitor.main.ts +++ b/src/main/powerMonitor.main.ts @@ -17,30 +17,20 @@ export class PowerMonitorMain { // 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"); - } + powerMonitor.on("suspend", () => { + this.main.messagingService.send("systemSuspended"); }); } 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"); - } + powerMonitor.on("lock-screen", () => { + this.main.messagingService.send("systemLocked"); }); } // System idle - global.setInterval(async () => { + global.setInterval(() => { const idleSeconds: number = powerMonitor.getSystemIdleTime(); const idle = idleSeconds >= IdleLockSeconds; if (idle) { @@ -48,21 +38,10 @@ export class PowerMonitorMain { 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"); - } + this.main.messagingService.send("systemIdle"); } this.idle = idle; }, IdleCheckInterval); } - - private async getVaultTimeoutOptions(): Promise<[number, string]> { - const timeout = await this.main.stateService.getVaultTimeout(); - const action = await this.main.stateService.getVaultTimeoutAction(); - return [timeout, action]; - } }