ipc-renderer.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. const { ipc } = process.electronBinding('ipc');
  2. const v8Util = process.electronBinding('v8_util');
  3. // Created by init.js.
  4. const ipcRenderer = v8Util.getHiddenValue<Electron.IpcRenderer>(global, 'ipc');
  5. const internal = false;
  6. // TODO(MarshallOfSound): Remove if statement when isolated_bundle and content_script_bundle are gone
  7. if (!ipcRenderer.send) {
  8. ipcRenderer.send = function (channel, ...args) {
  9. return ipc.send(internal, channel, args);
  10. };
  11. ipcRenderer.sendSync = function (channel, ...args) {
  12. return ipc.sendSync(internal, channel, args)[0];
  13. };
  14. ipcRenderer.sendToHost = function (channel, ...args) {
  15. return ipc.sendToHost(channel, args);
  16. };
  17. ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
  18. return ipc.sendTo(internal, false, webContentsId, channel, args);
  19. };
  20. ipcRenderer.invoke = async function (channel, ...args) {
  21. const { error, result } = await ipc.invoke(internal, channel, args);
  22. if (error) {
  23. throw new Error(`Error invoking remote method '${channel}': ${error}`);
  24. }
  25. return result;
  26. };
  27. ipcRenderer.postMessage = function (channel: string, message: any, transferables: any) {
  28. return ipc.postMessage(channel, message, transferables);
  29. };
  30. }
  31. export default ipcRenderer;