|
4 | 4 | using System; |
5 | 5 | using System.Linq; |
6 | 6 | using System.Collections.Generic; |
7 | | -using System.Diagnostics; |
8 | | -using CefSharp.OutOfProcess.Interface; |
9 | 7 | using System.Threading.Tasks; |
| 8 | +using CefSharp.OutOfProcess.Model; |
| 9 | +using System.Diagnostics; |
10 | 10 |
|
11 | 11 | namespace CefSharp.OutOfProcess.BrowserProcess |
12 | 12 | { |
@@ -124,5 +124,46 @@ void IOutOfProcessClientRpc.SetFocus(int browserId, bool focus) |
124 | 124 |
|
125 | 125 | browser?.GetBrowserHost().SetFocus(focus); |
126 | 126 | } |
| 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 | + } |
127 | 168 | } |
128 | 169 | } |
0 commit comments