diff --git a/src/background/idle.background.ts b/src/background/idle.background.ts new file mode 100644 index 0000000000..514a80caf3 --- /dev/null +++ b/src/background/idle.background.ts @@ -0,0 +1,31 @@ +import ConstantsService from '../services/constants.service'; +import LockService from '../services/lock.service'; +import MainBackground from './main.background'; + +import { StorageService } from '../services/abstractions/storage.service'; + +export default class IdleBackground { + private idle: any; + + constructor(private main: MainBackground, private lockService: LockService, + private storageService: StorageService) { + this.idle = chrome.idle; + } + + async init() { + if (!this.idle) { + return; + } + + if (this.idle.onStateChanged) { + this.idle.onStateChanged.addListener(async (newState: string) => { + if (newState === 'locked') { + const lockOption = await this.storageService.get(ConstantsService.lockOptionKey); + if (lockOption === -2) { + this.lockService.lock(); + } + } + }); + } + } +} diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 255dc5edb4..9c2f2977f8 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -6,6 +6,7 @@ import BrowserApi from '../browser/browserApi'; import CommandsBackground from './commands.background'; import ContextMenusBackground from './contextMenus.background'; +import IdleBackground from './idle.background'; import RuntimeBackground from './runtime.background'; import TabsBackground from './tabs.background'; import WebRequestBackground from './webRequest.background'; @@ -67,6 +68,7 @@ export default class MainBackground { private commandsBackground: CommandsBackground; private contextMenusBackground: ContextMenusBackground; + private idleBackground: IdleBackground; private runtimeBackground: RuntimeBackground; private tabsBackground: TabsBackground; private webRequestBackground: WebRequestBackground; @@ -118,6 +120,7 @@ export default class MainBackground { this.commandsBackground = new CommandsBackground(this, this.passwordGenerationService); this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService, this.passwordGenerationService); + this.idleBackground = new IdleBackground(this, this.lockService, this.storageService); this.runtimeBackground = new RuntimeBackground(this, this.autofillService, this.cipherService); this.tabsBackground = new TabsBackground(this); this.webRequestBackground = new WebRequestBackground(this.browserUtilsService, this.cipherService); @@ -127,6 +130,7 @@ export default class MainBackground { async bootstrap() { await this.commandsBackground.init(); await this.contextMenusBackground.init(); + await this.idleBackground.init(); await this.runtimeBackground.init(); await this.tabsBackground.init(); await this.webRequestBackground.init(); diff --git a/src/services/lock.service.ts b/src/services/lock.service.ts index 7f5f56e559..4476ee6f87 100644 --- a/src/services/lock.service.ts +++ b/src/services/lock.service.ts @@ -14,18 +14,6 @@ export default class LockService { 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 this.storageService.get(ConstantsService.lockOptionKey); - if (lockOption === -2) { - self.lock(); - } - } - }); - } } async checkLock(): Promise {