refactor: replace ipcRendererUtils.invoke() with ipcRendererInternal.invoke() (#19574)
This commit is contained in:
parent
698120daf0
commit
81e9dab52f
29 changed files with 195 additions and 164 deletions
|
@ -1,5 +1,5 @@
|
|||
import { nativeImage } from 'electron'
|
||||
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
||||
|
||||
// |options.types| can't be empty and must be an array
|
||||
function isValid (options: Electron.SourcesOptions) {
|
||||
|
@ -16,7 +16,7 @@ export async function getSources (options: Electron.SourcesOptions) {
|
|||
const { thumbnailSize = { width: 150, height: 150 } } = options
|
||||
const { fetchWindowIcons = false } = options
|
||||
|
||||
const sources = await ipcRendererUtils.invoke<ElectronInternal.GetSourcesResult[]>('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', {
|
||||
const sources = await ipcRendererInternal.invoke<ElectronInternal.GetSourcesResult[]>('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', {
|
||||
captureWindow,
|
||||
captureScreen,
|
||||
thumbnailSize,
|
||||
|
|
|
@ -21,11 +21,12 @@ ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
|
|||
return ipc.sendTo(internal, false, webContentsId, channel, args)
|
||||
}
|
||||
|
||||
ipcRenderer.invoke = function (channel, ...args) {
|
||||
return ipc.invoke(channel, args).then(({ error, result }) => {
|
||||
if (error) { throw new Error(`Error invoking remote method '${channel}': ${error}`) }
|
||||
return result
|
||||
})
|
||||
ipcRenderer.invoke = async function (channel, ...args) {
|
||||
const { error, result } = await ipc.invoke(internal, channel, args)
|
||||
if (error) {
|
||||
throw new Error(`Error invoking remote method '${channel}': ${error}`)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export default ipcRenderer
|
||||
|
|
|
@ -157,7 +157,7 @@ export function injectTo (extensionId: string, context: any) {
|
|||
console.error('options are not supported')
|
||||
}
|
||||
|
||||
ipcRendererUtils.invoke('CHROME_RUNTIME_SEND_MESSAGE', targetExtensionId, message).then(responseCallback)
|
||||
ipcRendererInternal.invoke('CHROME_RUNTIME_SEND_MESSAGE', targetExtensionId, message).then(responseCallback)
|
||||
},
|
||||
|
||||
onConnect: new Event(),
|
||||
|
@ -172,7 +172,7 @@ export function injectTo (extensionId: string, context: any) {
|
|||
details: Chrome.Tabs.ExecuteScriptDetails,
|
||||
resultCallback: Chrome.Tabs.ExecuteScriptCallback = () => {}
|
||||
) {
|
||||
ipcRendererUtils.invoke('CHROME_TABS_EXECUTE_SCRIPT', tabId, extensionId, details)
|
||||
ipcRendererInternal.invoke('CHROME_TABS_EXECUTE_SCRIPT', tabId, extensionId, details)
|
||||
.then((result: any) => resultCallback([result]))
|
||||
},
|
||||
|
||||
|
@ -183,7 +183,7 @@ export function injectTo (extensionId: string, context: any) {
|
|||
_options: Chrome.Tabs.SendMessageDetails,
|
||||
responseCallback: Chrome.Tabs.SendMessageCallback = () => {}
|
||||
) {
|
||||
ipcRendererUtils.invoke('CHROME_TABS_SEND_MESSAGE', tabId, extensionId, message).then(responseCallback)
|
||||
ipcRendererInternal.invoke('CHROME_TABS_SEND_MESSAGE', tabId, extensionId, message).then(responseCallback)
|
||||
},
|
||||
|
||||
onUpdated: new Event(),
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
||||
|
||||
const getStorage = (storageType: string, extensionId: number, callback: Function) => {
|
||||
if (typeof callback !== 'function') throw new TypeError('No callback provided')
|
||||
|
||||
ipcRendererUtils.invoke<string>('CHROME_STORAGE_READ', storageType, extensionId)
|
||||
ipcRendererInternal.invoke<string>('CHROME_STORAGE_READ', storageType, extensionId)
|
||||
.then(data => {
|
||||
if (data !== null) {
|
||||
callback(JSON.parse(data))
|
||||
|
@ -17,7 +17,7 @@ const getStorage = (storageType: string, extensionId: number, callback: Function
|
|||
|
||||
const setStorage = (storageType: string, extensionId: number, storage: Record<string, any>, callback: Function) => {
|
||||
const json = JSON.stringify(storage)
|
||||
ipcRendererUtils.invoke('CHROME_STORAGE_WRITE', storageType, extensionId, json)
|
||||
ipcRendererInternal.invoke('CHROME_STORAGE_WRITE', storageType, extensionId, json)
|
||||
.then(() => {
|
||||
if (callback) callback()
|
||||
})
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { invoke, invokeSync } from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
||||
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
|
||||
window.onload = function () {
|
||||
// Use menu API to show context menu.
|
||||
|
@ -19,7 +20,7 @@ function completeURL (project: string, path: string) {
|
|||
|
||||
// The DOM implementation expects (message?: string) => boolean
|
||||
(window.confirm as any) = function (message: string, title: string) {
|
||||
return invokeSync('ELECTRON_INSPECTOR_CONFIRM', message, title) as boolean
|
||||
return ipcRendererUtils.invokeSync('ELECTRON_INSPECTOR_CONFIRM', message, title) as boolean
|
||||
}
|
||||
|
||||
const useEditMenuItems = function (x: number, y: number, items: ContextMenuItem[]) {
|
||||
|
@ -32,7 +33,7 @@ const useEditMenuItems = function (x: number, y: number, items: ContextMenuItem[
|
|||
|
||||
const createMenu = function (x: number, y: number, items: ContextMenuItem[]) {
|
||||
const isEditMenu = useEditMenuItems(x, y, items)
|
||||
invoke<number>('ELECTRON_INSPECTOR_CONTEXT_MENU', items, isEditMenu).then(id => {
|
||||
ipcRendererInternal.invoke<number>('ELECTRON_INSPECTOR_CONTEXT_MENU', items, isEditMenu).then(id => {
|
||||
if (typeof id === 'number') {
|
||||
window.DevToolsAPI!.contextMenuItemSelected(id)
|
||||
}
|
||||
|
@ -41,7 +42,7 @@ const createMenu = function (x: number, y: number, items: ContextMenuItem[]) {
|
|||
}
|
||||
|
||||
const showFileChooserDialog = function (callback: (blob: File) => void) {
|
||||
invoke<[ string, any ]>('ELECTRON_INSPECTOR_SELECT_FILE').then(([path, data]) => {
|
||||
ipcRendererInternal.invoke<[ string, any ]>('ELECTRON_INSPECTOR_SELECT_FILE').then(([path, data]) => {
|
||||
if (path && data) {
|
||||
callback(dataToHtml5FileObject(path, data))
|
||||
}
|
||||
|
|
|
@ -4,38 +4,18 @@ 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[]) {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const requestId = ++nextId
|
||||
ipcRendererInternal.once(`${command}_RESPONSE_${requestId}`, (
|
||||
_event, error: Electron.SerializedError, result: any
|
||||
) => {
|
||||
if (error) {
|
||||
reject(errorUtils.deserialize(error))
|
||||
} else {
|
||||
resolve(result)
|
||||
}
|
||||
})
|
||||
ipcRendererInternal.send(command, requestId, ...args)
|
||||
ipcRendererInternal.on(channel, async (event, requestId, ...args) => {
|
||||
const replyChannel = `${channel}_RESPONSE_${requestId}`
|
||||
try {
|
||||
event.sender.send(replyChannel, null, await handler(event, ...args))
|
||||
} catch (error) {
|
||||
event.sender.send(replyChannel, errorUtils.serialize(error))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function invokeSync<T> (command: string, ...args: any[]): T {
|
||||
const [ error, result ] = ipcRendererInternal.sendSync(command, null, ...args)
|
||||
const [ error, result ] = ipcRendererInternal.sendSync(command, ...args)
|
||||
|
||||
if (error) {
|
||||
throw errorUtils.deserialize(error)
|
||||
|
|
|
@ -20,3 +20,11 @@ ipcRendererInternal.sendTo = function (webContentsId, channel, ...args) {
|
|||
ipcRendererInternal.sendToAll = function (webContentsId, channel, ...args) {
|
||||
return ipc.sendTo(internal, true, webContentsId, channel, args)
|
||||
}
|
||||
|
||||
ipcRendererInternal.invoke = async function<T> (channel: string, ...args: any[]) {
|
||||
const { error, result } = await ipc.invoke<T>(internal, channel, args)
|
||||
if (error) {
|
||||
throw new Error(`Error invoking remote method '${channel}': ${error}`)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { webFrame } from 'electron'
|
||||
import { invoke } from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
||||
|
||||
let shouldLog: boolean | null = null
|
||||
|
||||
|
@ -299,7 +299,7 @@ const logSecurityWarnings = function (
|
|||
|
||||
const getWebPreferences = async function () {
|
||||
try {
|
||||
return invoke<Electron.WebPreferences>('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES')
|
||||
return ipcRendererInternal.invoke<Electron.WebPreferences>('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES')
|
||||
} catch (error) {
|
||||
console.warn(`getLastWebPreferences() failed: ${error}`)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { webFrame, IpcMessageEvent } from 'electron'
|
||||
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
||||
import { invoke, invokeSync } from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
|
||||
import { WebViewImpl } from '@electron/internal/renderer/web-view/web-view-impl'
|
||||
|
||||
|
@ -93,11 +93,11 @@ export function deregisterEvents (viewInstanceId: number) {
|
|||
}
|
||||
|
||||
export function createGuest (params: Record<string, any>): Promise<number> {
|
||||
return invoke('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST', params)
|
||||
return ipcRendererInternal.invoke('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST', params)
|
||||
}
|
||||
|
||||
export function createGuestSync (params: Record<string, any>): number {
|
||||
return invokeSync('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST', params)
|
||||
return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST', params)
|
||||
}
|
||||
|
||||
export function attachGuest (
|
||||
|
@ -107,7 +107,7 @@ export function attachGuest (
|
|||
if (embedderFrameId < 0) { // this error should not happen.
|
||||
throw new Error('Invalid embedder frame')
|
||||
}
|
||||
invoke('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', embedderFrameId, elementInstanceId, guestInstanceId, params)
|
||||
ipcRendererInternal.invoke('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', embedderFrameId, elementInstanceId, guestInstanceId, params)
|
||||
}
|
||||
|
||||
export const guestViewInternalModule = {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
||||
import { WebViewImpl } from '@electron/internal/renderer/web-view/web-view-impl'
|
||||
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'
|
||||
|
||||
|
@ -196,7 +196,7 @@ class SrcAttribute extends WebViewAttribute {
|
|||
const method = 'loadURL'
|
||||
const args = [this.getValue(), opts]
|
||||
|
||||
ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', guestInstanceId, method, args)
|
||||
ipcRendererInternal.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', guestInstanceId, method, args)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { remote, 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 guestViewInternal from '@electron/internal/renderer/web-view/guest-view-internal'
|
||||
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'
|
||||
|
@ -253,7 +254,7 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem
|
|||
|
||||
const createNonBlockHandler = function (method: string) {
|
||||
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
|
||||
return ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args)
|
||||
return ipcRendererInternal.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -106,7 +106,7 @@ class LocationProxy {
|
|||
}
|
||||
|
||||
private _invokeWebContentsMethod (method: string, ...args: any[]) {
|
||||
return ipcRendererUtils.invoke('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, method, ...args)
|
||||
return ipcRendererInternal.invoke('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, method, ...args)
|
||||
}
|
||||
|
||||
private _invokeWebContentsMethodSync (method: string, ...args: any[]) {
|
||||
|
@ -158,7 +158,7 @@ class BrowserWindowProxy {
|
|||
}
|
||||
|
||||
public postMessage (message: any, targetOrigin: string) {
|
||||
ipcRendererUtils.invoke('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', this.guestId, message, toString(targetOrigin), window.location.origin)
|
||||
ipcRendererInternal.invoke('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', this.guestId, message, toString(targetOrigin), window.location.origin)
|
||||
}
|
||||
|
||||
public eval (code: string) {
|
||||
|
@ -166,11 +166,11 @@ class BrowserWindowProxy {
|
|||
}
|
||||
|
||||
private _invokeWindowMethod (method: string, ...args: any[]) {
|
||||
return ipcRendererUtils.invoke('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, method, ...args)
|
||||
return ipcRendererInternal.invoke('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, method, ...args)
|
||||
}
|
||||
|
||||
private _invokeWebContentsMethod (method: string, ...args: any[]) {
|
||||
return ipcRendererUtils.invoke('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, method, ...args)
|
||||
return ipcRendererInternal.invoke('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, method, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue