guest-view-internal.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict'
  2. const {ipcRenderer, webFrame} = require('electron')
  3. let requestId = 0
  4. const WEB_VIEW_EVENTS = {
  5. 'load-commit': ['url', 'isMainFrame'],
  6. 'did-attach': [],
  7. 'did-finish-load': [],
  8. 'did-fail-load': ['errorCode', 'errorDescription', 'validatedURL', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
  9. 'did-frame-finish-load': ['isMainFrame', 'frameProcessId', 'frameRoutingId'],
  10. 'did-start-loading': [],
  11. 'did-stop-loading': [],
  12. '-did-get-response-details': ['status', 'newURL', 'originalURL', 'httpResponseCode', 'requestMethod', 'referrer', 'headers', 'resourceType'],
  13. '-did-get-redirect-request': ['oldURL', 'newURL', 'isMainFrame'],
  14. 'dom-ready': [],
  15. 'console-message': ['level', 'message', 'line', 'sourceId'],
  16. 'context-menu': ['params'],
  17. 'devtools-opened': [],
  18. 'devtools-closed': [],
  19. 'devtools-focused': [],
  20. 'new-window': ['url', 'frameName', 'disposition', 'options'],
  21. 'will-navigate': ['url'],
  22. 'did-start-navigation': ['url', 'isInPlace', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
  23. 'did-navigate': ['url', 'httpResponseCode', 'httpStatusText'],
  24. 'did-frame-navigate': ['url', 'httpResponseCode', 'httpStatusText', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
  25. 'did-navigate-in-page': ['url', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
  26. 'close': [],
  27. 'crashed': [],
  28. 'gpu-crashed': [],
  29. 'plugin-crashed': ['name', 'version'],
  30. 'destroyed': [],
  31. 'page-title-updated': ['title', 'explicitSet'],
  32. 'page-favicon-updated': ['favicons'],
  33. 'enter-html-full-screen': [],
  34. 'leave-html-full-screen': [],
  35. 'media-started-playing': [],
  36. 'media-paused': [],
  37. 'found-in-page': ['result'],
  38. 'did-change-theme-color': ['themeColor'],
  39. 'update-target-url': ['url']
  40. }
  41. const DEPRECATED_EVENTS = {
  42. 'page-title-updated': 'page-title-set'
  43. }
  44. const dispatchEvent = function (webView, eventName, eventKey, ...args) {
  45. if (DEPRECATED_EVENTS[eventName] != null) {
  46. dispatchEvent(webView, DEPRECATED_EVENTS[eventName], eventKey, ...args)
  47. }
  48. const domEvent = new Event(eventName)
  49. WEB_VIEW_EVENTS[eventKey].forEach((prop, index) => {
  50. domEvent[prop] = args[index]
  51. })
  52. webView.dispatchEvent(domEvent)
  53. if (eventName === 'load-commit') {
  54. webView.onLoadCommit(domEvent)
  55. }
  56. }
  57. module.exports = {
  58. registerEvents: function (webView, viewInstanceId) {
  59. ipcRenderer.on(`ELECTRON_GUEST_VIEW_INTERNAL_DESTROY_GUEST-${viewInstanceId}`, function () {
  60. webFrame.detachGuest(webView.internalInstanceId)
  61. webView.guestInstanceId = undefined
  62. webView.reset()
  63. const domEvent = new Event('destroyed')
  64. webView.dispatchEvent(domEvent)
  65. })
  66. ipcRenderer.on(`ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-${viewInstanceId}`, function (event, eventName, ...args) {
  67. dispatchEvent(webView, eventName, eventName, ...args)
  68. })
  69. ipcRenderer.on(`ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE-${viewInstanceId}`, function (event, channel, ...args) {
  70. const domEvent = new Event('ipc-message')
  71. domEvent.channel = channel
  72. domEvent.args = args
  73. webView.dispatchEvent(domEvent)
  74. })
  75. ipcRenderer.on(`ELECTRON_GUEST_VIEW_INTERNAL_SIZE_CHANGED-${viewInstanceId}`, function (event, ...args) {
  76. const domEvent = new Event('size-changed')
  77. const props = ['oldWidth', 'oldHeight', 'newWidth', 'newHeight']
  78. for (let i = 0; i < props.length; i++) {
  79. const prop = props[i]
  80. domEvent[prop] = args[i]
  81. }
  82. webView.onSizeChanged(domEvent)
  83. })
  84. },
  85. deregisterEvents: function (viewInstanceId) {
  86. ipcRenderer.removeAllListeners(`ELECTRON_GUEST_VIEW_INTERNAL_DESTROY_GUEST-${viewInstanceId}`)
  87. ipcRenderer.removeAllListeners(`ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-${viewInstanceId}`)
  88. ipcRenderer.removeAllListeners(`ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE-${viewInstanceId}`)
  89. ipcRenderer.removeAllListeners(`ELECTRON_GUEST_VIEW_INTERNAL_SIZE_CHANGED-${viewInstanceId}`)
  90. },
  91. createGuest: function (params, callback) {
  92. requestId++
  93. ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST', params, requestId)
  94. ipcRenderer.once(`ELECTRON_RESPONSE_${requestId}`, callback)
  95. },
  96. attachGuest: function (elementInstanceId, guestInstanceId, params) {
  97. ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', elementInstanceId, guestInstanceId, params)
  98. webFrame.attachGuest(elementInstanceId)
  99. },
  100. destroyGuest: function (guestInstanceId) {
  101. ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_DESTROY_GUEST', guestInstanceId)
  102. },
  103. setSize: function (guestInstanceId, params) {
  104. ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_SET_SIZE', guestInstanceId, params)
  105. }
  106. }