Skip to content

Commit

Permalink
Test - Upgrade LoadRequestAsync extension to handle load errors
Browse files Browse the repository at this point in the history
  • Loading branch information
amaitland committed Jun 4, 2021
1 parent 1fb3f17 commit cd334e5
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions CefSharp.Test/WebBrowserTestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CefSharp.Test
{
public static class WebBrowserTestExtensions
{
public static Task LoadRequestAsync(this IWebBrowser browser, IRequest request)
public static Task<LoadUrlAsyncResponse> LoadRequestAsync(this IWebBrowser browser, IRequest request)
{
if(request == null)
{
Expand All @@ -18,22 +18,58 @@ public static Task LoadRequestAsync(this IWebBrowser browser, IRequest request)

//If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously
//and switch to tcs.TrySetResult below - no need for the custom extension method
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var tcs = new TaskCompletionSource<LoadUrlAsyncResponse>(TaskCreationOptions.RunContinuationsAsynchronously);

EventHandler<LoadingStateChangedEventArgs> handler = null;
handler = (sender, args) =>
EventHandler<LoadErrorEventArgs> loadErrorHandler = null;
EventHandler<LoadingStateChangedEventArgs> loadingStateChangeHandler = null;

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
if (args.ErrorCode == CefErrorCode.Aborted)
{
return;
}

//If LoadError was called then we'll remove both our handlers
//as we won't need to capture LoadingStateChanged, we know there
//was an error
browser.LoadError -= loadErrorHandler;
browser.LoadingStateChanged -= loadingStateChangeHandler;

tcs.TrySetResult(new LoadUrlAsyncResponse(args.ErrorCode, -1));
};

loadingStateChangeHandler = (sender, args) =>
{
//Wait for while page to finish loading not just the first frame
if (!args.IsLoading)
{
browser.LoadingStateChanged -= handler;
var host = args.Browser.GetHost();

var navEntry = host?.GetVisibleNavigationEntry();

int statusCode = navEntry?.HttpStatusCode ?? -1;

//By default 0 is some sort of error, we map that to -1
//so that it's clearer that something failed.
if (statusCode == 0)
{
statusCode = -1;
}

browser.LoadingStateChanged -= loadingStateChangeHandler;
//This is required when using a standard TaskCompletionSource
//Extension method found in the CefSharp.Internals namespace
tcs.TrySetResult(true);
tcs.TrySetResult(new LoadUrlAsyncResponse(CefErrorCode.None, statusCode));
}
};

browser.LoadingStateChanged += handler;
browser.LoadingStateChanged += loadingStateChangeHandler;

browser.GetMainFrame().LoadRequest(request);

Expand Down

0 comments on commit cd334e5

Please sign in to comment.