Skip to content

feat: allow defining RequestContext preferences #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,23 @@ Task IOutOfProcessClientRpc.CloseHost()
});
}

Task IOutOfProcessClientRpc.CreateBrowser(IntPtr parentHwnd, string url, int id)
Task IOutOfProcessClientRpc.CreateBrowser(IntPtr parentHwnd, string url, int id, IDictionary<string, object> requestContextPreferences)
{
//Debugger.Break();

return CefThread.ExecuteOnUiThread(() =>
{
var browser = new OutOfProcessChromiumWebBrowser(_outOfProcessServer, id, url);
IRequestContext requestContext = null;
if (requestContextPreferences != null)
{
requestContext = new RequestContext(Cef.GetGlobalRequestContext());
foreach (KeyValuePair<string, object> pref in requestContextPreferences)
{
requestContext.SetPreference(pref.Key, pref.Value, out _);
}
}

var browser = new OutOfProcessChromiumWebBrowser(_outOfProcessServer, id, url, requestContext);

var windowInfo = new WindowInfo();
windowInfo.WindowName = "CefSharpBrowserProcess";
Expand Down Expand Up @@ -124,5 +134,45 @@ void IOutOfProcessClientRpc.SetFocus(int browserId, bool focus)

browser?.GetBrowserHost().SetFocus(focus);
}

/// <summary>
/// Set Request Context Preferences of the browser.
/// </summary>
/// <param name="browserId">The browser id.</param>
/// <param name="preferences">The preferences.</param>
void IOutOfProcessClientRpc.SetRequestContextPreferences(int browserId, IDictionary<string, object> preferences)
{
var browser = _browsers.FirstOrDefault(x => x.Id == browserId);

if (browser?.GetRequestContext() is IRequestContext requestContext)
{
SetRequestContextPreferences(requestContext, preferences);
}
}

/// <summary>
/// Set Global Request Context Preferences for all browsers.
/// </summary>
/// <param name="preferences">The preferences.</param>
void IOutOfProcessClientRpc.SetGlobalRequestContextPreferences(IDictionary<string, object> preferences)
{
if (Cef.GetGlobalRequestContext() is IRequestContext requestContext)
{
SetRequestContextPreferences(requestContext, preferences);
}
}

void SetRequestContextPreferences(IRequestContext requestContext, IDictionary<string, object> preferences)
{
_ = CefThread.ExecuteOnUiThread(() =>
{
foreach (KeyValuePair<string, object> pref in preferences)
{
requestContext.SetPreference(pref.Key, pref.Value, out _);
}

return true;
});
}
}
}
27 changes: 24 additions & 3 deletions CefSharp.OutOfProcess.Core/OutOfProcessHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using StreamJsonRpc;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
Expand Down Expand Up @@ -85,11 +86,12 @@ public string ChromiumVersion
/// <param name="handle">handle used to host the control</param>
/// <param name="url"></param>
/// <param name="id"></param>
/// <param name="requestContextPreferences">request context preference.</param>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment needs to be updated to describe the actual behaviour, a new RequestContext sharing the same settings as the Global.

/// <returns></returns>
public bool CreateBrowser(IChromiumWebBrowserInternal browser, IntPtr handle, string url, out int id)
public bool CreateBrowser(IChromiumWebBrowserInternal browser, IntPtr handle, string url, out int id, IDictionary<string, object> requestContextPreferences = null)
{
id = _browserIdentifier++;
_ = _outOfProcessClient.CreateBrowser(handle, url, id);
_ = _outOfProcessClient.CreateBrowser(handle, url, id, requestContextPreferences);

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

return host.InitializedTask;
}
}

/// <summary>
/// Set Request Context Preferences of the browser.
/// </summary>
/// <param name="browserId">The browser id.</param>
/// <param name="preferences">The preferences.</param>
public void SetRequestContextPreferences(int browserId, IDictionary<string, object> preferences)
{
_outOfProcessClient.SetRequestContextPreferences(browserId, preferences);
}

/// <summary>
/// Set Global Request Context Preferences for all browsers.
/// </summary>
/// <param name="preferences">The preferences.</param>
public void SetGlobalRequestContextPreferences(IDictionary<string, object> preferences)
{
_outOfProcessClient.SetGlobalRequestContextPreferences(preferences);
}
}
}
17 changes: 16 additions & 1 deletion CefSharp.OutOfProcess.Interface/IOutOfProcessClientRpc.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace CefSharp.OutOfProcess.Interface
{
Expand Down Expand Up @@ -35,8 +36,22 @@ public interface IOutOfProcessClientRpc
/// <param name="parentHwnd">parent Hwnd</param>
/// <param name="url">start url</param>
/// <param name="browserId">browser id</param>
/// <param name="requestContextPreferences">Request Context Preferences to set.</param>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment needs to be updated to describe the actual behaviour, a new RequestContext sharing the same settings as the Global.

/// <returns>Task</returns>
Task CreateBrowser(IntPtr parentHwnd, string url, int browserId);
Task CreateBrowser(IntPtr parentHwnd, string url, int browserId, IDictionary<string, object> requestContextPreferences);

/// <summary>
/// Modify RequestContext-Preferences for an existing browser.
/// </summary>
/// <param name="browserId">browser id</param>
/// <param name="requestContextPreferences">Request Context Preferences to set.</param>
void SetRequestContextPreferences(int browserId, IDictionary<string, object> requestContextPreferences);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How often do you need to set multiple preferences at once? If I'm honest I don't love this. There's no error reporting, absolutely no idea if the preferences were set.

Personally I'd prefer to see something like the following where you get the bool and error string returned from the SetPreference call.

Task<SetRequestContextPreferenceResponse> SetRequestContextPreferenceAsync(int browserId, string name, object value);


/// <summary>
/// Modify Global RequestContext-Preferences
/// </summary>
/// /// <param name="requestContextPreferences">Request Context Preferences to set.</param>
void SetGlobalRequestContextPreferences(IDictionary<string, object> requestContextPreferences);

/// <summary>
/// Notify the browser that the window hosting it is about to be moved or resized.
Expand Down
21 changes: 19 additions & 2 deletions CefSharp.OutOfProcess.WinForms/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using CefSharp.OutOfProcess.Internal;
using CefSharp.Dom;
using PInvoke;
using System.Collections.Generic;

namespace CefSharp.OutOfProcess.WinForms
{
Expand All @@ -18,6 +19,11 @@ public class ChromiumWebBrowser : Control, IChromiumWebBrowserInternal
private OutOfProcessConnectionTransport _devToolsContextConnectionTransport;
private bool _devToolsReady;

/// <summary>
/// Contains the initial requests context preferences if any given in constructor.
/// </summary>
private IDictionary<string, object> _requestContextPreferences;

/// <inheritdoc/>
public event EventHandler DOMContentLoaded;
/// <inheritdoc/>
Expand Down Expand Up @@ -64,13 +70,15 @@ public class ChromiumWebBrowser : Control, IChromiumWebBrowserInternal
/// </summary>
/// <param name="host">Out of process host</param>
/// <param name="initialAddress">address that will be initially loaded in the browser</param>
public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress)
/// <param name="requestContextPreferences">requestContextPreferences to set</param>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment needs to be updated to describe the actual behaviour, a new RequestContext sharing the same settings as the Global.

public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress, IDictionary<string, object> requestContextPreferences = null)
{
if(host == null)
{
throw new ArgumentNullException(nameof(host));
}

_requestContextPreferences = requestContextPreferences;
_host = host;
_initialAddress = initialAddress;
}
Expand Down Expand Up @@ -128,7 +136,7 @@ protected override void OnHandleCreated(EventArgs e)

var size = Size;

_host.CreateBrowser(this, Handle, url: _initialAddress, out _id);
_host.CreateBrowser(this, Handle, url: _initialAddress, out _id, _requestContextPreferences);

_devToolsContextConnectionTransport = new OutOfProcessConnectionTransport(_id, _host);

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

/// <summary>
/// Set Request Context Preferences for this browser.
/// </summary>
/// <param name="preferences">The preferences.</param>
public void SetRequestContextPreferences(IDictionary<string, object> preferences)
{
_host.SetRequestContextPreferences(this._id, preferences);
}

/// <inheritdoc/>
public Task<Response> GoForwardAsync(NavigationOptions options = null)
{
Expand Down
21 changes: 19 additions & 2 deletions CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Windows.Interop;
using System.Windows.Threading;
using Window = System.Windows.Window;
using System.Collections.Generic;

namespace CefSharp.OutOfProcess.Wpf.HwndHost
{
Expand Down Expand Up @@ -131,6 +132,11 @@ private static extern IntPtr CreateWindowEx(int dwExStyle,
/// </summary>
private bool _initialFocus;

/// <summary>
/// Contains the initial requests context preferences if any given in constructor.
/// </summary>
private readonly IDictionary<string, object> _requestContextPreferences;

/// <summary>
/// Activates browser upon creation, the default value is false. Prior to version 73
/// the default behaviour was to activate browser on creation (Equivilent of setting this property to true).
Expand Down Expand Up @@ -279,13 +285,15 @@ public bool IsDisposed
/// </summary>
/// <param name="host">Out of process host</param>
/// <param name="initialAddress">address to load initially</param>
public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null)
/// <param name="requestContextPreferences">requestContextPreferences to set</param>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment needs to be updated to describe the actual behaviour, a new RequestContext sharing the same settings as the Global.

public ChromiumWebBrowser(OutOfProcessHost host, string initialAddress = null, IDictionary<string, object> requestContextPreferences = null)
{
if(host == null)
{
throw new ArgumentNullException(nameof(host));
}

_requestContextPreferences = requestContextPreferences;
_host = host;
_initialAddress = initialAddress;

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

/// <summary>
/// Set Request Context Preferences for this browser.
/// </summary>
/// <param name="preferences">The preferences.</param>
public void SetRequestContextPreferences(IDictionary<string, object> preferences)
{
_host.SetRequestContextPreferences(this._id, preferences);
}

private void PresentationSourceChangedHandler(object sender, SourceChangedEventArgs args)
{
if (args.NewSource != null)
Expand Down Expand Up @@ -492,7 +509,7 @@ protected override HandleRef BuildWindowCore(HandleRef hwndParent)
0);
}

_host.CreateBrowser(this, _hwndHost, url: _initialAddress, out _id);
_host.CreateBrowser(this, _hwndHost, url: _initialAddress, out _id, _requestContextPreferences);

_devToolsContextConnectionTransport = new OutOfProcessConnectionTransport(_id, _host);

Expand Down