@@ -814,7 +814,7 @@ void browserDispose(Event event) {
814
814
if (inCallback ) {
815
815
ICoreWebView2Controller controller1 = controller ;
816
816
controller .put_IsVisible (false );
817
- browser . getDisplay (). asyncExec (() -> {
817
+ executeAsynchronously (() -> {
818
818
controller1 .Close ();
819
819
controller1 .Release ();
820
820
});
@@ -960,7 +960,7 @@ private String getExposedUrl(String url) {
960
960
}
961
961
962
962
int handleCloseRequested (long pView , long pArgs ) {
963
- browser . getDisplay (). asyncExec (() -> {
963
+ executeAsynchronously (() -> {
964
964
if (browser .isDisposed ()) return ;
965
965
WindowEvent event = new WindowEvent (browser );
966
966
event .display = browser .getDisplay ();
@@ -978,7 +978,7 @@ int handleDocumentTitleChanged(long pView, long pArgs) {
978
978
long [] ppsz = new long [1 ];
979
979
webViewProvider .getWebView (false ).get_DocumentTitle (ppsz );
980
980
String title = wstrToString (ppsz [0 ], true );
981
- browser . getDisplay (). asyncExec (() -> {
981
+ executeAsynchronously (() -> {
982
982
if (browser .isDisposed ()) return ;
983
983
TitleEvent event = new TitleEvent (browser );
984
984
event .display = browser .getDisplay ();
@@ -1031,10 +1031,12 @@ int handleNavigationStarting(long pView, long pArgs, boolean top) {
1031
1031
event .location = url ;
1032
1032
event .top = top ;
1033
1033
event .doit = true ;
1034
- for (LocationListener listener : locationListeners ) {
1035
- listener .changing (event );
1036
- if (browser .isDisposed ()) return COM .S_OK ;
1037
- }
1034
+ executeAsynchronously (() -> {
1035
+ for (LocationListener listener : locationListeners ) {
1036
+ listener .changing (event );
1037
+ if (browser .isDisposed ()) return ;
1038
+ }
1039
+ });
1038
1040
// Save location and top for all events that use navigationId.
1039
1041
// will be eventually cleared again in handleNavigationCompleted().
1040
1042
navigations .put (pNavId [0 ], event );
@@ -1043,11 +1045,13 @@ int handleNavigationStarting(long pView, long pArgs, boolean top) {
1043
1045
settings .put_IsScriptEnabled (jsEnabled );
1044
1046
// Register browser functions in the new document.
1045
1047
if (!functions .isEmpty ()) {
1046
- StringBuilder sb = new StringBuilder ();
1047
- for (BrowserFunction function : functions .values ()) {
1048
- sb .append (function .functionString );
1049
- }
1050
- execute (sb .toString ());
1048
+ executeAsynchronously (() -> {
1049
+ StringBuilder sb = new StringBuilder ();
1050
+ for (BrowserFunction function : functions .values ()) {
1051
+ sb .append (function .functionString );
1052
+ }
1053
+ execute (sb .toString ());
1054
+ });
1051
1055
}
1052
1056
} else {
1053
1057
args .put_Cancel (true );
@@ -1093,7 +1097,7 @@ int handleSourceChanged(long pView, long pArgs) {
1093
1097
} else {
1094
1098
location = url ;
1095
1099
}
1096
- browser . getDisplay (). asyncExec (() -> {
1100
+ executeAsynchronously (() -> {
1097
1101
if (browser .isDisposed ()) return ;
1098
1102
LocationEvent event = new LocationEvent (browser );
1099
1103
event .display = browser .getDisplay ();
@@ -1110,7 +1114,7 @@ int handleSourceChanged(long pView, long pArgs) {
1110
1114
}
1111
1115
1112
1116
void sendProgressCompleted () {
1113
- browser . getDisplay (). asyncExec (() -> {
1117
+ executeAsynchronously (() -> {
1114
1118
if (browser .isDisposed ()) return ;
1115
1119
ProgressEvent event = new ProgressEvent (browser );
1116
1120
event .display = browser .getDisplay ();
@@ -1160,22 +1164,24 @@ int handleBasicAuthenticationRequested(long pView, long pArgs) {
1160
1164
args .get_Uri (ppv );
1161
1165
String uri = wstrToString (ppv [0 ], true );
1162
1166
1163
- for (AuthenticationListener authenticationListener : this .authenticationListeners ) {
1164
- AuthenticationEvent event = new AuthenticationEvent (browser );
1165
- event .location = uri ;
1166
- authenticationListener .authenticate (event );
1167
- if (!event .doit ) {
1168
- args .put_Cancel (true );
1169
- return COM .S_OK ;
1170
- }
1171
- if (event .user != null && event .password != null ) {
1172
- args .get_Response (ppv );
1173
- ICoreWebView2BasicAuthenticationResponse response = new ICoreWebView2BasicAuthenticationResponse (ppv [0 ]);
1174
- response .put_UserName (stringToWstr (event .user ));
1175
- response .put_Password (stringToWstr (event .password ));
1176
- return COM .S_OK ;
1167
+ executeAsynchronously (() -> {
1168
+ for (AuthenticationListener authenticationListener : this .authenticationListeners ) {
1169
+ AuthenticationEvent event = new AuthenticationEvent (browser );
1170
+ event .location = uri ;
1171
+ authenticationListener .authenticate (event );
1172
+ if (!event .doit ) {
1173
+ args .put_Cancel (true );
1174
+ return ;
1175
+ }
1176
+ if (event .user != null && event .password != null ) {
1177
+ args .get_Response (ppv );
1178
+ ICoreWebView2BasicAuthenticationResponse response = new ICoreWebView2BasicAuthenticationResponse (ppv [0 ]);
1179
+ response .put_UserName (stringToWstr (event .user ));
1180
+ response .put_Password (stringToWstr (event .password ));
1181
+ return ;
1182
+ }
1177
1183
}
1178
- }
1184
+ });
1179
1185
1180
1186
return COM .S_OK ;
1181
1187
}
@@ -1236,9 +1242,11 @@ int handleStatusBarTextChanged(long pView, long pArgs) {
1236
1242
statusTextEvent .display = browser .getDisplay ();
1237
1243
statusTextEvent .widget = browser ;
1238
1244
statusTextEvent .text = text ;
1239
- for (StatusTextListener statusTextListener : statusTextListeners ) {
1240
- statusTextListener .changed (statusTextEvent );
1241
- }
1245
+ executeAsynchronously (() -> {
1246
+ for (StatusTextListener statusTextListener : statusTextListeners ) {
1247
+ statusTextListener .changed (statusTextEvent );
1248
+ }
1249
+ });
1242
1250
return COM .S_OK ;
1243
1251
}
1244
1252
@@ -1267,7 +1275,7 @@ int handleNavigationCompleted(long pView, long pArgs, boolean top) {
1267
1275
int [] pIsSuccess = new int [1 ];
1268
1276
args .get_IsSuccess (pIsSuccess );
1269
1277
if (pIsSuccess [0 ] != 0 ) {
1270
- browser . getDisplay (). asyncExec (() -> {
1278
+ executeAsynchronously (() -> {
1271
1279
if (browser .isDisposed ()) return ;
1272
1280
LocationEvent event = new LocationEvent (browser );
1273
1281
event .display = browser .getDisplay ();
@@ -1284,42 +1292,66 @@ int handleNavigationCompleted(long pView, long pArgs, boolean top) {
1284
1292
return COM .S_OK ;
1285
1293
}
1286
1294
1287
- void updateWindowFeatures (ICoreWebView2NewWindowRequestedEventArgs args , WindowEvent event ) {
1288
- long [] ppv = new long [1 ];
1289
- int hr = args .get_WindowFeatures (ppv );
1290
- if (hr != COM .S_OK ) return ;
1291
- ICoreWebView2WindowFeatures features = new ICoreWebView2WindowFeatures (ppv [0 ]);
1292
-
1293
- int [] px = new int [1 ], py = new int [1 ];
1294
- features .get_HasPosition (px );
1295
- if (px [0 ] != 0 ) {
1296
- features .get_Left (px );
1297
- features .get_Top (py );
1298
- event .location = new Point (px [0 ], py [0 ]);
1299
- }
1300
- features .get_HasSize (px );
1301
- if (px [0 ] != 0 ) {
1302
- features .get_Width (px );
1303
- features .get_Height (py );
1304
- event .size = new Point (px [0 ], py [0 ]);
1305
- }
1306
- // event.addressBar = ???
1307
- features .get_ShouldDisplayMenuBar (px );
1308
- event .menuBar = px [0 ] != 0 ;
1309
- features .get_ShouldDisplayStatus (px );
1310
- event .statusBar = px [0 ] != 0 ;
1311
- features .get_ShouldDisplayToolbar (px );
1312
- event .toolBar = px [0 ] != 0 ;
1295
+ private class WindowFeatures {
1296
+ boolean hasPosition ;
1297
+ boolean hasSize ;
1298
+ int x , y , width , height ;
1299
+ boolean shouldDisplayMenuBar ;
1300
+ boolean shouldDisplayStatus ;
1301
+ boolean shouldDisplayToolBar ;
1302
+
1303
+ public WindowFeatures (ICoreWebView2NewWindowRequestedEventArgs args ) {
1304
+ long [] ppv = new long [1 ];
1305
+ int hr = args .get_WindowFeatures (ppv );
1306
+ if (hr != COM .S_OK ) SWT .error (SWT .ERROR_INVALID_ARGUMENT );
1307
+ ICoreWebView2WindowFeatures features = new ICoreWebView2WindowFeatures (ppv [0 ]);
1308
+ int [] px = new int [1 ];
1309
+ features .get_HasPosition (px );
1310
+ this .hasPosition = px [0 ] != 0 ;
1311
+ if (this .hasPosition ) {
1312
+ features .get_Left (px );
1313
+ this .x = px [0 ];
1314
+ features .get_Top (px );
1315
+ this .y = px [0 ];
1316
+ }
1317
+ features .get_HasSize (px );
1318
+ this .hasSize = px [0 ] != 0 ;
1319
+ if (this .hasSize ) {
1320
+ features .get_Width (px );
1321
+ this .width = px [0 ];
1322
+ features .get_Height (px );
1323
+ this .height = px [0 ];
1324
+ }
1325
+ features .get_ShouldDisplayMenuBar (px );
1326
+ this .shouldDisplayMenuBar = px [0 ] != 0 ;
1327
+ features .get_ShouldDisplayStatus (px );
1328
+ this .shouldDisplayStatus = px [0 ] != 0 ;
1329
+ features .get_ShouldDisplayToolbar (px );
1330
+ this .shouldDisplayToolBar = px [0 ] != 0 ;
1331
+ }
1332
+ }
1333
+
1334
+ void updateWindowFeatures (WindowFeatures windowFeatures , WindowEvent event ) {
1335
+ if (windowFeatures .hasPosition ) {
1336
+ event .location = new Point (windowFeatures .x , windowFeatures .y );
1337
+ }
1338
+ if (windowFeatures .hasSize ) {
1339
+ event .size = new Point (windowFeatures .width , windowFeatures .height );
1340
+ }
1341
+ event .menuBar = windowFeatures .shouldDisplayMenuBar ;
1342
+ event .statusBar = windowFeatures .shouldDisplayStatus ;
1343
+ event .toolBar = windowFeatures .shouldDisplayToolBar ;
1313
1344
}
1314
1345
1315
1346
int handleNewWindowRequested (long pView , long pArgs ) {
1316
1347
ICoreWebView2NewWindowRequestedEventArgs args = new ICoreWebView2NewWindowRequestedEventArgs (pArgs );
1317
1348
args .AddRef ();
1318
- long [] ppv = new long [1 ];
1319
- args .GetDeferral (ppv );
1320
- ICoreWebView2Deferral deferral = new ICoreWebView2Deferral (ppv [0 ]);
1349
+ WindowFeatures windowFeatures = new WindowFeatures (args );
1350
+ long [] ppszUrl = new long [1 ];
1351
+ args .get_Uri (ppszUrl );
1352
+ String url = getExposedUrl (wstrToString (ppszUrl [0 ], true ));
1321
1353
inNewWindow = true ;
1322
- browser . getDisplay (). asyncExec (() -> {
1354
+ executeAsynchronously (() -> {
1323
1355
try {
1324
1356
if (browser .isDisposed ()) return ;
1325
1357
WindowEvent openEvent = new WindowEvent (browser );
@@ -1332,30 +1364,24 @@ int handleNewWindowRequested(long pView, long pArgs) {
1332
1364
}
1333
1365
if (openEvent .browser != null && !openEvent .browser .isDisposed ()) {
1334
1366
WebBrowser other = openEvent .browser .webBrowser ;
1335
- args .put_Handled (true );
1336
1367
if (other instanceof Edge otherEdge ) {
1337
- args .put_NewWindow (otherEdge .webViewProvider .getWebView (true ).getAddress ());
1338
-
1368
+ otherEdge .setUrl (url , null , null );
1339
1369
// Send show event to the other browser.
1340
1370
WindowEvent showEvent = new WindowEvent (other .browser );
1341
1371
showEvent .display = browser .getDisplay ();
1342
1372
showEvent .widget = other .browser ;
1343
- updateWindowFeatures (args , showEvent );
1344
- for (VisibilityWindowListener showListener : other .visibilityWindowListeners ) {
1345
- showListener .show (showEvent );
1346
- if (other .browser .isDisposed ()) return ;
1347
- }
1373
+ updateWindowFeatures (windowFeatures , showEvent );
1374
+ for (VisibilityWindowListener showListener : other .visibilityWindowListeners ) {
1375
+ showListener .show (showEvent );
1376
+ if (other .browser .isDisposed ()) return ;
1377
+ }
1348
1378
}
1349
- } else if (openEvent .required ) {
1350
- args .put_Handled (true );
1351
1379
}
1352
1380
} finally {
1353
- deferral .Complete ();
1354
- deferral .Release ();
1355
- args .Release ();
1356
1381
inNewWindow = false ;
1357
1382
}
1358
1383
});
1384
+ args .put_Handled (true );
1359
1385
return COM .S_OK ;
1360
1386
}
1361
1387
@@ -1426,7 +1452,8 @@ int handleAcceleratorKeyPressed(long pView, long pArgs) {
1426
1452
}
1427
1453
} else {
1428
1454
keyEvent .type = SWT .KeyUp ;
1429
- browser .notifyListeners (keyEvent .type , keyEvent );
1455
+ executeAsynchronously (() -> browser .notifyListeners (keyEvent .type , keyEvent ));
1456
+
1430
1457
if (!keyEvent .doit ) {
1431
1458
args .put_Handled (true );
1432
1459
}
@@ -1500,6 +1527,10 @@ public boolean setText(String html, boolean trusted) {
1500
1527
return setWebpageData (URI_FOR_CUSTOM_TEXT_PAGE .toString (), null , null , html );
1501
1528
}
1502
1529
1530
+ private void executeAsynchronously (Runnable runnable ) {
1531
+ browser .getDisplay ().asyncExec (runnable );
1532
+ }
1533
+
1503
1534
private boolean setWebpageData (String url , String postData , String [] headers , String html ) {
1504
1535
// Feature in WebView2. Partial URLs like "www.example.com" are not accepted.
1505
1536
// Prepend the protocol if it's missing.
0 commit comments