fixed remote events

This commit is contained in:
antelle 2020-05-22 10:44:59 +02:00
parent 045fe99158
commit e9ef1bd698
No known key found for this signature in database
GPG Key ID: 63C9777AAB7C563C
2 changed files with 56 additions and 26 deletions

View File

@ -61,40 +61,45 @@ const UsbListener = {
logger.info('Stopping USB listener');
if (this.usb) {
this.usb._disableHotplugEvents();
if (this.attachedYubiKeys.length) {
this.attachedYubiKeys = [];
Events.emit('usb-devices-changed');
}
this.usb.off('attach', UsbListener.deviceAttached);
this.usb.off('detach', UsbListener.deviceDetached);
this.usb = null;
}
},
listen() {
this.usb.on('attach', device => {
if (this.isYubiKey(device)) {
this.attachedYubiKeys.push({ device });
logger.info(`YubiKey attached, total: ${this.attachedYubiKeys.length}`, device);
this.usb.on('attach', UsbListener.deviceAttached);
this.usb.on('detach', UsbListener.deviceDetached);
},
deviceAttached(device) {
if (UsbListener.isYubiKey(device)) {
UsbListener.attachedYubiKeys.push({ device });
logger.info(`YubiKey attached, total: ${UsbListener.attachedYubiKeys.length}`, device);
Events.emit('usb-devices-changed');
}
},
deviceDetached(device) {
if (UsbListener.isYubiKey(device)) {
const index = UsbListener.attachedYubiKeys.findIndex(
yk => yk.device.deviceAddress === device.deviceAddress
);
if (index >= 0) {
UsbListener.attachedYubiKeys.splice(index, 1);
logger.info(
`YubiKey detached, total: ${UsbListener.attachedYubiKeys.length}`,
device
);
Events.emit('usb-devices-changed');
}
});
this.usb.on('detach', device => {
if (this.isYubiKey(device)) {
const index = this.attachedYubiKeys.findIndex(
yk => yk.device.deviceAddress === device.deviceAddress
);
if (index >= 0) {
this.attachedYubiKeys.splice(index, 1);
logger.info(`YubiKey detached, total: ${this.attachedYubiKeys.length}`, device);
Events.emit('usb-devices-changed');
}
}
});
this.usb._enableHotplugEvents();
}
},
isYubiKey(device) {

View File

@ -16,6 +16,7 @@ let restartPending = false;
let mainWindowPosition = {};
let updateMainWindowPositionTimeout = null;
let mainWindowMaximized = false;
let usbBinding = null;
const windowPositionFileName = 'window-position.json';
const appSettingsFileName = 'app-settings.json';
@ -155,9 +156,7 @@ app.reqNative = function(mod) {
const fileName = `${mod}-${process.platform}-${process.arch}.node`;
const binding = require(`@keeweb/keeweb-native-modules/${fileName}`);
if (mod === 'usb') {
Object.keys(EventEmitter.prototype).forEach(key => {
binding[key] = EventEmitter.prototype[key];
});
usbBinding = initUsb(binding);
}
return binding;
};
@ -229,7 +228,7 @@ function createMainWindow() {
mainWindow.on('resize', delaySaveMainWindowPosition);
mainWindow.on('move', delaySaveMainWindowPosition);
mainWindow.on('restore', coerceMainWindowPositionToConnectedDisplay);
mainWindow.on('close', updateMainWindowPositionIfPending);
mainWindow.on('close', mainWindowClosed);
mainWindow.on('focus', mainWindowFocus);
mainWindow.on('blur', mainWindowBlur);
mainWindow.on('closed', () => {
@ -370,6 +369,12 @@ function mainWindowFocus() {
emitRemoteEvent('main-window-focus');
}
function mainWindowClosed() {
updateMainWindowPositionIfPending();
usbBinding?.removeAllListeners();
app.removeAllListeners('remote-app-event');
}
function emitRemoteEvent(e, arg) {
if (mainWindow && mainWindow.webContents) {
app.emit('remote-app-event', {
@ -652,6 +657,26 @@ function coerceMainWindowPositionToConnectedDisplay() {
updateMainWindowPosition();
}
function initUsb(binding) {
Object.keys(EventEmitter.prototype).forEach(key => {
binding[key] = EventEmitter.prototype[key];
});
binding.on('newListener', () => {
if (binding.listenerCount('attach') === 0 && binding.listenerCount('detach') === 0) {
binding._enableHotplugEvents();
}
});
binding.on('removeListener', () => {
if (binding.listenerCount('attach') === 0 && binding.listenerCount('detach') === 0) {
binding._disableHotplugEvents();
}
});
return binding;
}
function reportStartProfile() {
if (!perfTimestamps) {
return;