Skip to content

Commit 0076eb0

Browse files
committed
Add SetRequestContextPreferenceAsync
- Ability to set RequestContext preference - Upgrade StreamJsonRpc version
1 parent 8dd1a1c commit 0076eb0

File tree

17 files changed

+352
-225
lines changed

17 files changed

+352
-225
lines changed

CefSharp.OutOfProcess.BrowserProcess/BrowserProcessHandler.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using System;
55
using System.Linq;
66
using System.Collections.Generic;
7-
using System.Diagnostics;
8-
using CefSharp.OutOfProcess.Interface;
97
using System.Threading.Tasks;
8+
using CefSharp.OutOfProcess.Model;
9+
using System.Diagnostics;
1010

1111
namespace CefSharp.OutOfProcess.BrowserProcess
1212
{
@@ -124,5 +124,46 @@ void IOutOfProcessClientRpc.SetFocus(int browserId, bool focus)
124124

125125
browser?.GetBrowserHost().SetFocus(focus);
126126
}
127+
128+
/// <inheritdoc />
129+
Task<SetPreferenceResponse> IOutOfProcessClientRpc.SetRequestContextPreferenceAsync(int browserId, string name, object value)
130+
{
131+
//Debugger.Break();
132+
133+
if (CefThread.HasShutdown)
134+
{
135+
return Task.FromResult(new SetPreferenceResponse(false, "Cef.Shutdown has already been called, it is no longer possible to call SetPreferenceAsync."));
136+
}
137+
138+
var browser = _browsers.FirstOrDefault(x => x.Id == browserId);
139+
140+
if (browser == null)
141+
{
142+
return Task.FromResult(new SetPreferenceResponse(false, $"Browser with Id {browserId} was null."));
143+
}
144+
145+
return CefThread.ExecuteOnUiThread(() =>
146+
{
147+
var ctx = browser?.GetRequestContext();
148+
149+
if (ctx == null)
150+
{
151+
return new SetPreferenceResponse(false, "RequestContext was null.");
152+
}
153+
154+
// StreamJsonRpc is converting ints to long which makes sense as we accept
155+
// object. CefValue doesn't support Int64, so we just convert to int
156+
// This should hopefully be fine as it's unliklely any preference requires
157+
// an Int64 value.
158+
if (value?.GetType() == typeof(long))
159+
{
160+
value = Convert.ToInt32(value);
161+
}
162+
163+
var success = ctx.SetPreference(name, value, out string error);
164+
165+
return new SetPreferenceResponse(success, error);
166+
});
167+
}
127168
}
128169
}

CefSharp.OutOfProcess.BrowserProcess/CefSharp.OutOfProcess.BrowserProcess.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<ItemGroup>
1414
<PackageReference Include="PInvoke.Kernel32" Version="0.7.104" />
1515
<PackageReference Include="PInvoke.User32" Version="0.7.104" />
16-
<PackageReference Include="StreamJsonRpc" Version="2.11.35" />
16+
<PackageReference Include="StreamJsonRpc" Version="2.14.24" />
1717
<PackageReference Include="CefSharp.Common" Version="110.0.250" />
1818
</ItemGroup>
1919

CefSharp.OutOfProcess.BrowserProcess/OutOfProcessChromiumWebBrowser.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
using System.ComponentModel;
66
using CefSharp.Callback;
77
using System.IO;
8-
using CefSharp.OutOfProcess.Interface;
98
using System.Runtime.InteropServices;
10-
using PInvoke;
11-
using System.Diagnostics;
129

1310
namespace CefSharp.OutOfProcess.BrowserProcess
1411
{

CefSharp.OutOfProcess.Core/CefSharp.OutOfProcess.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<ItemGroup>
99
<PackageReference Include="PInvoke.Kernel32" Version="0.7.104" />
1010
<PackageReference Include="PInvoke.User32" Version="0.7.104" />
11-
<PackageReference Include="StreamJsonRpc" Version="2.11.35" />
11+
<PackageReference Include="StreamJsonRpc" Version="2.14.24" />
1212
<ProjectReference Include="..\CefSharp.OutOfProcess.Interface\CefSharp.OutOfProcess.Interface.csproj" />
1313

1414
<ProjectReference Include="..\CefSharp.Dom\lib\PuppeteerSharp\CefSharp.Dom.OutOfProcess.csproj" />

CefSharp.OutOfProcess.Core/IChromiumWebBrowser.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CefSharp.Dom;
2+
using CefSharp.OutOfProcess.Model;
23
using System;
34
using System.Threading.Tasks;
45

@@ -203,5 +204,16 @@ public interface IChromiumWebBrowser : IDisposable
203204
/// the navigation will resolve with the response of the last redirect. If can not go forward, resolves to null.</returns>
204205
/// <param name="options">Navigation parameters.</param>
205206
Task<Response> GoForwardAsync(NavigationOptions options = null);
207+
208+
/// <summary>
209+
/// Set the value associated with preference name. If value is null the
210+
/// preference will be restored to its default value. If setting the preference
211+
/// fails then <see cref="SetPreferenceResponse.ErrorMessage"/> will be populated
212+
/// with a detailed description of the problem.
213+
/// Preferences set via the command-line usually cannot be modified.
214+
/// </summary>
215+
/// <param name="name">The preference name</param>
216+
/// <param name="value">The preference value</param>
217+
Task<SetPreferenceResponse> SetRequestContextPreferenceAsync(string name, object value);
206218
}
207219
}

CefSharp.OutOfProcess.Core/OutOfProcessHost.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using CefSharp.OutOfProcess.Interface;
2-
using CefSharp.OutOfProcess.Internal;
1+
using CefSharp.OutOfProcess.Internal;
2+
using CefSharp.OutOfProcess.Model;
33
using PInvoke;
44
using StreamJsonRpc;
55
using System;
@@ -243,6 +243,21 @@ public void CloseBrowser(int id)
243243
_ = _outOfProcessClient.CloseBrowser(id);
244244
}
245245

246+
/// <summary>
247+
/// Set the value associated with preference name. If value is null the
248+
/// preference will be restored to its default value. If setting the preference
249+
/// fails then <see cref="SetPreferenceResponse.ErrorMessage"/> will be populated
250+
/// with a detailed description of the problem.
251+
/// Preferences set via the command-line usually cannot be modified.
252+
/// </summary>
253+
/// <param name="id">The browser id.</param>
254+
/// <param name="name">The preference name</param>
255+
/// <param name="value">The preference value</param>
256+
public Task<SetPreferenceResponse> SetRequestContextPreferenceAsync(int id, string name, object value)
257+
{
258+
return _outOfProcessClient.SetRequestContextPreferenceAsync(id, name, value);
259+
}
260+
246261
public void Dispose()
247262
{
248263
_ = _outOfProcessClient.CloseHost();

CefSharp.OutOfProcess.Interface/CefSharp.OutOfProcess.Interface.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5+
<RootNamespace>CefSharp.OutOfProcess</RootNamespace>
56
</PropertyGroup>
67

78
</Project>

CefSharp.OutOfProcess.Interface/IOutOfProcessClientRpc.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
1+
using CefSharp.OutOfProcess.Model;
2+
using System;
23
using System.Threading.Tasks;
34

4-
namespace CefSharp.OutOfProcess.Interface
5+
namespace CefSharp.OutOfProcess
56
{
67
/// <summary>
78
/// Send messages to the Remote Host (Browser process)
@@ -51,5 +52,17 @@ public interface IOutOfProcessClientRpc
5152
/// <param name="id">browser id</param>
5253
/// <param name="focus">set focus</param>
5354
void SetFocus(int browserId, bool focus);
55+
56+
/// <summary>
57+
/// Set the value associated with preference name. If value is null the
58+
/// preference will be restored to its default value. If setting the preference
59+
/// fails then <see cref="SetPreferenceResponse.ErrorMessage"/> will be populated
60+
/// with a detailed description of the problem.
61+
/// Preferences set via the command-line usually cannot be modified.
62+
/// </summary>
63+
/// <param name="browserId">The browser id.</param>
64+
/// <param name="name">The preference name</param>
65+
/// <param name="value">The preference value</param>
66+
Task<SetPreferenceResponse> SetRequestContextPreferenceAsync(int browserId, string name, object value);
5467
}
5568
}

CefSharp.OutOfProcess.Interface/IOutOfProcessHostRpc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace CefSharp.OutOfProcess.Interface
3+
namespace CefSharp.OutOfProcess
44
{
55
/// <summary>
66
/// Receive messages from the Remote Host (Browser process).
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace CefSharp.OutOfProcess.Model
2+
{
3+
/// <summary>
4+
/// Response when setting a Preference
5+
/// </summary>
6+
public class SetPreferenceResponse
7+
{
8+
/// <summary>
9+
/// Success
10+
/// </summary>
11+
public bool Success { get; private set; }
12+
13+
/// <summary>
14+
/// Error Message
15+
/// </summary>
16+
public string ErrorMessage { get; private set; }
17+
18+
/// <summary>
19+
/// Initializes a new instance of the SetPreferenceResponse class.
20+
/// </summary>
21+
/// <param name="success">success</param>
22+
/// <param name="errorMessage">error message</param>
23+
public SetPreferenceResponse(bool success, string errorMessage)
24+
{
25+
Success = success;
26+
ErrorMessage = errorMessage;
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)