diff --git a/CefSharp.Core/ManagedCefBrowserAdapter.cpp b/CefSharp.Core/ManagedCefBrowserAdapter.cpp index e5e02730c..ebd313959 100644 --- a/CefSharp.Core/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core/ManagedCefBrowserAdapter.cpp @@ -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>(requestContext))) { diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index a6c53beaa..1b4998887 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -52,7 +52,7 @@ namespace CefSharp.WinForms /// /// Browser initialization settings /// - private BrowserSettings browserSettings; + private IBrowserSettings browserSettings; /// /// 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 /// /// The browser settings. [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) { diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 6adb2b742..e17fc5242 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -97,7 +97,7 @@ namespace CefSharp.Wpf /// /// Browser initialization settings /// - private BrowserSettings browserSettings; + private IBrowserSettings browserSettings; /// /// 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. /// /// The browser settings. - 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. diff --git a/CefSharp/IBrowserSettings.cs b/CefSharp/IBrowserSettings.cs index ed46f528d..736f332c7 100644 --- a/CefSharp/IBrowserSettings.cs +++ b/CefSharp/IBrowserSettings.cs @@ -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 { /// /// Interface representing browser initialization settings. /// - public interface IBrowserSettings + public interface IBrowserSettings : IDisposable { /// /// StandardFontFamily