Implement port.sender
authorCheng Zhao <zcbenz@gmail.com>
Sat, 28 May 2016 07:01:16 +0000 (16:01 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Sat, 28 May 2016 07:01:16 +0000 (16:01 +0900)
lib/browser/chrome-extension.js
lib/renderer/chrome-api.js

index a03be0432ca206df6c74d50617b9df74b0bbaa2e..0dac4f985e0bcfbdb1dc1e156bfd3101b12c3d0c 100644 (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) {
index a1bbbe5f102a5095983a178913c380f84a13c34d..0c9fbe9e9e7bac19d172b1c8b5571669673a6458 100644 (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)
     }
   }