Skip to content

Commit 4ebe7de

Browse files
committed
OffScreen - Add ChromiumWebBrowser constructor overload with onAfterBrowserCreated delegate
Use the constructor overload as an alternative to the BrowserInitialized event Follow up to #3552
1 parent 2568236 commit 4ebe7de

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public partial class ChromiumWebBrowser : IRenderWebBrowser
4040
/// </summary>
4141
private bool browserCreated;
4242

43+
/// <summary>
44+
/// Action which is called immediately before the <see cref="BrowserInitialized"/> event after the
45+
/// uderlying Chromium Embedded Framework (CEF) browser has been created.
46+
/// </summary>
47+
private Action<IBrowser> onAfterBrowserCreatedDelegate;
48+
4349
/// <summary>
4450
/// Gets a value indicating whether this instance is disposed.
4551
/// </summary>
@@ -159,9 +165,15 @@ public bool IsBrowserInitialized
159165
/// <param name="browserSettings">The browser settings to use. If null, the default settings are used.</param>
160166
/// <param name="requestContext">See <see cref="RequestContext" /> for more details. Defaults to null</param>
161167
/// <param name="automaticallyCreateBrowser">automatically create the underlying Browser</param>
168+
/// <param name="onAfterBrowserCreated">
169+
/// Use as an alternative to the <see cref="BrowserInitialized"/> event. If the underlying Chromium Embedded Framework (CEF) browser is created successfully,
170+
/// this action is guranteed to be called after the browser created where as the <see cref="BrowserInitialized"/> event may be called before
171+
/// you have a chance to subscribe to the event as the CEF Browser is created async. (Issue https://github.com/cefsharp/CefSharp/issues/3552).
172+
/// </param>
162173
/// <exception cref="System.InvalidOperationException">Cef::Initialize() failed</exception>
163174
public ChromiumWebBrowser(HtmlString html, IBrowserSettings browserSettings = null,
164-
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true) : this(html.ToDataUriString(), browserSettings, requestContext, automaticallyCreateBrowser)
175+
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true,
176+
Action<IBrowser> onAfterBrowserCreated = null) : this(html.ToDataUriString(), browserSettings, requestContext, automaticallyCreateBrowser, onAfterBrowserCreated)
165177
{
166178
}
167179

@@ -176,9 +188,15 @@ public ChromiumWebBrowser(HtmlString html, IBrowserSettings browserSettings = nu
176188
/// <param name="browserSettings">The browser settings to use. If null, the default settings are used.</param>
177189
/// <param name="requestContext">See <see cref="RequestContext" /> for more details. Defaults to null</param>
178190
/// <param name="automaticallyCreateBrowser">automatically create the underlying Browser</param>
191+
/// <param name="onAfterBrowserCreated">
192+
/// Use as an alternative to the <see cref="BrowserInitialized"/> event. If the underlying Chromium Embedded Framework (CEF) browser is created successfully,
193+
/// this action is guranteed to be called after the browser created where as the <see cref="BrowserInitialized"/> event may be called before
194+
/// you have a chance to subscribe to the event as the CEF Browser is created async. (Issue https://github.com/cefsharp/CefSharp/issues/3552).
195+
/// </param>
179196
/// <exception cref="System.InvalidOperationException">Cef::Initialize() failed</exception>
180197
public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings = null,
181-
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true)
198+
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true,
199+
Action<IBrowser> onAfterBrowserCreated = null)
182200
{
183201
if (!Cef.IsInitialized)
184202
{
@@ -194,6 +212,7 @@ public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings
194212

195213
Cef.AddDisposable(this);
196214
Address = address;
215+
onAfterBrowserCreatedDelegate = onAfterBrowserCreated;
197216

198217
managedCefBrowserAdapter = ManagedCefBrowserAdapter.Create(this, true);
199218

@@ -658,6 +677,7 @@ void IRenderWebBrowser.OnVirtualKeyboardRequested(IBrowser browser, TextInputMod
658677
/// <param name="browser">The browser.</param>
659678
partial void OnAfterBrowserCreated(IBrowser browser)
660679
{
680+
onAfterBrowserCreatedDelegate?.Invoke(browser);
661681
BrowserInitialized?.Invoke(this, EventArgs.Empty);
662682
}
663683

0 commit comments

Comments
 (0)