Skip to content

Commit b0ab809

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

File tree

4 files changed

+153
-37
lines changed

4 files changed

+153
-37
lines changed

CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs

Lines changed: 56 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,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: 29 additions & 8 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;
@@ -83,13 +84,14 @@ public string ChromiumVersion
8384
/// </summary>
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>
86-
/// <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)
87+
/// <param name="url"></param>
88+
/// <param name="id">id.</param>
89+
/// <param name="requestContextPreferences">request context preference.</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,28 @@ 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+
}
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: 52 additions & 25 deletions
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
{
@@ -281,7 +282,7 @@ public bool IsDisposed
281282
/// <param name="initialAddress">address to load initially</param>
282283
public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null)
283284
{
284-
if(host == null)
285+
if (host == null)
285286
{
286287
throw new ArgumentNullException(nameof(host));
287288
}
@@ -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

@@ -511,7 +533,7 @@ protected override void DestroyWindowCore(HandleRef hwnd)
511533
///<inheritdoc/>
512534
protected override bool TabIntoCore(TraversalRequest request)
513535
{
514-
if(InternalIsBrowserInitialized())
536+
if (InternalIsBrowserInitialized())
515537
{
516538
_host.SetFocus(_id, true);
517539

@@ -524,7 +546,7 @@ protected override bool TabIntoCore(TraversalRequest request)
524546
///<inheritdoc/>
525547
protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
526548
{
527-
if(!e.Handled)
549+
if (!e.Handled)
528550
{
529551
if (InternalIsBrowserInitialized())
530552
{
@@ -567,18 +589,18 @@ protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lP
567589
{
568590
case WM_SETFOCUS:
569591
case WM_MOUSEACTIVATE:
570-
{
571-
if(InternalIsBrowserInitialized())
572592
{
573-
574-
_host.SetFocus(_id, true);
593+
if (InternalIsBrowserInitialized())
594+
{
595+
596+
_host.SetFocus(_id, true);
575597

576-
handled = true;
598+
handled = true;
577599

578-
return IntPtr.Zero;
600+
return IntPtr.Zero;
601+
}
602+
break;
579603
}
580-
break;
581-
}
582604
}
583605
return base.WndProc(hwnd, msg, wParam, lParam, ref handled);
584606
}
@@ -727,8 +749,8 @@ void IChromiumWebBrowserInternal.OnAfterBrowserCreated(IntPtr hwnd)
727749
{
728750
return;
729751
}
730-
731-
_browserHwnd = hwnd;
752+
753+
_browserHwnd = hwnd;
732754

733755
Interlocked.Exchange(ref _browserInitialized, 1);
734756

@@ -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>
@@ -1215,22 +1242,22 @@ private void OnWindowStateChanged(object sender, EventArgs e)
12151242
{
12161243
case WindowState.Normal:
12171244
case WindowState.Maximized:
1218-
{
1219-
if (_previousWindowState == WindowState.Minimized && InternalIsBrowserInitialized())
12201245
{
1221-
ResizeBrowser((int)ActualWidth, (int)ActualHeight);
1246+
if (_previousWindowState == WindowState.Minimized && InternalIsBrowserInitialized())
1247+
{
1248+
ResizeBrowser((int)ActualWidth, (int)ActualHeight);
1249+
}
1250+
break;
12221251
}
1223-
break;
1224-
}
12251252
case WindowState.Minimized:
1226-
{
1227-
if (InternalIsBrowserInitialized())
12281253
{
1229-
//Set the browser size to 0,0 to reduce CPU usage
1230-
ResizeBrowser(0, 0);
1254+
if (InternalIsBrowserInitialized())
1255+
{
1256+
//Set the browser size to 0,0 to reduce CPU usage
1257+
ResizeBrowser(0, 0);
1258+
}
1259+
break;
12311260
}
1232-
break;
1233-
}
12341261
}
12351262

12361263
_previousWindowState = window.WindowState;

0 commit comments

Comments
 (0)