2021-12-20 15:47:17 +01:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
2018-02-08 19:10:13 +01:00
|
|
|
|
2022-02-24 20:50:19 +01:00
|
|
|
import { app, ipcMain } from "electron";
|
2018-02-09 21:49:00 +01:00
|
|
|
|
2021-12-20 15:47:17 +01:00
|
|
|
import { StateService } from "jslib-common/abstractions/state.service";
|
2020-04-14 22:52:03 +02:00
|
|
|
|
2022-02-24 20:50:19 +01:00
|
|
|
import { Main } from "../main";
|
|
|
|
|
2021-12-20 15:47:17 +01:00
|
|
|
import { MenuUpdateRequest } from "./menu.updater";
|
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 {
|
2021-12-20 15:47:17 +01:00
|
|
|
private syncTimeout: NodeJS.Timer;
|
2018-02-09 21:49:00 +01:00
|
|
|
|
2021-12-20 15:47:17 +01:00
|
|
|
constructor(private main: Main, private stateService: StateService) {}
|
2018-02-09 21:49:00 +01:00
|
|
|
|
2021-12-20 15:47:17 +01:00
|
|
|
init() {
|
|
|
|
this.scheduleNextSync();
|
|
|
|
if (process.platform === "linux") {
|
|
|
|
this.stateService.setOpenAtLogin(fs.existsSync(this.linuxStartupFile()));
|
|
|
|
} else {
|
|
|
|
const loginSettings = app.getLoginItemSettings();
|
|
|
|
this.stateService.setOpenAtLogin(loginSettings.openAtLogin);
|
2018-02-08 19:10:13 +01:00
|
|
|
}
|
2021-12-20 15:47:17 +01:00
|
|
|
ipcMain.on("messagingService", async (event: any, message: any) => this.onMessage(message));
|
|
|
|
}
|
2018-02-09 21:49:00 +01:00
|
|
|
|
2021-12-20 15:47:17 +01:00
|
|
|
onMessage(message: any) {
|
|
|
|
switch (message.command) {
|
|
|
|
case "scheduleNextSync":
|
|
|
|
this.scheduleNextSync();
|
|
|
|
break;
|
|
|
|
case "updateAppMenu":
|
|
|
|
this.main.menuMain.updateApplicationMenuState(message.updateRequest);
|
|
|
|
this.updateTrayMenu(message.updateRequest);
|
|
|
|
break;
|
|
|
|
case "minimizeOnCopy":
|
|
|
|
this.stateService.getMinimizeOnCopyToClipboard().then((shouldMinimize) => {
|
|
|
|
if (shouldMinimize && this.main.windowMain.win !== null) {
|
|
|
|
this.main.windowMain.win.minimize();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "showTray":
|
|
|
|
this.main.trayMain.showTray();
|
|
|
|
break;
|
|
|
|
case "removeTray":
|
|
|
|
this.main.trayMain.removeTray();
|
|
|
|
break;
|
|
|
|
case "hideToTray":
|
|
|
|
this.main.trayMain.hideToTray();
|
|
|
|
break;
|
|
|
|
case "addOpenAtLogin":
|
|
|
|
this.addOpenAtLogin();
|
|
|
|
break;
|
|
|
|
case "removeOpenAtLogin":
|
|
|
|
this.removeOpenAtLogin();
|
2022-02-24 20:50:19 +01:00
|
|
|
break;
|
2021-12-20 15:47:17 +01:00
|
|
|
case "setFocus":
|
|
|
|
this.setFocus();
|
|
|
|
break;
|
|
|
|
case "getWindowIsFocused":
|
|
|
|
this.windowIsFocused();
|
|
|
|
break;
|
|
|
|
case "enableBrowserIntegration":
|
|
|
|
this.main.nativeMessagingMain.generateManifests();
|
|
|
|
this.main.nativeMessagingMain.listen();
|
|
|
|
break;
|
|
|
|
case "disableBrowserIntegration":
|
|
|
|
this.main.nativeMessagingMain.removeManifests();
|
|
|
|
this.main.nativeMessagingMain.stop();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-02-11 05:24:22 +01:00
|
|
|
}
|
2021-12-20 15:47:17 +01:00
|
|
|
}
|
2018-02-11 05:24:22 +01:00
|
|
|
|
2021-12-20 15:47:17 +01:00
|
|
|
private scheduleNextSync() {
|
|
|
|
if (this.syncTimeout) {
|
|
|
|
global.clearTimeout(this.syncTimeout);
|
2018-02-09 21:49:00 +01:00
|
|
|
}
|
2018-05-05 06:29:02 +02:00
|
|
|
|
2021-12-20 15:47:17 +01:00
|
|
|
this.syncTimeout = global.setTimeout(() => {
|
|
|
|
if (this.main.windowMain.win == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.main.windowMain.win.webContents.send("messagingService", {
|
|
|
|
command: "checkSyncVault",
|
|
|
|
});
|
|
|
|
}, SyncInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
private updateTrayMenu(updateRequest: MenuUpdateRequest) {
|
|
|
|
if (
|
|
|
|
this.main.trayMain == null ||
|
|
|
|
this.main.trayMain.contextMenu == null ||
|
|
|
|
updateRequest?.activeUserId == null
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-14 14:40:06 +01:00
|
|
|
const lockVaultTrayMenuItem = this.main.trayMain.contextMenu.getMenuItemById("lockVault");
|
2021-12-20 15:47:17 +01:00
|
|
|
const activeAccount = updateRequest.accounts[updateRequest.activeUserId];
|
2022-02-14 14:40:06 +01:00
|
|
|
if (lockVaultTrayMenuItem != null && activeAccount != null) {
|
|
|
|
lockVaultTrayMenuItem.enabled = activeAccount.isAuthenticated && !activeAccount.isLocked;
|
2018-05-05 06:29:02 +02:00
|
|
|
}
|
2021-12-20 15:47:17 +01:00
|
|
|
this.main.trayMain.updateContextMenu();
|
|
|
|
}
|
2020-11-27 10:58:47 +01:00
|
|
|
|
2021-12-20 15:47:17 +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
|
2021-12-20 15:47:17 +01:00
|
|
|
Exec=${app.getPath("exe")}
|
2020-12-14 22:45:36 +01:00
|
|
|
StartupNotify=false
|
|
|
|
Terminal=false`;
|
2020-11-27 10:58:47 +01:00
|
|
|
|
2021-12-20 15:47:17 +01:00
|
|
|
const dir = path.dirname(this.linuxStartupFile());
|
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
fs.mkdirSync(dir);
|
|
|
|
}
|
|
|
|
fs.writeFileSync(this.linuxStartupFile(), data);
|
|
|
|
} else {
|
|
|
|
app.setLoginItemSettings({ openAtLogin: true });
|
2020-11-27 10:58:47 +01:00
|
|
|
}
|
2021-12-20 15:47:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private removeOpenAtLogin() {
|
|
|
|
if (process.platform === "linux") {
|
|
|
|
if (fs.existsSync(this.linuxStartupFile())) {
|
|
|
|
fs.unlinkSync(this.linuxStartupFile());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
app.setLoginItemSettings({ openAtLogin: false });
|
2021-07-05 00:06:24 +02:00
|
|
|
}
|
2021-12-20 15:47:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private linuxStartupFile(): string {
|
|
|
|
return path.join(app.getPath("home"), ".config", "autostart", "bitwarden.desktop");
|
|
|
|
}
|
|
|
|
|
|
|
|
private setFocus() {
|
|
|
|
this.main.trayMain.restoreFromTray();
|
|
|
|
this.main.windowMain.win.focusOnWebView();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|