ipc-renderer.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict'
  2. const binding = process.atomBinding('ipc')
  3. const v8Util = process.atomBinding('v8_util')
  4. // Created by init.js.
  5. const ipcRenderer = v8Util.getHiddenValue(global, 'ipc')
  6. ipcRenderer.send = function (...args) {
  7. return binding.send('ipc-message', args)
  8. }
  9. ipcRenderer.sendSync = function (...args) {
  10. return JSON.parse(binding.sendSync('ipc-message-sync', args))
  11. }
  12. ipcRenderer.sendToHost = function (...args) {
  13. return binding.send('ipc-message-host', args)
  14. }
  15. ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
  16. if (typeof webContentsId !== 'number') {
  17. throw new TypeError('First argument has to be webContentsId')
  18. }
  19. ipcRenderer.send('ELECTRON_BROWSER_SEND_TO', false, webContentsId, channel, ...args)
  20. }
  21. ipcRenderer.sendToAll = function (webContentsId, channel, ...args) {
  22. if (typeof webContentsId !== 'number') {
  23. throw new TypeError('First argument has to be webContentsId')
  24. }
  25. ipcRenderer.send('ELECTRON_BROWSER_SEND_TO', true, webContentsId, channel, ...args)
  26. }
  27. const removeAllListeners = ipcRenderer.removeAllListeners.bind(ipcRenderer)
  28. ipcRenderer.removeAllListeners = function (...args) {
  29. if (args.length === 0) {
  30. throw new Error('Removing all listeners from ipcRenderer will make Electron internals stop working. Please specify a event name')
  31. }
  32. removeAllListeners(...args)
  33. }
  34. module.exports = ipcRenderer