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) {
}
}
-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)
})
}
}
}
+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 || {}
}
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)
}
}