WinForms/WPF - Change browserSettings to use the private field to avoid throw exception in dispose

- Changed to expose only the public interface (this is better for scenarios where the designer attempts to query the property and subsequently attempts to load libcef and fails).
- Make IBrowserSettings implement IDisposable
- ManagedCefBrowserAdapter::CreateBrowser now throws exception if browserSettings param is null

The WPF version won't actually throw an exception, just standardised it's code so they're both the same.

Issue #2638
This commit is contained in:
amaitland
2019-02-08 10:14:32 +10:00
parent 3c650a0121
commit 2c9155f1cb
4 changed files with 24 additions and 13 deletions
@@ -23,6 +23,11 @@ void ManagedCefBrowserAdapter::CreateBrowser(IWindowInfo^ windowInfo, BrowserSet
CefString addressNative = StringUtils::ToNative(address);
if (browserSettings == nullptr)
{
throw gcnew ArgumentNullException("browserSettings", "cannot be null");
}
if (!CefBrowserHost::CreateBrowser(*cefWindowInfoWrapper->GetWindowInfo(), _clientAdapter.get(), addressNative,
*browserSettings->_browserSettings, static_cast<CefRefPtr<CefRequestContext>>(requestContext)))
{
+12 -8
View File
@@ -52,7 +52,7 @@ namespace CefSharp.WinForms
/// <summary>
/// Browser initialization settings
/// </summary>
private BrowserSettings browserSettings;
private IBrowserSettings 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)
@@ -72,7 +72,7 @@ namespace CefSharp.WinForms
/// </summary>
/// <value>The browser settings.</value>
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DefaultValue(null)]
public BrowserSettings BrowserSettings
public IBrowserSettings BrowserSettings
{
get { return browserSettings; }
set
@@ -82,6 +82,10 @@ namespace CefSharp.WinForms
throw new Exception("Browser has already been created. BrowserSettings must be " +
"set before the underlying CEF browser is created.");
}
if (value != null && value.GetType() != typeof(BrowserSettings))
{
throw new Exception(string.Format("BrowserSettings can only be of type {0} or null", typeof(BrowserSettings)));
}
browserSettings = value;
}
}
@@ -415,9 +419,9 @@ namespace CefSharp.WinForms
ResourceHandlerFactory = new DefaultResourceHandlerFactory();
}
if (BrowserSettings == null)
if (browserSettings == null)
{
BrowserSettings = new BrowserSettings();
browserSettings = new BrowserSettings();
}
managedCefBrowserAdapter = new ManagedCefBrowserAdapter(this, false);
@@ -489,10 +493,10 @@ namespace CefSharp.WinForms
parentFormMessageInterceptor = null;
}
if (BrowserSettings != null)
if (browserSettings != null)
{
BrowserSettings.Dispose();
BrowserSettings = null;
browserSettings.Dispose();
browserSettings = null;
}
if (managedCefBrowserAdapter != null)
@@ -640,7 +644,7 @@ namespace CefSharp.WinForms
var windowInfo = new WindowInfo();
windowInfo.SetAsChild(Handle);
managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings, (RequestContext)RequestContext, Address);
managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings as BrowserSettings, requestContext as RequestContext, Address);
if (browserSettings != null)
{
+4 -4
View File
@@ -97,7 +97,7 @@ namespace CefSharp.Wpf
/// <summary>
/// Browser initialization settings
/// </summary>
private BrowserSettings browserSettings;
private IBrowserSettings 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)
@@ -124,7 +124,7 @@ namespace CefSharp.Wpf
/// Gets or sets the browser settings.
/// </summary>
/// <value>The browser settings.</value>
public BrowserSettings BrowserSettings
public IBrowserSettings BrowserSettings
{
get { return browserSettings; }
set
@@ -517,7 +517,7 @@ namespace CefSharp.Wpf
managedCefBrowserAdapter = new ManagedCefBrowserAdapter(this, true);
ResourceHandlerFactory = new DefaultResourceHandlerFactory();
BrowserSettings = new BrowserSettings();
browserSettings = new BrowserSettings();
RenderHandler = new InteropBitmapRenderHandler();
WpfKeyboardHandler = new WpfKeyboardHandler(this);
@@ -1748,7 +1748,7 @@ namespace CefSharp.Wpf
var windowInfo = CreateOffscreenBrowserWindowInfo(source == null ? IntPtr.Zero : source.Handle);
//Pass null in for Address and rely on Load being called in OnAfterBrowserCreated
//Workaround for issue https://github.com/cefsharp/CefSharp/issues/2300
managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings, (RequestContext)RequestContext, address: null);
managedCefBrowserAdapter.CreateBrowser(windowInfo, browserSettings as BrowserSettings, requestContext as RequestContext, address: null);
//Dispose of BrowserSettings as they shouldn't be reused ans it's not possible to change the settings
//after the browser has been created.
+3 -1
View File
@@ -2,12 +2,14 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
namespace CefSharp
{
/// <summary>
/// Interface representing browser initialization settings.
/// </summary>
public interface IBrowserSettings
public interface IBrowserSettings : IDisposable
{
/// <summary>
/// StandardFontFamily