Skip to content

Commit

Permalink
Revert "WinForms/WPF/OffScreen - Add WaitForBrowserInitialLoadAsync"
Browse files Browse the repository at this point in the history
Tests started failing so will revisit later.

This reverts commit 08e5140.
  • Loading branch information
amaitland committed Oct 11, 2021
1 parent 8bbe6a4 commit 544abc6
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 186 deletions.
101 changes: 0 additions & 101 deletions CefSharp.Core/WebBrowserExtensionsEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using CefSharp.Internals;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace CefSharp
Expand Down Expand Up @@ -51,106 +50,6 @@ public static Task<NavigationEntry> GetVisibleNavigationEntryAsync(this IWebBrow
return tcs.Task;
}

/// <summary>
/// See <see cref="IWebBrowser.WaitForBrowserInitialLoadAsync()"/> for details
/// </summary>
/// <param name="chromiumWebBrowser">ChromiumWebBrowser instance (cannot be null)</param>
/// <returns>See <see cref="IWebBrowser.WaitForBrowserInitialLoadAsync()"/> for details</returns>
public static async Task<LoadUrlAsyncResponse> WaitForBrowserInitialLoadAsync(IWebBrowser chromiumWebBrowser)
{
if (chromiumWebBrowser.IsDisposed)
{
throw new ObjectDisposedException(nameof(chromiumWebBrowser));
}

//If the browser has already initialized then we
//should check if the page has already loaded
if (chromiumWebBrowser.IsBrowserInitialized)
{
var browser = chromiumWebBrowser.GetBrowser();

if (!browser.IsLoading)
{
var navEntry = await chromiumWebBrowser.GetVisibleNavigationEntryAsync().ConfigureAwait(false);

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;
}

return new LoadUrlAsyncResponse(CefErrorCode.None, statusCode);
}
}

var tcs = new TaskCompletionSource<LoadUrlAsyncResponse>();

EventHandler<LoadErrorEventArgs> loadErrorHandler = null;
EventHandler<LoadingStateChangedEventArgs> loadingStateChangeHandler = null;

loadErrorHandler = (sender, args) =>
{
//Ignore Aborted
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;

//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) =>
{
//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;
}

//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;

var response = await tcs.Task.ConfigureAwait(false);

return response;
}

/// <summary>
/// Downloads the specified <paramref name="url"/> and calls <paramref name="completeHandler"/>
/// when the download is complete. Makes a GET Request.
Expand Down
16 changes: 8 additions & 8 deletions CefSharp.Test/DevTools/DevToolsClientFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task CanCaptureScreenshot()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

using (var devToolsClient = browser.GetDevToolsClient())
{
Expand Down Expand Up @@ -83,7 +83,7 @@ public async Task CanGetDevToolsProtocolVersion()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

using (var devToolsClient = browser.GetDevToolsClient())
{
Expand All @@ -104,7 +104,7 @@ public async Task CanEmulationCanEmulate()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

using (var devToolsClient = browser.GetDevToolsClient())
{
Expand All @@ -120,7 +120,7 @@ public async Task CanGetPageNavigationHistory()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

using (var devToolsClient = browser.GetDevToolsClient())
{
Expand All @@ -143,7 +143,7 @@ public async Task CanSetCookieForDomain(string name, string value, string domain
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

using (var devToolsClient = browser.GetDevToolsClient())
{
Expand All @@ -159,7 +159,7 @@ public async Task CanUseMultipleDevToolsClientInstancesPerBrowser()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

using (var devToolsClient = browser.GetDevToolsClient())
{
Expand Down Expand Up @@ -192,7 +192,7 @@ public async Task CanSetUserAgentOverride()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

using (var devToolsClient = browser.GetDevToolsClient())
{
Expand Down Expand Up @@ -309,7 +309,7 @@ public async Task ExecuteDevToolsMethodThrowsExceptionWithInvalidMethod()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

using (var devToolsClient = browser.GetDevToolsClient())
{
Expand Down
12 changes: 6 additions & 6 deletions CefSharp.Test/JavascriptBinding/IntegrationTestFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public async Task IsObjectCachedWithInvalidObjectNameReturnsFalse()
{
using (var browser = new ChromiumWebBrowser(CefExample.BindingApiCustomObjectNameTestUrl))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

//We'll execute twice using the different cased (camelcase naming and standard)
var response = await browser.EvaluateScriptAsync("CefSharp.IsObjectCached('doesntexist')");
Expand All @@ -132,7 +132,7 @@ public async Task JsBindingGlobalObjectNameCustomValueExecuteIsObjectCachedSucce
//To modify the settings we need to defer browser creation slightly
browser.CreateBrowser();

await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

var result = await browser.EvaluateScriptAsync("bindingApiObject.isObjectCached('doesntexist') === false");

Expand All @@ -151,7 +151,7 @@ public async Task JsBindingGlobalApiDisabled()
//To modify the settings we need to defer browser creation slightly
browser.CreateBrowser();

var loadResponse = await browser.WaitForBrowserInitialLoadAsync();
var loadResponse = await browser.LoadUrlAsync();

Assert.True(loadResponse.Success);

Expand All @@ -177,7 +177,7 @@ public async Task JsBindingGlobalApiEnabled()
//To modify the settings we need to defer browser creation slightly
browser.CreateBrowser();

await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

var response1 = await browser.EvaluateScriptAsync("typeof window.cefSharp === 'undefined'");
var response2 = await browser.EvaluateScriptAsync("typeof window.CefSharp === 'undefined'");
Expand All @@ -197,7 +197,7 @@ public async Task JsBindingRenderProcessId(string script)
{
using (var browser = new ChromiumWebBrowser(CefExample.BindingApiCustomObjectNameTestUrl))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

var result = await browser.EvaluateScriptAsync(script);

Expand All @@ -217,7 +217,7 @@ public async Task CanCallCefSharpBindObjectAsyncWithoutParams()
{
using (var browser = new ChromiumWebBrowser(CefExample.HelloWorldUrl))
{
await browser.WaitForBrowserInitialLoadAsync();
await browser.LoadUrlAsync();

//TODO: See if we can avoid GetAwaiter().GetResult()
var evt = Assert.Raises<JavascriptBindingEventArgs>(
Expand Down
Loading

0 comments on commit 544abc6

Please sign in to comment.