1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-03 13:33:32 +01:00
bitwarden-browser/src/background/idle.background.ts
2018-08-22 21:10:23 -04:00

66 lines
2.1 KiB
TypeScript

import { ConstantsService } from 'jslib/services/constants.service';
import {
LockService,
StorageService,
} from 'jslib/abstractions';
import { NotificationsService } from 'jslib/abstractions/notifications.service';
const IdleInterval = 60 * 5; // 5 minutes
export default class IdleBackground {
private idle: any;
private idleTimer: number = null;
private idleState = 'active';
constructor(private lockService: LockService, private storageService: StorageService,
private notificationsService: NotificationsService) {
this.idle = chrome.idle || browser.idle;
}
async init() {
if (!this.idle) {
return;
}
const idleHandler = (newState: string) => {
if (newState === 'active') {
this.notificationsService.reconnectFromActivity();
} else {
this.notificationsService.disconnectFromInactivity();
}
};
if (this.idle.onStateChanged && this.idle.setDetectionInterval) {
this.idle.setDetectionInterval(IdleInterval);
this.idle.onStateChanged.addListener(idleHandler);
} else {
this.pollIdle(idleHandler);
}
if (this.idle.onStateChanged) {
this.idle.onStateChanged.addListener(async (newState: string) => {
if (newState === 'locked') {
const lockOption = await this.storageService.get<number>(ConstantsService.lockOptionKey);
if (lockOption === -2) {
this.lockService.lock();
}
}
});
}
}
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);
}
this.idleTimer = window.setTimeout(() => this.pollIdle(handler), 5000);
});
}
}