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