Send messages between app via socket connection

This commit is contained in:
Kevin Sawicki 2017-06-28 09:27:19 -07:00
parent 628744f9e1
commit 523fbe2df9
2 changed files with 61 additions and 17 deletions

View file

@ -568,19 +568,46 @@ describe('app module', function () {
}) })
describe('mixed sandbox option', function () { describe('mixed sandbox option', function () {
let appProcess let appProcess = null
let server = null
const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-mixed-sandbox' : '/tmp/electron-mixed-sandbox'
afterEach(function () { beforeEach(function (done) {
fs.unlink(socketPath, () => {
server = net.createServer()
server.listen(socketPath)
done()
})
})
afterEach(function (done) {
if (appProcess != null) { if (appProcess != null) {
appProcess.kill() appProcess.kill()
} }
server.close(() => {
if (process.platform === 'win32') {
done()
} else {
fs.unlink(socketPath, () => {
done()
})
}
})
}) })
describe('when app.enableMixedSandbox() is called', () => { describe('when app.enableMixedSandbox() is called', () => {
it('adds --enable-sandbox to render processes created with sandbox: true', (done) => { it('adds --enable-sandbox to render processes created with sandbox: true', (done) => {
const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app') const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app')
appProcess = ChildProcess.spawn(remote.process.execPath, [appPath], {stdio: ['ignore', 'ipc', 'ignore']}) appProcess = ChildProcess.spawn(remote.process.execPath, [appPath])
appProcess.once('message', (argv) => {
server.once('error', (error) => {
done(error)
})
server.on('connection', (client) => {
client.once('data', function (data) {
const argv = JSON.parse(data)
assert.equal(argv.sandbox.includes('--enable-sandbox'), true) assert.equal(argv.sandbox.includes('--enable-sandbox'), true)
assert.equal(argv.sandbox.includes('--no-sandbox'), false) assert.equal(argv.sandbox.includes('--no-sandbox'), false)
@ -591,12 +618,20 @@ describe('app module', function () {
}) })
}) })
}) })
})
describe('when the app is launched with --enable-mixed-sandbox', () => { describe('when the app is launched with --enable-mixed-sandbox', () => {
it('adds --enable-sandbox to render processes created with sandbox: true', (done) => { it('adds --enable-sandbox to render processes created with sandbox: true', (done) => {
const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app') const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app')
appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, '--enable-mixed-sandbox'], {stdio: ['ignore', 'ipc', 'ignore']}) appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, '--enable-mixed-sandbox'])
appProcess.once('message', (argv) => {
server.once('error', (error) => {
done(error)
})
server.on('connection', (client) => {
client.once('data', function (data) {
const argv = JSON.parse(data)
assert.equal(argv.sandbox.includes('--enable-sandbox'), true) assert.equal(argv.sandbox.includes('--enable-sandbox'), true)
assert.equal(argv.sandbox.includes('--no-sandbox'), false) assert.equal(argv.sandbox.includes('--no-sandbox'), false)
@ -608,4 +643,5 @@ describe('app module', function () {
}) })
}) })
}) })
})
}) })

View file

@ -1,4 +1,5 @@
const {app, BrowserWindow, ipcMain} = require('electron') const {app, BrowserWindow, ipcMain} = require('electron')
const net = require('net')
const path = require('path') const path = require('path')
process.on('uncaughtException', () => { process.on('uncaughtException', () => {
@ -44,7 +45,14 @@ app.once('ready', () => {
} }
if (argv.sandbox != null && argv.noSandbox != null) { if (argv.sandbox != null && argv.noSandbox != null) {
process.send(argv) const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-mixed-sandbox' : '/tmp/electron-mixed-sandbox'
const client = net.connect(socketPath)
client.once('connect', () => {
client.end(JSON.stringify(argv))
})
client.once('end', () => {
app.exit(0)
})
} }
}) })
}) })