2020-11-25 20:43:29 +01:00
|
|
|
import { app, ipcMain } from 'electron';
|
2020-11-27 10:58:47 +01:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
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
|
|
|
|
2021-06-07 19:26:36 +02:00
|
|
|
import { ElectronConstants } from 'jslib-electron/electronConstants';
|
2020-04-14 22:52:03 +02:00
|
|
|
|
2021-06-07 19:26:36 +02:00
|
|
|
import { StorageService } from 'jslib-common/abstractions/storage.service';
|
2020-04-14 22:52:03 +02:00
|
|
|
|
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;
|
|
|
|
|
2020-04-14 22:52:03 +02:00
|
|
|
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();
|
2020-11-27 11:34:09 +01:00
|
|
|
if (process.platform === 'linux') {
|
|
|
|
this.storageService.save(ElectronConstants.openAtLogin, fs.existsSync(this.linuxStartupFile()));
|
|
|
|
} else {
|
2020-11-25 20:43:29 +01:00
|
|
|
const loginSettings = app.getLoginItemSettings();
|
|
|
|
this.storageService.save(ElectronConstants.openAtLogin, loginSettings.openAtLogin);
|
|
|
|
}
|
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':
|
2021-11-09 19:00:01 +01:00
|
|
|
this.main.menuMain.updateApplicationMenuState(message.isAuthenticated, message.isLocked,
|
|
|
|
message.enableChangeMasterPass);
|
2018-05-05 06:29:02 +02:00
|
|
|
this.updateTrayMenu(message.isAuthenticated, message.isLocked);
|
|
|
|
break;
|
2020-04-14 22:52:03 +02:00
|
|
|
case 'minimizeOnCopy':
|
|
|
|
this.storageService.get<boolean>(ElectronConstants.minimizeOnCopyToClipboardKey).then(
|
2021-02-03 19:21:22 +01:00
|
|
|
shouldMinimize => {
|
2020-11-30 15:10:57 +01:00
|
|
|
if (shouldMinimize && this.main.windowMain.win !== null) {
|
2020-04-14 22:52:03 +02:00
|
|
|
this.main.windowMain.win.minimize();
|
|
|
|
}
|
|
|
|
});
|
2020-04-14 22:11:41 +02:00
|
|
|
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;
|
2020-11-25 20:43:29 +01:00
|
|
|
case 'addOpenAtLogin':
|
2020-11-27 10:58:47 +01:00
|
|
|
this.addOpenAtLogin();
|
2020-11-23 15:04:35 +01:00
|
|
|
break;
|
2020-11-25 20:43:29 +01:00
|
|
|
case 'removeOpenAtLogin':
|
2020-11-27 10:58:47 +01:00
|
|
|
this.removeOpenAtLogin();
|
2020-11-30 14:58:52 +01:00
|
|
|
case 'setFocus':
|
|
|
|
this.setFocus();
|
|
|
|
break;
|
2021-07-05 00:06:24 +02:00
|
|
|
case 'getWindowIsFocused':
|
|
|
|
this.windowIsFocused();
|
|
|
|
break;
|
2020-10-07 15:11:01 +02:00
|
|
|
case 'enableBrowserIntegration':
|
2020-10-12 21:18:28 +02:00
|
|
|
this.main.nativeMessagingMain.generateManifests();
|
|
|
|
this.main.nativeMessagingMain.listen();
|
2020-10-07 15:11:01 +02:00
|
|
|
break;
|
|
|
|
case 'disableBrowserIntegration':
|
2020-10-12 21:18:28 +02:00
|
|
|
this.main.nativeMessagingMain.removeManifests();
|
|
|
|
this.main.nativeMessagingMain.stop();
|
2020-11-23 15:04:35 +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;
|
|
|
|
}
|
2020-12-17 21:53:38 +01:00
|
|
|
this.main.trayMain.updateContextMenu();
|
2018-05-05 06:29:02 +02:00
|
|
|
}
|
2020-11-27 10:58:47 +01:00
|
|
|
|
|
|
|
private addOpenAtLogin() {
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
const data = `[Desktop Entry]
|
2020-12-14 22:45:36 +01:00
|
|
|
Type=Application
|
|
|
|
Version=${app.getVersion()}
|
|
|
|
Name=Bitwarden
|
|
|
|
Comment=Bitwarden startup script
|
|
|
|
Exec=${app.getPath('exe')}
|
|
|
|
StartupNotify=false
|
|
|
|
Terminal=false`;
|
2020-11-27 10:58:47 +01:00
|
|
|
|
2020-11-27 11:34:09 +01:00
|
|
|
const dir = path.dirname(this.linuxStartupFile());
|
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
fs.mkdirSync(dir);
|
|
|
|
}
|
2020-11-27 10:58:47 +01:00
|
|
|
fs.writeFileSync(this.linuxStartupFile(), data);
|
|
|
|
} else {
|
|
|
|
app.setLoginItemSettings({openAtLogin: true});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private removeOpenAtLogin() {
|
|
|
|
if (process.platform === 'linux') {
|
2020-11-27 11:34:09 +01:00
|
|
|
if (fs.existsSync(this.linuxStartupFile())) {
|
|
|
|
fs.unlinkSync(this.linuxStartupFile());
|
2020-11-27 10:58:47 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
app.setLoginItemSettings({openAtLogin: false});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private linuxStartupFile(): string {
|
|
|
|
return path.join(app.getPath('home'), '.config', 'autostart', 'bitwarden.desktop');
|
|
|
|
}
|
2020-12-04 18:39:44 +01:00
|
|
|
|
2020-11-30 14:58:52 +01:00
|
|
|
private setFocus() {
|
|
|
|
this.main.trayMain.restoreFromTray();
|
|
|
|
this.main.windowMain.win.focusOnWebView();
|
|
|
|
}
|
2021-07-05 00:06:24 +02:00
|
|
|
|
|
|
|
private windowIsFocused() {
|
|
|
|
const windowIsFocused = this.main.windowMain.win.isFocused();
|
|
|
|
this.main.windowMain.win.webContents.send('messagingService', {
|
|
|
|
command: 'windowIsFocused',
|
|
|
|
windowIsFocused: windowIsFocused,
|
|
|
|
});
|
|
|
|
}
|
2018-02-08 19:10:13 +01:00
|
|
|
}
|