2018-05-05 06:29:02 +02:00
|
|
|
import {
|
|
|
|
app,
|
|
|
|
BrowserWindow,
|
|
|
|
MenuItemConstructorOptions,
|
|
|
|
} 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';
|
2018-05-31 15:08:09 +02:00
|
|
|
import { LowdbStorageService } from 'jslib/services/lowdbStorage.service';
|
2018-04-25 05:53:20 +02:00
|
|
|
|
2018-04-25 21:43:26 +02:00
|
|
|
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';
|
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;
|
2018-05-31 15:08:09 +02:00
|
|
|
storageService: LowdbStorageService;
|
2018-04-26 21:44:07 +02:00
|
|
|
messagingService: ElectronMainMessagingService;
|
2018-04-25 21:43:26 +02:00
|
|
|
keytarStorageListener: KeytarStorageListener;
|
2018-02-14 05:38:18 +01:00
|
|
|
|
|
|
|
windowMain: WindowMain;
|
|
|
|
messagingMain: MessagingMain;
|
|
|
|
updaterMain: UpdaterMain;
|
|
|
|
menuMain: MenuMain;
|
|
|
|
powerMonitorMain: PowerMonitorMain;
|
2018-05-04 18:45:42 +02:00
|
|
|
trayMain: TrayMain;
|
2018-02-14 05:38:18 +01:00
|
|
|
|
|
|
|
constructor() {
|
2018-02-15 21:27:56 +01:00
|
|
|
// Set paths for portable builds
|
2018-02-20 22:04:35 +01:00
|
|
|
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) {
|
2018-02-20 22:04:35 +01:00
|
|
|
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');
|
2018-02-20 22:04:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-05-31 15:08:09 +02:00
|
|
|
this.storageService = new LowdbStorageService(storageDefaults, app.getPath('userData'));
|
2018-02-14 05:38:18 +01:00
|
|
|
|
2018-04-24 22:30:47 +02:00
|
|
|
this.windowMain = new WindowMain(this.storageService);
|
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-25 21:43:26 +02:00
|
|
|
|
2018-04-26 21:44:07 +02:00
|
|
|
this.messagingService = new ElectronMainMessagingService(this.windowMain, (message) => {
|
|
|
|
this.messagingMain.onMessage(message);
|
|
|
|
});
|
|
|
|
|
2018-04-25 21:43:26 +02:00
|
|
|
this.keytarStorageListener = new KeytarStorageListener('Bitwarden');
|
2018-02-14 05:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bootstrap() {
|
2018-05-31 15:08:09 +02:00
|
|
|
this.storageService.init();
|
2018-04-25 21:43:26 +02:00
|
|
|
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();
|
|
|
|
this.powerMonitorMain.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'),
|
|
|
|
}]);
|
2018-04-25 05:53:20 +02:00
|
|
|
await this.updaterMain.init();
|
|
|
|
}, (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();
|