chore: convert rpc-server to ts (#24271)

This commit is contained in:
Jeremy Rose 2020-06-25 10:19:08 -07:00 committed by GitHub
parent 9b4572de44
commit 605e50269e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 26 deletions

View file

@ -237,7 +237,7 @@ auto_filenames = {
"lib/browser/navigation-controller.js", "lib/browser/navigation-controller.js",
"lib/browser/remote/objects-registry.ts", "lib/browser/remote/objects-registry.ts",
"lib/browser/remote/server.ts", "lib/browser/remote/server.ts",
"lib/browser/rpc-server.js", "lib/browser/rpc-server.ts",
"lib/browser/utils.ts", "lib/browser/utils.ts",
"lib/common/api/clipboard.ts", "lib/common/api/clipboard.ts",
"lib/common/api/deprecate.ts", "lib/common/api/deprecate.ts",

View file

@ -284,7 +284,7 @@ const unwrapArgs = function (sender: electron.WebContents, frameId: number, cont
}; };
const isRemoteModuleEnabledImpl = function (contents: electron.WebContents) { const isRemoteModuleEnabledImpl = function (contents: electron.WebContents) {
const webPreferences = (contents as any).getLastWebPreferences() || {}; const webPreferences = contents.getLastWebPreferences() || {};
return webPreferences.enableRemoteModule != null ? !!webPreferences.enableRemoteModule : false; return webPreferences.enableRemoteModule != null ? !!webPreferences.enableRemoteModule : false;
}; };

View file

@ -1,17 +1,15 @@
'use strict'; import * as electron from 'electron';
import * as fs from 'fs';
const electron = require('electron'); import { ipcMainInternal } from './ipc-main-internal';
const fs = require('fs'); import * as ipcMainUtils from './ipc-main-internal-utils';
import * as guestViewManager from './guest-view-manager';
import * as typeUtils from '../common/type-utils';
import { IpcMainInvokeEvent } from 'electron/main';
const eventBinding = process._linkedBinding('electron_browser_event'); const eventBinding = process._linkedBinding('electron_browser_event');
const clipboard = process._linkedBinding('electron_common_clipboard'); const clipboard = process._linkedBinding('electron_common_clipboard');
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal'); const emitCustomEvent = function (contents: electron.WebContents, eventName: string, ...args: any[]) {
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils');
const guestViewManager = require('@electron/internal/browser/guest-view-manager');
const typeUtils = require('@electron/internal/common/type-utils');
const emitCustomEvent = function (contents, eventName, ...args) {
const event = eventBinding.createWithSender(contents); const event = eventBinding.createWithSender(contents);
electron.app.emit(eventName, event, contents, ...args); electron.app.emit(eventName, event, contents, ...args);
@ -20,14 +18,14 @@ const emitCustomEvent = function (contents, eventName, ...args) {
return event; return event;
}; };
const logStack = function (contents, code, stack) { const logStack = function (contents: electron.WebContents, code: string, stack: string) {
if (stack) { if (stack) {
console.warn(`WebContents (${contents.id}): ${code}`, stack); console.warn(`WebContents (${contents.id}): ${code}`, stack);
} }
}; };
// Implements window.close() // Implements window.close()
ipcMainInternal.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) { ipcMainInternal.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event: ElectronInternal.IpcMainInternalEvent) {
const window = event.sender.getOwnerBrowserWindow(); const window = event.sender.getOwnerBrowserWindow();
if (window) { if (window) {
window.close(); window.close();
@ -35,7 +33,7 @@ ipcMainInternal.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
event.returnValue = null; event.returnValue = null;
}); });
ipcMainInternal.handle('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event) { ipcMainInternal.handle('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event: IpcMainInvokeEvent) {
return event.sender.getLastWebPreferences(); return event.sender.getLastWebPreferences();
}); });
@ -51,18 +49,18 @@ const allowedClipboardMethods = (() => {
} }
})(); })();
ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD_SYNC', function (event, method, ...args) { ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD_SYNC', function (event: IpcMainInvokeEvent, method: string, ...args: any[]) {
if (!allowedClipboardMethods.has(method)) { if (!allowedClipboardMethods.has(method)) {
throw new Error(`Invalid method: ${method}`); throw new Error(`Invalid method: ${method}`);
} }
return typeUtils.serialize(electron.clipboard[method](...typeUtils.deserialize(args))); return typeUtils.serialize((electron.clipboard as any)[method](...typeUtils.deserialize(args)));
}); });
if (BUILDFLAG(ENABLE_DESKTOP_CAPTURER)) { if (BUILDFLAG(ENABLE_DESKTOP_CAPTURER)) {
const desktopCapturer = require('@electron/internal/browser/desktop-capturer'); const desktopCapturer = require('@electron/internal/browser/desktop-capturer');
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', async function (event, options, stack) { ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', async function (event: IpcMainInvokeEvent, options: Electron.SourcesOptions, stack: string) {
logStack(event.sender, 'desktopCapturer.getSources()', stack); logStack(event.sender, 'desktopCapturer.getSources()', stack);
const customEvent = emitCustomEvent(event.sender, 'desktop-capturer-get-sources'); const customEvent = emitCustomEvent(event.sender, 'desktop-capturer-get-sources');
@ -74,7 +72,7 @@ if (BUILDFLAG(ENABLE_DESKTOP_CAPTURER)) {
return typeUtils.serialize(await desktopCapturer.getSourcesImpl(event, options)); return typeUtils.serialize(await desktopCapturer.getSourcesImpl(event, options));
}); });
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_MEDIA_SOURCE_ID_FOR_WEB_CONTENTS', function (event, webContentsId, stack) { ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_MEDIA_SOURCE_ID_FOR_WEB_CONTENTS', function (event: IpcMainInvokeEvent, webContentsId: number) {
return desktopCapturer.getMediaSourceIdForWebContents(event, webContentsId); return desktopCapturer.getMediaSourceIdForWebContents(event, webContentsId);
}); });
} }
@ -83,7 +81,7 @@ const isRemoteModuleEnabled = BUILDFLAG(ENABLE_REMOTE_MODULE)
? require('@electron/internal/browser/remote/server').isRemoteModuleEnabled ? require('@electron/internal/browser/remote/server').isRemoteModuleEnabled
: () => false; : () => false;
const getPreloadScript = async function (preloadPath) { const getPreloadScript = async function (preloadPath: string) {
let preloadSrc = null; let preloadSrc = null;
let preloadError = null; let preloadError = null;
try { try {
@ -94,7 +92,7 @@ const getPreloadScript = async function (preloadPath) {
return { preloadPath, preloadSrc, preloadError }; return { preloadPath, preloadSrc, preloadError };
}; };
ipcMainUtils.handleSync('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event) { ipcMainUtils.handleSync('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event: IpcMainInvokeEvent) {
const preloadPaths = event.sender._getPreloadPaths(); const preloadPaths = event.sender._getPreloadPaths();
const webPreferences = event.sender.getLastWebPreferences() || {}; const webPreferences = event.sender.getLastWebPreferences() || {};
@ -115,7 +113,7 @@ ipcMainUtils.handleSync('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event)
}; };
}); });
ipcMainInternal.on('ELECTRON_BROWSER_PRELOAD_ERROR', function (event, preloadPath, error) { ipcMainInternal.on('ELECTRON_BROWSER_PRELOAD_ERROR', function (event: ElectronInternal.IpcMainInternalEvent, preloadPath: string, error: Error) {
event.sender.emit('preload-error', event, preloadPath, error); event.sender.emit('preload-error', event, preloadPath, error);
}); });
@ -131,7 +129,7 @@ ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_GET_UPLOAD_TO_SERVER', () => {
return electron.crashReporter.getUploadToServer(); return electron.crashReporter.getUploadToServer();
}); });
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_SET_UPLOAD_TO_SERVER', (event, uploadToServer) => { ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_SET_UPLOAD_TO_SERVER', (event: IpcMainInvokeEvent, uploadToServer: boolean) => {
return electron.crashReporter.setUploadToServer(uploadToServer); return electron.crashReporter.setUploadToServer(uploadToServer);
}); });

View file

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

View file

@ -598,7 +598,7 @@ describe('chromium features', () => {
contents.sendInputEvent({ type: 'mouseDown', clickCount: 1, x: 1, y: 1 }); contents.sendInputEvent({ type: 'mouseDown', clickCount: 1, x: 1, y: 1 });
contents.sendInputEvent({ type: 'mouseUp', clickCount: 1, x: 1, y: 1 }); contents.sendInputEvent({ type: 'mouseUp', clickCount: 1, x: 1, y: 1 });
const [, window] = await emittedOnce(app, 'browser-window-created'); const [, window] = await emittedOnce(app, 'browser-window-created');
const preferences = (window.webContents as any).getLastWebPreferences(); const preferences = window.webContents.getLastWebPreferences();
expect(preferences.javascript).to.be.false(); expect(preferences.javascript).to.be.false();
}); });

View file

@ -32,6 +32,13 @@ declare namespace Electron {
interface WebContents { interface WebContents {
_getURL(): string; _getURL(): string;
getOwnerBrowserWindow(): Electron.BrowserWindow; getOwnerBrowserWindow(): Electron.BrowserWindow;
getLastWebPreferences(): Electron.WebPreferences;
_getPreloadPaths(): string[];
}
interface WebPreferences {
guestInstanceId?: number;
openerId?: number;
} }
interface SerializedError { interface SerializedError {