bitwarden-desktop/src/main/messaging.main.ts

78 lines
2.5 KiB
TypeScript
Raw Normal View History

import { ipcMain } from 'electron';
2018-02-08 19:10:13 +01:00
2018-02-14 05:38:18 +01:00
import { Main } from '../main';
2018-02-09 21:49:00 +01:00
import { ElectronConstants } from 'jslib/electron/electronConstants';
import { StorageService } from 'jslib/abstractions/storage.service';
2018-02-09 21:49:00 +01:00
const SyncInterval = 5 * 60 * 1000; // 5 minutes
2018-02-08 19:10:13 +01:00
export class MessagingMain {
2018-02-09 21:49:00 +01:00
private syncTimeout: NodeJS.Timer;
constructor(private main: Main, private storageService: StorageService) { }
2018-02-09 21:49:00 +01:00
2018-02-08 19:10:13 +01:00
init() {
2018-02-09 21:49:00 +01:00
this.scheduleNextSync();
2018-02-11 05:24:22 +01:00
ipcMain.on('messagingService', async (event: any, message: any) => this.onMessage(message));
2018-02-08 19:10:13 +01:00
}
2018-02-09 21:49:00 +01:00
2018-02-11 05:24:22 +01:00
onMessage(message: any) {
switch (message.command) {
case 'scheduleNextSync':
this.scheduleNextSync();
break;
2018-02-14 06:26:32 +01:00
case 'updateAppMenu':
this.main.menuMain.updateApplicationMenuState(message.isAuthenticated, message.isLocked);
2018-05-05 06:29:02 +02:00
this.updateTrayMenu(message.isAuthenticated, message.isLocked);
break;
case 'minimizeOnCopy':
this.storageService.get<boolean>(ElectronConstants.minimizeOnCopyToClipboardKey).then(
(shouldMinimize) => {
if (shouldMinimize && this.main.windowMain.win != null) {
this.main.windowMain.win.minimize();
}
});
break;
2018-05-05 06:29:02 +02:00
case 'showTray':
this.main.trayMain.showTray();
break;
case 'removeTray':
this.main.trayMain.removeTray();
break;
case 'hideToTray':
this.main.trayMain.hideToTray();
2018-02-14 06:26:32 +01:00
break;
2018-02-11 05:24:22 +01:00
default:
break;
}
}
2018-02-09 21:49:00 +01:00
private scheduleNextSync() {
if (this.syncTimeout) {
global.clearTimeout(this.syncTimeout);
}
this.syncTimeout = global.setTimeout(() => {
2018-02-16 15:59:03 +01:00
if (this.main.windowMain.win == null) {
return;
}
2018-02-14 05:38:18 +01:00
this.main.windowMain.win.webContents.send('messagingService', {
2018-02-09 21:49:00 +01:00
command: 'checkSyncVault',
});
}, SyncInterval);
}
2018-05-05 06:29:02 +02:00
private updateTrayMenu(isAuthenticated: boolean, isLocked: boolean) {
2018-05-30 05:11:28 +02:00
if (this.main.trayMain == null || this.main.trayMain.contextMenu == null) {
return;
}
2018-05-05 06:29:02 +02:00
const lockNowTrayMenuItem = this.main.trayMain.contextMenu.getMenuItemById('lockNow');
if (lockNowTrayMenuItem != null) {
lockNowTrayMenuItem.enabled = isAuthenticated && !isLocked;
}
}
2018-02-08 19:10:13 +01:00
}