1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-11 10:10:25 +01:00
bitwarden-browser/apps/browser/src/background/idle.background.ts
Cesar Gonzalez 670f33daa8
[PM-5743] Implement eslint rule for usage of window object in background script (#7849)
* [PM-5742] Rework Usage of Extension APIs that Cannot be Called with the Background Service Worker

* [PM-5742] Implementing jest tests for the updated BrowserApi methods

* [PM-5742] Implementing jest tests to validate logic within added API calls

* [PM-5742] Implementing jest tests to validate logic within added API calls

* [PM-5742] Fixing broken Jest tests

* [PM-5742] Fixing linter error

* [PM-5887] Refactor WebCryptoFunction to Remove Usage of the window Object in the Background Script

* [PM-5878] Rework `window` call within OverlayBackground to function within AutofillOverlayIframe service

* [PM-6122] Rework `window` call within NotificationBackground to function within content script

* [PM-5881] Adjust usage of the `chrome.extension.getViews` API to ensure expected behavior in manifest v3

* [PM-5881] Reworking how we handle early returns from `reloadOpenWindows`

* [PM-5881] Implementing jest test to validate changes within BrowserApi.reloadOpenWindows

* [PM-5743] Implement eslint rule to impeede usage of the `window` object in the background script

* [PM-5743] Working through fixing eslint rule errors, and setting up ignore statements for lines that will be refactored at a later date

* [PM-5743] Fixing broken jest tests

* [PM-5879] Removing `backgroundWindow` reference used for determing system theme preference in Safari

* [PM-5879] Removing `backgroundWindow` reference used for determing system theme preference in Safari

* [PM-5743] Updating references to NodeJS.Timeout

* [PM-5743] Adding notification bar and overaly content scripts to the eslint excluded files key

* [PM-5743] Adding other excluded files from the eslint rule

* [PM-5743] Reworking implementation to have the .eslintrc.json file present within the browser subdirectory
2024-03-29 15:55:23 +00:00

90 lines
3.6 KiB
TypeScript

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";
const IdleInterval = 60 * 5; // 5 minutes
export default class IdleBackground {
private idle: typeof chrome.idle | typeof browser.idle | null;
private idleTimer: number | NodeJS.Timeout = null;
private idleState = "active";
constructor(
private vaultTimeoutService: VaultTimeoutService,
private stateService: BrowserStateService,
private notificationsService: NotificationsService,
private accountService: AccountService,
) {
this.idle = chrome.idle || (browser != null ? browser.idle : null);
}
async init() {
if (!this.idle) {
return;
}
const idleHandler = (newState: string) => {
if (newState === "active") {
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.notificationsService.reconnectFromActivity();
} else {
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
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: 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);
}
}
}
}
},
);
}
}
private pollIdle(handler: (newState: string) => void) {
if (this.idleTimer != null) {
globalThis.clearTimeout(this.idleTimer);
this.idleTimer = null;
}
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.idle.queryState(IdleInterval, (state: string) => {
if (state !== this.idleState) {
this.idleState = state;
handler(state);
}
this.idleTimer = globalThis.setTimeout(() => this.pollIdle(handler), 5000);
});
}
}