diff --git a/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs b/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs index eb35359..1b52ae7 100644 --- a/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs +++ b/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs @@ -87,13 +87,23 @@ Task IOutOfProcessClientRpc.CloseHost() }); } - Task IOutOfProcessClientRpc.CreateBrowser(IntPtr parentHwnd, string url, int id) + Task IOutOfProcessClientRpc.CreateBrowser(IntPtr parentHwnd, string url, int id, IDictionary requestContextPreferences) { //Debugger.Break(); return CefThread.ExecuteOnUiThread(() => { - var browser = new OutOfProcessChromiumWebBrowser(_outOfProcessServer, id, url); + IRequestContext requestContext = null; + if (requestContextPreferences != null) + { + requestContext = new RequestContext(Cef.GetGlobalRequestContext()); + foreach (KeyValuePair pref in requestContextPreferences) + { + requestContext.SetPreference(pref.Key, pref.Value, out _); + } + } + + var browser = new OutOfProcessChromiumWebBrowser(_outOfProcessServer, id, url, requestContext); var windowInfo = new WindowInfo(); windowInfo.WindowName = "CefSharpBrowserProcess"; @@ -124,5 +134,45 @@ void IOutOfProcessClientRpc.SetFocus(int browserId, bool focus) browser?.GetBrowserHost().SetFocus(focus); } + + /// + /// Set Request Context Preferences of the browser. + /// + /// The browser id. + /// The preferences. + void IOutOfProcessClientRpc.SetRequestContextPreferences(int browserId, IDictionary preferences) + { + var browser = _browsers.FirstOrDefault(x => x.Id == browserId); + + if (browser?.GetRequestContext() is IRequestContext requestContext) + { + SetRequestContextPreferences(requestContext, preferences); + } + } + + /// + /// Set Global Request Context Preferences for all browsers. + /// + /// The preferences. + void IOutOfProcessClientRpc.SetGlobalRequestContextPreferences(IDictionary preferences) + { + if (Cef.GetGlobalRequestContext() is IRequestContext requestContext) + { + SetRequestContextPreferences(requestContext, preferences); + } + } + + void SetRequestContextPreferences(IRequestContext requestContext, IDictionary preferences) + { + _ = CefThread.ExecuteOnUiThread(() => + { + foreach (KeyValuePair pref in preferences) + { + requestContext.SetPreference(pref.Key, pref.Value, out _); + } + + return true; + }); + } } } diff --git a/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs b/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs index 1fec5c4..0f3a130 100644 --- a/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs +++ b/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs @@ -4,6 +4,7 @@ using StreamJsonRpc; using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Threading.Tasks; @@ -85,11 +86,12 @@ public string ChromiumVersion /// handle used to host the control /// /// + /// request context preference. /// - public bool CreateBrowser(IChromiumWebBrowserInternal browser, IntPtr handle, string url, out int id) + public bool CreateBrowser(IChromiumWebBrowserInternal browser, IntPtr handle, string url, out int id, IDictionary requestContextPreferences = null) { id = _browserIdentifier++; - _ = _outOfProcessClient.CreateBrowser(handle, url, id); + _ = _outOfProcessClient.CreateBrowser(handle, url, id, requestContextPreferences); return _browsers.TryAdd(id, browser); } @@ -253,6 +255,25 @@ public static Task CreateAsync(string path = HostExeName, stri host.Init(); return host.InitializedTask; - } + } + + /// + /// Set Request Context Preferences of the browser. + /// + /// The browser id. + /// The preferences. + public void SetRequestContextPreferences(int browserId, IDictionary preferences) + { + _outOfProcessClient.SetRequestContextPreferences(browserId, preferences); + } + + /// + /// Set Global Request Context Preferences for all browsers. + /// + /// The preferences. + public void SetGlobalRequestContextPreferences(IDictionary preferences) + { + _outOfProcessClient.SetGlobalRequestContextPreferences(preferences); + } } } diff --git a/CefSharp.OutOfProcess.Interface/IOutOfProcessClientRpc.cs b/CefSharp.OutOfProcess.Interface/IOutOfProcessClientRpc.cs index ffa40e8..af5c7a0 100644 --- a/CefSharp.OutOfProcess.Interface/IOutOfProcessClientRpc.cs +++ b/CefSharp.OutOfProcess.Interface/IOutOfProcessClientRpc.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using System.Collections.Generic; namespace CefSharp.OutOfProcess.Interface { @@ -35,8 +36,22 @@ public interface IOutOfProcessClientRpc /// parent Hwnd /// start url /// browser id + /// Request Context Preferences to set. /// Task - Task CreateBrowser(IntPtr parentHwnd, string url, int browserId); + Task CreateBrowser(IntPtr parentHwnd, string url, int browserId, IDictionary requestContextPreferences); + + /// + /// Modify RequestContext-Preferences for an existing browser. + /// + /// browser id + /// Request Context Preferences to set. + void SetRequestContextPreferences(int browserId, IDictionary requestContextPreferences); + + /// + /// Modify Global RequestContext-Preferences + /// + /// /// Request Context Preferences to set. + void SetGlobalRequestContextPreferences(IDictionary requestContextPreferences); /// /// Notify the browser that the window hosting it is about to be moved or resized. diff --git a/CefSharp.OutOfProcess.WinForms/ChromiumWebBrowser.cs b/CefSharp.OutOfProcess.WinForms/ChromiumWebBrowser.cs index a43100a..8d36a9b 100644 --- a/CefSharp.OutOfProcess.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.OutOfProcess.WinForms/ChromiumWebBrowser.cs @@ -5,6 +5,7 @@ using CefSharp.OutOfProcess.Internal; using CefSharp.Dom; using PInvoke; +using System.Collections.Generic; namespace CefSharp.OutOfProcess.WinForms { @@ -18,6 +19,11 @@ public class ChromiumWebBrowser : Control, IChromiumWebBrowserInternal private OutOfProcessConnectionTransport _devToolsContextConnectionTransport; private bool _devToolsReady; + /// + /// Contains the initial requests context preferences if any given in constructor. + /// + private IDictionary _requestContextPreferences; + /// public event EventHandler DOMContentLoaded; /// @@ -64,13 +70,15 @@ public class ChromiumWebBrowser : Control, IChromiumWebBrowserInternal /// /// Out of process host /// address that will be initially loaded in the browser - public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress) + /// requestContextPreferences to set + public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress, IDictionary requestContextPreferences = null) { if(host == null) { throw new ArgumentNullException(nameof(host)); } + _requestContextPreferences = requestContextPreferences; _host = host; _initialAddress = initialAddress; } @@ -128,7 +136,7 @@ protected override void OnHandleCreated(EventArgs e) var size = Size; - _host.CreateBrowser(this, Handle, url: _initialAddress, out _id); + _host.CreateBrowser(this, Handle, url: _initialAddress, out _id, _requestContextPreferences); _devToolsContextConnectionTransport = new OutOfProcessConnectionTransport(_id, _host); @@ -305,6 +313,15 @@ public Task GoBackAsync(NavigationOptions options = null) return _devToolsContext.GoBackAsync(options); } + /// + /// Set Request Context Preferences for this browser. + /// + /// The preferences. + public void SetRequestContextPreferences(IDictionary preferences) + { + _host.SetRequestContextPreferences(this._id, preferences); + } + /// public Task GoForwardAsync(NavigationOptions options = null) { diff --git a/CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs b/CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs index 93df485..8fd7fa0 100644 --- a/CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs +++ b/CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs @@ -17,6 +17,7 @@ using System.Windows.Interop; using System.Windows.Threading; using Window = System.Windows.Window; +using System.Collections.Generic; namespace CefSharp.OutOfProcess.Wpf.HwndHost { @@ -131,6 +132,11 @@ private static extern IntPtr CreateWindowEx(int dwExStyle, /// private bool _initialFocus; + /// + /// Contains the initial requests context preferences if any given in constructor. + /// + private readonly IDictionary _requestContextPreferences; + /// /// Activates browser upon creation, the default value is false. Prior to version 73 /// the default behaviour was to activate browser on creation (Equivilent of setting this property to true). @@ -279,13 +285,15 @@ public bool IsDisposed /// /// Out of process host /// address to load initially - public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null) + /// requestContextPreferences to set + public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null, IDictionary requestContextPreferences = null) { if(host == null) { throw new ArgumentNullException(nameof(host)); } + _requestContextPreferences = requestContextPreferences; _host = host; _initialAddress = initialAddress; @@ -418,6 +426,15 @@ public Task GoForwardAsync(NavigationOptions options = null) return _devToolsContext.GoForwardAsync(options); } + /// + /// Set Request Context Preferences for this browser. + /// + /// The preferences. + public void SetRequestContextPreferences(IDictionary preferences) + { + _host.SetRequestContextPreferences(this._id, preferences); + } + private void PresentationSourceChangedHandler(object sender, SourceChangedEventArgs args) { if (args.NewSource != null) @@ -492,7 +509,7 @@ protected override HandleRef BuildWindowCore(HandleRef hwndParent) 0); } - _host.CreateBrowser(this, _hwndHost, url: _initialAddress, out _id); + _host.CreateBrowser(this, _hwndHost, url: _initialAddress, out _id, _requestContextPreferences); _devToolsContextConnectionTransport = new OutOfProcessConnectionTransport(_id, _host);