Skip to content

Commit 1ff7d8a

Browse files
committed
feat: allow defining RequestContext params
1 parent cda6ce7 commit 1ff7d8a

File tree

5 files changed

+133
-10
lines changed

5 files changed

+133
-10
lines changed

CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs

+55-2
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,23 @@ Task IOutOfProcessClientRpc.CloseHost()
8787
});
8888
}
8989

90-
Task IOutOfProcessClientRpc.CreateBrowser(IntPtr parentHwnd, string url, int id)
90+
Task IOutOfProcessClientRpc.CreateBrowser(IntPtr parentHwnd, string url, int id, IDictionary<string, object> requestContextPreferences)
9191
{
9292
//Debugger.Break();
9393

9494
return CefThread.ExecuteOnUiThread(() =>
9595
{
96-
var browser = new OutOfProcessChromiumWebBrowser(_outOfProcessServer, id, url);
96+
IRequestContext requestContext = null;
97+
if (requestContextPreferences != null)
98+
{
99+
requestContext = new RequestContext();
100+
foreach (KeyValuePair<string, object> pref in requestContextPreferences)
101+
{
102+
requestContext.SetPreference(pref.Key, pref.Value, out _);
103+
}
104+
}
105+
106+
var browser = new OutOfProcessChromiumWebBrowser(_outOfProcessServer, id, url, requestContext);
97107

98108
var windowInfo = new WindowInfo();
99109
windowInfo.WindowName = "CefSharpBrowserProcess";
@@ -124,5 +134,48 @@ void IOutOfProcessClientRpc.SetFocus(int browserId, bool focus)
124134

125135
browser?.GetBrowserHost().SetFocus(focus);
126136
}
137+
138+
/// <summary>
139+
/// Set Request Context Preferences of the browser.
140+
/// </summary>
141+
/// <param name="browserId">The browser id.</param>
142+
/// <param name="preferences">The preferences.</param>
143+
void IOutOfProcessClientRpc.SetRequestContextPreferences(int browserId, IDictionary<string, object> preferences)
144+
{
145+
var browser = _browsers.FirstOrDefault(x => x.Id == browserId);
146+
147+
if (browser?.GetRequestContext() is IRequestContext requestContext)
148+
{
149+
CefThread.ExecuteOnUiThread(() =>
150+
{
151+
foreach (KeyValuePair<string, object> pref in preferences)
152+
{
153+
requestContext.SetPreference(pref.Key, pref.Value, out _);
154+
}
155+
156+
return true;
157+
});
158+
}
159+
}
160+
161+
/// <summary>
162+
/// Set Global Request Context Preferences for all browsers.
163+
/// </summary>
164+
/// <param name="preferences">The preferences.</param>
165+
void IOutOfProcessClientRpc.SetGlobalRequestContextPreferences(IDictionary<string, object> preferences)
166+
{
167+
if (Cef.GetGlobalRequestContext() is IRequestContext requestContext)
168+
{
169+
CefThread.ExecuteOnUiThread(() =>
170+
{
171+
foreach (KeyValuePair<string, object> pref in preferences)
172+
{
173+
requestContext.SetPreference(pref.Key, pref.Value, out _);
174+
}
175+
176+
return true;
177+
});
178+
}
179+
}
127180
}
128181
}

CefSharp.OutOfProcess.Core/OutOfProcessHost.cs

+24-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using StreamJsonRpc;
55
using System;
66
using System.Collections.Concurrent;
7+
using System.Collections.Generic;
78
using System.Diagnostics;
89
using System.IO;
910
using System.Threading.Tasks;
@@ -85,11 +86,12 @@ public string ChromiumVersion
8586
/// <param name="handle">handle used to host the control</param>
8687
/// <param name="url"></param>
8788
/// <param name="id"></param>
89+
/// <param name="requestContextPreferences">request context preference.</param>
8890
/// <returns></returns>
89-
public bool CreateBrowser(IChromiumWebBrowserInternal browser, IntPtr handle, string url, out int id)
91+
public bool CreateBrowser(IChromiumWebBrowserInternal browser, IntPtr handle, string url, out int id, IDictionary<string, object> requestContextPreferences = null)
9092
{
9193
id = _browserIdentifier++;
92-
_ = _outOfProcessClient.CreateBrowser(handle, url, id);
94+
_ = _outOfProcessClient.CreateBrowser(handle, url, id, requestContextPreferences);
9395

9496
return _browsers.TryAdd(id, browser);
9597
}
@@ -253,6 +255,25 @@ public static Task<OutOfProcessHost> CreateAsync(string path = HostExeName, stri
253255
host.Init();
254256

255257
return host.InitializedTask;
256-
}
258+
}
259+
260+
/// <summary>
261+
/// Set Request Context Preferences of the browser.
262+
/// </summary>
263+
/// <param name="browserId">The browser id.</param>
264+
/// <param name="preferences">The preferences.</param>
265+
public void SetRequestContextPreferences(int browserId, IDictionary<string, object> preferences)
266+
{
267+
_outOfProcessClient.SetRequestContextPreferences(browserId, preferences);
268+
}
269+
270+
/// <summary>
271+
/// Set Global Request Context Preferences for all browsers.
272+
/// </summary>
273+
/// <param name="preferences">The preferences.</param>
274+
public void SetGlobalRequestContextPreferences(IDictionary<string, object> preferences)
275+
{
276+
_outOfProcessClient.SetGlobalRequestContextPreferences(preferences);
277+
}
257278
}
258279
}

CefSharp.OutOfProcess.Interface/IOutOfProcessClientRpc.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Threading.Tasks;
3+
using System.Collections.Generic;
34

45
namespace CefSharp.OutOfProcess.Interface
56
{
@@ -35,8 +36,22 @@ public interface IOutOfProcessClientRpc
3536
/// <param name="parentHwnd">parent Hwnd</param>
3637
/// <param name="url">start url</param>
3738
/// <param name="browserId">browser id</param>
39+
/// <param name="requestContextPreferences">Request Context Preferences to set.</param>
3840
/// <returns>Task</returns>
39-
Task CreateBrowser(IntPtr parentHwnd, string url, int browserId);
41+
Task CreateBrowser(IntPtr parentHwnd, string url, int browserId, IDictionary<string, object> requestContextPreferences);
42+
43+
/// <summary>
44+
/// Modify RequestContext-Preferences for an existing browser.
45+
/// </summary>
46+
/// <param name="browserId">browser id</param>
47+
/// <param name="requestContextPreferences">Request Context Preferences to set.</param>
48+
void SetRequestContextPreferences(int browserId, IDictionary<string, object> requestContextPreferences);
49+
50+
/// <summary>
51+
/// Modify Global RequestContext-Preferences
52+
/// </summary>
53+
/// /// <param name="requestContextPreferences">Request Context Preferences to set.</param>
54+
void SetGlobalRequestContextPreferences(IDictionary<string, object> requestContextPreferences);
4055

4156
/// <summary>
4257
/// Notify the browser that the window hosting it is about to be moved or resized.

CefSharp.OutOfProcess.WinForms/ChromiumWebBrowser.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using CefSharp.OutOfProcess.Internal;
66
using CefSharp.Dom;
77
using PInvoke;
8+
using System.Collections.Generic;
89

910
namespace CefSharp.OutOfProcess.WinForms
1011
{
@@ -18,6 +19,11 @@ public class ChromiumWebBrowser : Control, IChromiumWebBrowserInternal
1819
private OutOfProcessConnectionTransport _devToolsContextConnectionTransport;
1920
private bool _devToolsReady;
2021

22+
/// <summary>
23+
/// Contains the initial requests context preferences if any given in constructor.
24+
/// </summary>
25+
private IDictionary<string, object> _requestContextPreferences;
26+
2127
/// <inheritdoc/>
2228
public event EventHandler DOMContentLoaded;
2329
/// <inheritdoc/>
@@ -64,13 +70,15 @@ public class ChromiumWebBrowser : Control, IChromiumWebBrowserInternal
6470
/// </summary>
6571
/// <param name="host">Out of process host</param>
6672
/// <param name="initialAddress">address that will be initially loaded in the browser</param>
67-
public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress)
73+
/// <param name="requestContextPreferences">requestContextPreferences to set</param>
74+
public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress, IDictionary<string, object> requestContextPreferences = null)
6875
{
6976
if(host == null)
7077
{
7178
throw new ArgumentNullException(nameof(host));
7279
}
7380

81+
_requestContextPreferences = requestContextPreferences;
7482
_host = host;
7583
_initialAddress = initialAddress;
7684
}
@@ -128,7 +136,7 @@ protected override void OnHandleCreated(EventArgs e)
128136

129137
var size = Size;
130138

131-
_host.CreateBrowser(this, Handle, url: _initialAddress, out _id);
139+
_host.CreateBrowser(this, Handle, url: _initialAddress, out _id, _requestContextPreferences);
132140

133141
_devToolsContextConnectionTransport = new OutOfProcessConnectionTransport(_id, _host);
134142

@@ -305,6 +313,15 @@ public Task<Response> GoBackAsync(NavigationOptions options = null)
305313
return _devToolsContext.GoBackAsync(options);
306314
}
307315

316+
/// <summary>
317+
/// Set Request Context Preferences for this browser.
318+
/// </summary>
319+
/// <param name="preferences">The preferences.</param>
320+
public void SetRequestContextPreferences(IDictionary<string, object> preferences)
321+
{
322+
_host.SetRequestContextPreferences(this._id, preferences);
323+
}
324+
308325
/// <inheritdoc/>
309326
public Task<Response> GoForwardAsync(NavigationOptions options = null)
310327
{

CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Windows.Interop;
1818
using System.Windows.Threading;
1919
using Window = System.Windows.Window;
20+
using System.Collections.Generic;
2021

2122
namespace CefSharp.OutOfProcess.Wpf.HwndHost
2223
{
@@ -131,6 +132,11 @@ private static extern IntPtr CreateWindowEx(int dwExStyle,
131132
/// </summary>
132133
private bool _initialFocus;
133134

135+
/// <summary>
136+
/// Contains the initial requests context preferences if any given in constructor.
137+
/// </summary>
138+
private readonly IDictionary<string, object> _requestContextPreferences;
139+
134140
/// <summary>
135141
/// Activates browser upon creation, the default value is false. Prior to version 73
136142
/// the default behaviour was to activate browser on creation (Equivilent of setting this property to true).
@@ -279,13 +285,15 @@ public bool IsDisposed
279285
/// </summary>
280286
/// <param name="host">Out of process host</param>
281287
/// <param name="initialAddress">address to load initially</param>
282-
public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null)
288+
/// <param name="requestContextPreferences">requestContextPreferences to set</param>
289+
public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null, IDictionary<string, object> requestContextPreferences = null)
283290
{
284291
if(host == null)
285292
{
286293
throw new ArgumentNullException(nameof(host));
287294
}
288295

296+
_requestContextPreferences = requestContextPreferences;
289297
_host = host;
290298
_initialAddress = initialAddress;
291299

@@ -418,6 +426,15 @@ public Task<Response> GoForwardAsync(NavigationOptions options = null)
418426
return _devToolsContext.GoForwardAsync(options);
419427
}
420428

429+
/// <summary>
430+
/// Set Request Context Preferences for this browser.
431+
/// </summary>
432+
/// <param name="preferences">The preferences.</param>
433+
public void SetRequestContextPreferences(IDictionary<string, object> preferences)
434+
{
435+
_host.SetRequestContextPreferences(this._id, preferences);
436+
}
437+
421438
private void PresentationSourceChangedHandler(object sender, SourceChangedEventArgs args)
422439
{
423440
if (args.NewSource != null)
@@ -492,7 +509,7 @@ protected override HandleRef BuildWindowCore(HandleRef hwndParent)
492509
0);
493510
}
494511

495-
_host.CreateBrowser(this, _hwndHost, url: _initialAddress, out _id);
512+
_host.CreateBrowser(this, _hwndHost, url: _initialAddress, out _id, _requestContextPreferences);
496513

497514
_devToolsContextConnectionTransport = new OutOfProcessConnectionTransport(_id, _host);
498515

0 commit comments

Comments
 (0)