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

@ -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 () {}
}
}