diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-02-28 01:13:06 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-03-06 18:56:49 +0100 |
commit | 2a5f7e3fecc98debea2b3408662d402a1e1681a0 (patch) | |
tree | 39b363cd8453007a8b6537e01813e0c8e61fe46c /packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts | |
parent | fix(service-preload): Browser view canvas background (diff) | |
download | sophie-2a5f7e3fecc98debea2b3408662d402a1e1681a0.tar.gz sophie-2a5f7e3fecc98debea2b3408662d402a1e1681a0.tar.zst sophie-2a5f7e3fecc98debea2b3408662d402a1e1681a0.zip |
feat: Handle service load failures
Adds a "failed" state for services where the BrowserView and WebContents
should be left around to keep history and allow people to navigate back.
Access to the browser history otherwise doesn't seem possible
(see https://github.com/electron/electron/issues/26727
and https://github.com/electron/electron/issues/7186),
so destroying BrowserView and managing our own history is not possible.
Also keep https://github.com/electron/electron/issues/24113 in mind.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts')
-rw-r--r-- | packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts b/packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts index e5fdf11..d90ff19 100644 --- a/packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts +++ b/packages/main/src/infrastructure/electron/impl/ElectronServiceView.ts | |||
@@ -22,12 +22,15 @@ import type { BrowserViewBounds } from '@sophie/shared'; | |||
22 | import { BrowserView } from 'electron'; | 22 | import { BrowserView } from 'electron'; |
23 | 23 | ||
24 | import type Service from '../../../stores/Service'; | 24 | import type Service from '../../../stores/Service'; |
25 | import { getLogger } from '../../../utils/log'; | ||
25 | import type Resources from '../../resources/Resources'; | 26 | import type Resources from '../../resources/Resources'; |
26 | import type { ServiceView } from '../types'; | 27 | import type { ServiceView } from '../types'; |
27 | 28 | ||
28 | import ElectronPartition from './ElectronPartition'; | 29 | import ElectronPartition from './ElectronPartition'; |
29 | import type ElectronViewFactory from './ElectronViewFactory'; | 30 | import type ElectronViewFactory from './ElectronViewFactory'; |
30 | 31 | ||
32 | const log = getLogger('ElectronServiceView'); | ||
33 | |||
31 | export default class ElectronServiceView implements ServiceView { | 34 | export default class ElectronServiceView implements ServiceView { |
32 | readonly id: string; | 35 | readonly id: string; |
33 | 36 | ||
@@ -72,20 +75,39 @@ export default class ElectronServiceView implements ServiceView { | |||
72 | } | 75 | } |
73 | }); | 76 | }); |
74 | 77 | ||
78 | webContents.on( | ||
79 | 'did-fail-load', | ||
80 | (_event, errorCode, errorDesc, url, isMainFrame) => { | ||
81 | if (isMainFrame) { | ||
82 | setLocation(url); | ||
83 | service.setFailed(errorCode, errorDesc); | ||
84 | log.warn( | ||
85 | 'Failed to load', | ||
86 | url, | ||
87 | 'in service', | ||
88 | this.id, | ||
89 | errorCode, | ||
90 | errorDesc, | ||
91 | ); | ||
92 | } | ||
93 | }, | ||
94 | ); | ||
95 | |||
75 | webContents.on('page-title-updated', (_event, title) => { | 96 | webContents.on('page-title-updated', (_event, title) => { |
76 | service.setTitle(title); | 97 | service.setTitle(title); |
77 | }); | 98 | }); |
78 | 99 | ||
79 | webContents.on('did-start-loading', () => { | 100 | webContents.on('did-start-loading', () => { |
80 | service.startedLoading(); | 101 | service.startLoading(); |
81 | }); | 102 | }); |
82 | 103 | ||
83 | webContents.on('did-stop-loading', () => { | 104 | webContents.on('did-stop-loading', () => { |
84 | service.finishedLoading(); | 105 | service.finishLoading(); |
85 | }); | 106 | }); |
86 | 107 | ||
87 | webContents.on('render-process-gone', () => { | 108 | webContents.on('render-process-gone', (_event, details) => { |
88 | service.crashed(); | 109 | const { reason, exitCode } = details; |
110 | service.setCrashed(reason, exitCode); | ||
89 | }); | 111 | }); |
90 | } | 112 | } |
91 | 113 | ||
@@ -93,8 +115,10 @@ export default class ElectronServiceView implements ServiceView { | |||
93 | return this.browserView.webContents.id; | 115 | return this.browserView.webContents.id; |
94 | } | 116 | } |
95 | 117 | ||
96 | loadURL(url: string): Promise<void> { | 118 | loadURL(url: string): void { |
97 | return this.browserView.webContents.loadURL(url); | 119 | this.browserView.webContents.loadURL(url).catch((error) => { |
120 | log.warn('Error while loading', url, 'in service', this.id, error); | ||
121 | }); | ||
98 | } | 122 | } |
99 | 123 | ||
100 | goBack(): void { | 124 | goBack(): void { |