From d5853488871acca696fe6e1e57a213e89db70a4e Mon Sep 17 00:00:00 2001 From: amaitland Date: Wed, 20 Dec 2017 12:19:41 +1000 Subject: [PATCH] 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 --- CefSharp.OffScreen/ChromiumWebBrowser.cs | 4 +-- CefSharp.WinForms/ChromiumWebBrowser.cs | 37 ++++++++++++++++++++++-- CefSharp.Wpf/ChromiumWebBrowser.cs | 28 ++++++++++++++++-- CefSharp/IWebBrowser.cs | 8 +++++ 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 01d850a11..19203d797 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -114,7 +114,7 @@ namespace CefSharp.OffScreen /// Gets the request context. /// /// The request context. - public RequestContext RequestContext { get; private set; } + public IRequestContext RequestContext { get; private set; } /// /// Implement 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); } /// diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 21a043207..e975435f8 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -40,6 +40,16 @@ namespace CefSharp.WinForms /// A flag that indicates whether or not has been called. /// private bool initialized; + /// + /// Has the underlying Cef Browser been created (slightly different to initliazed in that + /// the browser is initialized in an async fashion) + /// + private bool browserCreated; + /// + /// The request context (we deliberately use a private variable so we can throw an exception if + /// user attempts to set after browser created) + /// + private IRequestContext requestContext; /// /// Set to true while handing an activating WM_ACTIVATE message. @@ -58,7 +68,23 @@ namespace CefSharp.WinForms /// Gets or sets the request context. /// /// The request context. - 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; + } + } /// /// A flag that indicates whether the control is currently loading one or more web pages (true) or not (false). /// @@ -309,10 +335,13 @@ namespace CefSharp.WinForms /// Initializes a new instance of the class. /// /// The address. - public ChromiumWebBrowser(string address) + /// Request context that will be used for this browser instance, + /// if null the Global Request Context will be used + 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); } } diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index c8d0f2ed6..0479260da 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -55,7 +55,8 @@ namespace CefSharp.Wpf /// private bool ignoreUriChange; /// - /// The browser created + /// Has the underlying Cef Browser been created (slightly different to initliazed in that + /// the browser is initialized in an async fashion) /// private bool browserCreated; /// @@ -89,6 +90,11 @@ namespace CefSharp.Wpf /// and calling in a sync fashion on the UI thread was problematic. /// private Point browserScreenLocation; + /// + /// The request context (we deliberately use a private variable so we can throw an exception if + /// user attempts to set after browser created) + /// + private IRequestContext requestContext; /// /// A flag that indicates whether or not the designer is active @@ -105,7 +111,23 @@ namespace CefSharp.Wpf /// Gets or sets the request context. /// /// The request context. - 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; + } + } /// /// Implement and assign to handle dialog events. /// @@ -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; diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs index 32cf78a7d..7c47156ae 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -243,6 +243,14 @@ namespace CefSharp /// bool CanExecuteJavascriptInMainFrame { get; } + /// + /// 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() + /// + IRequestContext RequestContext { get; } + /// /// Attempts to give focus to the IWebBrowser control. ///