@@ -80,7 +80,7 @@ public WebViewEnvironment(ICoreWebView2Environment environment) {
80
80
81
81
WebViewEnvironment containingEnvironment ;
82
82
83
- static boolean inCallback ;
83
+ static int inCallback ;
84
84
boolean inNewWindow ;
85
85
private boolean inEvaluate ;
86
86
HashMap <Long , LocationEvent > navigations = new HashMap <>();
@@ -235,11 +235,11 @@ static void error(int code, int hr) {
235
235
236
236
static IUnknown newCallback (ICoreWebView2SwtCallback handler ) {
237
237
long punk = COM .CreateSwtWebView2Callback ((arg0 , arg1 ) -> {
238
- inCallback = true ;
238
+ inCallback ++ ;
239
239
try {
240
240
return handler .Invoke (arg0 , arg1 );
241
241
} finally {
242
- inCallback = false ;
242
+ inCallback -- ;
243
243
}
244
244
});
245
245
if (punk == 0 ) error (SWT .ERROR_NO_HANDLES , COM .E_OUTOFMEMORY );
@@ -552,7 +552,7 @@ void checkDeadlock() {
552
552
// and JavaScript callbacks are serialized. An event handler waiting
553
553
// for a completion of another handler will deadlock. Detect this
554
554
// situation and throw an exception instead.
555
- if (inCallback || inNewWindow ) {
555
+ if (inCallback > 0 || inNewWindow ) {
556
556
SWT .error (SWT .ERROR_FAILED_EVALUATE , null , " [WebView2: deadlock detected]" );
557
557
}
558
558
}
@@ -821,7 +821,7 @@ void browserDispose(Event event) {
821
821
if (controller != null ) {
822
822
// Bug in WebView2. Closing the controller from an event handler results
823
823
// in a crash. The fix is to delay the closure with asyncExec.
824
- if (inCallback ) {
824
+ if (inCallback > 0 ) {
825
825
ICoreWebView2Controller controller1 = controller ;
826
826
controller .put_IsVisible (false );
827
827
browser .getDisplay ().asyncExec (() -> {
@@ -916,7 +916,7 @@ public Object evaluate(String script) throws SWTException {
916
916
// Feature in WebView2. ExecuteScript works regardless of IsScriptEnabled setting.
917
917
// Disallow programmatic execution manually.
918
918
if (!jsEnabled ) return null ;
919
- if (inCallback ) {
919
+ if (inCallback > 0 ) {
920
920
// Execute script, but do not wait for async call to complete as otherwise it
921
921
// can cause a deadlock if execute inside a WebView callback.
922
922
execute (script );
@@ -1017,12 +1017,15 @@ long handleCallJava(int index, long bstrToken, long bstrArgsJson) {
1017
1017
String token = bstrToString (bstrToken );
1018
1018
BrowserFunction function = functions .get (index );
1019
1019
if (function != null && token .equals (function .token )) {
1020
+ inCallback ++;
1020
1021
try {
1021
1022
String argsJson = bstrToString (bstrArgsJson );
1022
1023
Object args = JSON .parse (argsJson .toCharArray ());
1023
1024
result = function .function ((Object []) args );
1024
1025
} catch (Throwable e ) {
1025
1026
result = WebBrowser .CreateErrorString (e .getLocalizedMessage ());
1027
+ } finally {
1028
+ inCallback --;
1026
1029
}
1027
1030
}
1028
1031
String json = JSON .stringify (result );
0 commit comments