Skip to content

Commit

Permalink
OffScreen - Add useLegacyRenderHandler optional param to constructor
Browse files Browse the repository at this point in the history
- Defaults to true
- For those using CaptureScreenshotAsync set to false to avoid
  creating DefaultRenderHandler
  • Loading branch information
amaitland committed Jan 15, 2023
1 parent 0d790cd commit a51da76
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 62 deletions.
19 changes: 16 additions & 3 deletions CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,15 @@ public bool IsBrowserInitialized
/// this action is guranteed to be called after the browser created where as the <see cref="BrowserInitialized"/> event may be called before
/// you have a chance to subscribe to the event as the CEF Browser is created async. (Issue https://github.com/cefsharp/CefSharp/issues/3552).
/// </param>
/// <param name="useLegacyRenderHandler">
/// For those using <see cref="ScreenshotAsync(bool, PopupBlending)"/> or <see cref="ScreenshotOrNull(PopupBlending)"/> then
/// this must be true, for those using <see cref="CaptureScreenshotAsync(CaptureScreenshotFormat?, int?, Viewport)"/> this can be false.
/// Lower memory usage when false. Defaults to true for backwards compatability.
/// </param>
/// <exception cref="System.InvalidOperationException">Cef::Initialize() failed</exception>
public ChromiumWebBrowser(HtmlString html, IBrowserSettings browserSettings = null,
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true,
Action<IBrowser> onAfterBrowserCreated = null) : this(html.ToDataUriString(), browserSettings, requestContext, automaticallyCreateBrowser, onAfterBrowserCreated)
Action<IBrowser> onAfterBrowserCreated = null, bool useLegacyRenderHandler = true) : this(html.ToDataUriString(), browserSettings, requestContext, automaticallyCreateBrowser, onAfterBrowserCreated, useLegacyRenderHandler)
{
}

Expand All @@ -195,10 +200,15 @@ public ChromiumWebBrowser(HtmlString html, IBrowserSettings browserSettings = nu
/// this action is guranteed to be called after the browser created where as the <see cref="BrowserInitialized"/> event may be called before
/// you have a chance to subscribe to the event as the CEF Browser is created async. (Issue https://github.com/cefsharp/CefSharp/issues/3552).
/// </param>
/// <param name="useLegacyRenderHandler">
/// For those using <see cref="ScreenshotAsync(bool, PopupBlending)"/> or <see cref="ScreenshotOrNull(PopupBlending)"/> then
/// this must be true, for those using <see cref="CaptureScreenshotAsync(CaptureScreenshotFormat?, int?, Viewport)"/> this can be false.
/// Lower memory usage when false. Defaults to true for backwards compatability.
/// </param>
/// <exception cref="System.InvalidOperationException">Cef::Initialize() failed</exception>
public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings = null,
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true,
Action<IBrowser> onAfterBrowserCreated = null)
Action<IBrowser> onAfterBrowserCreated = null, bool useLegacyRenderHandler = true)
{
if (!Cef.IsInitialized)
{
Expand All @@ -223,7 +233,10 @@ public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings
CreateBrowser(null, browserSettings);
}

RenderHandler = new DefaultRenderHandler(this);
if (useLegacyRenderHandler)
{
RenderHandler = new DefaultRenderHandler(this);
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion CefSharp.Test/ChromiumWebBrowserOffScreenFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Task IAsyncLifetime.DisposeAsync()

Task IAsyncLifetime.InitializeAsync()
{
Browser = new ChromiumWebBrowser(CefExample.HelloWorldUrl);
Browser = new ChromiumWebBrowser(CefExample.HelloWorldUrl, useLegacyRenderHandler: false);

return Browser.WaitForInitialLoadAsync();
}
Expand Down
22 changes: 11 additions & 11 deletions CefSharp.Test/DevTools/DevToolsClientFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public DevToolsClientFacts(ITestOutputHelper output, CefSharpFixture fixture)
[Fact]
public async Task CanCaptureScreenshot()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
using (var browser = new ChromiumWebBrowser("www.google.com", useLegacyRenderHandler: false))
{
await browser.WaitForInitialLoadAsync();

Expand Down Expand Up @@ -81,7 +81,7 @@ public void CanConvertDevToolsObjectToDictionary()
[Fact]
public async Task CanGetDevToolsProtocolVersion()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
using (var browser = new ChromiumWebBrowser("www.google.com", useLegacyRenderHandler: false))
{
await browser.WaitForInitialLoadAsync();

Expand All @@ -102,7 +102,7 @@ public async Task CanGetDevToolsProtocolVersion()
[Fact]
public async Task CanEmulationCanEmulate()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
using (var browser = new ChromiumWebBrowser("www.google.com", useLegacyRenderHandler: false))
{
await browser.WaitForInitialLoadAsync();

Expand All @@ -118,7 +118,7 @@ public async Task CanEmulationCanEmulate()
[Fact]
public async Task CanGetPageNavigationHistory()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
using (var browser = new ChromiumWebBrowser("www.google.com", useLegacyRenderHandler: false))
{
await browser.WaitForInitialLoadAsync();

Expand All @@ -141,7 +141,7 @@ public async Task CanGetPageNavigationHistory()
[InlineData("CefSharpTest1", "CefSharp Test Cookie2", CefExample.ExampleDomain, CookieSameSite.Lax)]
public async Task CanSetCookieForDomain(string name, string value, string domain, CookieSameSite sameSite)
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
using (var browser = new ChromiumWebBrowser("www.google.com", useLegacyRenderHandler: false))
{
await browser.WaitForInitialLoadAsync();

Expand All @@ -157,7 +157,7 @@ public async Task CanSetCookieForDomain(string name, string value, string domain
[Fact]
public async Task CanUseMultipleDevToolsClientInstancesPerBrowser()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
using (var browser = new ChromiumWebBrowser("www.google.com", useLegacyRenderHandler: false))
{
await browser.WaitForInitialLoadAsync();

Expand Down Expand Up @@ -190,7 +190,7 @@ public async Task CanUseMultipleDevToolsClientInstancesPerBrowser()
[Fact]
public async Task CanSetUserAgentOverride()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
using (var browser = new ChromiumWebBrowser("www.google.com", useLegacyRenderHandler: false))
{
await browser.WaitForInitialLoadAsync();

Expand Down Expand Up @@ -259,7 +259,7 @@ public async Task CanSetUserAgentOverride()
[Fact]
public async Task CanSetExtraHTTPHeaders()
{
using (var browser = new ChromiumWebBrowser("about:blank", automaticallyCreateBrowser: false))
using (var browser = new ChromiumWebBrowser("about:blank", automaticallyCreateBrowser: false, useLegacyRenderHandler: false))
{
await browser.CreateBrowserAsync();

Expand Down Expand Up @@ -307,7 +307,7 @@ public async Task CanSetExtraHTTPHeaders()
[Fact]
public async Task ExecuteDevToolsMethodThrowsExceptionWithInvalidMethod()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
using (var browser = new ChromiumWebBrowser("www.google.com", useLegacyRenderHandler: false))
{
await browser.WaitForInitialLoadAsync();

Expand All @@ -327,7 +327,7 @@ public async Task ExecuteDevToolsMethodThrowsExceptionWithInvalidMethod()
[Fact]
public async Task CanGetMediaQueries()
{
using (var browser = new ChromiumWebBrowser("https://cefsharp.github.io/demo/mediaqueryhover.html"))
using (var browser = new ChromiumWebBrowser("https://cefsharp.github.io/demo/mediaqueryhover.html", useLegacyRenderHandler: false))
{
await browser.WaitForInitialLoadAsync();

Expand All @@ -346,7 +346,7 @@ public async Task CanGetMediaQueries()
[Fact]
public async Task CanRegisterMultipleEventHandlers()
{
using (var browser = new ChromiumWebBrowser("about:blank", automaticallyCreateBrowser: false))
using (var browser = new ChromiumWebBrowser("about:blank", automaticallyCreateBrowser: false, useLegacyRenderHandler: false))
{
await browser.CreateBrowserAsync();

Expand Down
4 changes: 2 additions & 2 deletions CefSharp.Test/Javascript/EvaluateScriptAsPromiseAsyncFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public EvaluateScriptAsPromiseAsyncFacts(ITestOutputHelper output, CefSharpFixtu
[InlineData("var result = await fetch('./robots.txt'); return result.status;", true, "200")]
public async Task CanEvaluateScriptAsPromiseAsync(string script, bool success, string expected)
{
using (var browser = new ChromiumWebBrowser("http://www.google.com"))
using (var browser = new ChromiumWebBrowser("http://www.google.com", useLegacyRenderHandler: false))
{
var response = await browser.WaitForInitialLoadAsync();

Expand Down Expand Up @@ -66,7 +66,7 @@ public async Task CanEvaluateScriptAsPromiseAsync(string script, bool success, s
[InlineData("function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }; async function getValAfterSleep() { await sleep(1000); return { a: 'CefSharp', b: 42 }; }; await sleep(2000); const result = await getValAfterSleep(); await sleep(2000); return result;", true, "CefSharp", "42")]
public async Task CanEvaluateScriptAsPromiseAsyncReturnObject(string script, bool success, string expectedA, string expectedB)
{
using (var browser = new ChromiumWebBrowser("http://www.google.com"))
using (var browser = new ChromiumWebBrowser("http://www.google.com", useLegacyRenderHandler: false))
{
var response = await browser.WaitForInitialLoadAsync();

Expand Down
8 changes: 4 additions & 4 deletions CefSharp.Test/Navigation/WaitForNavigationAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task CanWork()
{
const string expected = CefExample.HelloWorldUrl;

using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl))
using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl, useLegacyRenderHandler: false))
{
var response = await browser.WaitForInitialLoadAsync();

Expand All @@ -54,7 +54,7 @@ public async Task CanWork()
public async Task CanWaitForInvalidDomain()
{
const string expected = "https://notfound.cefsharp.test";
using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl))
using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl, useLegacyRenderHandler: false))
{
var response = await browser.WaitForInitialLoadAsync();

Expand Down Expand Up @@ -82,7 +82,7 @@ public async Task CanTimeout()
{
const string expected = "The operation has timed out.";

using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl))
using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl, useLegacyRenderHandler: false))
{
var response = await browser.WaitForInitialLoadAsync();

Expand All @@ -104,7 +104,7 @@ public async Task CanCancel()
{
const string expected = "A task was canceled.";

using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl))
using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl, useLegacyRenderHandler: false))
{
var response = await browser.WaitForInitialLoadAsync();

Expand Down
Loading

0 comments on commit a51da76

Please sign in to comment.