Skip to content

Commit

Permalink
Use frame-scoped ipcMain
Browse files Browse the repository at this point in the history
Defense-in-depth.
  • Loading branch information
GarboMuffin committed Dec 19, 2024
1 parent 26c2433 commit ce1c04f
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 78 deletions.
4 changes: 1 addition & 3 deletions src-main/windows/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions src-main/windows/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 3 additions & 5 deletions src-main/windows/addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
28 changes: 13 additions & 15 deletions src-main/windows/desktop-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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];
Expand All @@ -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();
Expand All @@ -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'));
});

Expand Down
48 changes: 23 additions & 25 deletions src-main/windows/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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,
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');

Expand All @@ -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);
});

Expand All @@ -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;
});

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions src-main/windows/file-access-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 3 additions & 5 deletions src-main/windows/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ 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(),
strings: getStrings()
};
});

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);
});

Expand Down
12 changes: 5 additions & 7 deletions src-main/windows/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);
});

Expand Down
6 changes: 2 additions & 4 deletions src-main/windows/privacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
Loading

0 comments on commit ce1c04f

Please sign in to comment.