1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-08 09:43:42 +01:00
bitwarden-browser/src/main.ts

136 lines
5.3 KiB
TypeScript
Raw Normal View History

import { app, globalShortcut } from 'electron';
2018-02-15 21:27:56 +01:00
import * as path from 'path';
2018-01-16 21:58:17 +01:00
2018-02-14 05:38:18 +01:00
import { I18nService } from './services/i18n.service';
2018-02-08 19:10:13 +01:00
import { MenuMain } from './main/menu.main';
import { MessagingMain } from './main/messaging.main';
2018-02-11 05:24:22 +01:00
import { PowerMonitorMain } from './main/powerMonitor.main';
2018-04-24 22:30:47 +02:00
2018-04-25 05:53:20 +02:00
import { ConstantsService } from 'jslib/services/constants.service';
2019-02-02 18:27:06 +01:00
import { ElectronConstants } from 'jslib/electron/electronConstants';
import { KeytarStorageListener } from 'jslib/electron/keytarStorageListener';
2018-04-24 22:30:47 +02:00
import { ElectronLogService } from 'jslib/electron/services/electronLog.service';
2018-04-26 21:44:07 +02:00
import { ElectronMainMessagingService } from 'jslib/electron/services/electronMainMessaging.service';
2019-03-12 03:37:13 +01:00
import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service';
2018-05-08 18:05:05 +02:00
import { TrayMain } from 'jslib/electron/tray.main';
2018-05-04 20:16:28 +02:00
import { UpdaterMain } from 'jslib/electron/updater.main';
2018-04-24 22:30:47 +02:00
import { WindowMain } from 'jslib/electron/window.main';
2018-05-04 20:16:28 +02:00
2018-02-14 05:38:18 +01:00
export class Main {
2018-04-24 22:30:47 +02:00
logService: ElectronLogService;
2018-02-14 05:38:18 +01:00
i18nService: I18nService;
2019-03-12 03:37:13 +01:00
storageService: ElectronStorageService;
2018-04-26 21:44:07 +02:00
messagingService: ElectronMainMessagingService;
keytarStorageListener: KeytarStorageListener;
2018-02-14 05:38:18 +01:00
windowMain: WindowMain;
messagingMain: MessagingMain;
updaterMain: UpdaterMain;
menuMain: MenuMain;
powerMonitorMain: PowerMonitorMain;
trayMain: TrayMain;
2018-02-14 05:38:18 +01:00
constructor() {
2018-02-15 21:27:56 +01:00
// Set paths for portable builds
let appDataPath = null;
if (process.env.BITWARDEN_APPDATA_DIR != null) {
appDataPath = process.env.BITWARDEN_APPDATA_DIR;
2018-02-23 23:11:18 +01:00
} else if (process.platform === 'win32' && process.env.PORTABLE_EXECUTABLE_DIR != null) {
appDataPath = path.join(process.env.PORTABLE_EXECUTABLE_DIR, 'bitwarden-appdata');
2018-02-23 23:11:18 +01:00
} else if (process.platform === 'linux' && process.env.SNAP_USER_DATA != null) {
2018-02-23 23:16:00 +01:00
appDataPath = path.join(process.env.SNAP_USER_DATA, 'appdata');
}
app.on('ready', () => {
/*
globalShortcut.register('CommandOrControl+Shift+L', async () => {
if (this.windowMain.win === null) {
await this.windowMain.createWindow();
}
this.messagingService.send('focusSearch');
this.windowMain.win.show();
});
*/
globalShortcut.register('CommandOrControl+Shift+G', async () => {
if (this.windowMain.win === null) {
await this.windowMain.createWindow();
}
this.messagingService.send('openPasswordGenerator');
this.windowMain.win.show();
});
});
if (appDataPath != null) {
2018-02-15 21:27:56 +01:00
app.setPath('userData', appDataPath);
}
2018-02-25 15:51:21 +01:00
app.setPath('logs', path.join(app.getPath('userData'), 'logs'));
2018-02-15 21:27:56 +01:00
2018-02-14 05:38:18 +01:00
const args = process.argv.slice(1);
const watch = args.some((val) => val === '--watch');
if (watch) {
// tslint:disable-next-line
require('electron-reload')(__dirname, {});
}
2018-04-24 22:30:47 +02:00
this.logService = new ElectronLogService(null, app.getPath('userData'));
2018-02-14 05:38:18 +01:00
this.i18nService = new I18nService('en', './locales/');
2018-04-26 22:17:14 +02:00
const storageDefaults: any = {};
// Default lock options to "on restart".
storageDefaults[ConstantsService.lockOptionKey] = -1;
2019-03-12 03:37:13 +01:00
this.storageService = new ElectronStorageService(app.getPath('userData'), storageDefaults);
2018-02-14 05:38:18 +01:00
2019-04-14 03:37:42 +02:00
this.windowMain = new WindowMain(this.storageService, true);
2018-02-14 05:38:18 +01:00
this.messagingMain = new MessagingMain(this);
2018-05-04 20:16:28 +02:00
this.updaterMain = new UpdaterMain(this.i18nService, this.windowMain, 'desktop', () => {
this.menuMain.updateMenuItem.enabled = false;
}, () => {
this.menuMain.updateMenuItem.enabled = true;
}, () => {
this.menuMain.updateMenuItem.label = this.i18nService.t('restartToUpdate');
2018-11-27 14:32:04 +01:00
}, 'bitwarden');
2018-02-14 05:38:18 +01:00
this.menuMain = new MenuMain(this);
this.powerMonitorMain = new PowerMonitorMain(this);
2018-05-08 18:05:05 +02:00
this.trayMain = new TrayMain(this.windowMain, this.i18nService, this.storageService);
2018-04-26 21:44:07 +02:00
this.messagingService = new ElectronMainMessagingService(this.windowMain, (message) => {
this.messagingMain.onMessage(message);
});
this.keytarStorageListener = new KeytarStorageListener('Bitwarden');
2018-02-14 05:38:18 +01:00
}
bootstrap() {
this.keytarStorageListener.init();
2018-04-25 05:53:20 +02:00
this.windowMain.init().then(async () => {
const locale = await this.storageService.get<string>(ConstantsService.localeKey);
await this.i18nService.init(locale != null ? locale : app.getLocale());
this.messagingMain.init();
this.menuMain.init();
2018-05-08 18:05:05 +02:00
await this.trayMain.init('Bitwarden', [{
2018-05-05 06:29:02 +02:00
label: this.i18nService.t('lockNow'),
enabled: false,
id: 'lockNow',
click: () => this.messagingService.send('lockVault'),
}]);
2019-02-02 18:27:06 +01:00
if (await this.storageService.get<boolean>(ElectronConstants.enableStartToTrayKey)) {
this.trayMain.hideToTray();
}
2019-03-12 04:18:04 +01:00
this.powerMonitorMain.init();
await this.updaterMain.init();
2018-04-25 05:53:20 +02:00
}, (e: any) => {
// tslint:disable-next-line
console.error(e);
2018-02-14 05:38:18 +01:00
});
}
2018-01-16 23:30:57 +01:00
}
2018-02-14 05:38:18 +01:00
const main = new Main();
main.bootstrap();