Skip to content

Commit 4d4ff02

Browse files
committed
allow setting brwoser request preferences
1 parent df58aca commit 4d4ff02

File tree

3 files changed

+70
-29
lines changed

3 files changed

+70
-29
lines changed

CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,17 @@ void IOutOfProcessClientRpc.SetFocus(int browserId, bool focus)
136136
}
137137

138138
/// <summary>
139+
/// Update Request Context Preferences of the browser.
140+
/// </summary>
139141
/// <param name="browserId">The browser id.</param>
140142
/// <param name="preferences">The preferences.</param>
141-
/// </summary>
142143
void IOutOfProcessClientRpc.UpdateRequestContextPreferences(int browserId, IDictionary<string, object> preferences)
143144
{
144145
var browser = _browsers.FirstOrDefault(x => x.Id == browserId);
145146

146147
CefThread.ExecuteOnUiThread(() =>
147148
{
148-
if (browser.GetRequestContext() is IRequestContext requestContext)
149+
if (browser?.GetRequestContext() is IRequestContext requestContext)
149150
{
150151
foreach (KeyValuePair<string, object> pref in preferences)
151152
{
@@ -158,9 +159,9 @@ void IOutOfProcessClientRpc.UpdateRequestContextPreferences(int browserId, IDict
158159
}
159160

160161
/// <summary>
161-
/// <param name="browserId">The browser id.</param>
162-
/// <param name="preferences">The preferences.</param>
162+
/// Update Global Request Context Preferences for all browsers.
163163
/// </summary>
164+
/// <param name="preferences">The preferences.</param>
164165
void IOutOfProcessClientRpc.UpdateGlobalRequestContextPreferences(IDictionary<string, object> preferences)
165166
{
166167
CefThread.ExecuteOnUiThread(() =>

CefSharp.OutOfProcess.Core/OutOfProcessHost.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,24 @@ public static Task<OutOfProcessHost> CreateAsync(string path = HostExeName, stri
256256

257257
return host.InitializedTask;
258258
}
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+
}
259278
}
260279
}

CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs

Lines changed: 46 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,12 @@ public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null)
319320
UseLayoutRounding = true;
320321
}
321322

323+
public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null, IDictionary<string, object> requestContextPreferences = null)
324+
: this(host, initialAddress)
325+
{
326+
this.requestContextPreferences = requestContextPreferences;
327+
}
328+
322329
/// <inheritdoc/>
323330
int IChromiumWebBrowserInternal.Id
324331
{
@@ -418,6 +425,15 @@ public Task<Response> GoForwardAsync(NavigationOptions options = null)
418425
return _devToolsContext.GoForwardAsync(options);
419426
}
420427

428+
/// <summary>
429+
/// Update Global Request Context Preferences for all browsers.
430+
/// </summary>
431+
/// <param name="preferences">The preferences.</param>
432+
public void UpdateRequestContextPreferences(IDictionary<string, object> preferences)
433+
{
434+
_host.UpdateRequestContextPreferences(this._id, preferences);
435+
}
436+
421437
private void PresentationSourceChangedHandler(object sender, SourceChangedEventArgs args)
422438
{
423439
if (args.NewSource != null)
@@ -492,7 +508,7 @@ protected override HandleRef BuildWindowCore(HandleRef hwndParent)
492508
0);
493509
}
494510

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

497513
_devToolsContextConnectionTransport = new OutOfProcessConnectionTransport(_id, _host);
498514

@@ -511,7 +527,7 @@ protected override void DestroyWindowCore(HandleRef hwnd)
511527
///<inheritdoc/>
512528
protected override bool TabIntoCore(TraversalRequest request)
513529
{
514-
if(InternalIsBrowserInitialized())
530+
if (InternalIsBrowserInitialized())
515531
{
516532
_host.SetFocus(_id, true);
517533

@@ -524,7 +540,7 @@ protected override bool TabIntoCore(TraversalRequest request)
524540
///<inheritdoc/>
525541
protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
526542
{
527-
if(!e.Handled)
543+
if (!e.Handled)
528544
{
529545
if (InternalIsBrowserInitialized())
530546
{
@@ -567,18 +583,18 @@ protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lP
567583
{
568584
case WM_SETFOCUS:
569585
case WM_MOUSEACTIVATE:
570-
{
571-
if(InternalIsBrowserInitialized())
572586
{
573-
574-
_host.SetFocus(_id, true);
587+
if (InternalIsBrowserInitialized())
588+
{
589+
590+
_host.SetFocus(_id, true);
575591

576-
handled = true;
592+
handled = true;
577593

578-
return IntPtr.Zero;
594+
return IntPtr.Zero;
595+
}
596+
break;
579597
}
580-
break;
581-
}
582598
}
583599
return base.WndProc(hwnd, msg, wParam, lParam, ref handled);
584600
}
@@ -727,8 +743,8 @@ void IChromiumWebBrowserInternal.OnAfterBrowserCreated(IntPtr hwnd)
727743
{
728744
return;
729745
}
730-
731-
_browserHwnd = hwnd;
746+
747+
_browserHwnd = hwnd;
732748

733749
Interlocked.Exchange(ref _browserInitialized, 1);
734750

@@ -1070,6 +1086,11 @@ public IChromiumWebBrowser WebBrowser
10701086
public static readonly DependencyProperty WebBrowserProperty =
10711087
DependencyProperty.Register(nameof(WebBrowser), typeof(IChromiumWebBrowser), typeof(ChromiumWebBrowser), new UIPropertyMetadata(defaultValue: null));
10721088

1089+
/// <summary>
1090+
/// The requestContextPreferences used for the RequestContext.
1091+
/// </summary>
1092+
private readonly IDictionary<string, object> requestContextPreferences;
1093+
10731094
/// <summary>
10741095
/// Runs the specific Action on the Dispatcher in an async fashion
10751096
/// </summary>
@@ -1215,22 +1236,22 @@ private void OnWindowStateChanged(object sender, EventArgs e)
12151236
{
12161237
case WindowState.Normal:
12171238
case WindowState.Maximized:
1218-
{
1219-
if (_previousWindowState == WindowState.Minimized && InternalIsBrowserInitialized())
12201239
{
1221-
ResizeBrowser((int)ActualWidth, (int)ActualHeight);
1240+
if (_previousWindowState == WindowState.Minimized && InternalIsBrowserInitialized())
1241+
{
1242+
ResizeBrowser((int)ActualWidth, (int)ActualHeight);
1243+
}
1244+
break;
12221245
}
1223-
break;
1224-
}
12251246
case WindowState.Minimized:
1226-
{
1227-
if (InternalIsBrowserInitialized())
12281247
{
1229-
//Set the browser size to 0,0 to reduce CPU usage
1230-
ResizeBrowser(0, 0);
1248+
if (InternalIsBrowserInitialized())
1249+
{
1250+
//Set the browser size to 0,0 to reduce CPU usage
1251+
ResizeBrowser(0, 0);
1252+
}
1253+
break;
12311254
}
1232-
break;
1233-
}
12341255
}
12351256

12361257
_previousWindowState = window.WindowState;

0 commit comments

Comments
 (0)