2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2012 The CefSharp Authors. All rights reserved.
|
2015-08-03 10:41:29 +10:00
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
using System;
|
2019-10-04 10:47:59 +10:00
|
|
|
using System.Threading.Tasks;
|
2018-09-17 00:46:21 +02:00
|
|
|
using CefSharp.Handler;
|
2013-08-03 23:33:59 +03:00
|
|
|
|
2017-12-20 10:01:34 +10:00
|
|
|
namespace CefSharp.Example.Handlers
|
2012-02-23 15:08:50 -08:00
|
|
|
{
|
2017-12-20 10:01:34 +10:00
|
|
|
/// <summary>
|
2019-06-24 20:39:24 +10:00
|
|
|
/// <see cref="RequestHandler"/> provides a base class for you to inherit from
|
2017-12-20 10:01:34 +10:00
|
|
|
/// 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>
|
2019-06-24 20:39:24 +10:00
|
|
|
public class ExampleRequestHandler : RequestHandler
|
2012-02-23 15:08:50 -08:00
|
|
|
{
|
2014-06-12 01:55:10 +03:00
|
|
|
public static readonly string VersionNumberString = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}",
|
|
|
|
|
Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
|
2013-07-30 10:47:05 +03:00
|
|
|
|
2019-07-20 16:26:25 +10:00
|
|
|
protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
|
2013-11-08 16:13:13 +11:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-07-30 10:47:05 +03:00
|
|
|
|
2019-07-20 16:26:25 +10:00
|
|
|
protected override bool OnOpenUrlFromTab(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
|
2015-08-17 13:59:48 +10:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-20 16:26:25 +10:00
|
|
|
protected override bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
|
2014-11-25 15:51:55 -05:00
|
|
|
{
|
2019-10-04 10:47:59 +10:00
|
|
|
//NOTE: We also suggest you wrap callback in a using statement or explicitly execute callback.Dispose as callback wraps an unmanaged resource.
|
2015-09-09 09:18:27 +10:00
|
|
|
|
2019-10-04 10:47:59 +10:00
|
|
|
//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
|
2019-10-04 11:29:33 +10:00
|
|
|
//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
|
2019-10-04 10:47:59 +10:00
|
|
|
Task.Run(() =>
|
2015-06-13 10:32:15 -04:00
|
|
|
{
|
2019-10-04 10:47:59 +10:00
|
|
|
//NOTE: When executing the callback in an async fashion need to check to see if it's disposed
|
|
|
|
|
if (!callback.IsDisposed)
|
2015-09-01 12:16:08 +10:00
|
|
|
{
|
2019-10-04 10:47:59 +10:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-01 12:16:08 +10:00
|
|
|
}
|
2019-10-04 10:47:59 +10:00
|
|
|
});
|
2015-09-01 12:16:08 +10:00
|
|
|
|
2019-10-04 10:47:59 +10:00
|
|
|
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;
|
2014-11-25 15:51:55 -05:00
|
|
|
}
|
|
|
|
|
|
2019-07-20 16:26:25 +10:00
|
|
|
protected override bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
|
2013-11-08 16:13:13 +11:00
|
|
|
{
|
2019-10-04 11:29:33 +10:00
|
|
|
//NOTE: We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource.
|
2015-09-09 09:18:27 +10:00
|
|
|
|
2019-10-04 11:29:33 +10:00
|
|
|
//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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-07-30 10:47:05 +03:00
|
|
|
|
2019-10-04 11:29:33 +10:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
//Example #2
|
|
|
|
|
//Return false to cancel the request
|
|
|
|
|
//callback.Dispose();
|
|
|
|
|
//return false;
|
2016-12-12 15:48:15 -08:00
|
|
|
}
|
|
|
|
|
|
2019-07-20 16:26:25 +10:00
|
|
|
protected override void OnRenderProcessTerminated(IWebBrowser chromiumWebBrowser, IBrowser browser, CefTerminationStatus status)
|
2014-06-12 14:16:39 +10:00
|
|
|
{
|
|
|
|
|
// TODO: Add your own code here for handling scenarios where the Render Process terminated for one reason or another.
|
2019-07-20 16:26:25 +10:00
|
|
|
chromiumWebBrowser.Load(CefExample.RenderProcessCrashedUrl);
|
2014-06-12 14:16:39 +10:00
|
|
|
}
|
2015-04-22 16:29:18 +10:00
|
|
|
|
2019-06-24 21:22:20 +10:00
|
|
|
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
|
2015-05-07 13:50:19 +10:00
|
|
|
{
|
2019-06-24 20:54:51 +10:00
|
|
|
//NOTE: In most cases you examine the request.Url and only handle requests you are interested in
|
2019-07-10 20:27:39 +10:00
|
|
|
if (request.Url.ToLower().StartsWith("https://cefsharp.example")
|
2019-09-29 14:19:08 +10:00
|
|
|
|| request.Url.ToLower().StartsWith(CefSharpSchemeHandlerFactory.SchemeName)
|
2019-07-10 20:27:39 +10:00
|
|
|
|| request.Url.ToLower().StartsWith("mailto:")
|
|
|
|
|
|| request.Url.ToLower().StartsWith("https://googlechrome.github.io/samples/service-worker/"))
|
2019-06-24 20:54:51 +10:00
|
|
|
{
|
|
|
|
|
return new ExampleResourceRequestHandler();
|
|
|
|
|
}
|
2018-09-10 02:41:13 +02:00
|
|
|
|
2019-06-24 20:54:51 +10:00
|
|
|
return null;
|
2015-05-07 13:50:19 +10:00
|
|
|
}
|
2012-02-23 15:08:50 -08:00
|
|
|
}
|
2012-12-17 18:08:51 +02:00
|
|
|
}
|