mirror of
https://github.com/bitwarden/browser
synced 2025-12-18 01:03:35 +00:00
convert lock service to ts
This commit is contained in:
87
src/services/lock.service.ts
Normal file
87
src/services/lock.service.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import CipherService from './cipher.service';
|
||||
import ConstantsService from './constants.service';
|
||||
import CryptoService from './crypto.service';
|
||||
import FolderService from './folder.service';
|
||||
import UtilsService from './utils.service';
|
||||
|
||||
export default class LockService {
|
||||
constructor(private cipherService: CipherService, private folderService: FolderService,
|
||||
private cryptoService: CryptoService, private utilsService: UtilsService,
|
||||
private setIcon: Function, private refreshBadgeAndMenu: Function) {
|
||||
this.checkLock();
|
||||
setInterval(() => this.checkLock(), 10 * 1000); // check every 10 seconds
|
||||
|
||||
const self = this;
|
||||
if ((window as any).chrome.idle && (window as any).chrome.idle.onStateChanged) {
|
||||
(window as any).chrome.idle.onStateChanged.addListener(async (newState: string) => {
|
||||
if (newState === 'locked') {
|
||||
const lockOption = await UtilsService.getObjFromStorage<number>(ConstantsService.lockOptionKey);
|
||||
if (lockOption === -2) {
|
||||
self.lock();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async checkLock(): Promise<void> {
|
||||
const popupOpen = chrome.extension.getViews({ type: 'popup' }).length > 0;
|
||||
const tabOpen = chrome.extension.getViews({ type: 'tab' }).length > 0;
|
||||
const sidebarView = this.sidebarViewName();
|
||||
const sidebarOpen = sidebarView != null && chrome.extension.getViews({ type: sidebarView }).length > 0;
|
||||
|
||||
if (popupOpen || tabOpen || sidebarOpen) {
|
||||
// Do not lock
|
||||
return;
|
||||
}
|
||||
|
||||
const key = await this.cryptoService.getKey();
|
||||
if (key == null) {
|
||||
// no key so no need to lock
|
||||
return;
|
||||
}
|
||||
|
||||
const lockOption = await UtilsService.getObjFromStorage<number>(ConstantsService.lockOptionKey);
|
||||
if (lockOption == null || lockOption < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const lastActive = await UtilsService.getObjFromStorage<number>(ConstantsService.lastActiveKey);
|
||||
if (lastActive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const lockOptionSeconds = lockOption * 60;
|
||||
const diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
|
||||
if (diffSeconds >= lockOptionSeconds) {
|
||||
// need to lock now
|
||||
await this.lock();
|
||||
}
|
||||
}
|
||||
|
||||
async lock(): Promise<void> {
|
||||
await Promise.all([
|
||||
this.cryptoService.clearKey(),
|
||||
this.cryptoService.clearOrgKeys(true),
|
||||
this.cryptoService.clearPrivateKey(true),
|
||||
this.cryptoService.clearEncKey(true),
|
||||
]);
|
||||
|
||||
this.setIcon();
|
||||
this.folderService.clearCache();
|
||||
this.cipherService.clearCache();
|
||||
this.refreshBadgeAndMenu();
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
private sidebarViewName(): string {
|
||||
if ((window as any).chrome.sidebarAction && this.utilsService.isFirefox()) {
|
||||
return 'sidebar';
|
||||
} else if (this.utilsService.isOpera() && (typeof opr !== 'undefined') && opr.sidebarAction) {
|
||||
return 'sidebar_panel';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user