Implement port.sender

This commit is contained in:
Cheng Zhao 2016-05-28 16:01:16 +09:00
parent f5b430d9e1
commit db94121360
2 changed files with 23 additions and 18 deletions

View file

@ -93,7 +93,7 @@ ipcMain.on('CHROME_RUNTIME_CONNECT', function (event, hostname, connectInfo) {
event.sender.once('render-view-deleted', () => {
page.webContents.sendToAll(`CHROME_PORT_ONDISCONNECT_${portId}`)
})
page.webContents.sendToAll('CHROME_RUNTIME_ONCONNECT', event.sender.id, portId, connectInfo)
page.webContents.sendToAll('CHROME_RUNTIME_ONCONNECT', event.sender.id, portId, hostname, connectInfo)
})
ipcMain.on('CHROME_PORT_DISCONNECT', function (event, webContentsId, portId) {

View file

@ -24,41 +24,36 @@ class Event {
}
}
class OnConnect extends Event {
constructor () {
super()
ipcRenderer.on('CHROME_RUNTIME_ONCONNECT', (event, webContentsId, portId, connectInfo) => {
this.emit(new Port(webContentsId, portId, connectInfo.name))
})
class Tab {
constructor (webContentsId) {
this.id = webContentsId
}
}
class MessageSender {
constructor () {
this.tab = null
this.frameId = null
this.id = null
this.url = null
this.tlsChannelId = null
constructor (webContentsId, extensionId) {
this.tab = new Tab(webContentsId)
this.id = extensionId
this.url = `chrome-extension://${extensionId}`
}
}
class Port {
constructor (webContentsId, portId, name) {
constructor (webContentsId, portId, extensionId, name) {
this.webContentsId = webContentsId
this.portId = portId
this.name = name
this.onDisconnect = new Event()
this.onMessage = new Event()
this.sender = new MessageSender()
this.sender = new MessageSender(webContentsId, extensionId)
ipcRenderer.once(`CHROME_PORT_ONDISCONNECT_${portId}`, () => {
this._onDisconnect()
})
ipcRenderer.on(`CHROME_PORT_ONMESSAGE_${portId}`, (event, message) => {
this.onMessage.emit(message, new MessageSender(), function () {})
const sendResponse = function () { console.error('sendResponse is not implemented') }
this.onMessage.emit(message, this.sender, sendResponse)
})
}
@ -77,6 +72,16 @@ class Port {
}
}
class OnConnect extends Event {
constructor () {
super()
ipcRenderer.on('CHROME_RUNTIME_ONCONNECT', (event, webContentsId, portId, extensionId, connectInfo) => {
this.emit(new Port(webContentsId, portId, extensionId, connectInfo.name))
})
}
}
// Inject chrome API to the |context|
exports.injectTo = function (extensionId, context) {
const chrome = context.chrome = context.chrome || {}
@ -104,7 +109,7 @@ exports.injectTo = function (extensionId, context) {
}
const {webContentsId, portId} = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', targetExtensionId, connectInfo)
return new Port(webContentsId, portId, connectInfo.name)
return new Port(webContentsId, portId, extensionId, connectInfo.name)
}
}