refactor: use separate ipc-renderer-internal / ipc-main-internal APIs for Electron internals (#13940)
This commit is contained in:
parent
f7122610cc
commit
b50f86ef43
49 changed files with 322 additions and 133 deletions
|
@ -1,9 +1,10 @@
|
|||
'use strict'
|
||||
|
||||
const electron = require('electron')
|
||||
const { ipcMain, WebContentsView, TopLevelWindow } = electron
|
||||
const { WebContentsView, TopLevelWindow } = electron
|
||||
const { BrowserWindow } = process.atomBinding('window')
|
||||
const v8Util = process.atomBinding('v8_util')
|
||||
const ipcMain = require('@electron/internal/browser/ipc-main-internal')
|
||||
|
||||
Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype)
|
||||
|
||||
|
|
14
lib/browser/api/crash-reporter.js
Normal file
14
lib/browser/api/crash-reporter.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
'use strict'
|
||||
|
||||
const CrashReporter = require('@electron/internal/common/crash-reporter')
|
||||
const ipcMain = require('@electron/internal/browser/ipc-main-internal')
|
||||
|
||||
class CrashReporterMain extends CrashReporter {
|
||||
sendSync (channel, ...args) {
|
||||
const event = {}
|
||||
ipcMain.emit(channel, event, ...args)
|
||||
return event.returnValue
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new CrashReporterMain()
|
|
@ -1,17 +1,9 @@
|
|||
'use strict'
|
||||
|
||||
const EventEmitter = require('events').EventEmitter
|
||||
const { EventEmitter } = require('events')
|
||||
|
||||
const emitter = new EventEmitter()
|
||||
|
||||
const removeAllListeners = emitter.removeAllListeners.bind(emitter)
|
||||
emitter.removeAllListeners = function (...args) {
|
||||
if (args.length === 0) {
|
||||
throw new Error('Removing all listeners from ipcMain will make Electron internals stop working. Please specify a event name')
|
||||
}
|
||||
removeAllListeners(...args)
|
||||
}
|
||||
|
||||
// Do not throw exception when channel name is "error".
|
||||
emitter.on('error', () => {})
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ module.exports = [
|
|||
{ name: 'BrowserView', file: 'browser-view' },
|
||||
{ name: 'BrowserWindow', file: 'browser-window' },
|
||||
{ name: 'contentTracing', file: 'content-tracing' },
|
||||
{ name: 'crashReporter', file: 'crash-reporter' },
|
||||
{ name: 'dialog', file: 'dialog' },
|
||||
{ name: 'globalShortcut', file: 'global-shortcut' },
|
||||
{ name: 'ipcMain', file: 'ipc-main' },
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
'use strict'
|
||||
|
||||
const { ipcMain } = require('electron')
|
||||
const ipcMain = require('@electron/internal/browser/ipc-main-internal')
|
||||
|
||||
// The history operation in renderer is redirected to browser.
|
||||
ipcMain.on('ELECTRON_NAVIGATION_CONTROLLER', function (event, method, ...args) {
|
||||
|
|
|
@ -6,6 +6,7 @@ const path = require('path')
|
|||
const url = require('url')
|
||||
const { app, ipcMain, session, NavigationController, deprecate } = electron
|
||||
|
||||
const ipcMainInternal = require('@electron/internal/browser/ipc-main-internal')
|
||||
const errorUtils = require('@electron/internal/common/error-utils')
|
||||
|
||||
// session is not used here, the purpose is to make sure session is initalized
|
||||
|
@ -98,12 +99,45 @@ Object.setPrototypeOf(WebContents.prototype, NavigationController.prototype)
|
|||
// WebContents::send(channel, args..)
|
||||
// WebContents::sendToAll(channel, args..)
|
||||
WebContents.prototype.send = function (channel, ...args) {
|
||||
if (channel == null) throw new Error('Missing required channel argument')
|
||||
return this._send(false, channel, args)
|
||||
if (typeof channel !== 'string') {
|
||||
throw new Error('Missing required channel argument')
|
||||
}
|
||||
|
||||
const internal = false
|
||||
const sendToAll = false
|
||||
|
||||
return this._send(internal, sendToAll, channel, args)
|
||||
}
|
||||
WebContents.prototype.sendToAll = function (channel, ...args) {
|
||||
if (channel == null) throw new Error('Missing required channel argument')
|
||||
return this._send(true, channel, args)
|
||||
if (typeof channel !== 'string') {
|
||||
throw new Error('Missing required channel argument')
|
||||
}
|
||||
|
||||
const internal = false
|
||||
const sendToAll = true
|
||||
|
||||
return this._send(internal, sendToAll, channel, args)
|
||||
}
|
||||
|
||||
WebContents.prototype._sendInternal = function (channel, ...args) {
|
||||
if (typeof channel !== 'string') {
|
||||
throw new Error('Missing required channel argument')
|
||||
}
|
||||
|
||||
const internal = true
|
||||
const sendToAll = false
|
||||
|
||||
return this._send(internal, sendToAll, channel, args)
|
||||
}
|
||||
WebContents.prototype._sendInternalToAll = function (channel, ...args) {
|
||||
if (typeof channel !== 'string') {
|
||||
throw new Error('Missing required channel argument')
|
||||
}
|
||||
|
||||
const internal = true
|
||||
const sendToAll = true
|
||||
|
||||
return this._send(internal, sendToAll, channel, args)
|
||||
}
|
||||
|
||||
// Following methods are mapped to webFrame.
|
||||
|
@ -116,8 +150,7 @@ const webFrameMethods = [
|
|||
|
||||
const asyncWebFrameMethods = function (requestId, method, callback, ...args) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args)
|
||||
ipcMain.once(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, function (event, error, result) {
|
||||
ipcMainInternal.once(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, function (event, error, result) {
|
||||
if (error == null) {
|
||||
if (typeof callback === 'function') callback(result)
|
||||
resolve(result)
|
||||
|
@ -125,12 +158,13 @@ const asyncWebFrameMethods = function (requestId, method, callback, ...args) {
|
|||
reject(errorUtils.deserialize(error))
|
||||
}
|
||||
})
|
||||
this._sendInternal('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args)
|
||||
})
|
||||
}
|
||||
|
||||
for (const method of webFrameMethods) {
|
||||
WebContents.prototype[method] = function (...args) {
|
||||
this.send('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, args)
|
||||
this._sendInternal('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, args)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -277,6 +311,19 @@ WebContents.prototype._init = function () {
|
|||
ipcMain.emit(channel, event, ...args)
|
||||
})
|
||||
|
||||
this.on('ipc-internal-message', function (event, [channel, ...args]) {
|
||||
ipcMainInternal.emit(channel, event, ...args)
|
||||
})
|
||||
this.on('ipc-internal-message-sync', function (event, [channel, ...args]) {
|
||||
Object.defineProperty(event, 'returnValue', {
|
||||
set: function (value) {
|
||||
return event.sendReply([value])
|
||||
},
|
||||
get: function () {}
|
||||
})
|
||||
ipcMainInternal.emit(channel, event, ...args)
|
||||
})
|
||||
|
||||
// Handle context menu action request from pepper plugin.
|
||||
this.on('pepper-context-menu', function (event, params, callback) {
|
||||
// Access Menu via electron.Menu to prevent circular require.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue