Skip to content

Commit

Permalink
refactor: do not capture ipcMain messages
Browse files Browse the repository at this point in the history
But capture the return values of ipcRenderer.sendSync
and ipcRenderer.invoke
  • Loading branch information
tyn1998 committed Sep 25, 2022
1 parent 00eee42 commit b3840bc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 51 deletions.
28 changes: 16 additions & 12 deletions packages/core-electron-main/browser-preload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const os = require('os');

const { ipcRenderer } = require('electron');

// for generating requestId to pair request and response
const uuid = () => Date.now().toString(36) + Math.random().toString(36).substr(2);

const initForDevtools = () => {
const getCapturer = () => {
if (window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__?.captureIPC) {
Expand All @@ -22,9 +25,7 @@ const initForDevtools = () => {
const originalIpcRendererOn = ipcRenderer.on;
ipcRenderer.on = (channel, handler) => {
const proxyHandler = (event, ...args) => {
if (channel !== 'main->browser') {
capture({ ipcMethod: 'ipcRenderer.on', channel, args });
}
capture({ ipcMethod: 'ipcRenderer.on', channel, args });
handler(event, ...args);
};
return originalIpcRendererOn.call(ipcRenderer, channel, proxyHandler);
Expand All @@ -40,21 +41,24 @@ const initForDevtools = () => {
// ipcRenderer.sendSync
const originalIpcRendererSendSync = ipcRenderer.sendSync;
ipcRenderer.sendSync = (channel, ...args) => {
capture({ ipcMethod: 'ipcRenderer.sendSync', channel, args });
return originalIpcRendererSendSync.call(ipcRenderer, channel, ...args);
const requestId = uuid();
capture({ ipcMethod: 'ipcRenderer.sendSync', channel, requestId, args });
const result = originalIpcRendererSendSync.call(ipcRenderer, channel, ...args);
capture({ ipcMethod: 'ipcRenderer.sendSync', channel, requestId, result });
return result;
};

// ipcRenderer.invoke
const originalIpcRendererInvoke = ipcRenderer.invoke;
ipcRenderer.invoke = (channel, ...args) => {
capture({ ipcMethod: 'ipcRenderer.invoke', channel, args });
return originalIpcRendererInvoke.call(ipcRenderer, channel, ...args);
const requestId = uuid();
capture({ ipcMethod: 'ipcRenderer.invoke', channel, requestId, args });
const resultPromise = originalIpcRendererInvoke.call(ipcRenderer, channel, ...args);
resultPromise.then((result) => {
capture({ ipcMethod: 'ipcRenderer.invoke', channel, requestId, result });
});
return resultPromise;
};

// receive messages that transfered from main process and capture them
ipcRenderer.on('main->browser', (event, message) => {
capture(message);
});
};

const electronEnv = {};
Expand Down
33 changes: 0 additions & 33 deletions packages/core-electron-main/src/bootstrap/devtools.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/core-electron-main/src/bootstrap/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from '@opensumi/ide-core-common';
import { normalizedIpcHandlerPathAsync } from '@opensumi/ide-core-common/lib/utils/ipc';

import { initForDevtools } from './devtools';
import { ElectronAppConfig, ICodeWindow, ICodeWindowOptions } from './types';

const DEFAULT_WINDOW_HEIGHT = 700;
Expand Down Expand Up @@ -96,11 +95,6 @@ export class CodeWindow extends Disposable implements ICodeWindow {
...options,
});

if (this.appConfig.devtools) {
// initialize for OpenSumi DevTools
initForDevtools(this.browser);
}

if (options) {
if (options.extensionDir) {
this.extensionDir = options.extensionDir;
Expand Down

0 comments on commit b3840bc

Please sign in to comment.