/
ChromiumWebBrowser.cs
343 lines (285 loc) · 9.97 KB
/
ChromiumWebBrowser.cs
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
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright © 2010-2014 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp.Internals;
namespace CefSharp.WinForms
{
public class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrowser
{
private ManagedCefBrowserAdapter managedCefBrowserAdapter;
public BrowserSettings BrowserSettings { get; set; }
public string Title { get; set; }
public bool IsLoading { get; private set; }
public string TooltipText { get; private set; }
public string Address { get; set; }
public IDialogHandler DialogHandler { get; set; }
public IJsDialogHandler JsDialogHandler { get; set; }
public IKeyboardHandler KeyboardHandler { get; set; }
public IRequestHandler RequestHandler { get; set; }
public IDownloadHandler DownloadHandler { get; set; }
public ILifeSpanHandler LifeSpanHandler { get; set; }
public IMenuHandler MenuHandler { get; set; }
public bool CanGoForward { get; private set; }
public bool CanGoBack { get; private set; }
public bool CanReload { get; private set; }
public bool IsBrowserInitialized { get; private set; }
public IDictionary<string, object> BoundObjects { get; private set; }
public double ZoomLevel
{
get { return managedCefBrowserAdapter.GetZoomLevel(); }
set { managedCefBrowserAdapter.SetZoomLevel(value); }
}
static ChromiumWebBrowser()
{
Application.ApplicationExit += OnApplicationExit;
}
private static void OnApplicationExit(object sender, EventArgs e)
{
Cef.Shutdown();
}
public ChromiumWebBrowser(string address)
{
Cef.AddDisposable(this);
Address = address;
}
protected override void Dispose(bool disposing)
{
Cef.RemoveDisposable(this);
if (disposing)
{
if (managedCefBrowserAdapter != null)
{
managedCefBrowserAdapter.Dispose();
managedCefBrowserAdapter = null;
}
}
base.Dispose(disposing);
}
void IWebBrowserInternal.OnInitialized()
{
IsBrowserInitialized = true;
var handler = IsBrowserInitializedChanged;
if (handler != null)
{
handler(this, new IsBrowserInitializedChangedEventArgs(IsBrowserInitialized));
}
}
public void Load(String url)
{
managedCefBrowserAdapter.LoadUrl(url);
}
public void LoadHtml(string html, string url)
{
managedCefBrowserAdapter.LoadHtml(html, url);
}
public void RegisterJsObject(string name, object objectToBind)
{
throw new NotImplementedException();
}
public void ExecuteScriptAsync(string script)
{
managedCefBrowserAdapter.ExecuteScriptAsync(script);
}
public object EvaluateScript(string script)
{
return EvaluateScript(script, timeout: null);
}
public object EvaluateScript(string script, TimeSpan? timeout)
{
if (timeout == null)
{
timeout = TimeSpan.MaxValue;
}
return managedCefBrowserAdapter.EvaluateScript(script, timeout.Value);
}
public event EventHandler<LoadErrorEventArgs> LoadError;
public event EventHandler<FrameLoadStartEventArgs> FrameLoadStart;
public event EventHandler<FrameLoadEndEventArgs> FrameLoadEnd;
public event EventHandler<NavStateChangedEventArgs> NavStateChanged;
public event EventHandler<ConsoleMessageEventArgs> ConsoleMessage;
public event EventHandler<AddressChangedEventArgs> AddressChanged;
public event EventHandler<TitleChangedEventArgs> TitleChanged;
public event IsBrowserInitializedChangedEventHandler IsBrowserInitializedChanged;
public event IsLoadingChangedEventHandler IsLoadingChanged;
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
managedCefBrowserAdapter = new ManagedCefBrowserAdapter(this);
managedCefBrowserAdapter.CreateBrowser(BrowserSettings ?? new BrowserSettings(), Handle, Address);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (managedCefBrowserAdapter != null)
managedCefBrowserAdapter.OnSizeChanged(Handle);
}
void IWebBrowserInternal.SetAddress(string address)
{
Address = address;
var handler = AddressChanged;
if (handler != null)
{
handler(this, new AddressChangedEventArgs(address));
}
}
void IWebBrowserInternal.SetIsLoading(bool isLoading)
{
IsLoading = isLoading;
var handler = IsLoadingChanged;
if (handler != null)
{
handler(this, new IsLoadingChangedEventArgs(isLoading));
}
}
void IWebBrowserInternal.SetNavState(bool canGoBack, bool canGoForward, bool canReload)
{
CanGoBack = canGoBack;
CanGoForward = canGoForward;
CanReload = canReload;
var handler = NavStateChanged;
if (handler != null)
{
handler(this, new NavStateChangedEventArgs(canGoBack, canGoForward, canReload));
}
}
void IWebBrowserInternal.SetTitle(string title)
{
Title = title;
var handler = TitleChanged;
if (handler != null)
{
handler(this, new TitleChangedEventArgs(title));
}
}
void IWebBrowserInternal.SetTooltipText(string tooltipText)
{
TooltipText = tooltipText;
}
void IWebBrowserInternal.OnFrameLoadStart(string url, bool isMainFrame)
{
var handler = FrameLoadStart;
if (handler != null)
{
handler(this, new FrameLoadStartEventArgs(url, isMainFrame));
}
}
void IWebBrowserInternal.OnFrameLoadEnd(string url, bool isMainFrame, int httpStatusCode)
{
var handler = FrameLoadEnd;
if (handler != null)
{
handler(this, new FrameLoadEndEventArgs(url, isMainFrame, httpStatusCode));
}
}
void IWebBrowserInternal.OnTakeFocus(bool next)
{
SelectNextControl(this, next, true, true, true);
}
void IWebBrowserInternal.OnConsoleMessage(string message, string source, int line)
{
var handler = ConsoleMessage;
if (handler != null)
{
handler(this, new ConsoleMessageEventArgs(message, source, line));
}
}
void IWebBrowserInternal.OnLoadError(string url, CefErrorCode errorCode, string errorText)
{
var handler = LoadError;
if (handler != null)
{
handler(this, new LoadErrorEventArgs(url, errorCode, errorText));
}
}
public void Find(int identifier, string searchText, bool forward, bool matchCase, bool findNext)
{
managedCefBrowserAdapter.Find(identifier, searchText, forward, matchCase, findNext);
}
public void StopFinding(bool clearSelection)
{
managedCefBrowserAdapter.StopFinding(clearSelection);
}
void IWebBrowserInternal.ShowDevTools()
{
// TODO: Do something about this one.
var devToolsUrl = managedCefBrowserAdapter.DevToolsUrl;
throw new NotImplementedException();
}
void IWebBrowserInternal.CloseDevTools()
{
throw new NotImplementedException();
}
public void Stop()
{
managedCefBrowserAdapter.Stop();
}
public void Back()
{
managedCefBrowserAdapter.GoBack();
}
public void Forward()
{
managedCefBrowserAdapter.GoForward();
}
public void Reload()
{
Reload(false);
}
public void Reload(bool ignoreCache)
{
managedCefBrowserAdapter.Reload(ignoreCache);
}
public void Undo()
{
managedCefBrowserAdapter.Undo();
}
public void Redo()
{
managedCefBrowserAdapter.Redo();
}
public void Cut()
{
managedCefBrowserAdapter.Cut();
}
public void Copy()
{
managedCefBrowserAdapter.Copy();
}
public void Paste()
{
managedCefBrowserAdapter.Paste();
}
public void Delete()
{
managedCefBrowserAdapter.Delete();
}
public void SelectAll()
{
managedCefBrowserAdapter.SelectAll();
}
public void Print()
{
managedCefBrowserAdapter.Print();
}
public void ViewSource()
{
managedCefBrowserAdapter.ViewSource();
}
public Task<string> GetSourceAsync()
{
var taskStringVisitor = new TaskStringVisitor();
managedCefBrowserAdapter.GetSource(taskStringVisitor);
return taskStringVisitor.Task;
}
public Task<string> GetTextAsync()
{
var taskStringVisitor = new TaskStringVisitor();
managedCefBrowserAdapter.GetText(taskStringVisitor);
return taskStringVisitor.Task;
}
}
}