Refactor sandbox preload initialization. (#12877)

Use a single synchronous IPC call to retrieve data required by early
sandbox scripts. This has two purposes:

- Optimize preload script initialization by:
  - Using one synchronous IPC call to retrieve preload script,
  webContentsId (more on that later), process.{platform,execPath,env}
  - Lazy loading as many modules as possible.
- Fix #12316 for sandbox. @MarshallOfSound addressed the issue in
  #12342, but it was still present in sandbox mode. By loading
  webContentsId very early and skipping remote module at early
  startup, we fix it for sandbox.
This commit is contained in:
Thiago de Arruda 2018-05-21 09:56:05 -03:00 committed by Samuel Attard
parent ef7947d176
commit 6f076f7433
3 changed files with 62 additions and 21 deletions

View file

@ -3,6 +3,7 @@
const {Buffer} = require('buffer')
const electron = require('electron')
const {EventEmitter} = require('events')
const fs = require('fs')
const v8Util = process.atomBinding('v8_util')
const {ipcMain, isPromise, webContents} = electron
@ -448,3 +449,23 @@ ipcMain.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
}
event.returnValue = null
})
ipcMain.on('ELECTRON_BROWSER_SANDBOX_LOAD', function (event, preloadPath) {
let preloadSrc = null
let preloadError = null
if (preloadPath) {
try {
preloadSrc = fs.readFileSync(preloadPath).toString()
} catch (err) {
preloadError = {stack: err ? err.stack : (new Error(`Failed to load "${preloadPath}"`)).stack}
}
}
event.returnValue = {
preloadSrc: preloadSrc,
preloadError: preloadError,
webContentsId: event.sender.getId(),
platform: process.platform,
execPath: process.execPath,
env: process.env
}
})