Skip to content

Commit 9b2ef91

Browse files
committed
Edge: Fix URI_FOR_CUSTOM_TEXT_PAGE in case tmpdir contains unicode chars
We must ensure that we always use the ASCII-String (URL-encoded) everywhere. Refactor code a bit to make it possible to add a testcase. Fixes eclipse-platform#1912.
1 parent 909a985 commit 9b2ef91

File tree

2 files changed

+42
-9
lines changed
  • bundles/org.eclipse.swt
    • Eclipse SWT Browser/win32/org/eclipse/swt/browser
    • Eclipse SWT Tests/win32/org/eclipse/swt/browser

2 files changed

+42
-9
lines changed

bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/Edge.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
class Edge extends WebBrowser {
3636
static {
3737
Library.loadLibrary("WebView2Loader");
38+
setupLocationForCustomTextPage();
3839
}
3940

4041
// WebView2Loader.dll compatible version. This is NOT the minimal required version.
@@ -57,7 +58,7 @@ class Edge extends WebBrowser {
5758
* by Edge browser to navigate to for setting html content in the
5859
* DOM of the browser to enable it to load local resources.
5960
*/
60-
private static final URI URI_FOR_CUSTOM_TEXT_PAGE = setupAndGetLocationForCustomTextPage();
61+
static URI URI_FOR_CUSTOM_TEXT_PAGE;
6162
private static final String ABOUT_BLANK = "about:blank";
6263

6364
private static final int MAXIMUM_CREATION_RETRIES = 5;
@@ -195,16 +196,14 @@ private static record CursorPosition(Point location, boolean isInsideBrowser) {}
195196
};
196197
}
197198

198-
private static URI setupAndGetLocationForCustomTextPage() {
199-
URI absolutePath;
199+
static void setupLocationForCustomTextPage() {
200200
try {
201-
Path tempFile = Files.createTempFile("base", ".html");
202-
absolutePath = tempFile.toUri();
201+
Path tempFile = Files.createTempFile(Path.of(System.getProperty("java.io.tmpdir")), "base", ".html");
202+
URI_FOR_CUSTOM_TEXT_PAGE = URI.create(tempFile.toUri().toASCIIString());
203203
tempFile.toFile().deleteOnExit();
204204
} catch (IOException e) {
205-
absolutePath = URI.create(ABOUT_BLANK);
205+
URI_FOR_CUSTOM_TEXT_PAGE = URI.create(ABOUT_BLANK);
206206
}
207-
return absolutePath;
208207
}
209208

210209
static String wstrToString(long psz, boolean free) {
@@ -1487,7 +1486,7 @@ public void stop() {
14871486
webViewProvider.scheduleWebViewTask(() -> webViewProvider.getWebView(false).Stop());
14881487
}
14891488

1490-
private boolean isLocationForCustomText(String location) {
1489+
static boolean isLocationForCustomText(String location) {
14911490
try {
14921491
return URI_FOR_CUSTOM_TEXT_PAGE.equals(new URI(location));
14931492
} catch (URISyntaxException e) {
@@ -1497,7 +1496,7 @@ private boolean isLocationForCustomText(String location) {
14971496

14981497
@Override
14991498
public boolean setText(String html, boolean trusted) {
1500-
return setWebpageData(URI_FOR_CUSTOM_TEXT_PAGE.toASCIIString(), null, null, html);
1499+
return setWebpageData(URI_FOR_CUSTOM_TEXT_PAGE.toString(), null, null, html);
15011500
}
15021501

15031502
private boolean setWebpageData(String url, String postData, String[] headers, String html) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.eclipse.swt.browser;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import java.nio.file.*;
6+
7+
import org.eclipse.swt.internal.*;
8+
import org.junit.jupiter.api.*;
9+
import org.junit.jupiter.api.extension.*;
10+
11+
@ExtendWith(PlatformSpecificExecutionExtension.class)
12+
class EdgeTests {
13+
14+
/**
15+
* https://github.com/eclipse-platform/eclipse.platform.swt/issues/1912
16+
*/
17+
@Test
18+
public void handlingOfTempDirWithSpacesAndUnicodeCharacters() throws Exception {
19+
String originalTempDir = System.getProperty("java.io.tmpdir");
20+
String temporaryTempDirWithSpacesAndUnicode = Files.createTempDirectory("spaces and únîcòde").toString();
21+
System.setProperty("java.io.tmpdir", temporaryTempDirWithSpacesAndUnicode);
22+
try {
23+
Edge.setupLocationForCustomTextPage();
24+
25+
// URI_FOR_CUSTOM_TEXT_PAGE must already be encoded internally
26+
assertTrue(Edge.URI_FOR_CUSTOM_TEXT_PAGE.toString().contains("spaces%20and%20%C3%BAn%C3%AEc%C3%B2de"));
27+
assertTrue(Edge.isLocationForCustomText(Edge.URI_FOR_CUSTOM_TEXT_PAGE.toASCIIString()));
28+
29+
} finally {
30+
System.setProperty("java.io.tmpdir", originalTempDir);
31+
Edge.setupLocationForCustomTextPage(); // restore the original value
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)