Skip to content

Commit df58aca

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

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected override void Dispose(bool disposing)
4444
{
4545
base.Dispose(disposing);
4646

47-
if(disposing)
47+
if (disposing)
4848
{
4949
_jsonRpc?.Dispose();
5050
_jsonRpc = null;
@@ -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,47 @@ void IOutOfProcessClientRpc.SetFocus(int browserId, bool focus)
124134

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

CefSharp.OutOfProcess.Core/OutOfProcessHost.cs

Lines changed: 9 additions & 7 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;
@@ -84,12 +85,13 @@ public string ChromiumVersion
8485
/// <param name="browser">The <see cref="IChromiumWebBrowserInternal"/> that will host the browser</param>
8586
/// <param name="handle">handle used to host the control</param>
8687
/// <param name="url"></param>
87-
/// <param name="id"></param>
88-
/// <returns></returns>
89-
public bool CreateBrowser(IChromiumWebBrowserInternal browser, IntPtr handle, string url, out int id)
88+
/// <param name="requestContextPreferences">request context preference.</param>
89+
/// <param name="id">id.</param>
90+
/// <returns>if created.</returns>
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
}
@@ -236,7 +238,7 @@ public void Dispose()
236238

237239
public static Task<OutOfProcessHost> CreateAsync(string path = HostExeName, string cachePath = null)
238240
{
239-
if(string.IsNullOrEmpty(path))
241+
if (string.IsNullOrEmpty(path))
240242
{
241243
throw new ArgumentNullException(nameof(path));
242244
}
@@ -250,9 +252,9 @@ public static Task<OutOfProcessHost> CreateAsync(string path = HostExeName, stri
250252

251253
var host = new OutOfProcessHost(fullPath, cachePath);
252254

253-
host.Init();
255+
host.Init();
254256

255257
return host.InitializedTask;
256-
}
258+
}
257259
}
258260
}

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.

0 commit comments

Comments
 (0)