1+ const crypto = require ( 'crypto' ) ;
12const electron = require ( 'electron' ) ;
23const os = require ( 'os' ) ;
3- const { ipcMain } = electron ;
4+ const { BrowserWindow , ipcMain } = electron ;
45
56const { windowsEnableScreenProtection } = require ( '../../helpers/functions' ) ;
67const { EVENTS , STATES , AOT_WINDOW_NAME , EVENTS_CHANNEL } = require ( '../constants' ) ;
@@ -13,11 +14,15 @@ const {
1314 savePosition,
1415 setAspectRatioToResizeableWindow,
1516 setLogger,
16- windowExists,
17- getAotWindow
17+ windowExists
1818} = require ( './utils' ) ;
1919const aotConfig = require ( './config' ) ;
2020
21+ /**
22+ * Token for matching window open requests.
23+ */
24+ let aotMagic ;
25+
2126/**
2227 * The main window instance
2328 */
@@ -34,14 +39,23 @@ let isIntersecting;
3439 */
3540let _existingWindowOpenHandler ;
3641
42+ /**
43+ * The aot window instance
44+ */
45+ const getAotWindow = ( ) => BrowserWindow . getAllWindows ( ) . find ( win => {
46+ if ( ! win || win . isDestroyed ( ) || win . webContents . isCrashed ( ) ) return false ;
47+ const frameName = win . webContents . mainFrame . name || '' ;
48+ return frameName === `${ AOT_WINDOW_NAME } -${ aotMagic } ` ;
49+ } ) ;
50+
3751/**
3852 * Sends an update state event to renderer process
3953 * @param {string } value the updated aot window state
4054 */
41- const sendStateUpdate = state => {
55+ const sendStateUpdate = ( state , data = { } ) => {
4256 logInfo ( `sending ${ state } state update to renderer process` ) ;
4357
44- mainWindow . webContents . send ( EVENTS_CHANNEL , { name : EVENTS . UPDATE_STATE , state } ) ;
58+ mainWindow . webContents . send ( EVENTS_CHANNEL , { name : EVENTS . UPDATE_STATE , state, data } ) ;
4559} ;
4660
4761/**
@@ -87,9 +101,17 @@ const handleWindowCreated = window => {
87101const windowOpenHandler = args => {
88102 const { frameName } = args ;
89103
90- if ( frameName === AOT_WINDOW_NAME ) {
104+ if ( frameName . startsWith ( AOT_WINDOW_NAME ) ) {
91105 logInfo ( 'handling new aot window event' ) ;
92106
107+ const magic = frameName . split ( '-' ) [ 1 ] ;
108+
109+ if ( magic !== aotMagic ) {
110+ logInfo ( 'bad AoT window magic' ) ;
111+
112+ return { action : 'deny' } ;
113+ }
114+
93115 return {
94116 action : 'allow' ,
95117 overrideBrowserWindowOptions : {
@@ -114,16 +136,20 @@ const showAot = () => {
114136 logInfo ( 'show aot handler' ) ;
115137
116138 let state ;
139+ let data = { } ;
140+
117141 const aotWindow = getAotWindow ( ) ;
118142
119143 if ( windowExists ( aotWindow ) ) {
120144 state = STATES . SHOW ;
121145 aotWindow . showInactive ( ) ;
122146 } else {
123147 state = STATES . OPEN ;
148+ aotMagic = crypto . randomUUID ( ) . replaceAll ( '-' , '' ) ;
149+ data . aotMagic = aotMagic ;
124150 }
125151
126- sendStateUpdate ( state ) ;
152+ sendStateUpdate ( state , data ) ;
127153} ;
128154
129155/**
0 commit comments