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