Skip to content

Commit 48fb67d

Browse files
tnikolai2HeikoKlare
authored andcommitted
[Edge] Change lambdas for OS callbacks to classes
Contributes to supporting native images (GraalVM) for Edge.
1 parent 4862ef8 commit 48fb67d

File tree

1 file changed

+40
-20
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser

1 file changed

+40
-20
lines changed

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

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,26 @@ static void error(int code, int hr) {
233233
SWT.error(code, null, String.format(" [0x%08x]", hr));
234234
}
235235

236-
static IUnknown newCallback(ICoreWebView2SwtCallback handler) {
237-
long punk = COM.CreateSwtWebView2Callback((arg0, arg1) -> {
236+
static class HandleCoreWebView2SwtCallback implements ICoreWebView2SwtCallback {
237+
private final ICoreWebView2SwtCallback handler;
238+
239+
public HandleCoreWebView2SwtCallback(ICoreWebView2SwtCallback handler) {
240+
this.handler = handler;
241+
}
242+
243+
@Override
244+
public int Invoke(long arg0, long arg1) {
238245
inCallback++;
239246
try {
240247
return handler.Invoke(arg0, arg1);
241248
} finally {
242249
inCallback--;
243250
}
244-
});
251+
}
252+
}
253+
254+
static IUnknown newCallback(ICoreWebView2SwtCallback handler) {
255+
long punk = COM.CreateSwtWebView2Callback(new HandleCoreWebView2SwtCallback(handler));
245256
if (punk == 0) error(SWT.ERROR_NO_HANDLES, COM.E_OUTOFMEMORY);
246257
return new IUnknown(punk);
247258
}
@@ -789,7 +800,7 @@ void setupBrowser(int hr, long pv) {
789800
handler.Release();
790801
}
791802

792-
IUnknown hostDisp = newHostObject(this::handleCallJava);
803+
IUnknown hostDisp = newHostObject(new HandleCoreWebView2SwtHost(this.functions));
793804
long[] hostObj = { COM.VT_DISPATCH, hostDisp.getAddress(), 0 }; // VARIANT
794805
webView.AddHostObjectToScript("swt\0".toCharArray(), hostObj);
795806
hostDisp.Release();
@@ -1020,24 +1031,33 @@ int handleDocumentTitleChanged(long pView, long pArgs) {
10201031
return COM.S_OK;
10211032
}
10221033

1023-
long handleCallJava(int index, long bstrToken, long bstrArgsJson) {
1024-
Object result = null;
1025-
String token = bstrToString(bstrToken);
1026-
BrowserFunction function = functions.get(index);
1027-
if (function != null && token.equals (function.token)) {
1028-
inCallback++;
1029-
try {
1030-
String argsJson = bstrToString(bstrArgsJson);
1031-
Object args = JSON.parse(argsJson.toCharArray());
1032-
result = function.function ((Object[]) args);
1033-
} catch (Throwable e) {
1034-
result = WebBrowser.CreateErrorString(e.getLocalizedMessage());
1035-
} finally {
1036-
inCallback--;
1034+
static class HandleCoreWebView2SwtHost implements ICoreWebView2SwtHost {
1035+
private final Map<Integer, BrowserFunction> functions;
1036+
1037+
public HandleCoreWebView2SwtHost(Map<Integer, BrowserFunction> functions) {
1038+
this.functions = functions;
1039+
}
1040+
1041+
@Override
1042+
public long CallJava(int index, long bstrToken, long bstrArgsJson) {
1043+
Object result = null;
1044+
String token = bstrToString(bstrToken);
1045+
BrowserFunction function = functions.get(index);
1046+
if (function != null && token.equals(function.token)) {
1047+
inCallback++;
1048+
try {
1049+
String argsJson = bstrToString(bstrArgsJson);
1050+
Object args = JSON.parse(argsJson.toCharArray());
1051+
result = function.function((Object[]) args);
1052+
} catch (Throwable e) {
1053+
result = WebBrowser.CreateErrorString(e.getLocalizedMessage());
1054+
} finally {
1055+
inCallback--;
1056+
}
10371057
}
1058+
String json = JSON.stringify(result);
1059+
return COM.SysAllocStringLen(json.toCharArray(), json.length());
10381060
}
1039-
String json = JSON.stringify(result);
1040-
return COM.SysAllocStringLen(json.toCharArray(), json.length());
10411061
}
10421062

10431063
int handleFrameNavigationStarting(long pView, long pArgs) {

0 commit comments

Comments
 (0)