1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-11 10:10:25 +01:00
bitwarden-browser/src/main/messaging.main.ts

67 lines
2.0 KiB
TypeScript
Raw Normal View History

2018-02-08 19:10:13 +01:00
import { app, ipcMain } from 'electron';
2018-02-11 06:36:32 +01:00
import { getPassword, setPassword, deletePassword } from 'keytar';
2018-02-08 19:10:13 +01:00
2018-02-09 21:49:00 +01:00
import { WindowMain } from './window.main';
2018-02-11 07:02:04 +01:00
const KeytarService = 'Bitwarden';
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 windowMain: WindowMain) { }
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
ipcMain.on('keytar', async (event: any, message: any) => {
try {
let val: string = null;
if (message.action && message.key) {
if (message.action === 'getPassword') {
val = await getPassword(KeytarService, message.key);
} else if (message.action === 'setPassword' && message.value) {
await setPassword(KeytarService, message.key, message.value);
} else if (message.action === 'deletePassword') {
await deletePassword(KeytarService, message.key);
}
}
event.returnValue = val;
}
catch {
event.returnValue = null;
}
});
}
2018-02-09 21:49:00 +01:00
2018-02-11 05:24:22 +01:00
onMessage(message: any) {
switch (message.command) {
case 'loggedIn':
break;
case 'logout':
break;
case 'syncCompleted':
break;
case 'scheduleNextSync':
this.scheduleNextSync();
break;
default:
break;
}
}
2018-02-09 21:49:00 +01:00
private scheduleNextSync() {
if (this.syncTimeout) {
global.clearTimeout(this.syncTimeout);
}
this.syncTimeout = global.setTimeout(() => {
this.windowMain.win.webContents.send('messagingService', {
command: 'checkSyncVault',
});
}, SyncInterval);
}
2018-02-08 19:10:13 +01:00
}