Use ipcRenderer.sendTo to get rid of routers in main process

This commit is contained in:
Cheng Zhao 2016-05-28 21:13:00 +09:00
parent a58b84bbd7
commit ba315248e0
5 changed files with 33 additions and 50 deletions

View file

@ -80,31 +80,12 @@ ipcMain.on('CHROME_RUNTIME_CONNECT', function (event, extensionId, connectInfo)
event.returnValue = {webContentsId: page.webContents.id, portId: portId}
event.sender.once('render-view-deleted', () => {
page.webContents.sendToAll(`CHROME_PORT_ONDISCONNECT_${portId}`)
if (page.webContents.isDestroyed()) return
page.webContents.sendToAll(`CHROME_PORT_DISCONNECT_${portId}`)
})
page.webContents.sendToAll('CHROME_RUNTIME_ONCONNECT', event.sender.id, portId, extensionId, connectInfo)
})
ipcMain.on('CHROME_PORT_DISCONNECT', function (event, webContentsId, portId) {
const contents = webContents.fromId(webContentsId)
if (!contents) {
console.error(`Disconnet to unkown webContentsId ${webContentsId}`)
return
}
contents.sendToAll(`CHROME_PORT_ONDISCONNECT_${portId}`)
})
ipcMain.on('CHROME_PORT_POSTMESSAGE', function (event, webContentsId, portId, message) {
const contents = webContents.fromId(webContentsId)
if (!contents) {
console.error(`Sending message to unkown webContentsId ${webContentsId}`)
return
}
contents.sendToAll(`CHROME_PORT_ONMESSAGE_${portId}`, message)
})
ipcMain.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, webContentsId, extensionId, details) {
const contents = webContents.fromId(webContentsId)
if (!contents) {
@ -122,17 +103,7 @@ ipcMain.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, webContentsI
url = `chrome-extension://${extensionId}/${String(Math.random()).substr(2, 8)}.js`
}
contents.send('CHROME_TABS_EXECUTESCRIPT', requestId, event.sender.id, extensionId, url, code)
})
ipcMain.on('CHROME_TABS_EXECUTESCRIPT_RESULT', (event, requestId, webContentsId, result) => {
const contents = webContents.fromId(webContentsId)
if (!contents) {
console.error(`Sending message to unkown webContentsId ${webContentsId}`)
return
}
contents.send(`CHROME_TABS_EXECUTESCRIPT_RESULT_${requestId}`, result)
contents.send('CHROME_TABS_EXECUTESCRIPT', event.sender.id, requestId, extensionId, url, code)
})
// Transfer the content scripts to renderer.