Skip to content

Commit 28b10a7

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 015d865 commit 28b10a7

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
@@ -51,6 +51,12 @@ public partial class ChromiumWebBrowser : IRenderWebBrowser
5151
/// </summary>
5252
private int disposeSignaled;
5353

54+
/// <summary>
55+
/// Action which is called immediately before the <see cref="BrowserInitialized"/> event after the
56+
/// uderlying Chromium Embedded Framework (CEF) browser has been created.
57+
/// </summary>
58+
private Action<IBrowser> onAfterBrowserCreatedDelegate;
59+
5460
/// <summary>
5561
/// Gets a value indicating whether this instance is disposed.
5662
/// </summary>
@@ -170,9 +176,15 @@ public bool IsBrowserInitialized
170176
/// <param name="browserSettings">The browser settings to use. If null, the default settings are used.</param>
171177
/// <param name="requestContext">See <see cref="RequestContext" /> for more details. Defaults to null</param>
172178
/// <param name="automaticallyCreateBrowser">automatically create the underlying Browser</param>
179+
/// <param name="onAfterBrowserCreated">
180+
/// Use as an alternative to the <see cref="BrowserInitialized"/> event. If the underlying Chromium Embedded Framework (CEF) browser is created successfully,
181+
/// this action is guranteed to be called after the browser created where as the <see cref="BrowserInitialized"/> event may be called before
182+
/// you have a chance to subscribe to the event as the CEF Browser is created async. (Issue https://github.com/cefsharp/CefSharp/issues/3552).
183+
/// </param>
173184
/// <exception cref="System.InvalidOperationException">Cef::Initialize() failed</exception>
174185
public ChromiumWebBrowser(HtmlString html, IBrowserSettings browserSettings = null,
175-
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true) : this(html.ToDataUriString(), browserSettings, requestContext, automaticallyCreateBrowser)
186+
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true,
187+
Action<IBrowser> onAfterBrowserCreated = null) : this(html.ToDataUriString(), browserSettings, requestContext, automaticallyCreateBrowser, onAfterBrowserCreated)
176188
{
177189
}
178190

@@ -187,9 +199,15 @@ public ChromiumWebBrowser(HtmlString html, IBrowserSettings browserSettings = nu
187199
/// <param name="browserSettings">The browser settings to use. If null, the default settings are used.</param>
188200
/// <param name="requestContext">See <see cref="RequestContext" /> for more details. Defaults to null</param>
189201
/// <param name="automaticallyCreateBrowser">automatically create the underlying Browser</param>
202+
/// <param name="onAfterBrowserCreated">
203+
/// Use as an alternative to the <see cref="BrowserInitialized"/> event. If the underlying Chromium Embedded Framework (CEF) browser is created successfully,
204+
/// this action is guranteed to be called after the browser created where as the <see cref="BrowserInitialized"/> event may be called before
205+
/// you have a chance to subscribe to the event as the CEF Browser is created async. (Issue https://github.com/cefsharp/CefSharp/issues/3552).
206+
/// </param>
190207
/// <exception cref="System.InvalidOperationException">Cef::Initialize() failed</exception>
191208
public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings = null,
192-
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true)
209+
IRequestContext requestContext = null, bool automaticallyCreateBrowser = true,
210+
Action<IBrowser> onAfterBrowserCreated = null)
193211
{
194212
if (!Cef.IsInitialized)
195213
{
@@ -205,6 +223,7 @@ public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings
205223

206224
Cef.AddDisposable(this);
207225
Address = address;
226+
onAfterBrowserCreatedDelegate = onAfterBrowserCreated;
208227

209228
managedCefBrowserAdapter = ManagedCefBrowserAdapter.Create(this, true);
210229

@@ -678,6 +697,7 @@ void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
678697

679698
Interlocked.Exchange(ref browserInitialized, 1);
680699

700+
onAfterBrowserCreatedDelegate?.Invoke(browser);
681701
BrowserInitialized?.Invoke(this, EventArgs.Empty);
682702
}
683703

0 commit comments

Comments
 (0)