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.

View file

@ -352,12 +352,16 @@ ipcMain.on('ELECTRON_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', function (event, request
}
})
ipcMain.on('ELECTRON_BROWSER_SEND_TO', function (event, webContentsId, channel, ...args) {
ipcMain.on('ELECTRON_BROWSER_SEND_TO', function (event, sendToAll, webContentsId, channel, ...args) {
let contents = webContents.fromId(webContentsId)
if (!contents) {
console.error(`Sending message to WebContents with unknown ID ${webContentsId}`)
return
}
if (sendToAll) {
contents.sendToAll(channel, ...args)
} else {
contents.send(channel, ...args)
}
})

View file

@ -23,7 +23,15 @@ ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
throw new TypeError(`First argument has to be webContentsId`)
}
ipcRenderer.send('ELECTRON_BROWSER_SEND_TO', webContentsId, channel, ...args)
ipcRenderer.send('ELECTRON_BROWSER_SEND_TO', false, webContentsId, channel, ...args)
}
ipcRenderer.sendToAll = function (webContentsId, channel, ...args) {
if (typeof webContentsId !== 'number') {
throw new TypeError(`First argument has to be webContentsId`)
}
ipcRenderer.send('ELECTRON_BROWSER_SEND_TO', true, webContentsId, channel, ...args)
}
module.exports = ipcRenderer

View file

@ -51,10 +51,10 @@ class Port {
this.onMessage = new Event()
this.sender = new MessageSender(webContentsId, extensionId)
ipcRenderer.once(`CHROME_PORT_ONDISCONNECT_${portId}`, () => {
ipcRenderer.once(`CHROME_PORT_DISCONNECT_${portId}`, () => {
this._onDisconnect()
})
ipcRenderer.on(`CHROME_PORT_ONMESSAGE_${portId}`, (event, message) => {
ipcRenderer.on(`CHROME_PORT_POSTMESSAGE_${portId}`, (event, message) => {
const sendResponse = function () { console.error('sendResponse is not implemented') }
this.onMessage.emit(message, this.sender, sendResponse)
})
@ -63,17 +63,17 @@ class Port {
disconnect () {
if (this.disconnected) return
ipcRenderer.send('CHROME_PORT_DISCONNECT', this.webContentsId, this.portId)
ipcRenderer.sendToAll(this.webContentsId, `CHROME_PORT_DISCONNECT_${this.portId}`)
this._onDisconnect()
}
postMessage (message) {
ipcRenderer.send('CHROME_PORT_POSTMESSAGE', this.webContentsId, this.portId, message)
ipcRenderer.sendToAll(this.webContentsId, `CHROME_PORT_POSTMESSAGE_${this.portId}`, message)
}
_onDisconnect () {
this.disconnected = true
ipcRenderer.removeAllListeners(`CHROME_PORT_ONMESSAGE_${this.portId}`)
ipcRenderer.removeAllListeners(`CHROME_PORT_POSTMESSAGE_${this.portId}`)
this.onDisconnect.emit()
}
}
@ -131,7 +131,7 @@ exports.injectTo = function (extensionId, context) {
chrome.extension = {
getURL: chrome.runtime.getURL,
connect: chrome.runtime.connect
connect: chrome.runtime.connect,
onConnect: chrome.runtime.onConnect
}
@ -143,12 +143,12 @@ exports.injectTo = function (extensionId, context) {
}
chrome.pageAction = {
show: () {},
hide: () {},
setTitle: () {},
getTitle: () {},
setIcon: () {},
setPopup: () {},
getPopup: () {}
show () {},
hide () {},
setTitle () {},
getTitle () {},
setIcon () {},
setPopup () {},
getPopup () {}
}
}

View file

@ -43,9 +43,9 @@ const injectContentScript = function (extensionId, script) {
}
// Handle the request of chrome.tabs.executeJavaScript.
ipcRenderer.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, senderId, extensionId, url, code) {
ipcRenderer.on('CHROME_TABS_EXECUTESCRIPT', function (event, senderWebContentsId, requestId, extensionId, url, code) {
const result = runContentScript.call(window, extensionId, url, code)
ipcRenderer.send('CHROME_TABS_EXECUTESCRIPT_RESULT', requestId, senderId, result)
ipcRenderer.sendToAll(senderWebContentsId, `CHROME_TABS_EXECUTESCRIPT_RESULT_${requestId}`, result)
})
// Read the renderer process preferences.