web-frame.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { EventEmitter } from 'events'
  2. import { deprecate } from 'electron'
  3. const binding = process.electronBinding('web_frame')
  4. class WebFrame extends EventEmitter {
  5. constructor (public context: Window) {
  6. super()
  7. // Lots of webview would subscribe to webFrame's events.
  8. this.setMaxListeners(0)
  9. }
  10. findFrameByRoutingId (...args: Array<any>) {
  11. return getWebFrame(binding._findFrameByRoutingId(this.context, ...args))
  12. }
  13. getFrameForSelector (...args: Array<any>) {
  14. return getWebFrame(binding._getFrameForSelector(this.context, ...args))
  15. }
  16. findFrameByName (...args: Array<any>) {
  17. return getWebFrame(binding._findFrameByName(this.context, ...args))
  18. }
  19. get opener () {
  20. return getWebFrame(binding._getOpener(this.context))
  21. }
  22. get parent () {
  23. return getWebFrame(binding._getParent(this.context))
  24. }
  25. get top () {
  26. return getWebFrame(binding._getTop(this.context))
  27. }
  28. get firstChild () {
  29. return getWebFrame(binding._getFirstChild(this.context))
  30. }
  31. get nextSibling () {
  32. return getWebFrame(binding._getNextSibling(this.context))
  33. }
  34. get routingId () {
  35. return binding._getRoutingId(this.context)
  36. }
  37. // Deprecations
  38. // TODO(nitsakh): Remove in 6.0
  39. setIsolatedWorldSecurityOrigin (worldId: number, securityOrigin: string) {
  40. deprecate.warn('webFrame.setIsolatedWorldSecurityOrigin', 'webFrame.setIsolatedWorldInfo')
  41. binding.setIsolatedWorldInfo(this.context, worldId, { securityOrigin })
  42. }
  43. setIsolatedWorldContentSecurityPolicy (worldId: number, csp: string) {
  44. deprecate.warn('webFrame.setIsolatedWorldContentSecurityPolicy', 'webFrame.setIsolatedWorldInfo')
  45. binding.setIsolatedWorldInfo(this.context, worldId, {
  46. securityOrigin: window.location.origin,
  47. csp
  48. })
  49. }
  50. setIsolatedWorldHumanReadableName (worldId: number, name: string) {
  51. deprecate.warn('webFrame.setIsolatedWorldHumanReadableName', 'webFrame.setIsolatedWorldInfo')
  52. binding.setIsolatedWorldInfo(this.context, worldId, { name })
  53. }
  54. }
  55. // Populate the methods.
  56. for (const name in binding) {
  57. if (!name.startsWith('_')) { // some methods are manually populated above
  58. // TODO(felixrieseberg): Once we can type web_frame natives, we could
  59. // use a neat `keyof` here
  60. (WebFrame as any).prototype[name] = function (...args: Array<any>) {
  61. return binding[name](this.context, ...args)
  62. }
  63. }
  64. }
  65. // Helper to return WebFrame or null depending on context.
  66. // TODO(zcbenz): Consider returning same WebFrame for the same frame.
  67. function getWebFrame (context: Window) {
  68. return context ? new WebFrame(context) : null
  69. }
  70. const promisifiedMethods = new Set<string>([
  71. 'executeJavaScript',
  72. 'executeJavaScriptInIsolatedWorld'
  73. ])
  74. for (const method of promisifiedMethods) {
  75. (WebFrame as any).prototype[method] = deprecate.promisify((WebFrame as any).prototype[method])
  76. }
  77. const _webFrame = new WebFrame(window)
  78. export default _webFrame