Skip to content

Commit 96117f9

Browse files
committed
OffScreen - Add CreateBrowserAsync
Issue #3594
1 parent f6ea0de commit 96117f9

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,58 @@ public void CreateBrowser(IWindowInfo windowInfo = null, IBrowserSettings browse
329329
browserSettings = null;
330330
}
331331

332+
/// <summary>
333+
/// Create the underlying CEF browser. The address and request context passed into the constructor
334+
/// will be used. If a <see cref="Action{IBrowser}"/> delegate was passed
335+
/// into the constructor it will not be called as this method overrides that value internally.
336+
/// </summary>
337+
/// <param name="windowInfo">Window information used when creating the browser</param>
338+
/// <param name="browserSettings">Browser initialization settings</param>
339+
/// <exception cref="System.Exception">An instance of the underlying offscreen browser has already been created, this method can only be called once.</exception>
340+
/// <returns>
341+
/// A <see cref="Task{IBrowser}"/> that represents the creation of the underlying CEF browser (<see cref="IBrowser"/> instance.
342+
/// When the task completes then the CEF Browser will have been created and you can start performing basic tasks.
343+
/// Note that the control's <see cref="BrowserInitialized"/> event will be invoked after this task completes.
344+
/// </returns>
345+
public Task<IBrowser> CreateBrowserAsync(IWindowInfo windowInfo = null, IBrowserSettings browserSettings = null)
346+
{
347+
if (browserCreated)
348+
{
349+
throw new Exception("An instance of the underlying offscreen browser has already been created, this method can only be called once.");
350+
}
351+
352+
browserCreated = true;
353+
354+
if (browserSettings == null)
355+
{
356+
browserSettings = Core.ObjectFactory.CreateBrowserSettings(autoDispose: true);
357+
}
358+
359+
if (windowInfo == null)
360+
{
361+
windowInfo = Core.ObjectFactory.CreateWindowInfo();
362+
windowInfo.SetAsWindowless(IntPtr.Zero);
363+
}
364+
365+
var tcs = new TaskCompletionSource<IBrowser>();
366+
367+
onAfterBrowserCreatedDelegate = new Action<IBrowser>(b =>
368+
{
369+
tcs.TrySetResultAsync(b);
370+
});
371+
372+
managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings, RequestContext, Address);
373+
374+
//Dispose of BrowserSettings if we created it, if user created then they're responsible
375+
if (browserSettings.AutoDispose)
376+
{
377+
browserSettings.Dispose();
378+
}
379+
browserSettings = null;
380+
381+
return tcs.Task;
382+
}
383+
332384
/// <summary>
333385
/// Get/set the size of the Chromium viewport, in pixels.
334386
/// This also changes the size of the next rendered bitmap.

CefSharp.Test/OffScreen/OffScreenBrowserBasicFacts.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ public void BrowserRefCountDecrementedOnDispose()
8585
Assert.Equal(0, BrowserRefCounter.Instance.Count);
8686
}
8787

88+
[Fact]
89+
public async Task CanCreateBrowserAsync()
90+
{
91+
using (var chromiumWebBrowser = new ChromiumWebBrowser("http://www.google.com", automaticallyCreateBrowser: false))
92+
{
93+
var browser = await chromiumWebBrowser.CreateBrowserAsync();
94+
95+
Assert.NotNull(browser);
96+
Assert.False(browser.HasDocument);
97+
Assert.NotEqual(0, browser.Identifier);
98+
Assert.False(browser.IsDisposed);
99+
}
100+
}
101+
88102
[Fact]
89103
public async Task CanLoadGoogleAndEvaluateScript()
90104
{

0 commit comments

Comments
 (0)