Skip to content

Commit 605e502

Browse files
authored
chore: convert rpc-server to ts (electron#24271)
1 parent 9b4572d commit 605e502

File tree

6 files changed

+31
-26
lines changed

6 files changed

+31
-26
lines changed

filenames.auto.gni

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ auto_filenames = {
237237
"lib/browser/navigation-controller.js",
238238
"lib/browser/remote/objects-registry.ts",
239239
"lib/browser/remote/server.ts",
240-
"lib/browser/rpc-server.js",
240+
"lib/browser/rpc-server.ts",
241241
"lib/browser/utils.ts",
242242
"lib/common/api/clipboard.ts",
243243
"lib/common/api/deprecate.ts",

lib/browser/remote/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ const unwrapArgs = function (sender: electron.WebContents, frameId: number, cont
284284
};
285285

286286
const isRemoteModuleEnabledImpl = function (contents: electron.WebContents) {
287-
const webPreferences = (contents as any).getLastWebPreferences() || {};
287+
const webPreferences = contents.getLastWebPreferences() || {};
288288
return webPreferences.enableRemoteModule != null ? !!webPreferences.enableRemoteModule : false;
289289
};
290290

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
'use strict';
2-
3-
const electron = require('electron');
4-
const fs = require('fs');
1+
import * as electron from 'electron';
2+
import * as fs from 'fs';
3+
import { ipcMainInternal } from './ipc-main-internal';
4+
import * as ipcMainUtils from './ipc-main-internal-utils';
5+
import * as guestViewManager from './guest-view-manager';
6+
import * as typeUtils from '../common/type-utils';
7+
import { IpcMainInvokeEvent } from 'electron/main';
58

69
const eventBinding = process._linkedBinding('electron_browser_event');
710
const clipboard = process._linkedBinding('electron_common_clipboard');
811

9-
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal');
10-
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils');
11-
const guestViewManager = require('@electron/internal/browser/guest-view-manager');
12-
const typeUtils = require('@electron/internal/common/type-utils');
13-
14-
const emitCustomEvent = function (contents, eventName, ...args) {
12+
const emitCustomEvent = function (contents: electron.WebContents, eventName: string, ...args: any[]) {
1513
const event = eventBinding.createWithSender(contents);
1614

1715
electron.app.emit(eventName, event, contents, ...args);
@@ -20,22 +18,22 @@ const emitCustomEvent = function (contents, eventName, ...args) {
2018
return event;
2119
};
2220

23-
const logStack = function (contents, code, stack) {
21+
const logStack = function (contents: electron.WebContents, code: string, stack: string) {
2422
if (stack) {
2523
console.warn(`WebContents (${contents.id}): ${code}`, stack);
2624
}
2725
};
2826

2927
// Implements window.close()
30-
ipcMainInternal.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
28+
ipcMainInternal.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event: ElectronInternal.IpcMainInternalEvent) {
3129
const window = event.sender.getOwnerBrowserWindow();
3230
if (window) {
3331
window.close();
3432
}
3533
event.returnValue = null;
3634
});
3735

38-
ipcMainInternal.handle('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event) {
36+
ipcMainInternal.handle('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event: IpcMainInvokeEvent) {
3937
return event.sender.getLastWebPreferences();
4038
});
4139

@@ -51,18 +49,18 @@ const allowedClipboardMethods = (() => {
5149
}
5250
})();
5351

54-
ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD_SYNC', function (event, method, ...args) {
52+
ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD_SYNC', function (event: IpcMainInvokeEvent, method: string, ...args: any[]) {
5553
if (!allowedClipboardMethods.has(method)) {
5654
throw new Error(`Invalid method: ${method}`);
5755
}
5856

59-
return typeUtils.serialize(electron.clipboard[method](...typeUtils.deserialize(args)));
57+
return typeUtils.serialize((electron.clipboard as any)[method](...typeUtils.deserialize(args)));
6058
});
6159

6260
if (BUILDFLAG(ENABLE_DESKTOP_CAPTURER)) {
6361
const desktopCapturer = require('@electron/internal/browser/desktop-capturer');
6462

65-
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', async function (event, options, stack) {
63+
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', async function (event: IpcMainInvokeEvent, options: Electron.SourcesOptions, stack: string) {
6664
logStack(event.sender, 'desktopCapturer.getSources()', stack);
6765
const customEvent = emitCustomEvent(event.sender, 'desktop-capturer-get-sources');
6866

@@ -74,7 +72,7 @@ if (BUILDFLAG(ENABLE_DESKTOP_CAPTURER)) {
7472
return typeUtils.serialize(await desktopCapturer.getSourcesImpl(event, options));
7573
});
7674

77-
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_MEDIA_SOURCE_ID_FOR_WEB_CONTENTS', function (event, webContentsId, stack) {
75+
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_MEDIA_SOURCE_ID_FOR_WEB_CONTENTS', function (event: IpcMainInvokeEvent, webContentsId: number) {
7876
return desktopCapturer.getMediaSourceIdForWebContents(event, webContentsId);
7977
});
8078
}
@@ -83,7 +81,7 @@ const isRemoteModuleEnabled = BUILDFLAG(ENABLE_REMOTE_MODULE)
8381
? require('@electron/internal/browser/remote/server').isRemoteModuleEnabled
8482
: () => false;
8583

86-
const getPreloadScript = async function (preloadPath) {
84+
const getPreloadScript = async function (preloadPath: string) {
8785
let preloadSrc = null;
8886
let preloadError = null;
8987
try {
@@ -94,7 +92,7 @@ const getPreloadScript = async function (preloadPath) {
9492
return { preloadPath, preloadSrc, preloadError };
9593
};
9694

97-
ipcMainUtils.handleSync('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event) {
95+
ipcMainUtils.handleSync('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event: IpcMainInvokeEvent) {
9896
const preloadPaths = event.sender._getPreloadPaths();
9997
const webPreferences = event.sender.getLastWebPreferences() || {};
10098

@@ -115,7 +113,7 @@ ipcMainUtils.handleSync('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event)
115113
};
116114
});
117115

118-
ipcMainInternal.on('ELECTRON_BROWSER_PRELOAD_ERROR', function (event, preloadPath, error) {
116+
ipcMainInternal.on('ELECTRON_BROWSER_PRELOAD_ERROR', function (event: ElectronInternal.IpcMainInternalEvent, preloadPath: string, error: Error) {
119117
event.sender.emit('preload-error', event, preloadPath, error);
120118
});
121119

@@ -131,7 +129,7 @@ ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_GET_UPLOAD_TO_SERVER', () => {
131129
return electron.crashReporter.getUploadToServer();
132130
});
133131

134-
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_SET_UPLOAD_TO_SERVER', (event, uploadToServer) => {
132+
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_SET_UPLOAD_TO_SERVER', (event: IpcMainInvokeEvent, uploadToServer: boolean) => {
135133
return electron.crashReporter.setUploadToServer(uploadToServer);
136134
});
137135

spec-main/api-browser-window-spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ describe('BrowserWindow module', () => {
22132213
emittedOnce(app, 'web-contents-created'),
22142214
emittedOnce(ipcMain, 'answer')
22152215
]);
2216-
const webPreferences = (childWebContents as any).getLastWebPreferences();
2216+
const webPreferences = childWebContents.getLastWebPreferences();
22172217
expect(webPreferences.foo).to.equal('bar');
22182218
});
22192219

@@ -2530,7 +2530,7 @@ describe('BrowserWindow module', () => {
25302530
emittedOnce(app, 'web-contents-created'),
25312531
emittedOnce(ipcMain, 'answer')
25322532
]);
2533-
const webPreferences = (childWebContents as any).getLastWebPreferences();
2533+
const webPreferences = childWebContents.getLastWebPreferences();
25342534
expect(webPreferences.foo).to.equal('bar');
25352535
});
25362536

spec-main/chromium-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ describe('chromium features', () => {
598598
contents.sendInputEvent({ type: 'mouseDown', clickCount: 1, x: 1, y: 1 });
599599
contents.sendInputEvent({ type: 'mouseUp', clickCount: 1, x: 1, y: 1 });
600600
const [, window] = await emittedOnce(app, 'browser-window-created');
601-
const preferences = (window.webContents as any).getLastWebPreferences();
601+
const preferences = window.webContents.getLastWebPreferences();
602602
expect(preferences.javascript).to.be.false();
603603
});
604604

typings/internal-electron.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ declare namespace Electron {
3232
interface WebContents {
3333
_getURL(): string;
3434
getOwnerBrowserWindow(): Electron.BrowserWindow;
35+
getLastWebPreferences(): Electron.WebPreferences;
36+
_getPreloadPaths(): string[];
37+
}
38+
39+
interface WebPreferences {
40+
guestInstanceId?: number;
41+
openerId?: number;
3542
}
3643

3744
interface SerializedError {

0 commit comments

Comments
 (0)