2020-03-20 20:28:31 +00:00
import { EventEmitter } from 'events' ;
2020-07-23 21:32:20 +00:00
import deprecate from '@electron/internal/common/api/deprecate' ;
2016-01-12 02:40:23 +00:00
2020-06-23 03:32:45 +00:00
const binding = process . _linkedBinding ( 'electron_renderer_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 ) {
2020-03-20 20:28: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.
2020-03-20 20:28:31 +00:00
this . setMaxListeners ( 0 ) ;
2019-02-08 21:38:31 +00:00
}
2016-01-12 02:40:23 +00:00
2019-03-01 08:39:04 +00:00
findFrameByRoutingId ( . . . args : Array < any > ) {
2020-03-20 20:28:31 +00:00
return getWebFrame ( binding . _findFrameByRoutingId ( this . context , . . . args ) ) ;
2019-02-08 21:38:31 +00:00
}
2019-03-01 08:39:04 +00:00
getFrameForSelector ( . . . args : Array < any > ) {
2020-03-20 20:28:31 +00:00
return getWebFrame ( binding . _getFrameForSelector ( this . context , . . . args ) ) ;
2019-02-08 21:38:31 +00:00
}
2019-03-01 08:39:04 +00:00
findFrameByName ( . . . args : Array < any > ) {
2020-03-20 20:28:31 +00:00
return getWebFrame ( binding . _findFrameByName ( this . context , . . . args ) ) ;
2019-02-08 21:38:31 +00:00
}
get opener ( ) {
2020-03-20 20:28:31 +00:00
return getWebFrame ( binding . _getOpener ( this . context ) ) ;
2019-02-08 21:38:31 +00:00
}
get parent ( ) {
2020-03-20 20:28:31 +00:00
return getWebFrame ( binding . _getParent ( this . context ) ) ;
2019-02-08 21:38:31 +00:00
}
get top ( ) {
2020-03-20 20:28:31 +00:00
return getWebFrame ( binding . _getTop ( this . context ) ) ;
2019-02-08 21:38:31 +00:00
}
get firstChild ( ) {
2020-03-20 20:28:31 +00:00
return getWebFrame ( binding . _getFirstChild ( this . context ) ) ;
2019-02-08 21:38:31 +00:00
}
get nextSibling ( ) {
2020-03-20 20:28:31 +00:00
return getWebFrame ( binding . _getNextSibling ( this . context ) ) ;
2019-02-08 21:38:31 +00:00
}
get routingId ( ) {
2020-03-20 20:28:31 +00:00
return binding . _getRoutingId ( this . context ) ;
2019-02-08 21:38:31 +00:00
}
}
2020-07-23 21:32:20 +00:00
const { hasSwitch } = process . _linkedBinding ( 'electron_common_command_line' ) ;
const worldSafeJS = hasSwitch ( 'world-safe-execute-javascript' ) && hasSwitch ( 'context-isolation' ) ;
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 > ) {
2020-07-23 21:32:20 +00:00
if ( ! worldSafeJS && name . startsWith ( 'executeJavaScript' ) ) {
deprecate . log ( ` Security Warning: webFrame. ${ name } was called without worldSafeExecuteJavaScript enabled. This is considered unsafe. worldSafeExecuteJavaScript will be enabled by default in Electron 12. ` ) ;
}
2020-03-20 20:28:31 +00:00
return binding [ name ] ( this . context , . . . args ) ;
} ;
2020-07-23 21:32:20 +00:00
// TODO(MarshallOfSound): Remove once the above deprecation is removed
if ( name . startsWith ( 'executeJavaScript' ) ) {
( WebFrame as any ) . prototype [ ` _ ${ name } ` ] = function ( . . . args : Array < any > ) {
return binding [ name ] ( this . context , . . . args ) ;
} ;
}
2019-02-08 21:38:31 +00:00
}
}
// 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 ) {
2020-03-20 20:28:31 +00:00
return context ? new WebFrame ( context ) : null ;
2019-02-08 21:38:31 +00:00
}
2020-03-20 20:28:31 +00:00
const _webFrame = new WebFrame ( window ) ;
2019-03-01 08:39:04 +00:00
2020-03-20 20:28:31 +00:00
export default _webFrame ;