@@ -9,7 +9,7 @@ namespace CefSharp.Test
99{
1010 public static class WebBrowserTestExtensions
1111 {
12- public static Task LoadRequestAsync ( this IWebBrowser browser , IRequest request )
12+ public static Task < LoadUrlAsyncResponse > LoadRequestAsync ( this IWebBrowser browser , IRequest request )
1313 {
1414 if ( request == null )
1515 {
@@ -18,22 +18,58 @@ public static Task LoadRequestAsync(this IWebBrowser browser, IRequest request)
1818
1919 //If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously
2020 //and switch to tcs.TrySetResult below - no need for the custom extension method
21- var tcs = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
21+ var tcs = new TaskCompletionSource < LoadUrlAsyncResponse > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
2222
23- EventHandler < LoadingStateChangedEventArgs > handler = null ;
24- handler = ( sender , args ) =>
23+ EventHandler < LoadErrorEventArgs > loadErrorHandler = null ;
24+ EventHandler < LoadingStateChangedEventArgs > loadingStateChangeHandler = null ;
25+
26+ loadErrorHandler = ( sender , args ) =>
27+ {
28+ //Ignore Aborted
29+ //Currently invalid SSL certificates which aren't explicitly allowed
30+ //end up with CefErrorCode.Aborted, I've created the following PR
31+ //in the hopes of getting this fixed.
32+ //https://bitbucket.org/chromiumembedded/cef/pull-requests/373
33+ if ( args . ErrorCode == CefErrorCode . Aborted )
34+ {
35+ return ;
36+ }
37+
38+ //If LoadError was called then we'll remove both our handlers
39+ //as we won't need to capture LoadingStateChanged, we know there
40+ //was an error
41+ browser . LoadError -= loadErrorHandler ;
42+ browser . LoadingStateChanged -= loadingStateChangeHandler ;
43+
44+ tcs . TrySetResult ( new LoadUrlAsyncResponse ( args . ErrorCode , - 1 ) ) ;
45+ } ;
46+
47+ loadingStateChangeHandler = ( sender , args ) =>
2548 {
2649 //Wait for while page to finish loading not just the first frame
2750 if ( ! args . IsLoading )
2851 {
29- browser . LoadingStateChanged -= handler ;
52+ var host = args . Browser . GetHost ( ) ;
53+
54+ var navEntry = host ? . GetVisibleNavigationEntry ( ) ;
55+
56+ int statusCode = navEntry ? . HttpStatusCode ?? - 1 ;
57+
58+ //By default 0 is some sort of error, we map that to -1
59+ //so that it's clearer that something failed.
60+ if ( statusCode == 0 )
61+ {
62+ statusCode = - 1 ;
63+ }
64+
65+ browser . LoadingStateChanged -= loadingStateChangeHandler ;
3066 //This is required when using a standard TaskCompletionSource
3167 //Extension method found in the CefSharp.Internals namespace
32- tcs . TrySetResult ( true ) ;
68+ tcs . TrySetResult ( new LoadUrlAsyncResponse ( CefErrorCode . None , statusCode ) ) ;
3369 }
3470 } ;
3571
36- browser . LoadingStateChanged += handler ;
72+ browser . LoadingStateChanged += loadingStateChangeHandler ;
3773
3874 browser . GetMainFrame ( ) . LoadRequest ( request ) ;
3975
0 commit comments