Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
amartya4256 committed Aug 6, 2024
1 parent d5fe3dd commit 1e1cc4c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.swt.browser;

import java.io.*;
import java.net.*;
import java.nio.file.*;

import org.eclipse.swt.*;
Expand Down Expand Up @@ -58,18 +59,18 @@ public class Browser extends Composite {
/**
* @since 3.127
*/
public static final String BASE_URL;
public static final URI BASE_URI;

static {
String absolutePath;
try {
Path tempFile = Files.createTempFile("base", ".html");
absolutePath = tempFile.toUri().toString();
tempFile.toFile().deleteOnExit();
} catch (IOException e) {
absolutePath = "about:blank";
}
BASE_URL = absolutePath;
URI absolutePath;
try {
Path tempFile = Files.createTempFile("base", ".html");
absolutePath = tempFile.toUri();
tempFile.toFile().deleteOnExit();
} catch (IOException e) {
absolutePath = URI.create("about:blank");
}
BASE_URI = absolutePath;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.charset.*;
import java.time.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;

import org.eclipse.swt.*;
Expand Down Expand Up @@ -64,6 +65,8 @@ public WebViewEnvironment(ICoreWebView2Environment environment) {
boolean inNewWindow;
HashMap<Long, LocationEvent> navigations = new HashMap<>();
private String html;
private boolean needsInitialization;
private CompletableFuture<Boolean> initializationProcess = new CompletableFuture<>();

static {
NativeClearSessions = () -> {
Expand Down Expand Up @@ -302,12 +305,12 @@ void checkDeadlock() {
// and JavaScript callbacks are serialized. An event handler waiting
// for a completion of another handler will deadlock. Detect this
// situation and throw an exception instead.
if (inCallback || inNewWindow) {
if (leadsToDeadlock()) {
SWT.error(SWT.ERROR_FAILED_EVALUATE, null, " [WebView2: deadlock detected]");
}
}

boolean isBusy() {
boolean leadsToDeadlock() {
return inCallback || inNewWindow;
}

Expand Down Expand Up @@ -376,14 +379,24 @@ WebViewEnvironment createEnvironment() {

@Override
public void create(Composite parent, int style) {
checkDeadlock();
// If leads to deadlock then schedule the call the method in a different thread and move on.
// The webview calls are queued to be executed when it is done executing the current task.
if (leadsToDeadlock() && !needsInitialization) {
needsInitialization = true;
browser.getDisplay().asyncExec(() -> create(parent, style));
return;
}
needsInitialization = false;
containingEnvironment = createEnvironment();

long[] ppv = new long[1];
int hr = containingEnvironment.environment().QueryInterface(COM.IID_ICoreWebView2Environment2, ppv);
if (hr == COM.S_OK) environment2 = new ICoreWebView2Environment2(ppv[0]);

hr = callAndWait(ppv, completion -> containingEnvironment.environment().CreateCoreWebView2Controller(browser.handle, completion));
if(browser.isDisposed()) {
return;
}
switch (hr) {
case COM.S_OK:
break;
Expand Down Expand Up @@ -458,6 +471,7 @@ public void completed(ProgressEvent event) {
browser.addListener(SWT.Move, this::browserMove);

containingEnvironment.instances().add(this);
initializationProcess.complete(true);
}

void browserDispose(Event event) {
Expand Down Expand Up @@ -863,7 +877,7 @@ public void stop() {
}

private void writeToDefaultPathDOM() {
if(html != null && URI.create(getUrl()).equals(URI.create(Browser.BASE_URL))) {
if(html != null && URI.create(getUrl()).equals(Browser.BASE_URI)) {
boolean test = jsEnabled;
jsEnabled = true;
execute("document.open(); document.write(`" + html + "`); document.close();");
Expand All @@ -874,17 +888,29 @@ private void writeToDefaultPathDOM() {

@Override
public boolean setText(String html, boolean trusted) {
return setWebpageData(Browser.BASE_URL, null, null, html);
return setWebpageData(Browser.BASE_URI.toASCIIString(), null, null, html);
}

private boolean setWebpageData(String url, String postData, String[] headers, String html) {
private boolean setWebpageData(final String url, String postData, String[] headers, String html) {
// If the create() method hasn't finished executing, queue the call to wait for the browser to finish initializing
if (!initializationProcess.isDone()) {
browser.getDisplay().asyncExec(() -> {
waitForInitialization();
setWebpageData(url, postData, headers, html);
browserResize(new Event());
});
return true;
}
// Feature in WebView2. Partial URLs like "www.example.com" are not accepted.
// Prepend the protocol if it's missing.
final String normalisedUrl;
if (!url.matches("[a-z][a-z0-9+.-]*:.*")) {
url = "http://" + url;
normalisedUrl = "http://" + url;
} else {
normalisedUrl = url;
}
int hr;
char[] pszUrl = stringToWstr(url);
char[] pszUrl = stringToWstr(normalisedUrl);
this.html = html;
if (postData != null || headers != null) {
if (environment2 == null || webView_2 == null) {
Expand Down Expand Up @@ -920,6 +946,15 @@ private boolean setWebpageData(String url, String postData, String[] headers, St
return hr == COM.S_OK;
}

private void waitForInitialization() {
try {
initializationProcess.get(10, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public boolean setUrl(String url, String postData, String[] headers) {
return setWebpageData(url, postData, headers, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,8 @@ private void validateTitleChanged(String expectedTitle, Runnable browserSetFunc)
shell.open();

boolean hasFinished = waitForPassCondition(() -> actualTitle.get().length() != 0
&& !actualTitle.get().contains("about:blank")); // Windows sometimes does 2 loads, one "about:blank", and one actual load.
&& !actualTitle.get().contains("about:blank") // Windows sometimes does 2 loads, one "about:blank", and one actual load.
&& !Browser.BASE_URI.toASCIIString().contains(actualTitle.get())); // Edge Browser does 2 loads on setText, one for navigating to BASE_URL, and one actual load.
boolean passed = hasFinished && actualTitle.get().equals(expectedTitle);
String errMsg = "";
if (!hasFinished)
Expand Down

0 comments on commit 1e1cc4c

Please sign in to comment.