2019-03-01 08:39:04 +00:00
|
|
|
import { EventEmitter } from 'events'
|
|
|
|
import { deprecate } from 'electron'
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-03-18 19:37:06 +00:00
|
|
|
const binding = process.electronBinding('web_frame')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
class WebFrame extends EventEmitter {
|
2019-03-01 08:39:04 +00:00
|
|
|
constructor (public context: Window) {
|
2019-02-08 21:38:31 +00:00
|
|
|
super()
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-02-08 21:38:31 +00:00
|
|
|
// Lots of webview would subscribe to webFrame's events.
|
|
|
|
this.setMaxListeners(0)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-03-01 08:39:04 +00:00
|
|
|
findFrameByRoutingId (...args: Array<any>) {
|
2019-02-08 21:38:31 +00:00
|
|
|
return getWebFrame(binding._findFrameByRoutingId(this.context, ...args))
|
|
|
|
}
|
|
|
|
|
2019-03-01 08:39:04 +00:00
|
|
|
getFrameForSelector (...args: Array<any>) {
|
2019-02-08 21:38:31 +00:00
|
|
|
return getWebFrame(binding._getFrameForSelector(this.context, ...args))
|
|
|
|
}
|
|
|
|
|
2019-03-01 08:39:04 +00:00
|
|
|
findFrameByName (...args: Array<any>) {
|
2019-02-08 21:38:31 +00:00
|
|
|
return getWebFrame(binding._findFrameByName(this.context, ...args))
|
|
|
|
}
|
|
|
|
|
|
|
|
get opener () {
|
|
|
|
return getWebFrame(binding._getOpener(this.context))
|
|
|
|
}
|
|
|
|
|
|
|
|
get parent () {
|
|
|
|
return getWebFrame(binding._getParent(this.context))
|
|
|
|
}
|
|
|
|
|
|
|
|
get top () {
|
|
|
|
return getWebFrame(binding._getTop(this.context))
|
|
|
|
}
|
|
|
|
|
|
|
|
get firstChild () {
|
|
|
|
return getWebFrame(binding._getFirstChild(this.context))
|
|
|
|
}
|
|
|
|
|
|
|
|
get nextSibling () {
|
|
|
|
return getWebFrame(binding._getNextSibling(this.context))
|
|
|
|
}
|
|
|
|
|
|
|
|
get routingId () {
|
|
|
|
return binding._getRoutingId(this.context)
|
|
|
|
}
|
2019-02-13 18:05:28 +00:00
|
|
|
|
|
|
|
// Deprecations
|
|
|
|
// TODO(nitsakh): Remove in 6.0
|
2019-03-01 08:39:04 +00:00
|
|
|
setIsolatedWorldSecurityOrigin (worldId: number, securityOrigin: string) {
|
2019-02-13 18:05:28 +00:00
|
|
|
deprecate.warn('webFrame.setIsolatedWorldSecurityOrigin', 'webFrame.setIsolatedWorldInfo')
|
2019-01-21 20:45:15 +00:00
|
|
|
binding.setIsolatedWorldInfo(this.context, worldId, { securityOrigin })
|
2019-02-13 18:05:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 08:39:04 +00:00
|
|
|
setIsolatedWorldContentSecurityPolicy (worldId: number, csp: string) {
|
2019-02-13 18:05:28 +00:00
|
|
|
deprecate.warn('webFrame.setIsolatedWorldContentSecurityPolicy', 'webFrame.setIsolatedWorldInfo')
|
2019-01-21 20:45:15 +00:00
|
|
|
binding.setIsolatedWorldInfo(this.context, worldId, {
|
|
|
|
securityOrigin: window.location.origin,
|
|
|
|
csp
|
|
|
|
})
|
2019-02-13 18:05:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 08:39:04 +00:00
|
|
|
setIsolatedWorldHumanReadableName (worldId: number, name: string) {
|
2019-02-13 18:05:28 +00:00
|
|
|
deprecate.warn('webFrame.setIsolatedWorldHumanReadableName', 'webFrame.setIsolatedWorldInfo')
|
2019-01-21 20:45:15 +00:00
|
|
|
binding.setIsolatedWorldInfo(this.context, worldId, { name })
|
2019-02-13 18:05:28 +00:00
|
|
|
}
|
2019-02-08 21:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the methods.
|
|
|
|
for (const name in binding) {
|
2019-03-01 08:39:04 +00:00
|
|
|
if (!name.startsWith('_')) { // some methods are manually populated above
|
|
|
|
// TODO(felixrieseberg): Once we can type web_frame natives, we could
|
|
|
|
// use a neat `keyof` here
|
|
|
|
(WebFrame as any).prototype[name] = function (...args: Array<any>) {
|
2019-02-08 21:38:31 +00:00
|
|
|
return binding[name](this.context, ...args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to return WebFrame or null depending on context.
|
|
|
|
// TODO(zcbenz): Consider returning same WebFrame for the same frame.
|
2019-03-01 08:39:04 +00:00
|
|
|
function getWebFrame (context: Window) {
|
2019-02-08 21:38:31 +00:00
|
|
|
return context ? new WebFrame(context) : null
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:08:54 +00:00
|
|
|
const promisifiedMethods = new Set<string>([
|
|
|
|
'executeJavaScript',
|
|
|
|
'executeJavaScriptInIsolatedWorld'
|
|
|
|
])
|
|
|
|
|
|
|
|
for (const method of promisifiedMethods) {
|
|
|
|
(WebFrame as any).prototype[method] = deprecate.promisify((WebFrame as any).prototype[method])
|
|
|
|
}
|
|
|
|
|
2019-03-01 08:39:04 +00:00
|
|
|
const _webFrame = new WebFrame(window)
|
|
|
|
|
|
|
|
export default _webFrame
|