2022-06-14 17:10:53 +02:00
|
|
|
import { NotificationsService } from "@bitwarden/common/abstractions/notifications.service";
|
2022-08-30 16:11:19 +02:00
|
|
|
import { VaultTimeoutService } from "@bitwarden/common/abstractions/vaultTimeout/vaultTimeout.service";
|
2023-04-15 01:11:33 +02:00
|
|
|
import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum";
|
2021-09-17 15:44:27 +02:00
|
|
|
|
2023-06-06 22:34:53 +02:00
|
|
|
import { BrowserStateService } from "../platform/services/abstractions/browser-state.service";
|
2018-08-23 03:10:23 +02:00
|
|
|
|
|
|
|
const IdleInterval = 60 * 5; // 5 minutes
|
2018-01-04 23:04:26 +01:00
|
|
|
|
|
|
|
export default class IdleBackground {
|
|
|
|
private idle: any;
|
2018-08-23 03:10:23 +02:00
|
|
|
private idleTimer: number = null;
|
|
|
|
private idleState = "active";
|
2018-01-04 23:04:26 +01:00
|
|
|
|
2020-04-06 17:40:16 +02:00
|
|
|
constructor(
|
|
|
|
private vaultTimeoutService: VaultTimeoutService,
|
2022-11-23 23:26:57 +01:00
|
|
|
private stateService: BrowserStateService,
|
2018-08-23 03:10:23 +02:00
|
|
|
private notificationsService: NotificationsService
|
|
|
|
) {
|
2019-06-29 05:33:53 +02:00
|
|
|
this.idle = chrome.idle || (browser != null ? browser.idle : null);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 23:04:26 +01:00
|
|
|
async init() {
|
|
|
|
if (!this.idle) {
|
2021-12-21 15:43:35 +01:00
|
|
|
return;
|
2018-01-04 23:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const idleHandler = (newState: string) => {
|
2018-08-23 03:10:23 +02:00
|
|
|
if (newState === "active") {
|
2018-01-04 23:04:26 +01:00
|
|
|
this.notificationsService.reconnectFromActivity();
|
2021-12-21 15:43:35 +01:00
|
|
|
} else {
|
2018-08-23 03:10:23 +02:00
|
|
|
this.notificationsService.disconnectFromInactivity();
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
};
|
2018-01-04 23:04:26 +01:00
|
|
|
if (this.idle.onStateChanged && this.idle.setDetectionInterval) {
|
|
|
|
this.idle.setDetectionInterval(IdleInterval);
|
2018-08-23 03:10:23 +02:00
|
|
|
this.idle.onStateChanged.addListener(idleHandler);
|
2018-01-04 23:04:26 +01:00
|
|
|
} else {
|
2018-08-23 03:10:23 +02:00
|
|
|
this.pollIdle(idleHandler);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2018-01-04 23:04:26 +01:00
|
|
|
|
2018-08-23 03:10:23 +02:00
|
|
|
if (this.idle.onStateChanged) {
|
|
|
|
this.idle.onStateChanged.addListener(async (newState: string) => {
|
|
|
|
if (newState === "locked") {
|
|
|
|
// If the screen is locked or the screensaver activates
|
2022-01-27 22:22:51 +01:00
|
|
|
const timeout = await this.stateService.getVaultTimeout();
|
2020-04-06 17:40:16 +02:00
|
|
|
if (timeout === -2) {
|
|
|
|
// On System Lock vault timeout option
|
2022-01-27 22:22:51 +01:00
|
|
|
const action = await this.stateService.getVaultTimeoutAction();
|
2023-04-15 01:11:33 +02:00
|
|
|
if (action === VaultTimeoutAction.LogOut) {
|
2018-08-23 03:10:23 +02:00
|
|
|
await this.vaultTimeoutService.logOut();
|
|
|
|
} else {
|
2022-08-25 19:09:27 +02:00
|
|
|
await this.vaultTimeoutService.lock();
|
2018-08-23 03:10:23 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-04 23:04:26 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
2018-01-04 23:04:26 +01:00
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2018-08-23 03:10:23 +02:00
|
|
|
|
|
|
|
private pollIdle(handler: (newState: string) => void) {
|
|
|
|
if (this.idleTimer != null) {
|
|
|
|
window.clearTimeout(this.idleTimer);
|
|
|
|
this.idleTimer = null;
|
|
|
|
}
|
|
|
|
this.idle.queryState(IdleInterval, (state: string) => {
|
|
|
|
if (state !== this.idleState) {
|
|
|
|
this.idleState = state;
|
|
|
|
handler(state);
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2018-08-23 03:10:23 +02:00
|
|
|
this.idleTimer = window.setTimeout(() => this.pollIdle(handler), 5000);
|
2021-12-21 15:43:35 +01:00
|
|
|
});
|
|
|
|
}
|
2018-01-04 23:04:26 +01:00
|
|
|
}
|