-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOutOfProcessHost.cs
More file actions
279 lines (236 loc) · 9.42 KB
/
OutOfProcessHost.cs
File metadata and controls
279 lines (236 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using CefSharp.OutOfProcess.Interface;
using CefSharp.OutOfProcess.Internal;
using PInvoke;
using StreamJsonRpc;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace CefSharp.OutOfProcess
{
public class OutOfProcessHost : IOutOfProcessHostRpc, IDisposable
{
/// <summary>
/// The CefSharp.OutOfProcess.BrowserProcess.exe name
/// </summary>
public const string HostExeName = "CefSharp.OutOfProcess.BrowserProcess.exe";
private Process _browserProcess;
private JsonRpc _jsonRpc;
private IOutOfProcessClientRpc _outOfProcessClient;
private string _cefSharpVersion;
private string _cefVersion;
private string _chromiumVersion;
private int _uiThreadId;
private int _remoteuiThreadId;
private int _browserIdentifier = 1;
private string _outofProcessHostExePath;
private string _cachePath;
private ConcurrentDictionary<int, IChromiumWebBrowserInternal> _browsers = new ConcurrentDictionary<int, IChromiumWebBrowserInternal>();
private TaskCompletionSource<OutOfProcessHost> _processInitialized = new TaskCompletionSource<OutOfProcessHost>(TaskCreationOptions.RunContinuationsAsynchronously);
private OutOfProcessHost(string outOfProcessHostExePath, string cachePath = null)
{
_outofProcessHostExePath = outOfProcessHostExePath;
_cachePath = cachePath;
}
/// <summary>
/// UI Thread assocuated with this <see cref="OutOfProcessHost"/>
/// </summary>
public int UiThreadId
{
get { return _uiThreadId; }
}
/// <summary>
/// Thread Id of the UI Thread running in the Browser Process
/// </summary>
public int RemoteUiThreadId
{
get { return _remoteuiThreadId; }
}
/// <summary>
/// CefSharp Version
/// </summary>
public string CefSharpVersion
{
get { return _cefSharpVersion; }
}
/// <summary>
/// Cef Version
/// </summary>
public string CefVersion
{
get { return _cefVersion; }
}
/// <summary>
/// Chromium Version
/// </summary>
public string ChromiumVersion
{
get { return _chromiumVersion; }
}
/// <summary>
/// Sends an IPC message to the Browser Process instructing it
/// to create a new Out of process browser
/// </summary>
/// <param name="browser">The <see cref="IChromiumWebBrowserInternal"/> that will host the browser</param>
/// <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>
/// <returns></returns>
public bool CreateBrowser(IChromiumWebBrowserInternal browser, IntPtr handle, string url, out int id, IDictionary<string, object> requestContextPreferences = null)
{
id = _browserIdentifier++;
_ = _outOfProcessClient.CreateBrowser(handle, url, id, requestContextPreferences);
return _browsers.TryAdd(id, browser);
}
internal Task SendDevToolsMessageAsync(int browserId, string message)
{
return _outOfProcessClient.SendDevToolsMessage(browserId, message);
}
private Task<OutOfProcessHost> InitializedTask
{
get { return _processInitialized.Task; }
}
private void Init()
{
var currentProcess = Process.GetCurrentProcess();
var args = $"--parentProcessId={currentProcess.Id} --cachePath={_cachePath}";
_browserProcess = Process.Start(new ProcessStartInfo(_outofProcessHostExePath, args)
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
});
_browserProcess.Exited += OnBrowserProcessExited;
_jsonRpc = JsonRpc.Attach(_browserProcess.StandardInput.BaseStream, _browserProcess.StandardOutput.BaseStream);
_outOfProcessClient = _jsonRpc.Attach<IOutOfProcessClientRpc>();
_jsonRpc.AllowModificationWhileListening = true;
_jsonRpc.AddLocalRpcTarget<IOutOfProcessHostRpc>(this, null);
_jsonRpc.AllowModificationWhileListening = false;
_uiThreadId = Kernel32.GetCurrentThreadId();
}
private void OnBrowserProcessExited(object sender, EventArgs e)
{
var exitCode = _browserProcess.ExitCode;
}
void IOutOfProcessHostRpc.NotifyAddressChanged(int browserId, string address)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
chromiumWebBrowser.SetAddress(address);
}
}
void IOutOfProcessHostRpc.NotifyBrowserCreated(int browserId, IntPtr browserHwnd)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
chromiumWebBrowser.OnAfterBrowserCreated(browserHwnd);
}
}
void IOutOfProcessHostRpc.NotifyContextInitialized(int threadId, string cefSharpVersion, string cefVersion, string chromiumVersion)
{
_remoteuiThreadId = threadId;
_cefSharpVersion = cefSharpVersion;
_cefVersion = cefVersion;
_chromiumVersion = chromiumVersion;
_processInitialized.TrySetResult(this);
}
void IOutOfProcessHostRpc.NotifyDevToolsAgentDetached(int browserId)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
}
}
void IOutOfProcessHostRpc.NotifyDevToolsMessage(int browserId, string devToolsMessage)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
chromiumWebBrowser.OnDevToolsMessage(devToolsMessage);
}
}
void IOutOfProcessHostRpc.NotifyDevToolsReady(int browserId)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
chromiumWebBrowser.OnDevToolsReady();
}
}
void IOutOfProcessHostRpc.NotifyLoadingStateChange(int browserId, bool canGoBack, bool canGoForward, bool isLoading)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
chromiumWebBrowser.SetLoadingStateChange(canGoBack, canGoForward, isLoading);
}
}
void IOutOfProcessHostRpc.NotifyStatusMessage(int browserId, string statusMessage)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
chromiumWebBrowser.SetStatusMessage(statusMessage);
}
}
void IOutOfProcessHostRpc.NotifyTitleChanged(int browserId, string title)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
chromiumWebBrowser.SetTitle(title);
}
}
public void NotifyMoveOrResizeStarted(int id)
{
_outOfProcessClient.NotifyMoveOrResizeStarted(id);
}
/// <summary>
/// Set whether the browser is focused. (Used for Normal Rendering e.g. WinForms)
/// </summary>
/// <param name="id">browser id</param>
/// <param name="focus">set focus</param>
public void SetFocus(int id, bool focus)
{
_outOfProcessClient.SetFocus(id, focus);
}
public void CloseBrowser(int id)
{
_ = _outOfProcessClient.CloseBrowser(id);
}
public void Dispose()
{
_ = _outOfProcessClient.CloseHost();
_jsonRpc?.Dispose();
_jsonRpc = null;
}
public static Task<OutOfProcessHost> CreateAsync(string path = HostExeName, string cachePath = null)
{
if(string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path));
}
var fullPath = Path.GetFullPath(path);
if (!File.Exists(fullPath))
{
throw new FileNotFoundException("Unable to find Host executable.", path);
}
var host = new OutOfProcessHost(fullPath, cachePath);
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);
}
}
}