-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathExampleRequestHandler.cs
More file actions
127 lines (111 loc) · 5.93 KB
/
ExampleRequestHandler.cs
File metadata and controls
127 lines (111 loc) · 5.93 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
// Copyright © 2012 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.Threading.Tasks;
using CefSharp.Handler;
namespace CefSharp.Example.Handlers
{
/// <summary>
/// <see cref="RequestHandler"/> provides a base class for you to inherit from
/// you only need to implement the methods that are relevant to you.
/// If you implement the IRequestHandler interface you will need to
/// implement every method
/// </summary>
public class ExampleRequestHandler : RequestHandler
{
public static readonly string VersionNumberString = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}",
Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
return false;
}
protected override bool OnOpenUrlFromTab(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
return false;
}
protected override bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
//NOTE: We also suggest you wrap callback in a using statement or explicitly execute callback.Dispose as callback wraps an unmanaged resource.
//Example #1
//Return true and call IRequestCallback.Continue() at a later time to continue or cancel the request.
//In this instance we'll use a Task, typically you'd invoke a call to the UI Thread and display a Dialog to the user
//You can cast the IWebBrowser param to ChromiumWebBrowser to easily access
//control, from there you can invoke onto the UI thread, should be in an async fashion
Task.Run(() =>
{
//NOTE: When executing the callback in an async fashion need to check to see if it's disposed
if (!callback.IsDisposed)
{
using (callback)
{
//We'll allow the expired certificate from badssl.com
if (requestUrl.ToLower().Contains("https://expired.badssl.com/"))
{
callback.Continue(true);
}
else
{
callback.Continue(false);
}
}
}
});
return true;
//Example #2
//Execute the callback and return true to immediately allow the invalid certificate
//callback.Continue(true); //Callback will Dispose it's self once exeucted
//return true;
//Example #3
//Return false for the default behaviour (cancel request immediately)
//callback.Dispose(); //Dispose of callback
//return false;
}
protected override bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
//NOTE: We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource.
//Example #1
//Spawn a Task to execute our callback and return true;
//Typical usage would see you invoke onto the UI thread to open a username/password dialog
//Then execute the callback with the response username/password
//You can cast the IWebBrowser param to ChromiumWebBrowser to easily access
//control, from there you can invoke onto the UI thread, should be in an async fashion
//Load https://httpbin.org/basic-auth/cefsharp/passwd in the browser to test
Task.Run(() =>
{
using (callback)
{
if (originUrl.Contains("https://httpbin.org/basic-auth/"))
{
var parts = originUrl.Split('/');
var username = parts[parts.Length - 2];
var password = parts[parts.Length - 1];
callback.Continue(username, password);
}
}
});
return true;
//Example #2
//Return false to cancel the request
//callback.Dispose();
//return false;
}
protected override void OnRenderProcessTerminated(IWebBrowser chromiumWebBrowser, IBrowser browser, CefTerminationStatus status, int errorCode, string errorMessage)
{
// TODO: Add your own code here for handling scenarios where the Render Process terminated for one reason or another.
chromiumWebBrowser.Load(CefExample.RenderProcessCrashedUrl);
}
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
//NOTE: In most cases you examine the request.Url and only handle requests you are interested in
if (request.Url.ToLower().StartsWith("https://cefsharp.example")
|| request.Url.ToLower().StartsWith(CefSharpSchemeHandlerFactory.SchemeName)
|| request.Url.ToLower().StartsWith("mailto:")
|| request.Url.ToLower().StartsWith("https://googlechrome.github.io/samples/service-worker/"))
{
return new ExampleResourceRequestHandler();
}
return null;
}
}
}