From db941213601d389be2b1d8abf0b52694f0d9a9e0 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 28 May 2016 16:01:16 +0900 Subject: [PATCH] Implement port.sender --- lib/browser/chrome-extension.js | 2 +- lib/renderer/chrome-api.js | 39 ++++++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/browser/chrome-extension.js b/lib/browser/chrome-extension.js index a03be04..0dac4f9 100644 --- a/lib/browser/chrome-extension.js +++ b/lib/browser/chrome-extension.js @@ -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) { diff --git a/lib/renderer/chrome-api.js b/lib/renderer/chrome-api.js index a1bbbe5..0c9fbe9 100644 --- a/lib/renderer/chrome-api.js +++ b/lib/renderer/chrome-api.js @@ -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) } } -- 2.7.4