Skip to content

Commit afa7c77

Browse files
committed
fix(blocking): Url blocking canGoBack check added.
Ensures that error link is the same as current link before firing tabBackwards. Fixes #689
1 parent fc6f90c commit afa7c77

File tree

2 files changed

+57
-51
lines changed

2 files changed

+57
-51
lines changed

__tests__/components/Tab.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe( 'Tab', () => {
9393
browserState: { canGoBack: true }
9494
};
9595

96-
instance.didFailLoad( { errorDescription: 'ERR_BLOCKED_BY_CLIENT' } );
96+
instance.didFailLoad( { errorDescription: 'ERR_BLOCKED_BY_CLIENT', validatedURL : '' } );
9797
expect( props.addNotification ).toHaveBeenCalled();
9898
expect( props.tabBackwards ).toHaveBeenCalled();
9999
expect( props.closeTab ).not.toHaveBeenCalled();

app/components/Tab/Tab.tsx

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export default class Tab extends Component<TabProps, TabState> {
5353
return { hasError: true, theError: error };
5454
}
5555

56-
constructor( props ) {
57-
super( props );
56+
constructor( properties ) {
57+
super( properties );
5858
this.state = {
5959
browserState: {
6060
canGoBack: false,
@@ -82,14 +82,14 @@ export default class Tab extends Component<TabProps, TabState> {
8282
}
8383
};
8484

85-
buildMenu = webview => {
85+
buildMenu = ( webview ) => {
8686
if ( !webview.getWebContents ) return; // 'not now, as you're running jest;
8787
const { addTab, windowId } = this.props;
8888
// require here to avoid jest/electron remote issues
8989
const contextMenu = require( 'electron-context-menu' );
9090
contextMenu( {
9191
window: webview,
92-
append: params => [
92+
append: ( params ) => [
9393
{
9494
label: 'Open Link in New Tab.',
9595
visible: params.linkURL.length > 0,
@@ -176,21 +176,21 @@ export default class Tab extends Component<TabProps, TabState> {
176176
} );
177177
}
178178

179-
componentWillReceiveProps( nextProps ) {
180-
if ( JSON.stringify( nextProps ) === JSON.stringify( this.props ) ) return;
179+
componentWillReceiveProps( nextProperties ) {
180+
if ( JSON.stringify( nextProperties ) === JSON.stringify( this.props ) ) return;
181181
if ( !this.state.browserState.mountedAndReady ) return;
182182
const {
183183
focusWebview,
184184
isActiveTab,
185185
url,
186186
updateTab,
187187
index,
188-
shouldToggleDevTools,
188+
shouldToggleDevelopmentTools,
189189
shouldReload
190190
} = this.props;
191191
const { webview } = this;
192192
logger.info( 'Tab: did receive updated props' );
193-
if ( nextProps.shouldFocusWebview && isActiveTab ) {
193+
if ( nextProperties.shouldFocusWebview && isActiveTab ) {
194194
this.with( ( webview, webContents ) => {
195195
webview.focus();
196196
webContents.focus();
@@ -199,39 +199,39 @@ export default class Tab extends Component<TabProps, TabState> {
199199
}
200200
if (
201201
!this.props.shouldFocusWebview &&
202-
!nextProps.shouldFocusWebview &&
203-
nextProps.isActiveTab
202+
!nextProperties.shouldFocusWebview &&
203+
nextProperties.isActiveTab
204204
) {
205205
focusWebview( true );
206206
}
207-
const nextId = nextProps.webId || {};
207+
const nextId = nextProperties.webId || {};
208208
const currentId = this.props.webId || {};
209209
if ( nextId['@id'] !== currentId['@id'] ) {
210210
if ( !webview ) return;
211-
logger.info( 'New WebID set for ', nextProps.url );
212-
this.setCurrentWebId( nextProps.webId );
211+
logger.info( 'New WebID set for ', nextProperties.url );
212+
this.setCurrentWebId( nextProperties.webId );
213213
}
214-
if ( nextProps.url && nextProps.url !== url ) {
214+
if ( nextProperties.url && nextProperties.url !== url ) {
215215
if ( !webview ) return;
216-
const webviewSrc = parseURL( webview.src );
216+
const webviewSource = parseURL( webview.src );
217217
if (
218-
webviewSrc.href === '' ||
219-
`${webviewSrc.protocol}${webviewSrc.hostname}` === 'about:blank' ||
220-
urlHasChanged( webview.src, nextProps.url )
218+
webviewSource.href === '' ||
219+
`${webviewSource.protocol}${webviewSource.hostname}` === 'about:blank' ||
220+
urlHasChanged( webview.src, nextProperties.url )
221221
) {
222-
this.loadURL( nextProps.url );
222+
this.loadURL( nextProperties.url );
223223
}
224224
}
225-
if ( !shouldReload && nextProps.shouldReload ) {
226-
logger.verbose( 'Should reload URL: ', nextProps.url );
225+
if ( !shouldReload && nextProperties.shouldReload ) {
226+
logger.verbose( 'Should reload URL: ', nextProperties.url );
227227
this.reload();
228228
const tabUpdate = {
229229
index,
230230
shouldReload: false
231231
};
232232
updateTab( tabUpdate );
233233
}
234-
if ( !shouldToggleDevTools && nextProps.shouldToggleDevTools ) {
234+
if ( !shouldToggleDevelopmentTools && nextProperties.shouldToggleDevTools ) {
235235
this.isDevToolsOpened() ? this.closeDevTools() : this.openDevTools();
236236
const tabUpdate = {
237237
index,
@@ -241,7 +241,7 @@ export default class Tab extends Component<TabProps, TabState> {
241241
}
242242
}
243243

244-
updateBrowserState( props = {} ) {
244+
updateBrowserState( properties = {} ) {
245245
const { webview } = this;
246246
if ( !webview ) {
247247
return;
@@ -253,7 +253,7 @@ export default class Tab extends Component<TabProps, TabState> {
253253
...this.state.browserState,
254254
canGoBack: webview.canGoBack(),
255255
canGoForward: webview.canGoForward(),
256-
...props
256+
...properties
257257
};
258258
this.setState( { browserState } );
259259
}
@@ -268,17 +268,17 @@ export default class Tab extends Component<TabProps, TabState> {
268268
}
269269
this.updateBrowserState( { loading: false, mountedAndReady: true } );
270270
if ( url && url !== 'about:blank' ) {
271-
this.loadURL( url ).catch( err => console.info( 'err in loadurl', err ) );
271+
this.loadURL( url ).catch( ( error ) => console.info( 'err in loadurl', error ) );
272272
this.setCurrentWebId( null );
273273
}
274274
}
275275

276-
onCrash = e => {
276+
onCrash = ( e ) => {
277277
console.error( e );
278278
logger.error( 'The webview crashed', e );
279279
};
280280

281-
onGpuCrash = e => {
281+
onGpuCrash = ( e ) => {
282282
console.error( e );
283283
logger.error( 'The webview GPU crashed', e );
284284
};
@@ -300,7 +300,7 @@ export default class Tab extends Component<TabProps, TabState> {
300300
} );
301301
}
302302

303-
didFailLoad( err ) {
303+
didFailLoad( error ) {
304304
const {
305305
url,
306306
index,
@@ -311,7 +311,10 @@ export default class Tab extends Component<TabProps, TabState> {
311311
windowId
312312
} = this.props;
313313
const { webview } = this;
314-
const urlObj = stdUrl.parse( url );
314+
const urlObject = stdUrl.parse( url );
315+
const errorUrl = error.validatedURL;
316+
317+
logger.info( 'didfail load', error );
315318
const renderError = ( header, subHeader ) => {
316319
const errorAsHtml = ReactDOMServer.renderToStaticMarkup(
317320
<Error error={{ header, subHeader }} />
@@ -328,31 +331,34 @@ export default class Tab extends Component<TabProps, TabState> {
328331
}
329332
` );
330333
};
331-
if ( urlObj.hostname === '127.0.0.1' || urlObj.hostname === 'localhost' ) {
334+
if ( urlObject.hostname === '127.0.0.1' || urlObject.hostname === 'localhost' ) {
332335
try {
333336
renderError( 'Page Load Failed' );
334337
} catch ( scriptError ) {
335338
logger.error( scriptError );
336339
}
337340
return;
338341
}
339-
if ( err && err.errorDescription === 'ERR_INVALID_URL' ) {
342+
if ( error && error.errorDescription === 'ERR_INVALID_URL' ) {
340343
try {
341344
renderError( `Invalid URL: ${url}` );
342345
} catch ( scriptError ) {
343346
logger.error( scriptError );
344347
}
345348
return;
346349
}
347-
if ( err && err.errorDescription === 'ERR_BLOCKED_BY_CLIENT' ) {
350+
if ( error && error.errorDescription === 'ERR_BLOCKED_BY_CLIENT' ) {
348351
const notification = {
349352
title: 'Blocked URL',
350-
body: url
353+
body: errorUrl
351354
};
355+
352356
addNotification( notification );
353-
if ( this.state.browserState.canGoBack ) {
357+
358+
// check its the same link incase of double click
359+
if ( this.state.browserState.canGoBack && !urlHasChanged( errorUrl, url ) ) {
354360
tabBackwards( { index, windowId } );
355-
} else {
361+
} else if ( !this.state.browserState.canGoBack ) {
356362
closeTab( { index, windowId } );
357363
// add a fresh tab (should be only if no more tabs present)
358364
addTab( { url: 'about:blank', windowId, isActiveTab: true } );
@@ -453,10 +459,10 @@ export default class Tab extends Component<TabProps, TabState> {
453459

454460
didGetRedirectRequest( e ) {
455461
const { oldURL, newURL } = e;
456-
const prev = oldURL;
462+
const previous = oldURL;
457463
const next = newURL;
458464
logger.info( 'Webview: did get redirect request' );
459-
if ( prev === this.state.browserState.url ) {
465+
if ( previous === this.state.browserState.url ) {
460466
this.updateBrowserState( { redirects: [next] } );
461467
}
462468
}
@@ -490,7 +496,7 @@ export default class Tab extends Component<TabProps, TabState> {
490496
}
491497

492498
// TODO Move this functinoality to extensions
493-
updateTheIdInWebview = newWebId => {
499+
updateTheIdInWebview = ( newWebId ) => {
494500
const { updateTab, index, webId } = this.props;
495501
const { webview } = this;
496502
const theWebId = newWebId || webId;
@@ -560,15 +566,15 @@ For updates or to submit ideas and suggestions, visit https://github.com/maidsaf
560566
return frozen;
561567
}
562568

563-
with( cb, opts = { insist: false } ) {
569+
with( callback, options = { insist: false } ) {
564570
const { webview } = this;
565571
if ( !webview ) return;
566572
const webContents = webview.getWebContents();
567573
if ( !webContents ) {
568574
return;
569575
}
570576
if ( webContents.isDestroyed() ) return;
571-
cb( webview, webContents );
577+
callback( webview, webContents );
572578
}
573579

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

582588
stop() {
583-
this.with( wv => wv.stop() );
589+
this.with( ( wv ) => wv.stop() );
584590
}
585591

586592
reload() {
587593
logger.info( 'webview reloading' );
588-
this.with( wv => {
594+
this.with( ( wv ) => {
589595
wv.reload();
590596
} );
591597
}
592598

593599
goBack( e ) {
594-
this.with( wv => wv.goBack() );
600+
this.with( ( wv ) => wv.goBack() );
595601
}
596602

597603
goForward() {
598604
console.warn(
599605
'Electron bug preventing goForward: https://github.com/electron/electron/issues/9999'
600606
);
601-
this.with( wv => wv.goForward() );
607+
this.with( ( wv ) => wv.goForward() );
602608
}
603609

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

630636
if ( this.state && this.state.hasError ) {
631-
const err = this.state.theError;
632-
const stringError = JSON.stringify( err, [
637+
const error = this.state.theError;
638+
const stringError = JSON.stringify( error, [
633639
'message',
634640
'arguments',
635641
'type',
636642
'name'
637643
] );
638-
logger.error( 'Error from Tab.jsx', err );
644+
logger.error( 'Error from Tab.jsx', error );
639645
logger.error( stringError );
640646
// You can render any custom fallback UI
641647
return (
642648
<div className={moddedClass}>
643649
<h4>Something went wrong with this tab.</h4>
644650
<span>
645-
{JSON.stringify( err, ['message', 'arguments', 'type', 'name'] )}
651+
{JSON.stringify( error, ['message', 'arguments', 'type', 'name'] )}
646652
</span>
647653
</div>
648654
);
@@ -655,7 +661,7 @@ For updates or to submit ideas and suggestions, visit https://github.com/maidsaf
655661
tabIndex="0"
656662
preload={injectPath}
657663
partition="persist:safe-tab"
658-
ref={c => {
664+
ref={( c ) => {
659665
this.webview = c;
660666
}}
661667
/>

0 commit comments

Comments
 (0)