Browse Source

menu and main keytar storage service

pull/7/head
Kyle Spearrin 8 years ago
parent
commit
a4cb908390
  1. 19
      .vscode/launch.json
  2. 2
      jslib
  3. 11
      src/main.ts
  4. 112
      src/main/menu.main.ts
  5. 34
      src/main/messaging.main.ts

19
.vscode/launch.json vendored

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}/build",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"args": [
"."
]
}
]
}

2
jslib

@ -1 +1 @@ @@ -1 +1 @@
Subproject commit 6e6dc422ac13047427d21d44463a5de2a3db7ebb
Subproject commit 42bf9b2edbafcda5d180df6d2746f19881a63315

11
src/main.ts

@ -3,9 +3,11 @@ import * as path from 'path'; @@ -3,9 +3,11 @@ import * as path from 'path';
// import { ElectronMainMessagingService } from 'jslib/electron/services/desktopMainMessaging.service';
import { MenuMain } from './main/menu.main';
import { MessagingMain } from './main/messaging.main';
import { I18nService } from './services/i18n.service';
import { KeytarStorageListener } from 'jslib/electron/keytarStorageListener';
import { ElectronLogService } from 'jslib/electron/services/electronLog.service';
import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service';
import { WindowMain } from 'jslib/electron/window.main';
@ -14,9 +16,11 @@ export class Main { @@ -14,9 +16,11 @@ export class Main {
logService: ElectronLogService;
i18nService: I18nService;
storageService: ElectronStorageService;
keytarStorageListener: KeytarStorageListener;
windowMain: WindowMain;
messagingMain: MessagingMain;
menuMain: MenuMain;
constructor() {
// Set paths for portable builds
@ -48,12 +52,17 @@ export class Main { @@ -48,12 +52,17 @@ export class Main {
// this.messagingService = new DesktopMainMessagingService(this);
this.windowMain = new WindowMain(this.storageService);
this.messagingMain = new MessagingMain(this);
this.menuMain = new MenuMain(this);
this.messagingMain = new MessagingMain(this.windowMain);
this.keytarStorageListener = new KeytarStorageListener('Bitwarden Directory Connector');
}
bootstrap() {
this.keytarStorageListener.init();
this.windowMain.init().then(async () => {
await this.i18nService.init(app.getLocale());
this.menuMain.init();
this.messagingMain.init();
}, (e: any) => {
// tslint:disable-next-line

112
src/main/menu.main.ts

@ -0,0 +1,112 @@ @@ -0,0 +1,112 @@
import {
app,
BrowserWindow,
clipboard,
dialog,
ipcMain,
Menu,
MenuItem,
MenuItemConstructorOptions,
shell,
} from 'electron';
import { Main } from '../main';
import { BaseMenu } from 'jslib/electron/baseMenu';
import { ConstantsService } from 'jslib/services/constants.service';
export class MenuMain extends BaseMenu {
menu: Menu;
logOut: MenuItem;
constructor(private main: Main) {
super(main.i18nService, main.windowMain, 'Bitwarden Directory Connector',
() => { /* TODO: Log Out Message */ });
}
init() {
this.initProperties();
this.initContextMenu();
this.initApplicationMenu();
this.logOut = this.menu.getMenuItemById('logOut');
this.updateApplicationMenuState(false, true);
}
updateApplicationMenuState(isAuthenticated: boolean, isLocked: boolean) {
this.logOut.enabled = isAuthenticated;
}
private initApplicationMenu() {
const accountSubmenu: MenuItemConstructorOptions[] = [
this.logOutMenuItemOptions,
];
const template: MenuItemConstructorOptions[] = [
{
label: this.i18nService.t('file'),
submenu: [ this.logOutMenuItemOptions ],
},
this.editMenuItemOptions,
{
label: this.main.i18nService.t('view'),
submenu: this.viewSubMenuItemOptions,
},
this.windowMenuItemOptions,
];
const firstMenuOptions: MenuItemConstructorOptions[] = [
{ type: 'separator' },
{
label: this.i18nService.t('settings'),
id: 'settings',
click: () => { /* Something */ },
},
];
const updateMenuItem = {
label: this.i18nService.t('checkForUpdates'),
click: () => { /* Something */ },
id: 'checkForUpdates',
};
if (process.platform === 'darwin') {
const firstMenuPart: MenuItemConstructorOptions[] = [
{
label: this.i18nService.t('aboutBitwarden'),
role: 'about',
},
updateMenuItem,
];
template.unshift({
label: this.appName,
submenu: firstMenuPart.concat(firstMenuOptions, [
{ type: 'separator' },
], this.macAppMenuItemOptions),
});
// Window menu
template[template.length - 1].submenu = this.macWindowSubmenuOptions;
} else {
// File menu
template[0].submenu = (template[0].submenu as MenuItemConstructorOptions[]).concat(
firstMenuOptions);
// About menu
const aboutMenuAdditions: MenuItemConstructorOptions[] = [
{ type: 'separator' },
updateMenuItem,
];
aboutMenuAdditions.push(this.aboutMenuItemOptions);
template[template.length - 1].submenu =
(template[template.length - 1].submenu as MenuItemConstructorOptions[]).concat(aboutMenuAdditions);
}
this.menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(this.menu);
}
}

34
src/main/messaging.main.ts

@ -3,44 +3,18 @@ import { @@ -3,44 +3,18 @@ import {
ipcMain,
} from 'electron';
import {
deletePassword,
getPassword,
setPassword,
} from 'keytar';
import { Main } from '../main';
import { WindowMain } from 'jslib/electron/window.main';
const KeytarService = 'Bitwarden';
const SyncInterval = 5 * 60 * 1000; // 5 minutes
export class MessagingMain {
private syncTimeout: NodeJS.Timer;
constructor(private main: Main) { }
constructor(private windowMain: WindowMain) { }
init() {
this.scheduleNextSync();
ipcMain.on('messagingService', async (event: any, message: any) => this.onMessage(message));
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;
} catch {
event.returnValue = null;
}
});
}
onMessage(message: any) {
@ -62,11 +36,11 @@ export class MessagingMain { @@ -62,11 +36,11 @@ export class MessagingMain {
}
this.syncTimeout = global.setTimeout(() => {
if (this.main.windowMain.win == null) {
if (this.windowMain.win == null) {
return;
}
this.main.windowMain.win.webContents.send('messagingService', {
this.windowMain.win.webContents.send('messagingService', {
command: 'checkSyncVault',
});
}, SyncInterval);

Loading…
Cancel
Save