@@ -264,7 +264,7 @@ static int callAndWait(long[] ppv, ToIntFunction<IUnknown> callable) {
264
264
phr [0 ] = callable .applyAsInt (completion );
265
265
// "completion" callback may be called asynchronously,
266
266
// so keep processing next OS message that may call it
267
- processOSMessagesUntil (() -> phr [0 ] != COM .S_OK || ppv [0 ] != 0 , Display .getCurrent ());
267
+ processOSMessagesUntil (() -> phr [0 ] != COM .S_OK || ppv [0 ] != 0 , Optional . empty (), Display .getCurrent ());
268
268
completion .Release ();
269
269
return phr [0 ];
270
270
}
@@ -282,7 +282,7 @@ int callAndWait(String[] pstr, ToIntFunction<IUnknown> callable) {
282
282
phr [0 ] = callable .applyAsInt (completion );
283
283
// "completion" callback may be called asynchronously,
284
284
// so keep processing next OS message that may call it
285
- processOSMessagesUntil (() -> phr [0 ] != COM .S_OK || pstr [0 ] != null , browser .getDisplay ());
285
+ processOSMessagesUntil (() -> phr [0 ] != COM .S_OK || pstr [0 ] != null , Optional . empty (), browser .getDisplay ());
286
286
completion .Release ();
287
287
return phr [0 ];
288
288
}
@@ -318,9 +318,8 @@ void releaseWebViews() {
318
318
}
319
319
320
320
class WebViewProvider {
321
-
322
321
private CompletableFuture <WebViewWrapper > webViewWrapperFuture = wakeDisplayAfterFuture (new CompletableFuture <>());
323
- private CompletableFuture <Void > lastWebViewTask = webViewWrapperFuture .thenRun (() -> {});;
322
+ private CompletableFuture <Void > lastWebViewTask = webViewWrapperFuture .thenRun (() -> {});
324
323
325
324
ICoreWebView2 initializeWebView (ICoreWebView2Controller controller ) {
326
325
long [] ppv = new long [1 ];
@@ -333,7 +332,12 @@ ICoreWebView2 initializeWebView(ICoreWebView2Controller controller) {
333
332
webViewWrapper .webView_11 = initializeWebView_11 (webView );
334
333
webViewWrapper .webView_12 = initializeWebView_12 (webView );
335
334
webViewWrapper .webView_13 = initializeWebView_13 (webView );
336
- webViewWrapperFuture .complete (webViewWrapper );
335
+ boolean success = webViewWrapperFuture .complete (webViewWrapper );
336
+ // Release the webViews if the webViewWrapperFuture has already timed out and completed exceptionally
337
+ if (!success && webViewWrapperFuture .isCompletedExceptionally ()) {
338
+ webViewWrapper .releaseWebViews ();
339
+ return null ;
340
+ }
337
341
return webView ;
338
342
}
339
343
@@ -342,7 +346,7 @@ private void abortInitialization() {
342
346
}
343
347
344
348
void releaseWebView () {
345
- processOSMessagesUntil (webViewWrapperFuture ::isDone , browser .getDisplay ());
349
+ processOSMessagesUntil (webViewWrapperFuture ::isDone , Optional . of (() -> webViewWrapperFuture . completeExceptionally ( createTimeOutException ())), browser .getDisplay ());
346
350
webViewWrapperFuture .join ().releaseWebViews ();
347
351
}
348
352
@@ -393,13 +397,13 @@ private ICoreWebView2_13 initializeWebView_13(ICoreWebView2 webView) {
393
397
394
398
private WebViewWrapper getWebViewWrapper (boolean waitForPendingWebviewTasksToFinish ) {
395
399
if (waitForPendingWebviewTasksToFinish ) {
396
- processOSMessagesUntil (lastWebViewTask ::isDone , browser .getDisplay ());
400
+ processOSMessagesUntil (lastWebViewTask ::isDone , Optional . empty (), browser .getDisplay ());
397
401
}
398
402
return webViewWrapperFuture .join ();
399
403
}
400
404
401
405
private WebViewWrapper getWebViewWrapper () {
402
- processOSMessagesUntil (webViewWrapperFuture ::isDone , browser .getDisplay ());
406
+ processOSMessagesUntil (webViewWrapperFuture ::isDone , Optional . of (() -> webViewWrapperFuture . completeExceptionally ( createTimeOutException ())), browser .getDisplay ());
403
407
return webViewWrapperFuture .join ();
404
408
}
405
409
@@ -484,7 +488,7 @@ private <T> CompletableFuture<T> wakeDisplayAfterFuture(CompletableFuture<T> fut
484
488
* events for initialization. Thus, this method does not implement an ordinary
485
489
* readAndDispatch loop, but waits for an OS event to be processed.
486
490
*/
487
- private static void processOSMessagesUntil (Supplier <Boolean > condition , Display display ) {
491
+ private static void processOSMessagesUntil (Supplier <Boolean > condition , Optional < Runnable > timeoutHandler , Display display ) {
488
492
MSG msg = new MSG ();
489
493
AtomicBoolean timeoutOccurred = new AtomicBoolean ();
490
494
// The timer call also wakes up the display to avoid being stuck in display.sleep()
@@ -497,10 +501,15 @@ private static void processOSMessagesUntil(Supplier<Boolean> condition, Display
497
501
}
498
502
}
499
503
if (!condition .get ()) {
500
- SWT .error (SWT .ERROR_UNSPECIFIED , null , " Waiting for Edge operation to terminate timed out" );
504
+ timeoutHandler .ifPresent (Runnable ::run );
505
+ throw createTimeOutException ();
501
506
}
502
507
}
503
508
509
+ private static SWTException createTimeOutException () {
510
+ return new SWTException (SWT .ERROR_UNSPECIFIED , " Waiting for Edge operation to terminate timed out" );
511
+ }
512
+
504
513
static ICoreWebView2CookieManager getCookieManager () {
505
514
WebViewEnvironment environmentWrapper = webViewEnvironments .get (Display .getCurrent ());
506
515
if (environmentWrapper == null ) {
@@ -642,7 +651,10 @@ private IUnknown createControllerInitializationCallback(int previousAttempts) {
642
651
switch (result ) {
643
652
case COM .S_OK :
644
653
new IUnknown (pv ).AddRef ();
645
- setupBrowser (result , pv );
654
+ boolean success = setupBrowser (result , pv );
655
+ if (!success ) {
656
+ initializationRollback .run ();
657
+ }
646
658
break ;
647
659
case COM .E_WRONG_THREAD :
648
660
initializationAbortion .run ();
@@ -666,10 +678,13 @@ private IUnknown createControllerInitializationCallback(int previousAttempts) {
666
678
});
667
679
}
668
680
669
- void setupBrowser (int hr , long pv ) {
681
+ boolean setupBrowser (int hr , long pv ) {
670
682
long [] ppv = new long [] {pv };
671
683
controller = new ICoreWebView2Controller (ppv [0 ]);
672
684
final ICoreWebView2 webView = webViewProvider .initializeWebView (controller );
685
+ if (webView == null ) {
686
+ return false ;
687
+ }
673
688
webView .get_Settings (ppv );
674
689
settings = new ICoreWebView2Settings (ppv [0 ]);
675
690
@@ -769,6 +784,7 @@ void setupBrowser(int hr, long pv) {
769
784
if (browser .isFocusControl ()) {
770
785
browserFocusIn (new Event ());
771
786
}
787
+ return true ;
772
788
}
773
789
774
790
void browserDispose (Event event ) {
0 commit comments