diff --git a/apps/browser/src/background/idle.background.ts b/apps/browser/src/background/idle.background.ts index ccc76883b11..7ef6f7090bd 100644 --- a/apps/browser/src/background/idle.background.ts +++ b/apps/browser/src/background/idle.background.ts @@ -1,5 +1,8 @@ +import { firstValueFrom } from "rxjs"; + import { NotificationsService } from "@bitwarden/common/abstractions/notifications.service"; import { VaultTimeoutService } from "@bitwarden/common/abstractions/vault-timeout/vault-timeout.service"; +import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum"; import { BrowserStateService } from "../platform/services/abstractions/browser-state.service"; @@ -7,7 +10,7 @@ import { BrowserStateService } from "../platform/services/abstractions/browser-s const IdleInterval = 60 * 5; // 5 minutes export default class IdleBackground { - private idle: any; + private idle: typeof chrome.idle | typeof browser.idle | null; private idleTimer: number = null; private idleState = "active"; @@ -15,6 +18,7 @@ export default class IdleBackground { private vaultTimeoutService: VaultTimeoutService, private stateService: BrowserStateService, private notificationsService: NotificationsService, + private accountService: AccountService, ) { this.idle = chrome.idle || (browser != null ? browser.idle : null); } @@ -39,21 +43,27 @@ export default class IdleBackground { } if (this.idle.onStateChanged) { - this.idle.onStateChanged.addListener(async (newState: string) => { - if (newState === "locked") { - // If the screen is locked or the screensaver activates - const timeout = await this.stateService.getVaultTimeout(); - if (timeout === -2) { - // On System Lock vault timeout option - const action = await this.stateService.getVaultTimeoutAction(); - if (action === VaultTimeoutAction.LogOut) { - await this.vaultTimeoutService.logOut(); - } else { - await this.vaultTimeoutService.lock(); + this.idle.onStateChanged.addListener( + async (newState: chrome.idle.IdleState | browser.idle.IdleState) => { + if (newState === "locked") { + // Need to check if any of the current users have their timeout set to `onLocked` + const allUsers = await firstValueFrom(this.accountService.accounts$); + for (const userId in allUsers) { + // If the screen is locked or the screensaver activates + const timeout = await this.stateService.getVaultTimeout({ userId: userId }); + if (timeout === -2) { + // On System Lock vault timeout option + const action = await this.stateService.getVaultTimeoutAction({ userId: userId }); + if (action === VaultTimeoutAction.LogOut) { + await this.vaultTimeoutService.logOut(userId); + } else { + await this.vaultTimeoutService.lock(userId); + } + } } } - } - }); + }, + ); } } diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index 2c1573d4e9b..cc2439ed833 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -729,6 +729,7 @@ export default class MainBackground { this.vaultTimeoutService, this.stateService, this.notificationsService, + this.accountService, ); this.webRequestBackground = new WebRequestBackground( this.platformUtilsService,