Skip to content

Commit

Permalink
fix(blocking): Url blocking canGoBack check added.
Browse files Browse the repository at this point in the history
Ensures that error link is the same as current link before firing tabBackwards.

Fixes maidsafe#689
  • Loading branch information
joshuef committed Apr 23, 2019
1 parent fc6f90c commit afa7c77
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 51 deletions.
2 changes: 1 addition & 1 deletion __tests__/components/Tab.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe( 'Tab', () => {
browserState: { canGoBack: true }
};

instance.didFailLoad( { errorDescription: 'ERR_BLOCKED_BY_CLIENT' } );
instance.didFailLoad( { errorDescription: 'ERR_BLOCKED_BY_CLIENT', validatedURL : '' } );
expect( props.addNotification ).toHaveBeenCalled();
expect( props.tabBackwards ).toHaveBeenCalled();
expect( props.closeTab ).not.toHaveBeenCalled();
Expand Down
106 changes: 56 additions & 50 deletions app/components/Tab/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export default class Tab extends Component<TabProps, TabState> {
return { hasError: true, theError: error };
}

constructor( props ) {
super( props );
constructor( properties ) {
super( properties );
this.state = {
browserState: {
canGoBack: false,
Expand Down Expand Up @@ -82,14 +82,14 @@ export default class Tab extends Component<TabProps, TabState> {
}
};

buildMenu = webview => {
buildMenu = ( webview ) => {
if ( !webview.getWebContents ) return; // 'not now, as you're running jest;
const { addTab, windowId } = this.props;
// require here to avoid jest/electron remote issues
const contextMenu = require( 'electron-context-menu' );
contextMenu( {
window: webview,
append: params => [
append: ( params ) => [
{
label: 'Open Link in New Tab.',
visible: params.linkURL.length > 0,
Expand Down Expand Up @@ -176,21 +176,21 @@ export default class Tab extends Component<TabProps, TabState> {
} );
}

componentWillReceiveProps( nextProps ) {
if ( JSON.stringify( nextProps ) === JSON.stringify( this.props ) ) return;
componentWillReceiveProps( nextProperties ) {
if ( JSON.stringify( nextProperties ) === JSON.stringify( this.props ) ) return;
if ( !this.state.browserState.mountedAndReady ) return;
const {
focusWebview,
isActiveTab,
url,
updateTab,
index,
shouldToggleDevTools,
shouldToggleDevelopmentTools,
shouldReload
} = this.props;
const { webview } = this;
logger.info( 'Tab: did receive updated props' );
if ( nextProps.shouldFocusWebview && isActiveTab ) {
if ( nextProperties.shouldFocusWebview && isActiveTab ) {
this.with( ( webview, webContents ) => {
webview.focus();
webContents.focus();
Expand All @@ -199,39 +199,39 @@ export default class Tab extends Component<TabProps, TabState> {
}
if (
!this.props.shouldFocusWebview &&
!nextProps.shouldFocusWebview &&
nextProps.isActiveTab
!nextProperties.shouldFocusWebview &&
nextProperties.isActiveTab
) {
focusWebview( true );
}
const nextId = nextProps.webId || {};
const nextId = nextProperties.webId || {};
const currentId = this.props.webId || {};
if ( nextId['@id'] !== currentId['@id'] ) {
if ( !webview ) return;
logger.info( 'New WebID set for ', nextProps.url );
this.setCurrentWebId( nextProps.webId );
logger.info( 'New WebID set for ', nextProperties.url );
this.setCurrentWebId( nextProperties.webId );
}
if ( nextProps.url && nextProps.url !== url ) {
if ( nextProperties.url && nextProperties.url !== url ) {
if ( !webview ) return;
const webviewSrc = parseURL( webview.src );
const webviewSource = parseURL( webview.src );
if (
webviewSrc.href === '' ||
`${webviewSrc.protocol}${webviewSrc.hostname}` === 'about:blank' ||
urlHasChanged( webview.src, nextProps.url )
webviewSource.href === '' ||
`${webviewSource.protocol}${webviewSource.hostname}` === 'about:blank' ||
urlHasChanged( webview.src, nextProperties.url )
) {
this.loadURL( nextProps.url );
this.loadURL( nextProperties.url );
}
}
if ( !shouldReload && nextProps.shouldReload ) {
logger.verbose( 'Should reload URL: ', nextProps.url );
if ( !shouldReload && nextProperties.shouldReload ) {
logger.verbose( 'Should reload URL: ', nextProperties.url );
this.reload();
const tabUpdate = {
index,
shouldReload: false
};
updateTab( tabUpdate );
}
if ( !shouldToggleDevTools && nextProps.shouldToggleDevTools ) {
if ( !shouldToggleDevelopmentTools && nextProperties.shouldToggleDevTools ) {
this.isDevToolsOpened() ? this.closeDevTools() : this.openDevTools();
const tabUpdate = {
index,
Expand All @@ -241,7 +241,7 @@ export default class Tab extends Component<TabProps, TabState> {
}
}

updateBrowserState( props = {} ) {
updateBrowserState( properties = {} ) {
const { webview } = this;
if ( !webview ) {
return;
Expand All @@ -253,7 +253,7 @@ export default class Tab extends Component<TabProps, TabState> {
...this.state.browserState,
canGoBack: webview.canGoBack(),
canGoForward: webview.canGoForward(),
...props
...properties
};
this.setState( { browserState } );
}
Expand All @@ -268,17 +268,17 @@ export default class Tab extends Component<TabProps, TabState> {
}
this.updateBrowserState( { loading: false, mountedAndReady: true } );
if ( url && url !== 'about:blank' ) {
this.loadURL( url ).catch( err => console.info( 'err in loadurl', err ) );
this.loadURL( url ).catch( ( error ) => console.info( 'err in loadurl', error ) );
this.setCurrentWebId( null );
}
}

onCrash = e => {
onCrash = ( e ) => {
console.error( e );
logger.error( 'The webview crashed', e );
};

onGpuCrash = e => {
onGpuCrash = ( e ) => {
console.error( e );
logger.error( 'The webview GPU crashed', e );
};
Expand All @@ -300,7 +300,7 @@ export default class Tab extends Component<TabProps, TabState> {
} );
}

didFailLoad( err ) {
didFailLoad( error ) {
const {
url,
index,
Expand All @@ -311,7 +311,10 @@ export default class Tab extends Component<TabProps, TabState> {
windowId
} = this.props;
const { webview } = this;
const urlObj = stdUrl.parse( url );
const urlObject = stdUrl.parse( url );
const errorUrl = error.validatedURL;

logger.info( 'didfail load', error );
const renderError = ( header, subHeader ) => {
const errorAsHtml = ReactDOMServer.renderToStaticMarkup(
<Error error={{ header, subHeader }} />
Expand All @@ -328,31 +331,34 @@ export default class Tab extends Component<TabProps, TabState> {
}
` );
};
if ( urlObj.hostname === '127.0.0.1' || urlObj.hostname === 'localhost' ) {
if ( urlObject.hostname === '127.0.0.1' || urlObject.hostname === 'localhost' ) {
try {
renderError( 'Page Load Failed' );
} catch ( scriptError ) {
logger.error( scriptError );
}
return;
}
if ( err && err.errorDescription === 'ERR_INVALID_URL' ) {
if ( error && error.errorDescription === 'ERR_INVALID_URL' ) {
try {
renderError( `Invalid URL: ${url}` );
} catch ( scriptError ) {
logger.error( scriptError );
}
return;
}
if ( err && err.errorDescription === 'ERR_BLOCKED_BY_CLIENT' ) {
if ( error && error.errorDescription === 'ERR_BLOCKED_BY_CLIENT' ) {
const notification = {
title: 'Blocked URL',
body: url
body: errorUrl
};

addNotification( notification );
if ( this.state.browserState.canGoBack ) {

// check its the same link incase of double click
if ( this.state.browserState.canGoBack && !urlHasChanged( errorUrl, url ) ) {
tabBackwards( { index, windowId } );
} else {
} else if ( !this.state.browserState.canGoBack ) {
closeTab( { index, windowId } );
// add a fresh tab (should be only if no more tabs present)
addTab( { url: 'about:blank', windowId, isActiveTab: true } );
Expand Down Expand Up @@ -453,10 +459,10 @@ export default class Tab extends Component<TabProps, TabState> {

didGetRedirectRequest( e ) {
const { oldURL, newURL } = e;
const prev = oldURL;
const previous = oldURL;
const next = newURL;
logger.info( 'Webview: did get redirect request' );
if ( prev === this.state.browserState.url ) {
if ( previous === this.state.browserState.url ) {
this.updateBrowserState( { redirects: [next] } );
}
}
Expand Down Expand Up @@ -490,7 +496,7 @@ export default class Tab extends Component<TabProps, TabState> {
}

// TODO Move this functinoality to extensions
updateTheIdInWebview = newWebId => {
updateTheIdInWebview = ( newWebId ) => {
const { updateTab, index, webId } = this.props;
const { webview } = this;
const theWebId = newWebId || webId;
Expand Down Expand Up @@ -560,15 +566,15 @@ For updates or to submit ideas and suggestions, visit https://github.com/maidsaf
return frozen;
}

with( cb, opts = { insist: false } ) {
with( callback, options = { insist: false } ) {
const { webview } = this;
if ( !webview ) return;
const webContents = webview.getWebContents();
if ( !webContents ) {
return;
}
if ( webContents.isDestroyed() ) return;
cb( webview, webContents );
callback( webview, webContents );
}

openDevTools() {
Expand All @@ -580,28 +586,28 @@ For updates or to submit ideas and suggestions, visit https://github.com/maidsaf
}

stop() {
this.with( wv => wv.stop() );
this.with( ( wv ) => wv.stop() );
}

reload() {
logger.info( 'webview reloading' );
this.with( wv => {
this.with( ( wv ) => {
wv.reload();
} );
}

goBack( e ) {
this.with( wv => wv.goBack() );
this.with( ( wv ) => wv.goBack() );
}

goForward() {
console.warn(
'Electron bug preventing goForward: https://github.com/electron/electron/issues/9999'
);
this.with( wv => wv.goForward() );
this.with( ( wv ) => wv.goForward() );
}

loadURL = async input => {
loadURL = async ( input ) => {
const { webview } = this;
const url = addTrailingSlashIfNeeded( input );
logger.info( 'Webview: loading url:', url );
Expand All @@ -628,21 +634,21 @@ For updates or to submit ideas and suggestions, visit https://github.com/maidsaf
}

if ( this.state && this.state.hasError ) {
const err = this.state.theError;
const stringError = JSON.stringify( err, [
const error = this.state.theError;
const stringError = JSON.stringify( error, [
'message',
'arguments',
'type',
'name'
] );
logger.error( 'Error from Tab.jsx', err );
logger.error( 'Error from Tab.jsx', error );
logger.error( stringError );
// You can render any custom fallback UI
return (
<div className={moddedClass}>
<h4>Something went wrong with this tab.</h4>
<span>
{JSON.stringify( err, ['message', 'arguments', 'type', 'name'] )}
{JSON.stringify( error, ['message', 'arguments', 'type', 'name'] )}
</span>
</div>
);
Expand All @@ -655,7 +661,7 @@ For updates or to submit ideas and suggestions, visit https://github.com/maidsaf
tabIndex="0"
preload={injectPath}
partition="persist:safe-tab"
ref={c => {
ref={( c ) => {
this.webview = c;
}}
/>
Expand Down

0 comments on commit afa7c77

Please sign in to comment.