forked from cefsharp/CefSharp.OutOfProcess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutOfProcessHost.cs
More file actions
331 lines (269 loc) · 11.7 KB
/
OutOfProcessHost.cs
File metadata and controls
331 lines (269 loc) · 11.7 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using CefSharp.OutOfProcess.Interface;
using CefSharp.OutOfProcess.Interface.Callbacks;
using CefSharp.OutOfProcess.Internal;
using PInvoke;
using StreamJsonRpc;
using System;
using System.Collections.Concurrent;
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;
}
public event EventHandler<DownloadCallbackDetails> DownloadCallback;
public event EventHandler<BeforeDownloadCallbackDetails> BeforeDownloadCallback;
public event EventHandler<JsDialogCallbackDetails> JsDialogCallback;
/// <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>
/// <returns></returns>
public bool CreateBrowser(IChromiumWebBrowserInternal browser, IntPtr handle, string url, out int id)
{
id = _browserIdentifier++;
_ = _outOfProcessClient.CreateBrowser(handle, url, id);
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)
{
GetBrowser(browserId)?.SetAddress(address);
}
void IOutOfProcessHostRpc.NotifyBrowserCreated(int browserId, IntPtr browserHwnd)
{
GetBrowser(browserId)?.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)
{
GetBrowser(browserId)?.OnDevToolsMessage(devToolsMessage);
}
void IOutOfProcessHostRpc.NotifyDevToolsReady(int browserId)
{
GetBrowser(browserId)?.OnDevToolsReady();
}
void IOutOfProcessHostRpc.NotifyLoadingStateChange(int browserId, bool canGoBack, bool canGoForward, bool isLoading)
{
GetBrowser(browserId)?.SetLoadingStateChange(canGoBack, canGoForward, isLoading);
}
void IOutOfProcessHostRpc.NotifyStatusMessage(int browserId, string statusMessage)
{
GetBrowser(browserId)?.SetStatusMessage(statusMessage);
}
void IOutOfProcessHostRpc.NotifyTitleChanged(int browserId, string title)
{
GetBrowser(browserId)?.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;
}
void IOutOfProcessHostRpc.OnBeforeDownload(int browserId, CefSharp.OutOfProcess.Interface.Callbacks.DownloadItem downloadItem, int callback)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
chromiumWebBrowser.DownloadHandler?.OnBeforeDownload(chromiumWebBrowser, downloadItem, new DownloadCallbackProxy(this, callback, chromiumWebBrowser));
}
}
void IOutOfProcessHostRpc.OnDownloadUpdated(int browserId, CefSharp.OutOfProcess.Interface.Callbacks.DownloadItem downloadItem, int callback)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
GetBrowser(browserId)?.DownloadHandler?.OnDownloadUpdated(chromiumWebBrowser, downloadItem, new DownloadCallbackProxy(this, callback, chromiumWebBrowser));
}
}
private IChromiumWebBrowserInternal GetBrowser(int browserId)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
return chromiumWebBrowser;
}
return null;
}
Task<bool> IOutOfProcessHostRpc.OnCanDownloadAsync(int browserId, string url, string requestMethod)
{
if (_browsers.TryGetValue(browserId, out var chromiumWebBrowser))
{
var handler = chromiumWebBrowser.DownloadHandler;
if (handler == null)
{
return Task.FromResult(false);
}
var result = handler.CanDownload(chromiumWebBrowser, url, requestMethod);
return Task.FromResult(result);
}
return Task.FromResult(false);
}
internal void InvokeBeforeDownloadCallback(BeforeDownloadCallbackDetails callbackDetails) => BeforeDownloadCallback.Invoke(this, callbackDetails);
internal void InvokeDownloadCallback(DownloadCallbackDetails callbackDetails) => DownloadCallback.Invoke(this, callbackDetails);
internal void InvokeJsDialogCallback(JsDialogCallbackDetails callbackDetails) => JsDialogCallback.Invoke(this, callbackDetails);
public bool OnBeforeUnloadDialog(int browserId, string messageText, bool isReload, int callback)
{
if (GetBrowser(browserId) is IChromiumWebBrowserInternal chromiumWebBrowser)
{
return chromiumWebBrowser?.JsDialogHandler?.OnBeforeUnloadDialog(chromiumWebBrowser, messageText, isReload, new JsDialogCallbackProxy(this, callback, chromiumWebBrowser)) ?? false;
}
return false;
}
public void OnDialogClosed(int browserId)
{
if (GetBrowser(browserId) is IChromiumWebBrowserInternal chromiumWebBrowser)
{
chromiumWebBrowser?.JsDialogHandler?.OnDialogClosed(chromiumWebBrowser);
}
}
public bool OnJSDialog(int browserId, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, int callback, ref bool suppressMessage)
{
if (GetBrowser(browserId) is IChromiumWebBrowserInternal chromiumWebBrowser)
{
return chromiumWebBrowser?.JsDialogHandler?.OnJSDialog(chromiumWebBrowser,originUrl, dialogType, messageText,defaultPromptText,new JsDialogCallbackProxy(this, callback,chromiumWebBrowser), ref suppressMessage) ?? false;
}
return false;
}
public void OnResetDialogState(int browserId)
{
if (GetBrowser(browserId) is IChromiumWebBrowserInternal chromiumWebBrowser)
{
chromiumWebBrowser?.JsDialogHandler?.OnResetDialogState(chromiumWebBrowser);
}
}
}
}