refactor: make ELECTRON_BROWSER_SANDBOX_LOAD handler async (#17107)

This commit is contained in:
Milan Burda 2019-03-08 11:21:41 +01:00 committed by Alexey Kuzmin
parent 17dab8f239
commit d34f81972d
2 changed files with 10 additions and 6 deletions

View file

@ -3,6 +3,7 @@
const electron = require('electron') const electron = require('electron')
const { EventEmitter } = require('events') const { EventEmitter } = require('events')
const fs = require('fs') const fs = require('fs')
const util = require('util')
const v8Util = process.atomBinding('v8_util') const v8Util = process.atomBinding('v8_util')
const eventBinding = process.atomBinding('event') const eventBinding = process.atomBinding('event')
@ -495,12 +496,14 @@ ipcMainUtils.handle('ELECTRON_BROWSER_CLIPBOARD_WRITE_FIND_TEXT', function (even
return electron.clipboard.writeFindText(text) return electron.clipboard.writeFindText(text)
}) })
const getPreloadScript = function (preloadPath) { const readFile = util.promisify(fs.readFile)
const getPreloadScript = async function (preloadPath) {
let preloadSrc = null let preloadSrc = null
let preloadError = null let preloadError = null
if (preloadPath) { if (preloadPath) {
try { try {
preloadSrc = fs.readFileSync(preloadPath).toString() preloadSrc = (await readFile(preloadPath)).toString()
} catch (err) { } catch (err) {
preloadError = errorUtils.serialize(err) preloadError = errorUtils.serialize(err)
} }
@ -508,14 +511,14 @@ const getPreloadScript = function (preloadPath) {
return { preloadPath, preloadSrc, preloadError } return { preloadPath, preloadSrc, preloadError }
} }
ipcMainInternal.on('ELECTRON_BROWSER_SANDBOX_LOAD', function (event) { ipcMainUtils.handle('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event) {
const preloadPaths = [ const preloadPaths = [
...(event.sender.session ? event.sender.session.getPreloads() : []), ...(event.sender.session ? event.sender.session.getPreloads() : []),
event.sender._getPreloadPath() event.sender._getPreloadPath()
] ]
event.returnValue = { return {
preloadScripts: preloadPaths.map(path => getPreloadScript(path)), preloadScripts: await Promise.all(preloadPaths.map(path => getPreloadScript(path))),
isRemoteModuleEnabled: isRemoteModuleEnabled(event.sender), isRemoteModuleEnabled: isRemoteModuleEnabled(event.sender),
isWebViewTagEnabled: guestViewManager.isWebViewTagEnabled(event.sender), isWebViewTagEnabled: guestViewManager.isWebViewTagEnabled(event.sender),
process: { process: {

View file

@ -27,10 +27,11 @@ for (const prop of Object.keys(EventEmitter.prototype)) {
Object.setPrototypeOf(process, EventEmitter.prototype) Object.setPrototypeOf(process, EventEmitter.prototype)
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal')
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
const { const {
preloadScripts, isRemoteModuleEnabled, isWebViewTagEnabled, process: processProps preloadScripts, isRemoteModuleEnabled, isWebViewTagEnabled, process: processProps
} = ipcRendererInternal.sendSync('ELECTRON_BROWSER_SANDBOX_LOAD') } = ipcRendererUtils.invokeSync('ELECTRON_BROWSER_SANDBOX_LOAD')
process.isRemoteModuleEnabled = isRemoteModuleEnabled process.isRemoteModuleEnabled = isRemoteModuleEnabled