Skip to content
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

feat: allow defining RequestContext preferences #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jodett
Copy link

@jodett jodett commented Jan 23, 2023

Thanks for your great work.

For our case we would like to set some RequestContext properties for our browser instances.
We have the feeling others could use that feature too and would be happy to share if you agree.
It would be great if you could review it and if it meets your standards merge it.
If you have any suggestions, let us know so we can improve this PR.

Best Regards

@amaitland amaitland changed the title feat: allow defining RequestContext params feat: allow defining RequestContext preferences Jan 23, 2023
Copy link
Member

@amaitland amaitland left a comment

Choose a reason for hiding this comment

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

Thanks for the PR 👍

In future I'd ask that you perform your own review before submitting your Pull Request.
Make sure code formatting is consistent with current formatting, remove any formatting only or unrelated code changes (there are multiple in this case).

In terms of functionality it looks like you've only added to the WPF implementation, The WinForms implementation should also be updated to support the same functionality (no mention of this being WPF only in your comments or title).

@@ -44,7 +44,7 @@ protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if(disposing)
if (disposing)
Copy link
Member

Choose a reason for hiding this comment

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

In future please avoid formatting only changes.

Copy link
Author

@jodett jodett Jan 24, 2023

Choose a reason for hiding this comment

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

forgot to disable the autoformatter, pardon, should now be cleaned, i resolved all other formatting comments and left this one open for you to confirm it's ok now

CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs Outdated Show resolved Hide resolved
CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs Outdated Show resolved Hide resolved
CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs Outdated Show resolved Hide resolved
CefSharp.OutOfProcess.Wpf.HwndHost/ChromiumWebBrowser.cs Outdated Show resolved Hide resolved
/// Modify Global RequestContext-Preferences
/// </summary>
/// /// <param name="requestContextPreferences">Request Context Preferences to set.</param>
void UpdateGlobalRequestContextPreferences(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.

What's the use case for this method exactly?

Copy link
Author

Choose a reason for hiding this comment

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

we don't have one atm, just added it because CEF allows setting global requestContext preferences.
should i remove it?

/// </summary>
/// <param name="browserId">browser id</param>
/// <param name="requestContextPreferences">Request Context Preferences to set.</param>
void UpdateRequestContextPreferences(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.

Please rename method to SetRequestContextPreferences so it matches RequestContext.SetPreference naming.

Copy link
Author

Choose a reason for hiding this comment

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

done

{
foreach (KeyValuePair<string, object> pref in preferences)
{
requestContext?.SetPreference(pref.Key, pref.Value, out _);
Copy link
Member

Choose a reason for hiding this comment

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

There's no point looping through all the kvps in the dict if the request context is null.

Copy link
Author

Choose a reason for hiding this comment

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

i removed the null-conditional operator, because the "is"-clause makes sure we only proceed with a non-null requestContext

IRequestContext requestContext = null;
if (requestContextPreferences != null)
{
requestContext = new RequestContext();
Copy link
Member

Choose a reason for hiding this comment

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

Assigning a new RequestContext here dramatically changes the behaviour, you've not provided any way to manage the RequestContextSettings, so you'll always end up with an in memory cache.

This needs a rethink.

Copy link
Author

Choose a reason for hiding this comment

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

would it use the previous configured cefsettings if i use the globalrequestcontext values?
and therefore use the cachePath set there

@jodett jodett force-pushed the feat/requestContext branch 2 times, most recently from 0ecbeb9 to 87ca41d Compare January 24, 2023 07:21
@jodett jodett requested a review from amaitland January 25, 2023 06:30
@jodett
Copy link
Author

jodett commented Jan 31, 2023

Thanks for your reply.

Excuse my first attempt, i didn't expect such a fast response and should have opened it as a draft first.
I removed all styling-only changes.
I hope i have honored all your suggestions and would just need a decision if the methods manipulating the GlobalRequestContext should be removed.
We don't have a usecase for these but thought it would make the PR more feature-complete.
I also added the implementation for WinForms.

Awaiting your thoughts with Best Regards
Jo

Copy link
Member

@amaitland amaitland left a comment

Choose a reason for hiding this comment

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

See comments inline

@@ -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.

@@ -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.

@@ -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.

@@ -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.

/// </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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants