From dc96f1c70ecb929ce31e8ba9ccc417810e9b5eee Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 8 Mar 2019 11:03:39 +0000 Subject: [PATCH] Remove duped code --- electron_app/src/electron-main.js | 86 ------------------------------- 1 file changed, 86 deletions(-) diff --git a/electron_app/src/electron-main.js b/electron_app/src/electron-main.js index 45952448..41361c1a 100644 --- a/electron_app/src/electron-main.js +++ b/electron_app/src/electron-main.js @@ -399,92 +399,6 @@ app.on('before-quit', () => { } }); -// Counter for keybindings we have registered -let ioHookTasks = 0; - -// Limit for amount of keybindings that can be -// registered at once. -const keybindingRegistrationLimit = 1; - -// Fires when a global keybinding is being registered -ipcMain.on('register-keybinding', function(ev, keybinding) { - // Prevent registering more than the defined limit - if (ioHookTasks >= keybindingRegistrationLimit) { - ioHookTasks = keybindingRegistrationLimit; - return; - } - - // Start listening for global keyboard shortcuts - if (ioHookTasks <= 0) { - ioHookTasks = 0; - ioHook.start(); - } - ioHookTasks++; - - ioHook.registerShortcut(keybinding.code, () => { - ev.sender.send('keybinding-pressed', keybinding.name); - }, () => { - ev.sender.send('keybinding-released', keybinding.name); - }); -}); - -// Fires when a global keybinding is being unregistered -ipcMain.on('unregister-keybinding', function(ev, keybindingCode) { - // Stop listening for global keyboard shortcuts if we're - // unregistering the last one - if (ioHookTasks <= 1) { - ioHook.stop(); - } - ioHookTasks--; - - ioHook.unregisterShortcutByKeys(keybindingCode); -}); - -// Tell renderer process what key was pressed -// iohook has its own encoding for keys, so we can't just use a -// listener in the renderer process to register iohook shortcuts -let renderProcessID = null; -const reportKeyEvent = function(keyEvent) { - // "this" is the renderer process because we call this method with .bind() - renderProcessID.sender.send('keypress', { - keydown: keyEvent.type == 'keydown', - keycode: keyEvent.keycode, - }); -}; - -// Fires when listening on all keys -// !!Security note: Ensure iohook is only allowed to listen to keybindings -// when the browser window is in focus, else an XSS could lead to keylogging -// Currently, this is achieved by leveraging browserWindow to act on focus loss -ipcMain.on('start-listening-keys', function(ev, keybindingCode) { - // Start recording keypresses - if (ioHookTasks <= 0) { - ioHookTasks = 0; - ioHook.start(); - } - ioHookTasks++; - - renderProcessID = ev; - ioHook.on('keydown', reportKeyEvent); - ioHook.on('keyup', reportKeyEvent); -}); - -const stopListeningKeys = () => { - // Stop recording keypresses - ioHook.off('keydown', reportKeyEvent); - ioHook.off('keyup', reportKeyEvent); -}; - -ipcMain.on('stop-listening-keys', () => { - if (ioHookTasks <= 1) { - ioHookTasks = 1; - ioHook.stop(); - } - ioHookTasks--; - - stopListeningKeys(); -}); - app.on('second-instance', (ev, commandLine, workingDirectory) => { // If other instance launched with --hidden then skip showing window if (commandLine.includes('--hidden')) return;