Skip to content

Commit c1125f3

Browse files
author
Akos Kitta
committed
Fixed FS path vs encoded URL comparision when handling stop request.
Ref: eclipse-theia/theia#11226 Signed-off-by: Akos Kitta <[email protected]>
1 parent 7abfa53 commit c1125f3

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

arduino-ide-extension/src/electron-main/arduino-electron-main-module.ts

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
IDEUpdaterPath,
2020
} from '../common/protocol/ide-updater';
2121
import { IDEUpdaterImpl } from './ide-updater/ide-updater-impl';
22+
import { TheiaElectronWindow } from './theia/theia-electron-window';
23+
import { TheiaElectronWindow as DefaultTheiaElectronWindow } from '@theia/core/lib/electron-main/theia-electron-window';
2224

2325
export default new ContainerModule((bind, unbind, isBound, rebind) => {
2426
bind(ElectronMainApplication).toSelf().inSingletonScope();
@@ -56,4 +58,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
5658
)
5759
)
5860
.inSingletonScope();
61+
62+
bind(TheiaElectronWindow).toSelf();
63+
rebind(DefaultTheiaElectronWindow).toService(TheiaElectronWindow);
5964
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { injectable } from '@theia/core/shared/inversify';
2+
import { isWindows } from '@theia/core/lib/common/os';
3+
import { StopReason } from '@theia/core/lib/electron-common/messaging/electron-messages';
4+
import { TheiaElectronWindow as DefaultTheiaElectronWindow } from '@theia/core/lib/electron-main/theia-electron-window';
5+
import { FileUri } from '@theia/core/lib/node';
6+
7+
@injectable()
8+
export class TheiaElectronWindow extends DefaultTheiaElectronWindow {
9+
protected async handleStopRequest(
10+
onSafeCallback: () => unknown,
11+
reason: StopReason
12+
): Promise<boolean> {
13+
// Only confirm close to windows that have loaded our front end.
14+
let currentUrl = this.window.webContents.getURL(); // this comes from electron, expected to be an URL encoded string. e.g: space will be `%20`
15+
let frontendUri = FileUri.create(
16+
this.globals.THEIA_FRONTEND_HTML_PATH
17+
).toString(false); // Map the FS path to an URI, ensure the encoding is not skipped, so that a space will be `%20`.
18+
// Since our resolved frontend HTML path might contain backward slashes on Windows, we normalize everything first.
19+
if (isWindows) {
20+
currentUrl = currentUrl.replace(/\\/g, '/');
21+
frontendUri = frontendUri.replace(/\\/g, '/');
22+
}
23+
const safeToClose =
24+
!currentUrl.includes(frontendUri) || (await this.checkSafeToStop(reason));
25+
if (safeToClose) {
26+
try {
27+
await onSafeCallback();
28+
return true;
29+
} catch (e) {
30+
console.warn(`Request ${StopReason[reason]} failed.`, e);
31+
}
32+
}
33+
return false;
34+
}
35+
}

0 commit comments

Comments
 (0)