mirror of https://github.com/keeweb/keeweb
fixed remote events
parent
045fe99158
commit
e9ef1bd698
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue