2019-02-13 04:10:53 +00:00
const { helper } = require ( './helper' ) ;
const { Events } = require ( './Events' ) ;
/ * *
* @ internal
* /
class NextNavigationWatchdog {
constructor ( session , navigatedFrame ) {
this . _navigatedFrame = navigatedFrame ;
this . _promise = new Promise ( x => this . _resolveCallback = x ) ;
this . _navigation = null ;
this . _eventListeners = [
helper . addEventListener ( session , 'Page.navigationStarted' , this . _onNavigationStarted . bind ( this ) ) ,
helper . addEventListener ( session , 'Page.sameDocumentNavigation' , this . _onSameDocumentNavigation . bind ( this ) ) ,
] ;
}
promise ( ) {
return this . _promise ;
}
navigation ( ) {
return this . _navigation ;
}
_onNavigationStarted ( params ) {
if ( params . frameId === this . _navigatedFrame . _frameId ) {
this . _navigation = {
navigationId : params . navigationId ,
url : params . url ,
} ;
this . _resolveCallback ( ) ;
}
}
_onSameDocumentNavigation ( params ) {
if ( params . frameId === this . _navigatedFrame . _frameId ) {
this . _navigation = {
navigationId : null ,
} ;
this . _resolveCallback ( ) ;
}
}
dispose ( ) {
helper . removeEventListeners ( this . _eventListeners ) ;
}
}
/ * *
* @ internal
* /
class NavigationWatchdog {
constructor ( session , navigatedFrame , networkManager , targetNavigationId , targetURL , firedEvents ) {
this . _navigatedFrame = navigatedFrame ;
this . _targetNavigationId = targetNavigationId ;
this . _firedEvents = firedEvents ;
this . _targetURL = targetURL ;
this . _promise = new Promise ( x => this . _resolveCallback = x ) ;
this . _navigationRequest = null ;
const check = this . _checkNavigationComplete . bind ( this ) ;
this . _eventListeners = [
2019-02-20 06:08:09 +00:00
helper . addEventListener ( session , Events . JugglerSession . Disconnected , ( ) => this . _resolveCallback ( new Error ( 'Navigation failed because browser has disconnected!' ) ) ) ,
2019-02-13 04:10:53 +00:00
helper . addEventListener ( session , 'Page.eventFired' , check ) ,
helper . addEventListener ( session , 'Page.frameAttached' , check ) ,
helper . addEventListener ( session , 'Page.frameDetached' , check ) ,
helper . addEventListener ( session , 'Page.navigationStarted' , check ) ,
helper . addEventListener ( session , 'Page.navigationCommitted' , check ) ,
helper . addEventListener ( session , 'Page.navigationAborted' , this . _onNavigationAborted . bind ( this ) ) ,
helper . addEventListener ( networkManager , Events . NetworkManager . Request , this . _onRequest . bind ( this ) ) ,
2019-02-14 08:06:30 +00:00
helper . addEventListener ( navigatedFrame . _frameManager , Events . FrameManager . FrameDetached , check ) ,
2019-02-13 04:10:53 +00:00
] ;
check ( ) ;
}
_onRequest ( request ) {
if ( request . frame ( ) !== this . _navigatedFrame || ! request . isNavigationRequest ( ) )
return ;
this . _navigationRequest = request ;
}
navigationResponse ( ) {
return this . _navigationRequest ? this . _navigationRequest . response ( ) : null ;
}
_checkNavigationComplete ( ) {
2019-02-14 08:06:30 +00:00
if ( this . _navigatedFrame . isDetached ( ) ) {
this . _resolveCallback ( new Error ( 'Navigating frame was detached' ) ) ;
} else if ( this . _navigatedFrame . _lastCommittedNavigationId === this . _targetNavigationId
2019-02-13 04:10:53 +00:00
&& checkFiredEvents ( this . _navigatedFrame , this . _firedEvents ) ) {
this . _resolveCallback ( null ) ;
}
function checkFiredEvents ( frame , firedEvents ) {
for ( const subframe of frame . _children ) {
if ( ! checkFiredEvents ( subframe , firedEvents ) )
return false ;
}
return firedEvents . every ( event => frame . _firedEvents . has ( event ) ) ;
}
}
_onNavigationAborted ( params ) {
if ( params . frameId === this . _navigatedFrame . _frameId && params . navigationId === this . _targetNavigationId )
this . _resolveCallback ( new Error ( 'Navigation to ' + this . _targetURL + ' failed: ' + params . errorText ) ) ;
}
promise ( ) {
return this . _promise ;
}
dispose ( ) {
helper . removeEventListeners ( this . _eventListeners ) ;
}
}
module . exports = { NavigationWatchdog , NextNavigationWatchdog } ;