From 1ff7d8affa63845bff34fd8f4bab46d3d2f488cc Mon Sep 17 00:00:00 2001 From: joachimd Date: Tue, 24 Jan 2023 08:34:25 +0100 Subject: [PATCH 1/3] feat: allow defining RequestContext params --- .../BrowserProcessHandler.cs | 57 ++++++++++++++++++- .../OutOfProcessHost.cs | 27 ++++++++- .../IOutOfProcessClientRpc.cs | 17 +++++- .../ChromiumWebBrowser.cs | 21 ++++++- .../ChromiumWebBrowser.cs | 21 ++++++- 5 files changed, 133 insertions(+), 10 deletions(-) diff --git a/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs b/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs index eb35359..64234bc 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(); + 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,48 @@ 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) + { + CefThread.ExecuteOnUiThread(() => + { + foreach (KeyValuePair pref in preferences) + { + requestContext.SetPreference(pref.Key, pref.Value, out _); + } + + return true; + }); + } + } + + /// + /// Set Global Request Context Preferences for all browsers. + /// + /// The preferences. + void IOutOfProcessClientRpc.SetGlobalRequestContextPreferences(IDictionary preferences) + { + if (Cef.GetGlobalRequestContext() is IRequestContext requestContext) + { + 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); From 43b6af6419321be8b96de591c2ebeb2e955a53b3 Mon Sep 17 00:00:00 2001 From: joachimd Date: Tue, 24 Jan 2023 17:45:56 +0100 Subject: [PATCH 2/3] fix: use globalRequestcontext as template for configurable requestcontext per browser --- CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs b/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs index 64234bc..d38c852 100644 --- a/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs +++ b/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs @@ -96,7 +96,7 @@ Task IOutOfProcessClientRpc.CreateBrowser(IntPtr parentHwnd, string url, int id, IRequestContext requestContext = null; if (requestContextPreferences != null) { - requestContext = new RequestContext(); + requestContext = new RequestContext(Cef.GetGlobalRequestContext()); foreach (KeyValuePair pref in requestContextPreferences) { requestContext.SetPreference(pref.Key, pref.Value, out _); From 28d7c614f078b71211689e4a2b0b813aca57e62c Mon Sep 17 00:00:00 2001 From: joachimd Date: Wed, 25 Jan 2023 08:30:31 +0100 Subject: [PATCH 3/3] cleanup code --- .../BrowserProcessHandler.cs | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs b/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs index d38c852..1b52ae7 100644 --- a/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs +++ b/CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs @@ -146,15 +146,7 @@ void IOutOfProcessClientRpc.SetRequestContextPreferences(int browserId, IDiction if (browser?.GetRequestContext() is IRequestContext requestContext) { - CefThread.ExecuteOnUiThread(() => - { - foreach (KeyValuePair pref in preferences) - { - requestContext.SetPreference(pref.Key, pref.Value, out _); - } - - return true; - }); + SetRequestContextPreferences(requestContext, preferences); } } @@ -166,16 +158,21 @@ void IOutOfProcessClientRpc.SetGlobalRequestContextPreferences(IDictionary - { - foreach (KeyValuePair pref in preferences) - { - requestContext.SetPreference(pref.Key, pref.Value, out _); - } - - return true; - }); + 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; + }); + } } }