diff --git a/src-main/windows/about.js b/src-main/windows/about.js index e6e8d098..48fa52c4 100644 --- a/src-main/windows/about.js +++ b/src-main/windows/about.js @@ -12,9 +12,7 @@ class AboutWindow extends AbstractWindow { this.window.setMaximizable(false); this.window.setTitle(translate('about').replace('{APP_NAME}', APP_NAME)); - const ipc = this.window.webContents.ipc; - - ipc.on('get-info', (event) => { + this.ipc.on('get-info', (event) => { event.returnValue = { version: packageJSON.version, dist: getDist(), diff --git a/src-main/windows/abstract.js b/src-main/windows/abstract.js index 916381eb..92b55f75 100644 --- a/src-main/windows/abstract.js +++ b/src-main/windows/abstract.js @@ -40,6 +40,11 @@ class AbstractWindow { this.window.setBounds(bounds); } + /** + * ipcMain object scoped to the window's main frame only. + */ + this.ipc = this.window.webContents.mainFrame.ipc; + this.initialURL = null; this.protocol = null; diff --git a/src-main/windows/addons.js b/src-main/windows/addons.js index 03350dce..d0062e17 100644 --- a/src-main/windows/addons.js +++ b/src-main/windows/addons.js @@ -18,17 +18,15 @@ class AddonsWindow extends AbstractWindow { }); this.window.setTitle(`${translate('addon-settings')} - ${APP_NAME}`); - const ipc = this.window.webContents.ipc; - - ipc.on('alert', (event, message) => { + this.ipc.on('alert', (event, message) => { event.returnValue = prompts.alert(this.window, message); }); - ipc.on('confirm', (event, message) => { + this.ipc.on('confirm', (event, message) => { event.returnValue = prompts.confirm(this.window, message); }); - ipc.handle('export-settings', async (event, settings) => { + this.ipc.handle('export-settings', async (event, settings) => { const result = await dialog.showSaveDialog(this.window, { defaultPath: path.join(app.getPath('downloads'), 'turbowarp-addon-settings.json'), filters: [ diff --git a/src-main/windows/desktop-settings.js b/src-main/windows/desktop-settings.js index 2dc0e6c7..2e1aa036 100644 --- a/src-main/windows/desktop-settings.js +++ b/src-main/windows/desktop-settings.js @@ -14,16 +14,14 @@ class DesktopSettingsWindow extends AbstractWindow { this.window.setMinimizable(false); this.window.setMaximizable(false); - const ipc = this.window.webContents.ipc; - - ipc.on('get-strings', (event) => { + this.ipc.on('get-strings', (event) => { event.returnValue = { locale: getLocale(), strings: getStrings() } }); - ipc.on('get-settings', (event) => { + this.ipc.on('get-settings', (event) => { event.returnValue = { updateCheckerAllowed: isUpdateCheckerAllowed(), updateChecker: settings.updateChecker, @@ -39,12 +37,12 @@ class DesktopSettingsWindow extends AbstractWindow { }; }); - ipc.handle('set-update-checker', async (event, updateChecker) => { + this.ipc.handle('set-update-checker', async (event, updateChecker) => { settings.updateChecker = updateChecker; await settings.save(); }); - ipc.handle('enumerate-media-devices', async () => { + this.ipc.handle('enumerate-media-devices', async () => { // Imported late due to circular dependencies const EditorWindow = require('./editor'); const anEditorWindow = AbstractWindow.getWindowsByClass(EditorWindow)[0]; @@ -55,44 +53,44 @@ class DesktopSettingsWindow extends AbstractWindow { return anEditorWindow.enumerateMediaDevices(); }); - ipc.handle('set-microphone', async (event, microphone) => { + this.ipc.handle('set-microphone', async (event, microphone) => { settings.microphone = microphone; await settings.save(); }); - ipc.handle('set-camera', async (event, camera) => { + this.ipc.handle('set-camera', async (event, camera) => { settings.camera = camera; await settings.save(); }); - ipc.handle('set-hardware-acceleration', async (event, hardwareAcceleration) => { + this.ipc.handle('set-hardware-acceleration', async (event, hardwareAcceleration) => { settings.hardwareAcceleration = hardwareAcceleration; await settings.save(); }); - ipc.handle('set-background-throttling', async (event, backgroundThrottling) => { + this.ipc.handle('set-background-throttling', async (event, backgroundThrottling) => { settings.backgroundThrottling = backgroundThrottling; AbstractWindow.settingsChanged(); await settings.save(); }); - ipc.handle('set-bypass-cors', async (event, bypassCORS) => { + this.ipc.handle('set-bypass-cors', async (event, bypassCORS) => { settings.bypassCORS = bypassCORS; await settings.save(); }); - ipc.handle('set-spellchecker', async (event, spellchecker) => { + this.ipc.handle('set-spellchecker', async (event, spellchecker) => { settings.spellchecker = spellchecker; AbstractWindow.settingsChanged(); await settings.save(); }); - ipc.handle('set-exit-fullscreen-on-escape', async (event, exitFullscreenOnEscape) => { + this.ipc.handle('set-exit-fullscreen-on-escape', async (event, exitFullscreenOnEscape) => { settings.exitFullscreenOnEscape = exitFullscreenOnEscape; await settings.save(); }); - ipc.handle('set-rich-presence', async (event, richPresence) => { + this.ipc.handle('set-rich-presence', async (event, richPresence) => { settings.richPresence = richPresence; if (richPresence) { RichPresence.enable(); @@ -102,7 +100,7 @@ class DesktopSettingsWindow extends AbstractWindow { await settings.save(); }); - ipc.handle('open-user-data', async () => { + this.ipc.handle('open-user-data', async () => { shell.showItemInFolder(app.getPath('userData')); }); diff --git a/src-main/windows/editor.js b/src-main/windows/editor.js index 6c8c6b77..cc4c79c6 100644 --- a/src-main/windows/editor.js +++ b/src-main/windows/editor.js @@ -295,17 +295,15 @@ class EditorWindow extends ProjectRunningWindow { this.updateRichPresence(); }); - const ipc = this.window.webContents.ipc; - - ipc.on('is-initially-fullscreen', (e) => { + this.ipc.on('is-initially-fullscreen', (e) => { e.returnValue = isInitiallyFullscreen; }); - ipc.handle('get-initial-file', () => { + this.ipc.handle('get-initial-file', () => { return this.activeFileId; }); - ipc.handle('get-file', async (event, id) => { + this.ipc.handle('get-file', async (event, id) => { const file = getFileById(id); const {name, data} = await file.read(); return { @@ -315,7 +313,7 @@ class EditorWindow extends ProjectRunningWindow { }; }); - ipc.on('set-locale', async (event, locale) => { + this.ipc.on('set-locale', async (event, locale) => { if (settings.locale !== locale) { settings.locale = locale; updateLocale(locale); @@ -333,11 +331,11 @@ class EditorWindow extends ProjectRunningWindow { }; }); - ipc.handle('set-changed', (event, changed) => { + this.ipc.handle('set-changed', (event, changed) => { this.window.setDocumentEdited(changed); }); - ipc.handle('opened-file', (event, id) => { + this.ipc.handle('opened-file', (event, id) => { const file = getFileById(id); if (file.type !== TYPE_FILE) { throw new Error('Not a file'); @@ -347,12 +345,12 @@ class EditorWindow extends ProjectRunningWindow { this.window.setRepresentedFilename(file.path); }); - ipc.handle('closed-file', () => { + this.ipc.handle('closed-file', () => { this.activeFileId = null; this.window.setRepresentedFilename(''); }); - ipc.handle('show-open-file-picker', async () => { + this.ipc.handle('show-open-file-picker', async () => { const result = await dialog.showOpenDialog(this.window, { properties: ['openFile'], defaultPath: settings.lastDirectory, @@ -380,7 +378,7 @@ class EditorWindow extends ProjectRunningWindow { }; }); - ipc.handle('show-save-file-picker', async (event, suggestedName) => { + this.ipc.handle('show-save-file-picker', async (event, suggestedName) => { const result = await dialog.showSaveDialog(this.window, { defaultPath: path.join(settings.lastDirectory, suggestedName), filters: [ @@ -423,14 +421,14 @@ class EditorWindow extends ProjectRunningWindow { }; }); - ipc.handle('get-preferred-media-devices', () => { + this.ipc.handle('get-preferred-media-devices', () => { return { microphone: settings.microphone, camera: settings.camera }; }); - ipc.on('start-write-stream', async (startEvent, id) => { + this.ipc.on('start-write-stream', async (startEvent, id) => { const file = getFileById(id); if (file.type !== TYPE_FILE) { throw new Error('Not a file'); @@ -503,39 +501,39 @@ class EditorWindow extends ProjectRunningWindow { port.start(); }); - ipc.on('alert', (event, message) => { + this.ipc.on('alert', (event, message) => { event.returnValue = prompts.alert(this.window, message); }); - ipc.on('confirm', (event, message) => { + this.ipc.on('confirm', (event, message) => { event.returnValue = prompts.confirm(this.window, message); }); - ipc.handle('open-packager', () => { + this.ipc.handle('open-packager', () => { PackagerWindow.forEditor(this); }); - ipc.handle('open-new-window', () => { + this.ipc.handle('open-new-window', () => { EditorWindow.newWindow(); }); - ipc.handle('open-addon-settings', (event, search) => { + this.ipc.handle('open-addon-settings', (event, search) => { AddonsWindow.show(search); }); - ipc.handle('open-desktop-settings', () => { + this.ipc.handle('open-desktop-settings', () => { DesktopSettingsWindow.show(); }); - ipc.handle('open-privacy', () => { + this.ipc.handle('open-privacy', () => { PrivacyWindow.show(); }); - ipc.handle('open-about', () => { + this.ipc.handle('open-about', () => { AboutWindow.show(); }); - ipc.handle('get-advanced-customizations', async () => { + this.ipc.handle('get-advanced-customizations', async () => { const USERSCRIPT_PATH = path.join(app.getPath('userData'), 'userscript.js'); const USERSTYLE_PATH = path.join(app.getPath('userData'), 'userstyle.css'); @@ -550,7 +548,7 @@ class EditorWindow extends ProjectRunningWindow { }; }); - ipc.handle('check-drag-and-drop-path', (event, filePath) => { + this.ipc.handle('check-drag-and-drop-path', (event, filePath) => { FileAccessWindow.check(filePath); }); @@ -560,7 +558,7 @@ class EditorWindow extends ProjectRunningWindow { */ this.isInEditorFullScreen = false; - ipc.handle('set-is-full-screen', (event, isFullScreen) => { + this.ipc.handle('set-is-full-screen', (event, isFullScreen) => { this.isInEditorFullScreen = !!isFullScreen; }); @@ -590,7 +588,7 @@ class EditorWindow extends ProjectRunningWindow { enumerateMediaDevices () { // Used by desktop settings return new Promise((resolve, reject) => { - this.window.webContents.ipc.once('enumerated-media-devices', (event, result) => { + this.ipc.once('enumerated-media-devices', (event, result) => { if (typeof result.error !== 'undefined') { reject(result.error); } else { diff --git a/src-main/windows/file-access-window.js b/src-main/windows/file-access-window.js index 2a60cb39..c04bdbd4 100644 --- a/src-main/windows/file-access-window.js +++ b/src-main/windows/file-access-window.js @@ -38,9 +38,7 @@ class FileAccessWindow extends AbstractWindow { /** @type {boolean} */ this.ready = false; - const ipc = this.window.webContents.ipc; - - ipc.on('init', (e) => { + this.ipc.on('init', (e) => { this.ready = true; e.returnValue = { diff --git a/src-main/windows/migrate.js b/src-main/windows/migrate.js index e56e7dba..60dcfd16 100644 --- a/src-main/windows/migrate.js +++ b/src-main/windows/migrate.js @@ -24,9 +24,7 @@ class MigrateWindow extends AbstractWindow { this.resolveCallback = resolve; }); - const ipc = this.window.webContents.ipc; - - ipc.on('get-info', (event) => { + this.ipc.on('get-info', (event) => { event.returnValue = { oldDataVersion, locale: getLocale(), @@ -34,11 +32,11 @@ class MigrateWindow extends AbstractWindow { }; }); - ipc.handle('done', async () => { + this.ipc.handle('done', async () => { await this.done(true); }); - ipc.handle('continue-anyways', async () => { + this.ipc.handle('continue-anyways', async () => { await this.done(false); }); diff --git a/src-main/windows/packager.js b/src-main/windows/packager.js index e5aba656..a334abed 100644 --- a/src-main/windows/packager.js +++ b/src-main/windows/packager.js @@ -16,13 +16,11 @@ class PackagerWindow extends AbstractWindow { event.preventDefault(); }); - const ipc = this.window.webContents.ipc; - - ipc.on('is-mas', (event) => { + this.ipc.on('is-mas', (event) => { event.returnValue = !!process.mas; }); - ipc.on('import-project-with-port', (event) => { + this.ipc.on('import-project-with-port', (event) => { const port = event.ports[0]; if (this.editorWindow.window.isDestroyed()) { port.postMessage({ @@ -33,15 +31,15 @@ class PackagerWindow extends AbstractWindow { this.editorWindow.window.webContents.postMessage('export-project-to-port', null, [port]); }); - ipc.on('alert', (event, message) => { + this.ipc.on('alert', (event, message) => { event.returnValue = prompts.alert(this.window, message); }); - ipc.on('confirm', (event, message) => { + this.ipc.on('confirm', (event, message) => { event.returnValue = prompts.confirm(this.window, message); }); - ipc.handle('check-drag-and-drop-path', (event, path) => { + this.ipc.handle('check-drag-and-drop-path', (event, path) => { FileAccessWindow.check(path); }); diff --git a/src-main/windows/privacy.js b/src-main/windows/privacy.js index c503b3f9..68f44c6f 100644 --- a/src-main/windows/privacy.js +++ b/src-main/windows/privacy.js @@ -8,13 +8,11 @@ class PrivacyWindow extends AbstractWindow { constructor () { super(); - const ipc = this.window.webContents.ipc; - - ipc.on('is-update-checker-allowed', (e) => { + this.ipc.on('is-update-checker-allowed', (e) => { e.returnValue = isUpdateCheckerAllowed(); }); - ipc.handle('open-desktop-settings', () => { + this.ipc.handle('open-desktop-settings', () => { DesktopSettingsWindow.show(); }); diff --git a/src-main/windows/security-prompt.js b/src-main/windows/security-prompt.js index 62863e2c..f05adafe 100644 --- a/src-main/windows/security-prompt.js +++ b/src-main/windows/security-prompt.js @@ -75,9 +75,7 @@ class SecurityPromptWindow extends AbstractWindow { this.promptResolve = resolve; }); - const ipc = this.window.webContents.ipc; - - ipc.on('init', (event) => { + this.ipc.on('init', (event) => { event.returnValue = { type, APP_NAME, @@ -86,7 +84,7 @@ class SecurityPromptWindow extends AbstractWindow { }; }); - ipc.handle('ready', (event, options) => { + this.ipc.handle('ready', (event, options) => { const contentHeight = +options.height; const [minWidth, minHeight] = this.window.getMinimumSize(); @@ -98,7 +96,7 @@ class SecurityPromptWindow extends AbstractWindow { this.show(); }); - ipc.handle('done', (event, allowed) => { + this.ipc.handle('done', (event, allowed) => { this.promptResolve(!!allowed); // destroy() won't run the close event diff --git a/src-main/windows/update.js b/src-main/windows/update.js index 82029270..f8aefa60 100644 --- a/src-main/windows/update.js +++ b/src-main/windows/update.js @@ -9,9 +9,7 @@ class UpdateWindow extends AbstractWindow { this.window.setTitle(`${translate('update.window-title')} - ${APP_NAME}`); - const ipc = this.window.webContents.ipc; - - ipc.on('get-strings', (event) => { + this.ipc.on('get-strings', (event) => { event.returnValue = { appName: APP_NAME, locale: getLocale(), @@ -19,7 +17,7 @@ class UpdateWindow extends AbstractWindow { }; }); - ipc.on('get-info', (event) => { + this.ipc.on('get-info', (event) => { event.returnValue = { currentVersion, latestVersion, @@ -27,7 +25,7 @@ class UpdateWindow extends AbstractWindow { }; }); - ipc.handle('download', () => { + this.ipc.handle('download', () => { this.window.destroy(); const params = new URLSearchParams(); @@ -58,7 +56,7 @@ class UpdateWindow extends AbstractWindow { ignoreUpdate(latestVersion, until); }; - ipc.handle('ignore', (event, permanently) => { + this.ipc.handle('ignore', (event, permanently) => { this.window.destroy(); ignore(permanently); });