Browse Source

Add ipcRenderer.sendTo

Cheng Zhao 9 years ago
parent
commit
ae1f442b02
3 changed files with 39 additions and 2 deletions
  1. 11 1
      lib/browser/rpc-server.js
  2. 8 0
      lib/renderer/api/ipc-renderer.js
  3. 20 1
      lib/renderer/chrome-api.js

+ 11 - 1
lib/browser/rpc-server.js

@@ -2,7 +2,7 @@
 
 const electron = require('electron')
 const v8Util = process.atomBinding('v8_util')
-const {ipcMain, isPromise} = electron
+const {ipcMain, isPromise, webContents} = electron
 
 const objectsRegistry = require('./objects-registry')
 
@@ -351,3 +351,13 @@ ipcMain.on('ELECTRON_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', function (event, request
     event.returnValue = exceptionToMeta(error)
   }
 })
+
+ipcMain.on('ELECTRON_BROWSER_SEND_TO', function (event, webContentsId, channel, ...args) {
+  let contents = webContents.fromId(webContentsId)
+  if (!contents) {
+    console.error(`Sending message to WebContents with unknown ID ${webContentsId}`)
+    return
+  }
+
+  contents.send(channel, ...args)
+})

+ 8 - 0
lib/renderer/api/ipc-renderer.js

@@ -18,4 +18,12 @@ ipcRenderer.sendToHost = function (...args) {
   return binding.send('ipc-message-host', args)
 }
 
+ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
+  if (typeof webContentsId !== 'number') {
+    throw new TypeError(`First argument has to be webContentsId`)
+  }
+
+  ipcRenderer.send('ELECTRON_BROWSER_SEND_TO', webContentsId, channel, ...args)
+}
+
 module.exports = ipcRenderer

+ 20 - 1
lib/renderer/chrome-api.js

@@ -130,6 +130,25 @@ exports.injectTo = function (extensionId, context) {
   }
 
   chrome.extension = {
-    getURL: chrome.runtime.getURL
+    getURL: chrome.runtime.getURL,
+    connect: chrome.runtime.connect
+    onConnect: chrome.runtime.onConnect
+  }
+
+  chrome.storage = {
+    sync: {
+      get () {},
+      set () {}
+    }
+  }
+
+  chrome.pageAction = {
+    show: () {},
+    hide: () {},
+    setTitle: () {},
+    getTitle: () {},
+    setIcon: () {},
+    setPopup: () {},
+    getPopup: () {}
   }
 }