diff --git a/jslib b/jslib index a67ea2422f..d37fa836da 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit a67ea2422f082c6b884a3c7187e17a318048f7f5 +Subproject commit d37fa836da523ae69413881f4af929c6808a688b diff --git a/src/background/idle.background.ts b/src/background/idle.background.ts index de02b0772f..8054c5a95f 100644 --- a/src/background/idle.background.ts +++ b/src/background/idle.background.ts @@ -1,18 +1,21 @@ -import MainBackground from './main.background'; - -import { ConstantsService } from 'jslib/services'; +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 main: MainBackground, private lockService: LockService, - private storageService: StorageService) { - this.idle = chrome.idle; + constructor(private lockService: LockService, private storageService: StorageService, + private notificationsService: NotificationsService) { + this.idle = chrome.idle || browser.idle; } async init() { @@ -20,6 +23,20 @@ export default class IdleBackground { 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') { @@ -31,4 +48,18 @@ export default class IdleBackground { }); } } + + 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); + }); + } } diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 1c751d012a..0a6dfec028 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -180,7 +180,7 @@ export default class MainBackground { if (!this.isSafari) { this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService, this.passwordGenerationService, this.analytics, this.platformUtilsService); - this.idleBackground = new IdleBackground(this, this.lockService, this.storageService); + this.idleBackground = new IdleBackground(this.lockService, this.storageService, this.notificationsService); this.webRequestBackground = new WebRequestBackground(this.platformUtilsService, this.cipherService); this.windowsBackground = new WindowsBackground(this); }