Add RequestContext getter property to IWebBrowser

WPF - Add checks so if RequestContext is set after browser created an exception is thrown
WinForms - Add checks so if RequestContext is set after browser created an exception is thrown
This commit is contained in:
amaitland
2017-12-20 12:19:41 +10:00
parent e4dbc51635
commit d585348887
4 changed files with 69 additions and 8 deletions
+2 -2
View File
@@ -114,7 +114,7 @@ namespace CefSharp.OffScreen
/// Gets the request context.
/// </summary>
/// <value>The request context.</value>
public RequestContext RequestContext { get; private set; }
public IRequestContext RequestContext { get; private set; }
/// <summary>
/// Implement <see cref="IJsDialogHandler" /> and assign to handle events related to JavaScript Dialogs.
@@ -439,7 +439,7 @@ namespace CefSharp.OffScreen
browserCreated = true;
managedCefBrowserAdapter.CreateOffscreenBrowser(windowHandle, BrowserSettings, RequestContext, Address);
managedCefBrowserAdapter.CreateOffscreenBrowser(windowHandle, BrowserSettings, (RequestContext)RequestContext, Address);
}
/// <summary>
+34 -3
View File
@@ -40,6 +40,16 @@ namespace CefSharp.WinForms
/// A flag that indicates whether or not <see cref="InitializeFieldsAndCefIfRequired"/> has been called.
/// </summary>
private bool initialized;
/// <summary>
/// Has the underlying Cef Browser been created (slightly different to initliazed in that
/// the browser is initialized in an async fashion)
/// </summary>
private bool browserCreated;
/// <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>
private IRequestContext requestContext;
/// <summary>
/// Set to true while handing an activating WM_ACTIVATE message.
@@ -58,7 +68,23 @@ namespace CefSharp.WinForms
/// Gets or sets the request context.
/// </summary>
/// <value>The request context.</value>
public RequestContext RequestContext { get; set; }
public IRequestContext RequestContext
{
get { return requestContext; }
set
{
if (browserCreated)
{
throw new Exception("Browser has already been created. RequestContext must be" +
"set before the underlying CEF browser is created.");
}
if(value != null && value != typeof(RequestContext))
{
throw new Exception(string.Format("RequestContxt can only be of type {0} or null", typeof(RequestContext)));
}
requestContext = value;
}
}
/// <summary>
/// A flag that indicates whether the control is currently loading one or more web pages (true) or not (false).
/// </summary>
@@ -309,10 +335,13 @@ namespace CefSharp.WinForms
/// Initializes a new instance of the <see cref="ChromiumWebBrowser"/> class.
/// </summary>
/// <param name="address">The address.</param>
public ChromiumWebBrowser(string address)
/// <param name="requestContext">Request context that will be used for this browser instance,
/// if null the Global Request Context will be used</param>
public ChromiumWebBrowser(string address, IRequestContext requestContext = null)
{
Dock = DockStyle.Fill;
Address = address;
RequestContext = requestContext;
InitializeFieldsAndCefIfRequired();
}
@@ -519,9 +548,11 @@ namespace CefSharp.WinForms
[MethodImpl(MethodImplOptions.NoInlining)]
private void CreateBrowser()
{
browserCreated = true;
if (((IWebBrowserInternal)this).HasParent == false)
{
managedCefBrowserAdapter.CreateBrowser(BrowserSettings, RequestContext, Handle, Address);
managedCefBrowserAdapter.CreateBrowser(BrowserSettings, (RequestContext)requestContext, Handle, Address);
}
}
+25 -3
View File
@@ -55,7 +55,8 @@ namespace CefSharp.Wpf
/// </summary>
private bool ignoreUriChange;
/// <summary>
/// The browser created
/// Has the underlying Cef Browser been created (slightly different to initliazed in that
/// the browser is initialized in an async fashion)
/// </summary>
private bool browserCreated;
/// <summary>
@@ -89,6 +90,11 @@ namespace CefSharp.Wpf
/// and calling in a sync fashion on the UI thread was problematic.
/// </summary>
private Point browserScreenLocation;
/// <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>
private IRequestContext requestContext;
/// <summary>
/// A flag that indicates whether or not the designer is active
@@ -105,7 +111,23 @@ namespace CefSharp.Wpf
/// Gets or sets the request context.
/// </summary>
/// <value>The request context.</value>
public RequestContext RequestContext { get; set; }
public IRequestContext RequestContext
{
get { return requestContext; }
set
{
if (browserCreated)
{
throw new Exception("Browser has already been created. RequestContext must be" +
"set before the underlying CEF browser is created.");
}
if (value != null && value != typeof(RequestContext))
{
throw new Exception(string.Format("RequestContxt can only be of type {0} or null", typeof(RequestContext)));
}
requestContext = value;
}
}
/// <summary>
/// Implement <see cref="IDialogHandler" /> and assign to handle dialog events.
/// </summary>
@@ -1635,7 +1657,7 @@ namespace CefSharp.Wpf
var webBrowserInternal = this as IWebBrowserInternal;
if (!webBrowserInternal.HasParent)
{
managedCefBrowserAdapter.CreateOffscreenBrowser(source == null ? IntPtr.Zero : source.Handle, BrowserSettings, RequestContext, Address);
managedCefBrowserAdapter.CreateOffscreenBrowser(source == null ? IntPtr.Zero : source.Handle, BrowserSettings, (RequestContext)RequestContext, Address);
}
browserCreated = true;
+8
View File
@@ -243,6 +243,14 @@ namespace CefSharp
/// </summary>
bool CanExecuteJavascriptInMainFrame { get; }
/// <summary>
/// Gets the custom request context assigned to this browser instance
/// If no instance was assigned this will be null and the global
/// request context will have been used for this browser.
/// You can access the global request context through Cef.GetGlobalRequestContext()
/// </summary>
IRequestContext RequestContext { get; }
/// <summary>
/// Attempts to give focus to the IWebBrowser control.
/// </summary>