8000 refactor: add ipcMainUtils.invokeInWebContents / ipcRendererUtils.handle helpers by miniak · Pull Request #17313 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: add ipcMainUtils.invokeInWebContents / ipcRendererUtils.handle helpers #17313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/browser/api/web-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { app, ipcMain, session, deprecate } = electron

const NavigationController = require('@electron/internal/browser/navigation-controller')
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
const errorUtils = require('@electron/internal/common/error-utils')

// session is not used here, the purpose is to make sure session is initalized
Expand Down Expand Up @@ -193,7 +194,7 @@ const asyncWebFrameMethods = function (requestId, method, callback, ...args) {

for (const method of webFrameMethods) {
WebContents.prototype[method] = function (...args) {
< 8000 /td> this._sendInternal('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, args)
ipcMainUtils.invokeInWebContents(this, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, ...args)
}
}

Expand Down
20 changes: 19 additions & 1 deletion lib/browser/ipc-main-internal-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal'
import * as errorUtils from '@electron/internal/common/error-utils'

type IPCHandler = (...args: any[]) => any
type IPCHandler = (event: ElectronInternal.IpcMainInternalEvent, ...args: any[]) => any

const callHandler = async function (handler: IPCHandler, event: ElectronInternal.IpcMainInternalEvent, args: any[], reply: (args: any[]) => void) {
try {
Expand All @@ -23,3 +23,21 @@ export const handle = function <T extends IPCHandler> (channel: string, handler:
})
})
}

let nextId = 0

export function invokeInWebContents<T> (sender: Electron.WebContentsInternal, command: string, ...args: any[]) {
return new Promise<T>((resolve, reject) => {
const requestId = ++nextId
ipcMainInternal.once(`${command}_RESPONSE_${requestId}`, (
_event, error: Electron.SerializedError, result: any
) => {
if (error) {
reject(errorUtils.deserialize(error))
} else {
resolve(result)
}
})
sender._sendInternal(command, requestId, ...args)
})
}
15 changes: 15 additions & 0 deletions lib/renderer/ipc-renderer-internal-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
import * as errorUtils from '@electron/internal/common/error-utils'

type IPCHandler = (event: Electron.IpcRendererEvent, ...args: any[]) => any

export const handle = function <T extends IPCHandler> (channel: string, handler: T) {
ipcRendererInternal.on(channel, (event, requestId, ...args) => {
new Promise(resolve => resolve(handler(event, ...args))
).then(result => {
return [null, result]
}, error => {
return [errorUtils.serialize(error)]
}).then(responseArgs => {
event.sender.send(`${channel}_RESPONSE_${requestId}`, ...responseArgs)
})
})
}

let nextId = 0

export function invoke<T> (command: string, ...args: any[]) {
Expand Down
7 changes: 4 additions & 3 deletions lib/renderer/web-frame-init.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { webFrame, WebFrame } from 'electron'
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
import * as errorUtils from '@electron/internal/common/error-utils'

// All keys of WebFrame that extend Function
Expand All @@ -10,13 +11,13 @@ type WebFrameMethod = {

export const webFrameInit = () => {
// Call webFrame method
ipcRendererInternal.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (
_event, method: keyof WebFrameMethod, args: any[]
ipcRendererUtils.handle('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (
event, method: keyof WebFrameMethod, ...args: any[]
) => {
// The TypeScript compiler cannot handle the sheer number of
// call signatures here and simply gives up. Incorrect invocations
// will be caught by "keyof WebFrameMethod" though.
(webFrame[method] as any)(...args)
return (webFrame[method] as any)(...args)
})

ipcRendererInternal.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (
Expand Down
4 changes: 4 additions & 0 deletions typings/internal-electron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ declare namespace Electron {
sendToAll(webContentsId: number, channel: string, ...args: any[]): void
}

interface WebContentsInternal extends Electron.WebContents {
_sendInternal(channel: string, ...args: any[]): void;
}

const deprecate: ElectronInternal.DeprecationUtil;
}

Expand Down
0