2018-09-10 02:41:13 +02:00
// Copyright © 2015 The CefSharp Authors. All rights reserved.
2015-09-18 13:22:11 +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 ;
2016-12-12 15:48:15 -08:00
using System.Security.Cryptography.X509Certificates ;
2018-09-10 02:41:13 +02:00
using System.Windows.Forms ;
2018-09-17 00:46:21 +02:00
using CefSharp.Example.Handlers ;
2015-09-18 13:22:11 +10:00
namespace CefSharp.WinForms.Example.Handlers
{
2019-06-24 20:39:24 +10:00
public class WinFormsRequestHandler : ExampleRequestHandler
2016-12-12 15:48:15 -08:00
{
2019-06-24 21:22:20 +10:00
private readonly Action < string , int? > openNewTab ;
2016-12-12 15:48:15 -08:00
public WinFormsRequestHandler ( Action < string , int? > openNewTab )
{
this . openNewTab = openNewTab ;
}
2019-07-20 16:26:25 +10:00
protected override bool OnOpenUrlFromTab ( IWebBrowser chromiumWebBrowser , IBrowser browser , IFrame frame , string targetUrl , WindowOpenDisposition targetDisposition , bool userGesture )
2016-12-12 15:48:15 -08:00
{
2018-09-10 02:41:13 +02:00
if ( openNewTab = = null )
2016-12-12 15:48:15 -08:00
{
return false ;
}
2019-07-20 16:26:25 +10:00
var control = ( Control ) chromiumWebBrowser ;
2016-12-12 15:48:15 -08:00
control . InvokeOnUiThreadIfRequired ( delegate ( )
{
openNewTab ( targetUrl , null ) ;
2018-09-10 02:41:13 +02:00
} ) ;
2016-12-12 15:48:15 -08:00
return true ;
}
2019-07-20 16:26:25 +10:00
protected override bool OnSelectClientCertificate ( IWebBrowser chromiumWebBrowser , IBrowser browser , bool isProxy , string host , int port , X509Certificate2Collection certificates , ISelectClientCertificateCallback callback )
2016-12-12 15:48:15 -08:00
{
2019-07-20 16:26:25 +10:00
var control = ( Control ) chromiumWebBrowser ;
2016-12-13 10:20:25 +10:00
2018-09-10 02:41:13 +02:00
control . InvokeOnUiThreadIfRequired ( delegate ( )
2016-12-12 15:48:15 -08:00
{
2016-12-13 10:20:25 +10:00
var selectedCertificateCollection = X509Certificate2UI . SelectFromCollection ( certificates , "Certificates Dialog" , "Select Certificate for authentication" , X509SelectionFlag . SingleSelection ) ;
2019-07-31 19:41:08 +10:00
if ( selectedCertificateCollection . Count > 0 )
{
//X509Certificate2UI.SelectFromCollection returns a collection, we've used SingleSelection, so just take the first
//The underlying CEF implementation only accepts a single certificate
callback . Select ( selectedCertificateCollection [ 0 ] ) ;
}
else
{
//User canceled no certificate should be selected.
callback . Select ( null ) ;
}
2018-09-10 02:41:13 +02:00
} ) ;
2016-12-12 15:48:15 -08:00
return true ;
}
}
2015-09-18 13:22:11 +10:00
}