WinForms - Dispose of BrowserSettings after browser created

BrowserSettings must be set before the call to CefBrowserHost::CreateBrowser for this reason we enforce these checks.
Throw exception if attempt to set BrowserSettings after browser has been created

Initially I was going to make BrowserSettings a private set, unfortunately that probably won't work if you are using the designer
as there would be no way to instantiate a new instance. It was possible to lazy initialize in the get method, that doesn't really provide
a descriptive error message, I could have updated the documentation to describe the behaviour, unfortunately people just don't really the docs.
This commit is contained in:
amaitland
2019-01-09 17:12:28 +10:00
parent e0bab6fad2
commit ee666b3cb6
+24 -2
View File
@@ -50,6 +50,10 @@ namespace CefSharp.WinForms
/// </summary>
private bool browserCreated;
/// <summary>
/// Browser initialization settings
/// </summary>
private BrowserSettings browserSettings;
/// <summary>
/// The request context (we deliberately use a private variable so we can throw an exception if
/// user attempts to set after browser created)
/// </summary>
@@ -68,7 +72,19 @@ namespace CefSharp.WinForms
/// </summary>
/// <value>The browser settings.</value>
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DefaultValue(null)]
public BrowserSettings BrowserSettings { get; set; }
public BrowserSettings BrowserSettings
{
get { return browserSettings; }
set
{
if (browserCreated)
{
throw new Exception("Browser has already been created. BrowserSettings must be" +
"set before the underlying CEF browser is created.");
}
browserSettings = value;
}
}
/// <summary>
/// Gets or sets the request context.
/// </summary>
@@ -624,7 +640,13 @@ namespace CefSharp.WinForms
var windowInfo = new WindowInfo();
windowInfo.SetAsChild(Handle);
managedCefBrowserAdapter.CreateBrowser(windowInfo, BrowserSettings, (RequestContext)RequestContext, Address);
managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings, (RequestContext)RequestContext, Address);
if (browserSettings != null)
{
browserSettings.Dispose();
browserSettings = null;
}
}
else
{