mirror of
https://github.com/bitwarden/browser.git
synced 2024-09-30 04:28:19 +02:00
disconnect and connect on idle states
This commit is contained in:
parent
cde6697721
commit
22c991f655
2
jslib
2
jslib
@ -1 +1 @@
|
|||||||
Subproject commit a67ea2422f082c6b884a3c7187e17a318048f7f5
|
Subproject commit d37fa836da523ae69413881f4af929c6808a688b
|
@ -1,18 +1,21 @@
|
|||||||
import MainBackground from './main.background';
|
import { ConstantsService } from 'jslib/services/constants.service';
|
||||||
|
|
||||||
import { ConstantsService } from 'jslib/services';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
LockService,
|
LockService,
|
||||||
StorageService,
|
StorageService,
|
||||||
} from 'jslib/abstractions';
|
} from 'jslib/abstractions';
|
||||||
|
import { NotificationsService } from 'jslib/abstractions/notifications.service';
|
||||||
|
|
||||||
|
const IdleInterval = 60 * 5; // 5 minutes
|
||||||
|
|
||||||
export default class IdleBackground {
|
export default class IdleBackground {
|
||||||
private idle: any;
|
private idle: any;
|
||||||
|
private idleTimer: number = null;
|
||||||
|
private idleState = 'active';
|
||||||
|
|
||||||
constructor(private main: MainBackground, private lockService: LockService,
|
constructor(private lockService: LockService, private storageService: StorageService,
|
||||||
private storageService: StorageService) {
|
private notificationsService: NotificationsService) {
|
||||||
this.idle = chrome.idle;
|
this.idle = chrome.idle || browser.idle;
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
@ -20,6 +23,20 @@ export default class IdleBackground {
|
|||||||
return;
|
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) {
|
if (this.idle.onStateChanged) {
|
||||||
this.idle.onStateChanged.addListener(async (newState: string) => {
|
this.idle.onStateChanged.addListener(async (newState: string) => {
|
||||||
if (newState === 'locked') {
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -180,7 +180,7 @@ export default class MainBackground {
|
|||||||
if (!this.isSafari) {
|
if (!this.isSafari) {
|
||||||
this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService,
|
this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService,
|
||||||
this.passwordGenerationService, this.analytics, this.platformUtilsService);
|
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.webRequestBackground = new WebRequestBackground(this.platformUtilsService, this.cipherService);
|
||||||
this.windowsBackground = new WindowsBackground(this);
|
this.windowsBackground = new WindowsBackground(this);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user