-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
/// <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); | ||
} | ||
|
@@ -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); | ||
} | ||
} | ||
} |
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 | ||
{ | ||
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
/// <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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using CefSharp.OutOfProcess.Internal; | ||
using CefSharp.Dom; | ||
using PInvoke; | ||
using System.Collections.Generic; | ||
|
||
namespace CefSharp.OutOfProcess.WinForms | ||
{ | ||
|
@@ -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/> | ||
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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; | ||
} | ||
|
@@ -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); | ||
|
||
|
@@ -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) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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). | ||
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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; | ||
|
||
|
@@ -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) | ||
|
@@ -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); | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.