Skip to content

Commit ce1c04f

Browse files
committed
Use frame-scoped ipcMain
Defense-in-depth.
1 parent 26c2433 commit ce1c04f

File tree

11 files changed

+63
-78
lines changed

11 files changed

+63
-78
lines changed

src-main/windows/about.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ class AboutWindow extends AbstractWindow {
1212
this.window.setMaximizable(false);
1313
this.window.setTitle(translate('about').replace('{APP_NAME}', APP_NAME));
1414

15-
const ipc = this.window.webContents.ipc;
16-
17-
ipc.on('get-info', (event) => {
15+
this.ipc.on('get-info', (event) => {
1816
event.returnValue = {
1917
version: packageJSON.version,
2018
dist: getDist(),

src-main/windows/abstract.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class AbstractWindow {
4040
this.window.setBounds(bounds);
4141
}
4242

43+
/**
44+
* ipcMain object scoped to the window's main frame only.
45+
*/
46+
this.ipc = this.window.webContents.mainFrame.ipc;
47+
4348
this.initialURL = null;
4449
this.protocol = null;
4550

src-main/windows/addons.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@ class AddonsWindow extends AbstractWindow {
1818
});
1919
this.window.setTitle(`${translate('addon-settings')} - ${APP_NAME}`);
2020

21-
const ipc = this.window.webContents.ipc;
22-
23-
ipc.on('alert', (event, message) => {
21+
this.ipc.on('alert', (event, message) => {
2422
event.returnValue = prompts.alert(this.window, message);
2523
});
2624

27-
ipc.on('confirm', (event, message) => {
25+
this.ipc.on('confirm', (event, message) => {
2826
event.returnValue = prompts.confirm(this.window, message);
2927
});
3028

31-
ipc.handle('export-settings', async (event, settings) => {
29+
this.ipc.handle('export-settings', async (event, settings) => {
3230
const result = await dialog.showSaveDialog(this.window, {
3331
defaultPath: path.join(app.getPath('downloads'), 'turbowarp-addon-settings.json'),
3432
filters: [

src-main/windows/desktop-settings.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ class DesktopSettingsWindow extends AbstractWindow {
1414
this.window.setMinimizable(false);
1515
this.window.setMaximizable(false);
1616

17-
const ipc = this.window.webContents.ipc;
18-
19-
ipc.on('get-strings', (event) => {
17+
this.ipc.on('get-strings', (event) => {
2018
event.returnValue = {
2119
locale: getLocale(),
2220
strings: getStrings()
2321
}
2422
});
2523

26-
ipc.on('get-settings', (event) => {
24+
this.ipc.on('get-settings', (event) => {
2725
event.returnValue = {
2826
updateCheckerAllowed: isUpdateCheckerAllowed(),
2927
updateChecker: settings.updateChecker,
@@ -39,12 +37,12 @@ class DesktopSettingsWindow extends AbstractWindow {
3937
};
4038
});
4139

42-
ipc.handle('set-update-checker', async (event, updateChecker) => {
40+
this.ipc.handle('set-update-checker', async (event, updateChecker) => {
4341
settings.updateChecker = updateChecker;
4442
await settings.save();
4543
});
4644

47-
ipc.handle('enumerate-media-devices', async () => {
45+
this.ipc.handle('enumerate-media-devices', async () => {
4846
// Imported late due to circular dependencies
4947
const EditorWindow = require('./editor');
5048
const anEditorWindow = AbstractWindow.getWindowsByClass(EditorWindow)[0];
@@ -55,44 +53,44 @@ class DesktopSettingsWindow extends AbstractWindow {
5553
return anEditorWindow.enumerateMediaDevices();
5654
});
5755

58-
ipc.handle('set-microphone', async (event, microphone) => {
56+
this.ipc.handle('set-microphone', async (event, microphone) => {
5957
settings.microphone = microphone;
6058
await settings.save();
6159
});
6260

63-
ipc.handle('set-camera', async (event, camera) => {
61+
this.ipc.handle('set-camera', async (event, camera) => {
6462
settings.camera = camera;
6563
await settings.save();
6664
});
6765

68-
ipc.handle('set-hardware-acceleration', async (event, hardwareAcceleration) => {
66+
this.ipc.handle('set-hardware-acceleration', async (event, hardwareAcceleration) => {
6967
settings.hardwareAcceleration = hardwareAcceleration;
7068
await settings.save();
7169
});
7270

73-
ipc.handle('set-background-throttling', async (event, backgroundThrottling) => {
71+
this.ipc.handle('set-background-throttling', async (event, backgroundThrottling) => {
7472
settings.backgroundThrottling = backgroundThrottling;
7573
AbstractWindow.settingsChanged();
7674
await settings.save();
7775
});
7876

79-
ipc.handle('set-bypass-cors', async (event, bypassCORS) => {
77+
this.ipc.handle('set-bypass-cors', async (event, bypassCORS) => {
8078
settings.bypassCORS = bypassCORS;
8179
await settings.save();
8280
});
8381

84-
ipc.handle('set-spellchecker', async (event, spellchecker) => {
82+
this.ipc.handle('set-spellchecker', async (event, spellchecker) => {
8583
settings.spellchecker = spellchecker;
8684
AbstractWindow.settingsChanged();
8785
await settings.save();
8886
});
8987

90-
ipc.handle('set-exit-fullscreen-on-escape', async (event, exitFullscreenOnEscape) => {
88+
this.ipc.handle('set-exit-fullscreen-on-escape', async (event, exitFullscreenOnEscape) => {
9189
settings.exitFullscreenOnEscape = exitFullscreenOnEscape;
9290
await settings.save();
9391
});
9492

95-
ipc.handle('set-rich-presence', async (event, richPresence) => {
93+
this.ipc.handle('set-rich-presence', async (event, richPresence) => {
9694
settings.richPresence = richPresence;
9795
if (richPresence) {
9896
RichPresence.enable();
@@ -102,7 +100,7 @@ class DesktopSettingsWindow extends AbstractWindow {
102100
await settings.save();
103101
});
104102

105-
ipc.handle('open-user-data', async () => {
103+
this.ipc.handle('open-user-data', async () => {
106104
shell.showItemInFolder(app.getPath('userData'));
107105
});
108106

src-main/windows/editor.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -295,17 +295,15 @@ class EditorWindow extends ProjectRunningWindow {
295295
this.updateRichPresence();
296296
});
297297

298-
const ipc = this.window.webContents.ipc;
299-
300-
ipc.on('is-initially-fullscreen', (e) => {
298+
this.ipc.on('is-initially-fullscreen', (e) => {
301299
e.returnValue = isInitiallyFullscreen;
302300
});
303301

304-
ipc.handle('get-initial-file', () => {
302+
this.ipc.handle('get-initial-file', () => {
305303
return this.activeFileId;
306304
});
307305

308-
ipc.handle('get-file', async (event, id) => {
306+
this.ipc.handle('get-file', async (event, id) => {
309307
const file = getFileById(id);
310308
const {name, data} = await file.read();
311309
return {
@@ -315,7 +313,7 @@ class EditorWindow extends ProjectRunningWindow {
315313
};
316314
});
317315

318-
ipc.on('set-locale', async (event, locale) => {
316+
this.ipc.on('set-locale', async (event, locale) => {
319317
if (settings.locale !== locale) {
320318
settings.locale = locale;
321319
updateLocale(locale);
@@ -333,11 +331,11 @@ class EditorWindow extends ProjectRunningWindow {
333331
};
334332
});
335333

336-
ipc.handle('set-changed', (event, changed) => {
334+
this.ipc.handle('set-changed', (event, changed) => {
337335
this.window.setDocumentEdited(changed);
338336
});
339337

340-
ipc.handle('opened-file', (event, id) => {
338+
this.ipc.handle('opened-file', (event, id) => {
341339
const file = getFileById(id);
342340
if (file.type !== TYPE_FILE) {
343341
throw new Error('Not a file');
@@ -347,12 +345,12 @@ class EditorWindow extends ProjectRunningWindow {
347345
this.window.setRepresentedFilename(file.path);
348346
});
349347

350-
ipc.handle('closed-file', () => {
348+
this.ipc.handle('closed-file', () => {
351349
this.activeFileId = null;
352350
this.window.setRepresentedFilename('');
353351
});
354352

355-
ipc.handle('show-open-file-picker', async () => {
353+
this.ipc.handle('show-open-file-picker', async () => {
356354
const result = await dialog.showOpenDialog(this.window, {
357355
properties: ['openFile'],
358356
defaultPath: settings.lastDirectory,
@@ -380,7 +378,7 @@ class EditorWindow extends ProjectRunningWindow {
380378
};
381379
});
382380

383-
ipc.handle('show-save-file-picker', async (event, suggestedName) => {
381+
this.ipc.handle('show-save-file-picker', async (event, suggestedName) => {
384382
const result = await dialog.showSaveDialog(this.window, {
385383
defaultPath: path.join(settings.lastDirectory, suggestedName),
386384
filters: [
@@ -423,14 +421,14 @@ class EditorWindow extends ProjectRunningWindow {
423421
};
424422
});
425423

426-
ipc.handle('get-preferred-media-devices', () => {
424+
this.ipc.handle('get-preferred-media-devices', () => {
427425
return {
428426
microphone: settings.microphone,
429427
camera: settings.camera
430428
};
431429
});
432430

433-
ipc.on('start-write-stream', async (startEvent, id) => {
431+
this.ipc.on('start-write-stream', async (startEvent, id) => {
434432
const file = getFileById(id);
435433
if (file.type !== TYPE_FILE) {
436434
throw new Error('Not a file');
@@ -503,39 +501,39 @@ class EditorWindow extends ProjectRunningWindow {
503501
port.start();
504502
});
505503

506-
ipc.on('alert', (event, message) => {
504+
this.ipc.on('alert', (event, message) => {
507505
event.returnValue = prompts.alert(this.window, message);
508506
});
509507

510-
ipc.on('confirm', (event, message) => {
508+
this.ipc.on('confirm', (event, message) => {
511509
event.returnValue = prompts.confirm(this.window, message);
512510
});
513511

514-
ipc.handle('open-packager', () => {
512+
this.ipc.handle('open-packager', () => {
515513
PackagerWindow.forEditor(this);
516514
});
517515

518-
ipc.handle('open-new-window', () => {
516+
this.ipc.handle('open-new-window', () => {
519517
EditorWindow.newWindow();
520518
});
521519

522-
ipc.handle('open-addon-settings', (event, search) => {
520+
this.ipc.handle('open-addon-settings', (event, search) => {
523521
AddonsWindow.show(search);
524522
});
525523

526-
ipc.handle('open-desktop-settings', () => {
524+
this.ipc.handle('open-desktop-settings', () => {
527525
DesktopSettingsWindow.show();
528526
});
529527

530-
ipc.handle('open-privacy', () => {
528+
this.ipc.handle('open-privacy', () => {
531529
PrivacyWindow.show();
532530
});
533531

534-
ipc.handle('open-about', () => {
532+
this.ipc.handle('open-about', () => {
535533
AboutWindow.show();
536534
});
537535

538-
ipc.handle('get-advanced-customizations', async () => {
536+
this.ipc.handle('get-advanced-customizations', async () => {
539537
const USERSCRIPT_PATH = path.join(app.getPath('userData'), 'userscript.js');
540538
const USERSTYLE_PATH = path.join(app.getPath('userData'), 'userstyle.css');
541539

@@ -550,7 +548,7 @@ class EditorWindow extends ProjectRunningWindow {
550548
};
551549
});
552550

553-
ipc.handle('check-drag-and-drop-path', (event, filePath) => {
551+
this.ipc.handle('check-drag-and-drop-path', (event, filePath) => {
554552
FileAccessWindow.check(filePath);
555553
});
556554

@@ -560,7 +558,7 @@ class EditorWindow extends ProjectRunningWindow {
560558
*/
561559
this.isInEditorFullScreen = false;
562560

563-
ipc.handle('set-is-full-screen', (event, isFullScreen) => {
561+
this.ipc.handle('set-is-full-screen', (event, isFullScreen) => {
564562
this.isInEditorFullScreen = !!isFullScreen;
565563
});
566564

@@ -590,7 +588,7 @@ class EditorWindow extends ProjectRunningWindow {
590588
enumerateMediaDevices () {
591589
// Used by desktop settings
592590
return new Promise((resolve, reject) => {
593-
this.window.webContents.ipc.once('enumerated-media-devices', (event, result) => {
591+
this.ipc.once('enumerated-media-devices', (event, result) => {
594592
if (typeof result.error !== 'undefined') {
595593
reject(result.error);
596594
} else {

src-main/windows/file-access-window.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ class FileAccessWindow extends AbstractWindow {
3838
/** @type {boolean} */
3939
this.ready = false;
4040

41-
const ipc = this.window.webContents.ipc;
42-
43-
ipc.on('init', (e) => {
41+
this.ipc.on('init', (e) => {
4442
this.ready = true;
4543

4644
e.returnValue = {

src-main/windows/migrate.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,19 @@ class MigrateWindow extends AbstractWindow {
2424
this.resolveCallback = resolve;
2525
});
2626

27-
const ipc = this.window.webContents.ipc;
28-
29-
ipc.on('get-info', (event) => {
27+
this.ipc.on('get-info', (event) => {
3028
event.returnValue = {
3129
oldDataVersion,
3230
locale: getLocale(),
3331
strings: getStrings()
3432
};
3533
});
3634

37-
ipc.handle('done', async () => {
35+
this.ipc.handle('done', async () => {
3836
await this.done(true);
3937
});
4038

41-
ipc.handle('continue-anyways', async () => {
39+
this.ipc.handle('continue-anyways', async () => {
4240
await this.done(false);
4341
});
4442

src-main/windows/packager.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ class PackagerWindow extends AbstractWindow {
1616
event.preventDefault();
1717
});
1818

19-
const ipc = this.window.webContents.ipc;
20-
21-
ipc.on('is-mas', (event) => {
19+
this.ipc.on('is-mas', (event) => {
2220
event.returnValue = !!process.mas;
2321
});
2422

25-
ipc.on('import-project-with-port', (event) => {
23+
this.ipc.on('import-project-with-port', (event) => {
2624
const port = event.ports[0];
2725
if (this.editorWindow.window.isDestroyed()) {
2826
port.postMessage({
@@ -33,15 +31,15 @@ class PackagerWindow extends AbstractWindow {
3331
this.editorWindow.window.webContents.postMessage('export-project-to-port', null, [port]);
3432
});
3533

36-
ipc.on('alert', (event, message) => {
34+
this.ipc.on('alert', (event, message) => {
3735
event.returnValue = prompts.alert(this.window, message);
3836
});
3937

40-
ipc.on('confirm', (event, message) => {
38+
this.ipc.on('confirm', (event, message) => {
4139
event.returnValue = prompts.confirm(this.window, message);
4240
});
4341

44-
ipc.handle('check-drag-and-drop-path', (event, path) => {
42+
this.ipc.handle('check-drag-and-drop-path', (event, path) => {
4543
FileAccessWindow.check(path);
4644
});
4745

src-main/windows/privacy.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ class PrivacyWindow extends AbstractWindow {
88
constructor () {
99
super();
1010

11-
const ipc = this.window.webContents.ipc;
12-
13-
ipc.on('is-update-checker-allowed', (e) => {
11+
this.ipc.on('is-update-checker-allowed', (e) => {
1412
e.returnValue = isUpdateCheckerAllowed();
1513
});
1614

17-
ipc.handle('open-desktop-settings', () => {
15+
this.ipc.handle('open-desktop-settings', () => {
1816
DesktopSettingsWindow.show();
1917
});
2018

0 commit comments

Comments
 (0)