Skip to content

Commit 05750f8

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

File tree

4 files changed

+123
-7
lines changed

4 files changed

+123
-7
lines changed

CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs

Lines changed: 55 additions & 2 deletions
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+
/// Update 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.UpdateRequestContextPreferences(int browserId, IDictionary<string, object> preferences)
144+
{
145+
var browser = _browsers.FirstOrDefault(x => x.Id == browserId);
146+
147+
CefThread.ExecuteOnUiThread(() =>
148+
{
149+
if (browser?.GetRequestContext() is IRequestContext requestContext)
150+
{
151+
foreach (KeyValuePair<string, object> pref in preferences)
152+
{
153+
requestContext?.SetPreference(pref.Key, pref.Value, out _);
154+
}
155+
}
156+
157+
return true;
158+
});
159+
}
160+
161+
/// <summary>
162+
/// Update Global Request Context Preferences for all browsers.
163+
/// </summary>
164+
/// <param name="preferences">The preferences.</param>
165+
void IOutOfProcessClientRpc.UpdateGlobalRequestContextPreferences(IDictionary<string, object> preferences)
166+
{
167+
CefThread.ExecuteOnUiThread(() =>
168+
{
169+
if (Cef.GetGlobalRequestContext() is IRequestContext requestContext)
170+
{
171+
foreach (KeyValuePair<string, object> pref in preferences)
172+
{
173+
requestContext?.SetPreference(pref.Key, pref.Value, out _);
174+
}
175+
}
176+
177+
return true;
178+
});
179+
}
127180
}
128181
}

CefSharp.OutOfProcess.Core/OutOfProcessHost.cs

Lines changed: 24 additions & 3 deletions
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+
/// Update 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 UpdateRequestContextPreferences(int browserId, IDictionary<string, object> preferences)
266+
{
267+
_outOfProcessClient.UpdateRequestContextPreferences(browserId, preferences);
268+
}
269+
270+
/// <summary>
271+
/// Update Global Request Context Preferences for all browsers.
272+
/// </summary>
273+
/// <param name="preferences">The preferences.</param>
274+
public void UpdateGlobalRequestContextPreferences(IDictionary<string, object> preferences)
275+
{
276+
_outOfProcessClient.UpdateGlobalRequestContextPreferences(preferences);
277+
}
257278
}
258279
}

CefSharp.OutOfProcess.Interface/IOutOfProcessClientRpc.cs

Lines changed: 16 additions & 1 deletion
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 UpdateRequestContextPreferences(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 UpdateGlobalRequestContextPreferences(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.Wpf.HwndHost/ChromiumWebBrowser.cs

Lines changed: 28 additions & 1 deletion
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
{
@@ -319,6 +320,18 @@ public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null)
319320
UseLayoutRounding = true;
320321
}
321322

323+
/// <summary>
324+
/// Initializes a new instance of the <see cref="ChromiumWebBrowser"/> instance.
325+
/// </summary>
326+
/// <param name="host">Out of process host</param>
327+
/// <param name="initialAddress">address to load initially</param>
328+
/// <param name="requestContextPreferences">requestContextPreferences to set</param>
329+
public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null, IDictionary<string, object> requestContextPreferences = null)
330+
: this(host, initialAddress)
331+
{
332+
this.requestContextPreferences = requestContextPreferences;
333+
}
334+
322335
/// <inheritdoc/>
323336
int IChromiumWebBrowserInternal.Id
324337
{
@@ -418,6 +431,15 @@ public Task<Response> GoForwardAsync(NavigationOptions options = null)
418431
return _devToolsContext.GoForwardAsync(options);
419432
}
420433

434+
/// <summary>
435+
/// Update Global Request Context Preferences for all browsers.
436+
/// </summary>
437+
/// <param name="preferences">The preferences.</param>
438+
public void UpdateRequestContextPreferences(IDictionary<string, object> preferences)
439+
{
440+
_host.UpdateRequestContextPreferences(this._id, preferences);
441+
}
442+
421443
private void PresentationSourceChangedHandler(object sender, SourceChangedEventArgs args)
422444
{
423445
if (args.NewSource != null)
@@ -492,7 +514,7 @@ protected override HandleRef BuildWindowCore(HandleRef hwndParent)
492514
0);
493515
}
494516

495-
_host.CreateBrowser(this, _hwndHost, url: _initialAddress, out _id);
517+
_host.CreateBrowser(this, _hwndHost, url: _initialAddress, out _id, requestContextPreferences);
496518

497519
_devToolsContextConnectionTransport = new OutOfProcessConnectionTransport(_id, _host);
498520

@@ -1070,6 +1092,11 @@ public IChromiumWebBrowser WebBrowser
10701092
public static readonly DependencyProperty WebBrowserProperty =
10711093
DependencyProperty.Register(nameof(WebBrowser), typeof(IChromiumWebBrowser), typeof(ChromiumWebBrowser), new UIPropertyMetadata(defaultValue: null));
10721094

1095+
/// <summary>
1096+
/// The requestContextPreferences used for the RequestContext.
1097+
/// </summary>
1098+
private readonly IDictionary<string, object> requestContextPreferences;
1099+
10731100
/// <summary>
10741101
/// Runs the specific Action on the Dispatcher in an async fashion
10751102
/// </summary>

0 commit comments

Comments
 (0)