forked from cefsharp/CefSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExamplePresenter.cs
More file actions
310 lines (252 loc) · 10.4 KB
/
Copy pathExamplePresenter.cs
File metadata and controls
310 lines (252 loc) · 10.4 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
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace CefSharp.Example
{
public class ExamplePresenter : IRequestHandler
{
// Use when debugging the actual SubProcess, to make breakpoints etc. inside that project work.
private const bool debuggingSubProcess = false;
public static void Init()
{
var settings = new CefSettings();
settings.RemoteDebuggingPort = 8088;
if (debuggingSubProcess)
{
settings.BrowserSubprocessPath = "..\\..\\..\\..\\CefSharp.BrowserSubprocess\\bin\\x86\\Debug\\CefSharp.BrowserSubprocess.exe";
}
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = CefSharpSchemeHandlerFactory.SchemeName,
SchemeHandlerFactory = new CefSharpSchemeHandlerFactory()
});
if (!Cef.Initialize(settings))
{
if (Environment.GetCommandLineArgs().Contains("--type=renderer"))
{
Environment.Exit(0);
}
else
{
return;
}
}
Cef.RegisterJsObject("bound", new BoundObject());
}
public static string DefaultUrl = "custom://cefsharp/home";
private static readonly Uri resource_url = new Uri("http://test/resource/load");
private readonly IWebBrowser model;
private readonly Action<Action> uiThreadInvoke;
public ExamplePresenter(IWebBrowser model, Action<Action> uiThreadInvoke)
{
this.model = model;
this.uiThreadInvoke = uiThreadInvoke;
var version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}",
Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
//view.DisplayOutput(version);
model.RequestHandler = this;
//model.PropertyChanged += OnModelPropertyChanged;
//model.ConsoleMessage += OnModelConsoleMessage;
//// file
//view.ShowDevToolsActivated += OnViewShowDevToolsActivated;
//view.CloseDevToolsActivated += OnViewCloseDevToolsActivated;
//view.ExitActivated += OnViewExitActivated;
//// edit
//view.UndoActivated += OnViewUndoActivated;
//view.RedoActivated += OnViewRedoActivated;
//view.CutActivated += OnViewCutActivated;
//view.CopyActivated += OnViewCopyActivated;
//view.PasteActivated += OnViewPasteActivated;
//view.DeleteActivated += OnViewDeleteActivated;
//view.SelectAllActivated += OnViewSelectAllActivated;
//// test
//view.TestResourceLoadActivated += OnViewTestResourceLoadActivated;
//view.TestSchemeLoadActivated += OnViewTestSchemeLoadActivated;
//view.TestExecuteScriptActivated += OnViewTestExecuteScriptActivated;
//view.TestEvaluateScriptActivated += OnViewTestEvaluateScriptActivated;
//view.TestBindActivated += OnViewTestBindActivated;
//view.TestConsoleMessageActivated += OnViewTestConsoleMessageActivated;
//view.TestTooltipActivated += OnViewTestTooltipActivated;
//view.TestPopupActivated += OnViewTestPopupActivated;
//view.TestLoadStringActivated += OnViewTestLoadStringActivated;
//view.TestCookieVisitorActivated += OnViewTestCookieVisitorActivated;
//// navigation
// TODO: This one might actually be needed.
//view.UrlActivated += OnViewUrlActivated;
//view.ForwardActivated += OnViewForwardActivated;
//view.BackActivated += OnViewBackActivated;
}
//private void OnModelPropertyChanged(object sender, PropertyChangedEventArgs e)
//{
// switch (e.PropertyName)
// {
// case "Title":
// uiThreadInvoke(() => view.SetTitle(model.Title));
// break;
// case "Address":
// uiThreadInvoke(() => view.SetAddress(model.Address));
// break;
// case "CanGoBack":
// uiThreadInvoke(() => view.SetCanGoBack(model.CanGoBack));
// break;
// case "CanGoForward":
// uiThreadInvoke(() => view.SetCanGoForward(model.CanGoForward));
// break;
// case "IsLoading":
// uiThreadInvoke(() => view.SetIsLoading(model.IsLoading));
// break;
// }
//}
// private void OnModelConsoleMessage(object sender, ConsoleMessageEventArgs e)
// {
// uiThreadInvoke(() => view.DisplayOutput(e.Message));
// }
// private void OnViewShowDevToolsActivated(object sender, EventArgs e)
// {
// model.ShowDevTools();
// }
// private void OnViewCloseDevToolsActivated(object sender, EventArgs e)
// {
// model.CloseDevTools();
// }
// private void OnViewExitActivated(object sender, EventArgs e)
// {
// var disposableModel = model as IDisposable;
// if (disposableModel != null)
// {
// disposableModel.Dispose();
// }
// Cef.Shutdown();
// Environment.Exit(0);
// }
// void OnViewUndoActivated(object sender, EventArgs e)
// {
// model.Undo();
// }
// void OnViewRedoActivated(object sender, EventArgs e)
// {
// model.Redo();
// }
// void OnViewCutActivated(object sender, EventArgs e)
// {
// model.Cut();
// }
// void OnViewCopyActivated(object sender, EventArgs e)
// {
// model.Copy();
// }
// void OnViewPasteActivated(object sender, EventArgs e)
// {
// model.Paste();
// }
// void OnViewDeleteActivated(object sender, EventArgs e)
// {
// model.Delete();
// }
// void OnViewSelectAllActivated(object sender, EventArgs e)
// {
// model.SelectAll();
// }
// private void OnViewTestResourceLoadActivated(object sender, EventArgs e)
// {
// //model.Load(resource_url);
// }
// private void OnViewTestSchemeLoadActivated(object sender, EventArgs e)
// {
// //model.Load(scheme_url);
// }
// private void OnViewTestExecuteScriptActivated(object sender, EventArgs e)
// {
// var script = String.Format("document.body.style.background = '{0}'",
// colors[color_index++]);
// if (color_index >= colors.Length)
// {
// color_index = 0;
// }
// view.ExecuteScript(script);
// }
// private void OnViewTestEvaluateScriptActivated(object sender, EventArgs e)
// {
// var rand = new Random();
// var x = rand.Next(1, 10);
// var y = rand.Next(1, 10);
// var script = String.Format("{0} + {1}", x, y);
// var result = view.EvaluateScript(script);
// var output = String.Format("{0} => {1}", script, result);
// uiThreadInvoke(() => view.DisplayOutput(output));
// }
// private void OnViewTestBindActivated(object sender, EventArgs e)
// {
// //model.Load(bind_url);
// }
// private void OnViewTestConsoleMessageActivated(object sender, EventArgs e)
// {
// var script = "console.log('Hello, world!')";
// view.ExecuteScript(script);
// }
// private void OnViewTestTooltipActivated(object sender, EventArgs e)
// {
// //model.Load(tooltip_url);
// }
// private void OnViewTestPopupActivated(object sender, EventArgs e)
// {
// //model.Load(popup_url);
// }
// private void OnViewTestLoadStringActivated(object sender, EventArgs e)
// {
// model.LoadHtml(string.Format("<html><body><a href='{0}'>CefSharp Home</a></body></html>", DefaultUrl));
// }
// private void OnViewTestCookieVisitorActivated(object sender, EventArgs e)
// {
// Cef.VisitAllCookies(this);
// }
// TODO: This one might be needed...
//private void OnViewUrlActivated(object sender, string address)
//{
// model.Load(address);
//}
// private void OnViewBackActivated(object sender, EventArgs e)
// {
// model.Back();
// }
// private void OnViewForwardActivated(object sender, EventArgs e)
// {
// model.Forward();
// }
bool IRequestHandler.OnBeforeBrowse(IWebBrowser browser, IRequest request, NavigationType naigationvType, bool isRedirect)
{
return false;
}
bool IRequestHandler.OnBeforeResourceLoad(IWebBrowser browser, IRequestResponse requestResponse)
{
IRequest request = requestResponse.Request;
if (request.Url.StartsWith(resource_url.ToString()))
{
Stream resourceStream = new MemoryStream(Encoding.UTF8.GetBytes(
"<html><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p></body></html>"));
requestResponse.RespondWith(resourceStream, "text/html");
}
return false;
}
void IRequestHandler.OnResourceResponse(IWebBrowser browser, string url, int status, string statusText, string mimeType, WebHeaderCollection headers)
{
}
bool IRequestHandler.GetDownloadHandler(IWebBrowser browser, out IDownloadHandler handler)
{
handler = new DownloadHandler();
return true;
}
bool IRequestHandler.GetAuthCredentials(IWebBrowser browser, bool isProxy, string host, int port, string realm, string scheme, ref string username, ref string password)
{
return false;
}
// bool ICookieVisitor.Visit(Cookie cookie, int count, int total, ref bool deleteCookie)
// {
// Console.WriteLine("Cookie #{0}: {1}", count, cookie.Name);
// return true;
// }
}
}