mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
* docs: add references to app.whenReady() in isReady
* refactor: prefer app.whenReady()
In the docs, specs, and lib, replace instances of `app.once('ready')`
(seen occasionally) and `app.on('ready')` (extremely common) with
`app.whenReady()`.
It's better to encourage users to use whenReady():
1. it handles the edge case of registering for 'ready' after it's fired
2. it avoids the minor wart of leaving an active listener alive for
an event that wll never fire again
78 lines
1.8 KiB
JavaScript
78 lines
1.8 KiB
JavaScript
const { app, BrowserWindow, ipcMain } = require('electron')
|
|
const net = require('net')
|
|
const path = require('path')
|
|
|
|
process.on('uncaughtException', () => {
|
|
app.exit(1)
|
|
})
|
|
|
|
if (process.argv.includes('--app-enable-sandbox')) {
|
|
app.enableSandbox()
|
|
}
|
|
|
|
let currentWindowSandboxed = false
|
|
|
|
app.whenReady().then(() => {
|
|
function testWindow (isSandboxed, callback) {
|
|
currentWindowSandboxed = isSandboxed
|
|
const currentWindow = new BrowserWindow({
|
|
show: false,
|
|
webPreferences: {
|
|
preload: path.join(__dirname, 'electron-app-mixed-sandbox-preload.js'),
|
|
sandbox: isSandboxed
|
|
}
|
|
})
|
|
currentWindow.loadURL('about:blank')
|
|
currentWindow.webContents.once('devtools-opened', () => {
|
|
if (isSandboxed) {
|
|
argv.sandboxDevtools = true
|
|
} else {
|
|
argv.noSandboxDevtools = true
|
|
}
|
|
if (callback) {
|
|
callback()
|
|
}
|
|
finish()
|
|
})
|
|
currentWindow.webContents.openDevTools()
|
|
}
|
|
|
|
const argv = {
|
|
sandbox: null,
|
|
noSandbox: null,
|
|
sandboxDevtools: null,
|
|
noSandboxDevtools: null
|
|
}
|
|
|
|
let connected = false
|
|
|
|
testWindow(true, () => {
|
|
testWindow()
|
|
})
|
|
|
|
function finish () {
|
|
if (connected && argv.sandbox != null && argv.noSandbox != null &&
|
|
argv.noSandboxDevtools != null && argv.sandboxDevtools != null) {
|
|
client.once('end', () => {
|
|
app.exit(0)
|
|
})
|
|
client.end(JSON.stringify(argv))
|
|
}
|
|
}
|
|
|
|
const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-mixed-sandbox' : '/tmp/electron-mixed-sandbox'
|
|
const client = net.connect(socketPath, () => {
|
|
connected = true
|
|
finish()
|
|
})
|
|
|
|
ipcMain.on('argv', (event, value) => {
|
|
if (currentWindowSandboxed) {
|
|
argv.sandbox = value
|
|
} else {
|
|
argv.noSandbox = value
|
|
}
|
|
finish()
|
|
})
|
|
})
|