From 4112340e48467c93ffc7f606d5861015e79d29bb Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Thu, 28 Oct 2021 10:51:18 +1000 Subject: [PATCH] Core - LoadUrlAsync require url and remove SyncContext param - Remove url optional null, a Url is now required and an exception will be thrown if null or empty. If the browser wasn't loading an null was passed in then the task would have never resolved. This provides a predictable behaviour. A new WaitForInitialLoadAsync method was added to simplify waiting for initial browser load. - Remove SynchronizationContext param, best to let the framework handle Issue #3842 --- CefSharp.OffScreen.Example/Program.cs | 2 +- .../ChromiumWebBrowserOffScreenFixture.cs | 2 +- CefSharp/IWebBrowser.cs | 3 +- .../Partial/ChromiumWebBrowser.Partial.cs | 4 +- CefSharp/WebBrowserExtensions.cs | 60 ++++++------------- 5 files changed, 24 insertions(+), 47 deletions(-) diff --git a/CefSharp.OffScreen.Example/Program.cs b/CefSharp.OffScreen.Example/Program.cs index ea0cef447..534bb6e95 100644 --- a/CefSharp.OffScreen.Example/Program.cs +++ b/CefSharp.OffScreen.Example/Program.cs @@ -93,7 +93,7 @@ namespace CefSharp.OffScreen.Example } }; } - await browser.LoadUrlAsync(); + await browser.WaitForInitialLoadAsync(); //Check preferences on the CEF UI Thread await Cef.UIThreadTaskFactory.StartNew(delegate diff --git a/CefSharp.Test/ChromiumWebBrowserOffScreenFixture.cs b/CefSharp.Test/ChromiumWebBrowserOffScreenFixture.cs index b92007d9e..40b47798c 100644 --- a/CefSharp.Test/ChromiumWebBrowserOffScreenFixture.cs +++ b/CefSharp.Test/ChromiumWebBrowserOffScreenFixture.cs @@ -24,7 +24,7 @@ namespace CefSharp.Test { Browser = new ChromiumWebBrowser(CefExample.HelloWorldUrl); - return Browser.LoadUrlAsync(); + return Browser.WaitForInitialLoadAsync(); } } } diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs index c433abcd7..56169138b 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -104,12 +104,11 @@ namespace CefSharp /// Load the in the main frame of the browser /// /// url to load - /// SynchronizationContext to execute the continuation on, if null then the ThreadPool will be used. /// /// A that can be awaited to load the and return the HttpStatusCode and . /// A HttpStatusCode equal to 200 and is considered a success. /// - Task LoadUrlAsync(string url = null, SynchronizationContext ctx = null); + Task LoadUrlAsync(string url); /// /// Wait for the Browser to finish loading the initial web page. diff --git a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs index 83b46bb61..6f0097be6 100644 --- a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs +++ b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs @@ -353,10 +353,10 @@ namespace CefSharp.WinForms } /// - public Task LoadUrlAsync(string url = null, SynchronizationContext ctx = null) + public Task LoadUrlAsync(string url) { //LoadUrlAsync is actually a static method so that CefSharp.Wpf.HwndHost can reuse the code - return CefSharp.WebBrowserExtensions.LoadUrlAsync(this, url, ctx); + return CefSharp.WebBrowserExtensions.LoadUrlAsync(this, url); } /// diff --git a/CefSharp/WebBrowserExtensions.cs b/CefSharp/WebBrowserExtensions.cs index 9aba8568a..12a4ac17a 100644 --- a/CefSharp/WebBrowserExtensions.cs +++ b/CefSharp/WebBrowserExtensions.cs @@ -247,17 +247,21 @@ namespace CefSharp } /// - /// See for details + /// See for details /// /// ChromiumWebBrowser instance (cannot be null) /// /// Load the in the main frame of the browser /// /// url to load - /// SynchronizationContext to execute the continuation on, if null then the ThreadPool will be used. - /// See for details - public static Task LoadUrlAsync(IWebBrowser chromiumWebBrowser, string url = null, SynchronizationContext ctx = null) + /// See for details + public static Task LoadUrlAsync(IWebBrowser chromiumWebBrowser, string url) { + if(string.IsNullOrEmpty(url)) + { + throw new ArgumentNullException(nameof(url)); + } + var tcs = new TaskCompletionSource(); EventHandler loadErrorHandler = null; @@ -265,11 +269,8 @@ namespace CefSharp loadErrorHandler = (sender, args) => { - //Ignore Aborted - //Currently invalid SSL certificates which aren't explicitly allowed - //end up with CefErrorCode.Aborted, I've created the following PR - //in the hopes of getting this fixed. - //https://bitbucket.org/chromiumembedded/cef/pull-requests/373 + //Actions that trigger a download will raise an aborted error. + //Generally speaking Aborted is safe to ignore if (args.ErrorCode == CefErrorCode.Aborted) { return; @@ -281,20 +282,10 @@ namespace CefSharp chromiumWebBrowser.LoadError -= loadErrorHandler; chromiumWebBrowser.LoadingStateChanged -= loadingStateChangeHandler; - if (ctx == null) - { - //Ensure our continuation is executed on the ThreadPool - //For the .Net Core implementation we could use - //TaskCreationOptions.RunContinuationsAsynchronously - tcs.TrySetResultAsync(new LoadUrlAsyncResponse(args.ErrorCode, -1)); - } - else - { - ctx.Post(new SendOrPostCallback((o) => - { - tcs.TrySetResult(new LoadUrlAsyncResponse(args.ErrorCode, -1)); - }), null); - } + //Ensure our continuation is executed on the ThreadPool + //For the .Net Core implementation we could use + //TaskCreationOptions.RunContinuationsAsynchronously + tcs.TrySetResultAsync(new LoadUrlAsyncResponse(args.ErrorCode, -1)); }; loadingStateChangeHandler = (sender, args) => @@ -322,30 +313,17 @@ namespace CefSharp statusCode = -1; } - if (ctx == null) - { - //Ensure our continuation is executed on the ThreadPool - //For the .Net Core implementation we could use - //TaskCreationOptions.RunContinuationsAsynchronously - tcs.TrySetResultAsync(new LoadUrlAsyncResponse(CefErrorCode.None, statusCode)); - } - else - { - ctx.Post(new SendOrPostCallback((o) => - { - tcs.TrySetResult(new LoadUrlAsyncResponse(CefErrorCode.None, statusCode)); - }), null); - } + //Ensure our continuation is executed on the ThreadPool + //For the .Net Core implementation we could use + //TaskCreationOptions.RunContinuationsAsynchronously + tcs.TrySetResultAsync(new LoadUrlAsyncResponse(CefErrorCode.None, statusCode)); } }; chromiumWebBrowser.LoadError += loadErrorHandler; chromiumWebBrowser.LoadingStateChanged += loadingStateChangeHandler; - if (!string.IsNullOrEmpty(url)) - { - chromiumWebBrowser.Load(url); - } + chromiumWebBrowser.Load(url); return tcs.Task; }