Skip to content

Commit 4f8bdc5

Browse files
committed
OffScreen - ChromeRuntime doesn't work with LoadURL
- After switching to the Chrome bootstrap, the browser wouldn't load if you passed in an empty string to the constructor, we can defer loading until after another LoadUrl call is made. Resolves #4832
1 parent 5b29591 commit 4f8bdc5

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

CefSharp.OffScreen/ChromiumWebBrowser.cs

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

44+
/// <summary>
45+
/// When an empty address is passed into the constructor, we defer browser creation
46+
/// until we have a Url.
47+
/// Issue https://github.com/cefsharp/CefSharp/issues/4832
48+
/// </summary>
49+
private bool createBrowserOnNextLoadUrlCall;
50+
/// <summary>
51+
/// BrowserSettings used by createBrowserOnNextLoadCall
52+
/// </summary>
53+
private IBrowserSettings browserSettings;
54+
4455
/// <summary>
4556
/// Action which is called immediately before the <see cref="BrowserInitialized"/> event after the
4657
/// uderlying Chromium Embedded Framework (CEF) browser has been created.
@@ -191,7 +202,10 @@ public ChromiumWebBrowser(HtmlString html, IBrowserSettings browserSettings = nu
191202
/// that you set <paramref name="automaticallyCreateBrowser"/> to false, subscribe to the event and then call <see cref="CreateBrowser(IWindowInfo, IBrowserSettings)"/>
192203
/// to ensure you are subscribe to the event before it's fired (Issue https://github.com/cefsharp/CefSharp/issues/3552).
193204
/// </summary>
194-
/// <param name="address">Initial address (url) to load</param>
205+
/// <param name="address">
206+
/// Initial address (url) to load. If <see cref="string.Empty"/> then cretion of the browser
207+
/// will be deferred until <see cref="LoadUrl(string)"/> is called with a non empty Url.
208+
/// </param>
195209
/// <param name="browserSettings">The browser settings to use. If null, the default settings are used.</param>
196210
/// <param name="requestContext">See <see cref="RequestContext" /> for more details. Defaults to null</param>
197211
/// <param name="automaticallyCreateBrowser">automatically create the underlying Browser</param>
@@ -230,7 +244,15 @@ public ChromiumWebBrowser(string address = "", IBrowserSettings browserSettings
230244

231245
if (automaticallyCreateBrowser)
232246
{
233-
CreateBrowser(null, browserSettings);
247+
if (string.IsNullOrEmpty(address))
248+
{
249+
createBrowserOnNextLoadUrlCall = true;
250+
this.browserSettings = browserSettings;
251+
}
252+
else
253+
{
254+
CreateBrowser(null, browserSettings);
255+
}
234256
}
235257

236258
if (useLegacyRenderHandler)
@@ -676,6 +698,16 @@ public void Load(string url)
676698
return;
677699
}
678700

701+
if (createBrowserOnNextLoadUrlCall)
702+
{
703+
createBrowserOnNextLoadUrlCall = false;
704+
Address = url;
705+
CreateBrowser(null, browserSettings);
706+
browserSettings = null;
707+
708+
return;
709+
}
710+
679711
//There's a small window here between CreateBrowser
680712
//and OnAfterBrowserCreated where the Address prop
681713
//will be updated, though LoadUrl won't be called.

CefSharp.Test/OffScreen/OffScreenBrowserTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ public async Task ShouldWorkWhenLoadingGoogle()
4444
}
4545
}
4646

47+
[Fact]
48+
public async Task ShouldWorkWhenPassingEmptyUrlToConstructor()
49+
{
50+
using (var browser = new ChromiumWebBrowser(string.Empty, useLegacyRenderHandler: false))
51+
{
52+
var response = await browser.LoadUrlAsync("www.google.com");
53+
var mainFrame = browser.GetMainFrame();
54+
55+
Assert.True(response.Success);
56+
Assert.True(mainFrame.IsValid);
57+
Assert.Contains("www.google", mainFrame.Url);
58+
Assert.Equal(200, response.HttpStatusCode);
59+
60+
output.WriteLine("Url {0}", mainFrame.Url);
61+
}
62+
}
63+
4764
[Theory(Skip = "Not working with Chrome bootstrap")]
4865
[InlineData("http://httpbin.org/post")]
4966
public async Task ShouldWorkWhenLoadingRequestWithPostData(string url)

0 commit comments

Comments
 (0)