mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 04:00:07 +00:00
Enhancement - Add LoadUrlAsync (#3596)
Core - Add LoadUrlAsync - Add IWebBrowser.LoadUrlAsync - Add LoadUrlAsyncResponse - Update unit tests to use new method - Add static method to allow for CefSharp.Wpf.HwndHost to share the same code.
This commit is contained in:
@@ -55,7 +55,7 @@ namespace CefSharp.Test.DevTools
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
using (var devToolsClient = browser.GetDevToolsClient())
|
||||
{
|
||||
@@ -76,7 +76,7 @@ namespace CefSharp.Test.DevTools
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
using (var devToolsClient = browser.GetDevToolsClient())
|
||||
{
|
||||
@@ -92,7 +92,7 @@ namespace CefSharp.Test.DevTools
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
using (var devToolsClient = browser.GetDevToolsClient())
|
||||
{
|
||||
@@ -115,7 +115,7 @@ namespace CefSharp.Test.DevTools
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
using (var devToolsClient = browser.GetDevToolsClient())
|
||||
{
|
||||
@@ -131,7 +131,7 @@ namespace CefSharp.Test.DevTools
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
using (var devToolsClient = browser.GetDevToolsClient())
|
||||
{
|
||||
@@ -164,7 +164,7 @@ namespace CefSharp.Test.DevTools
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
using (var devToolsClient = browser.GetDevToolsClient())
|
||||
{
|
||||
|
||||
@@ -106,7 +106,7 @@ namespace CefSharp.Test.JavascriptBinding
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser(CefExample.BindingApiCustomObjectNameTestUrl))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
//We'll execute twice using the different cased (camelcase naming and standard)
|
||||
var response = await browser.EvaluateScriptAsync("CefSharp.IsObjectCached('doesntexist')");
|
||||
@@ -132,7 +132,7 @@ namespace CefSharp.Test.JavascriptBinding
|
||||
//To modify the settings we need to defer browser creation slightly
|
||||
browser.CreateBrowser();
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
var result = await browser.EvaluateScriptAsync("bindingApiObject.isObjectCached('doesntexist') === false");
|
||||
|
||||
@@ -195,7 +195,7 @@ namespace CefSharp.Test.JavascriptBinding
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser(CefExample.BindingApiCustomObjectNameTestUrl))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
var result = await browser.EvaluateScriptAsync(script);
|
||||
|
||||
@@ -215,7 +215,7 @@ namespace CefSharp.Test.JavascriptBinding
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser(CefExample.HelloWorldUrl))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
//TODO: See if we can avoid GetAwaiter().GetResult()
|
||||
var evt = Assert.Raises<JavascriptBindingEventArgs>(
|
||||
|
||||
@@ -47,16 +47,51 @@ namespace CefSharp.Test.OffScreen
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
Assert.Contains("www.google", mainFrame.Url);
|
||||
Assert.Equal(200, response.HttpStatusCode);
|
||||
|
||||
output.WriteLine("Url {0}", mainFrame.Url);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CanLoadInvalidDomain()
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("notfound.cefsharp.test"))
|
||||
{
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
Assert.Contains("notfound.cefsharp.test", mainFrame.Url);
|
||||
Assert.Equal(CefErrorCode.NameNotResolved, response.ErrorCode);
|
||||
|
||||
output.WriteLine("Url {0}", mainFrame.Url);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CanLoadExpiredBadSsl()
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("https://expired.badssl.com/"))
|
||||
{
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
Assert.Contains("", mainFrame.Url);
|
||||
Assert.Equal(-1, response.HttpStatusCode);
|
||||
|
||||
output.WriteLine("Url {0}", mainFrame.Url);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BrowserRefCountDecrementedOnDispose()
|
||||
{
|
||||
@@ -104,7 +139,9 @@ namespace CefSharp.Test.OffScreen
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -137,7 +174,10 @@ namespace CefSharp.Test.OffScreen
|
||||
browser.JavascriptObjectRepository.Register("bound", boundObj, true);
|
||||
#endif
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
browser.GetMainFrame().ExecuteJavaScriptAsync(script);
|
||||
|
||||
await Task.Delay(2000);
|
||||
@@ -146,7 +186,7 @@ namespace CefSharp.Test.OffScreen
|
||||
boundObj.MethodCalled = false;
|
||||
|
||||
browser.Load("https://www.google.com");
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
browser.GetMainFrame().ExecuteJavaScriptAsync(script);
|
||||
await Task.Delay(2000);
|
||||
Assert.True(boundObj.MethodCalled);
|
||||
@@ -178,7 +218,7 @@ namespace CefSharp.Test.OffScreen
|
||||
#endif
|
||||
};
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
browser.GetMainFrame().ExecuteJavaScriptAsync(script);
|
||||
|
||||
await Task.Delay(2000);
|
||||
@@ -196,7 +236,9 @@ namespace CefSharp.Test.OffScreen
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("http://www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -227,7 +269,9 @@ namespace CefSharp.Test.OffScreen
|
||||
browser.JavascriptObjectRepository.Settings.JavascriptBindingApiGlobalObjectName = rootObjName;
|
||||
browser.CreateBrowser();
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -251,7 +295,9 @@ namespace CefSharp.Test.OffScreen
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("http://www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -281,7 +327,9 @@ namespace CefSharp.Test.OffScreen
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("http://www.google.com"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -308,7 +356,9 @@ namespace CefSharp.Test.OffScreen
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser("https://code.jquery.com/jquery-3.4.1.min.js"))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -344,7 +394,9 @@ namespace CefSharp.Test.OffScreen
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser(url))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var htmlSrc = await browser.GetSourceAsync();
|
||||
|
||||
@@ -388,7 +440,7 @@ namespace CefSharp.Test.OffScreen
|
||||
}
|
||||
});
|
||||
|
||||
await chromiumWebBrowser.LoadPageAsync();
|
||||
await chromiumWebBrowser.LoadUrlAsync();
|
||||
|
||||
chromiumWebBrowser.StartDownload(url);
|
||||
|
||||
@@ -447,15 +499,17 @@ namespace CefSharp.Test.OffScreen
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser(firstUrl))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
Assert.True(browser.CanExecuteJavascriptInMainFrame);
|
||||
|
||||
await browser.LoadPageAsync(secondUrl);
|
||||
await browser.LoadUrlAsync(secondUrl);
|
||||
|
||||
Assert.True(browser.CanExecuteJavascriptInMainFrame);
|
||||
|
||||
await browser.LoadPageAsync(firstUrl);
|
||||
await browser.LoadUrlAsync(firstUrl);
|
||||
|
||||
Assert.True(browser.CanExecuteJavascriptInMainFrame);
|
||||
}
|
||||
@@ -469,7 +523,9 @@ namespace CefSharp.Test.OffScreen
|
||||
//To use LoadRequest we must first load a web page
|
||||
using (var browser = new ChromiumWebBrowser(new HtmlString("Testing")))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var request = new Request();
|
||||
request.Url = "http://httpbin.org/post";
|
||||
@@ -510,7 +566,9 @@ namespace CefSharp.Test.OffScreen
|
||||
|
||||
using (var browser = new ChromiumWebBrowser("http://cefsharp.github.io/", requestContext: requestContext))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -544,7 +602,9 @@ namespace CefSharp.Test.OffScreen
|
||||
|
||||
using (var browser = new ChromiumWebBrowser("http://cefsharp.github.io/", requestContext: requestContext))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -594,7 +654,9 @@ namespace CefSharp.Test.OffScreen
|
||||
|
||||
using (var browser = new ChromiumWebBrowser("http://cefsharp.github.io/", requestContext: requestContext))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -612,7 +674,9 @@ namespace CefSharp.Test.OffScreen
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser(new HtmlString("Testing")))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
var response = await browser.LoadUrlAsync();
|
||||
|
||||
Assert.True(response.Success);
|
||||
|
||||
var source = await browser.GetSourceAsync();
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace CefSharp.Test.PostMessage
|
||||
//Page.AddScriptToEvaluateOnNewDocument (via DevTools)
|
||||
using (var browser = new ChromiumWebBrowser(new HtmlString("Initial Load")))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
using (var devToolsClient = browser.GetDevToolsClient())
|
||||
{
|
||||
|
||||
@@ -9,34 +9,6 @@ namespace CefSharp.Test
|
||||
{
|
||||
public static class WebBrowserTestExtensions
|
||||
{
|
||||
public static Task LoadPageAsync(this IWebBrowser browser, string address = null)
|
||||
{
|
||||
//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);
|
||||
|
||||
EventHandler<LoadingStateChangedEventArgs> handler = null;
|
||||
handler = (sender, args) =>
|
||||
{
|
||||
//Wait for while page to finish loading not just the first frame
|
||||
if (!args.IsLoading)
|
||||
{
|
||||
browser.LoadingStateChanged -= handler;
|
||||
//This is required when using a standard TaskCompletionSource
|
||||
//Extension method found in the CefSharp.Internals namespace
|
||||
tcs.TrySetResult(true);
|
||||
}
|
||||
};
|
||||
|
||||
browser.LoadingStateChanged += handler;
|
||||
|
||||
if (!string.IsNullOrEmpty(address))
|
||||
{
|
||||
browser.Load(address);
|
||||
}
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
public static Task LoadRequestAsync(this IWebBrowser browser, IRequest request)
|
||||
{
|
||||
if(request == null)
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace CefSharp.Test.WinForms
|
||||
browser.Size = new System.Drawing.Size(1024, 768);
|
||||
browser.CreateControl();
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -52,7 +52,7 @@ namespace CefSharp.Test.WinForms
|
||||
browser.Size = new System.Drawing.Size(1024, 768);
|
||||
browser.CreateControl();
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -75,7 +75,7 @@ namespace CefSharp.Test.WinForms
|
||||
browser.Size = new System.Drawing.Size(1024, 768);
|
||||
browser.CreateControl();
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -97,7 +97,7 @@ namespace CefSharp.Test.WinForms
|
||||
browser.Size = new System.Drawing.Size(1024, 768);
|
||||
browser.CreateControl();
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -117,7 +117,7 @@ namespace CefSharp.Test.WinForms
|
||||
browser.Size = new System.Drawing.Size(1024, 768);
|
||||
browser.CreateControl();
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace CefSharp.Test.Wpf
|
||||
{
|
||||
using (var browser = new ChromiumWebBrowser(null, "www.google.com", new Size(1024, 786)))
|
||||
{
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -46,7 +46,7 @@ namespace CefSharp.Test.Wpf
|
||||
browser.RequestContext = new RequestContext();
|
||||
browser.CreateBrowser(null, new Size(1024, 786));
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
@@ -65,7 +65,7 @@ namespace CefSharp.Test.Wpf
|
||||
|
||||
browser.CreateBrowser(null, new Size(1024, 786));
|
||||
|
||||
await browser.LoadPageAsync();
|
||||
await browser.LoadUrlAsync();
|
||||
|
||||
var mainFrame = browser.GetMainFrame();
|
||||
Assert.True(mainFrame.IsValid);
|
||||
|
||||
@@ -127,6 +127,7 @@
|
||||
<Compile Include="Internals\MimeTypeMapping.cs" />
|
||||
<Compile Include="Internals\Tasks\SyncContextTaskCompletionSource.cs" />
|
||||
<Compile Include="IRegistration.cs" />
|
||||
<Compile Include="LoadUrlAsyncResponse.cs" />
|
||||
<Compile Include="UrlRequestClient.cs" />
|
||||
<Compile Include="JavascriptBinding\JavascriptBindingSettings.cs" />
|
||||
<Compile Include="Internals\NoOpBrowserRefCounter.cs" />
|
||||
|
||||
+14
-1
@@ -3,6 +3,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
@@ -79,11 +81,22 @@ namespace CefSharp
|
||||
event EventHandler<JavascriptMessageReceivedEventArgs> JavascriptMessageReceived;
|
||||
|
||||
/// <summary>
|
||||
/// Loads the specified URL.
|
||||
/// Loads the specified <paramref name="url"/> in the Main Frame
|
||||
/// </summary>
|
||||
/// <param name="url">The URL to be loaded.</param>
|
||||
void Load(string url);
|
||||
|
||||
/// <summary>
|
||||
/// Load the <paramref name="url"/> in the main frame of the browser
|
||||
/// </summary>
|
||||
/// <param name="url">url to load</param>
|
||||
/// <param name="ctx">SynchronizationContext to execute the continuation on, if null then the ThreadPool will be used.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="Task{int}"/> that can be awaited to load the <paramref name="url"/> and return the HttpStatusCode and <see cref="CefErrorCode"/>.
|
||||
/// A HttpStatusCode equal to 200 and <see cref="CefErrorCode.None"/> is considered a success.
|
||||
/// </returns>
|
||||
Task<LoadUrlAsyncResponse> LoadUrlAsync(string url = null, SynchronizationContext ctx = null);
|
||||
|
||||
/// <summary>
|
||||
/// The javascript object repository, one repository per ChromiumWebBrowser instance.
|
||||
/// </summary>
|
||||
|
||||
@@ -315,6 +315,15 @@ namespace CefSharp.WinForms
|
||||
OnAfterBrowserCreated(browser);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Task<LoadUrlAsyncResponse> LoadUrlAsync(string url = null, SynchronizationContext ctx = null)
|
||||
{
|
||||
//LoadUrlAsync is actually a static method so that CefSharp.Wpf.HwndHost can reuse the code
|
||||
//It's not actually an extension method so we can have it included as part of the
|
||||
//IWebBrowser interface
|
||||
return CefSharp.WebBrowserExtensions.LoadUrlAsync(this, url, ctx);
|
||||
}
|
||||
|
||||
partial void OnAfterBrowserCreated(IBrowser browser);
|
||||
|
||||
private void SetHandlersToNullExceptLifeSpan()
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright © 2021 The CefSharp Authors. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
namespace CefSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// Response returned from <see cref="IWebBrowser.LoadUrlAsync(string, System.Threading.SynchronizationContext)"/>
|
||||
/// </summary>
|
||||
public class LoadUrlAsyncResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// Error Code. If the network request was made successfully this value will be <see cref="CefErrorCode.None"/>
|
||||
/// (no error occured)
|
||||
/// </summary>
|
||||
public CefErrorCode ErrorCode { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Http Status Code. If <see cref="ErrorCode"/> is not equal to <see cref="CefErrorCode.None"/>
|
||||
/// then this value will be -1.
|
||||
/// </summary>
|
||||
public int HttpStatusCode { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// If <see cref="ErrorCode"/> is equal to <see cref="CefErrorCode.None"/> and
|
||||
/// <see cref="HttpStatusCode"/> is equal to 200 (OK) then the main frame loaded without
|
||||
/// critical error.
|
||||
/// </summary>
|
||||
public bool Success
|
||||
{
|
||||
get { return ErrorCode == CefErrorCode.None && HttpStatusCode == 200; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor
|
||||
/// </summary>
|
||||
/// <param name="errorCode">CEF Error Code</param>
|
||||
/// <param name="httpStatusCode">Http Status Code</param>
|
||||
public LoadUrlAsyncResponse(CefErrorCode errorCode, int httpStatusCode)
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
HttpStatusCode = httpStatusCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using System.Collections.Specialized;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CefSharp.Internals;
|
||||
using CefSharp.Web;
|
||||
@@ -242,6 +243,110 @@ namespace CefSharp
|
||||
host.StartDownload(url);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// See <see cref="IWebBrowser.LoadUrlAsync(string, SynchronizationContext)"/> for details
|
||||
/// </summary>
|
||||
/// <param name="chromiumWebBrowser">ChromiumWebBrowser instance (cannot be null)</param>
|
||||
/// <summary>
|
||||
/// Load the <paramref name="url"/> in the main frame of the browser
|
||||
/// </summary>
|
||||
/// <param name="url">url to load</param>
|
||||
/// <param name="ctx">SynchronizationContext to execute the continuation on, if null then the ThreadPool will be used.</param>
|
||||
/// <returns>See <see cref="IWebBrowser.LoadUrlAsync(string, SynchronizationContext)"/> for details</returns>
|
||||
public static Task<LoadUrlAsyncResponse> LoadUrlAsync(IWebBrowser chromiumWebBrowser, string url = null, SynchronizationContext ctx = null)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<LoadUrlAsyncResponse>();
|
||||
|
||||
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
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
loadingStateChangeHandler = (sender, args) =>
|
||||
{
|
||||
//Wait for IsLoading = false
|
||||
if (!args.IsLoading)
|
||||
{
|
||||
//If LoadingStateChanged was called then we'll remove both our handlers
|
||||
//as LoadError won't be called, our site has loaded with a valid HttpStatusCode
|
||||
//HttpStatusCodes can still be for example 404, this is considered a successful request,
|
||||
//the server responded, it just didn't have the page you were after.
|
||||
chromiumWebBrowser.LoadError -= loadErrorHandler;
|
||||
chromiumWebBrowser.LoadingStateChanged -= loadingStateChangeHandler;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
chromiumWebBrowser.LoadError += loadErrorHandler;
|
||||
chromiumWebBrowser.LoadingStateChanged += loadingStateChangeHandler;
|
||||
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
{
|
||||
chromiumWebBrowser.Load(url);
|
||||
}
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute some Javascript code in the context of this WebBrowser. As the method name implies, the script will be executed
|
||||
/// asynchronously, and the method therefore returns before the script has actually been executed. This simple helper extension
|
||||
|
||||
Reference in New Issue
Block a user