Skip to content

Commit

Permalink
fix(aot) validate frame name
Browse files Browse the repository at this point in the history
  • Loading branch information
saghul authored Jan 31, 2023
1 parent a87361c commit 660d3b8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
40 changes: 33 additions & 7 deletions alwaysontop/main/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const crypto = require('crypto');
const electron = require('electron');
const os = require('os');
const { ipcMain } = electron;
const { BrowserWindow, ipcMain } = electron;

const { windowsEnableScreenProtection } = require('../../helpers/functions');
const { EVENTS, STATES, AOT_WINDOW_NAME, EVENTS_CHANNEL } = require('../constants');
Expand All @@ -13,11 +14,15 @@ const {
savePosition,
setAspectRatioToResizeableWindow,
setLogger,
windowExists,
getAotWindow
windowExists
} = require('./utils');
const aotConfig = require('./config');

/**
* Token for matching window open requests.
*/
let aotMagic;

/**
* The main window instance
*/
Expand All @@ -34,14 +39,23 @@ let isIntersecting;
*/
let _existingWindowOpenHandler;

/**
* The aot window instance
*/
const getAotWindow = () => BrowserWindow.getAllWindows().find(win => {
if (!win || win.isDestroyed() || win.webContents.isCrashed()) return false;
const frameName = win.webContents.mainFrame.name || '';
return frameName === `${AOT_WINDOW_NAME}-${aotMagic}`;
});

/**
* Sends an update state event to renderer process
* @param {string} value the updated aot window state
*/
const sendStateUpdate = state => {
const sendStateUpdate = (state, data = {}) => {
logInfo(`sending ${state} state update to renderer process`);

mainWindow.webContents.send(EVENTS_CHANNEL, { name: EVENTS.UPDATE_STATE, state });
mainWindow.webContents.send(EVENTS_CHANNEL, { name: EVENTS.UPDATE_STATE, state, data });
};

/**
Expand Down Expand Up @@ -87,9 +101,17 @@ const handleWindowCreated = window => {
const windowOpenHandler = args => {
const { frameName } = args;

if (frameName === AOT_WINDOW_NAME) {
if (frameName.startsWith(AOT_WINDOW_NAME)) {
logInfo('handling new aot window event');

const magic = frameName.split('-')[1];

if (magic !== aotMagic) {
logInfo('bad AoT window magic');

return { action: 'deny' };
}

return {
action: 'allow',
overrideBrowserWindowOptions: {
Expand All @@ -114,16 +136,20 @@ const showAot = () => {
logInfo('show aot handler');

let state;
let data = {};

const aotWindow = getAotWindow();

if (windowExists(aotWindow)) {
state = STATES.SHOW;
aotWindow.showInactive();
} else {
state = STATES.OPEN;
aotMagic = crypto.randomUUID().replaceAll('-', '');
data.aotMagic = aotMagic;
}

sendStateUpdate(state);
sendStateUpdate(state, data);
};

/**
Expand Down
13 changes: 1 addition & 12 deletions alwaysontop/main/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const Store = require('electron-store');
const electron = require('electron');
const os = require('os');
const log = require('@jitsi/logger');
const { SIZE, ASPECT_RATIO, STORAGE, AOT_WINDOW_NAME } = require('../constants');
const { BrowserWindow } = electron;
const { SIZE, ASPECT_RATIO, STORAGE } = require('../constants');

/**
* Stores the current size of the AOT during the conference
Expand All @@ -27,15 +26,6 @@ let logger;
const store = new Store();


/**
* The aot window instance
*/
const getAotWindow = () => BrowserWindow.getAllWindows().find(win => {
if (!win || win.isDestroyed() || win.webContents.isCrashed()) return false;
const frameName = win.webContents.mainFrame.name || '';
return frameName === AOT_WINDOW_NAME;
});

/**
* Changes the window resize functionality to respect the passed aspect ratio.
*
Expand Down Expand Up @@ -244,7 +234,6 @@ const windowExists = browserWindow => {
};

module.exports = {
getAotWindow,
getPosition,
getSize,
logError,
Expand Down
11 changes: 6 additions & 5 deletions alwaysontop/render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ class AlwaysOnTop extends EventEmitter {
/**
* Opens a new window
*/
_openNewWindow() {
_openNewWindow(magic) {
logInfo('new window');
this._api.on('largeVideoChanged', this._updateLargeVideoSrc);
this._api.on('prejoinVideoChanged', this._updateLargeVideoSrc);
this._api.on('videoMuteStatusChanged', this._updateLargeVideoSrc);

this._aotWindow = window.open('', AOT_WINDOW_NAME);
this._aotWindow = window.open('', `${AOT_WINDOW_NAME}-${magic}`);
this._aotWindow.alwaysOnTop = {
api: this._api,
dismiss: this._dismiss,
Expand Down Expand Up @@ -291,7 +291,7 @@ class AlwaysOnTop extends EventEmitter {
_onAotEvent (event, { name, ...rest }) {
switch (name) {
case EVENTS.UPDATE_STATE:
this._handleStateChange(rest.state);
this._handleStateChange(rest.state, rest.data);
break;
}
}
Expand All @@ -300,16 +300,17 @@ class AlwaysOnTop extends EventEmitter {
* Handler for state updates
*
* @param {string} state updated state
* @param {Object} data ancillary data to the event
*/
_handleStateChange (state) {
_handleStateChange (state, data) {
logInfo(`handling ${state} state update from main process`);

switch (state) {
case STATES.HIDE:
this._hideWindow();
break;
case STATES.OPEN:
this._openNewWindow();
this._openNewWindow(data.aotMagic);
break;
case STATES.SHOW:
this._showWindow();
Expand Down

0 comments on commit 660d3b8

Please sign in to comment.