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

75 lines
2.1 KiB
TypeScript
Raw Normal View History

2018-02-11 07:21:01 +01:00
import {
app,
ipcMain,
} from 'electron';
import {
deletePassword,
getPassword,
setPassword,
} from 'keytar';
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
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;
2018-02-14 05:38:18 +01:00
constructor(private main: Main) { }
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
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;
2018-02-11 07:21:01 +01:00
} catch {
2018-02-08 19:10:13 +01:00
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 'scheduleNextSync':
this.scheduleNextSync();
break;
2018-02-14 06:26:32 +01:00
case 'updateAppMenu':
this.main.menuMain.updateApplicationMenuState(message.isAuthenticated, message.isLocked);
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-02-08 19:10:13 +01:00
}