Skip to content

Commit

Permalink
v23 release changes: Upgrade to latest CefSharp/Chrome v92. Added han…
Browse files Browse the repository at this point in the history
…dler overrides for Authentication / Login Credentials with a dialog box prompt. Show messages for page loading errors
  • Loading branch information
Hou5e committed Sep 17, 2021
1 parent 7085928 commit 55f0157
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 47 deletions.
28 changes: 14 additions & 14 deletions Browser/FoldingBrowser.vbproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\CefSharp.Common.92.0.251\build\CefSharp.Common.props" Condition="Exists('packages\CefSharp.Common.92.0.251\build\CefSharp.Common.props')" />
<Import Project="packages\cef.redist.x86.92.0.25\build\cef.redist.x86.props" Condition="Exists('packages\cef.redist.x86.92.0.25\build\cef.redist.x86.props')" />
<Import Project="packages\cef.redist.x64.92.0.25\build\cef.redist.x64.props" Condition="Exists('packages\cef.redist.x64.92.0.25\build\cef.redist.x64.props')" />
<Import Project="packages\CefSharp.Common.92.0.260\build\CefSharp.Common.props" Condition="Exists('packages\CefSharp.Common.92.0.260\build\CefSharp.Common.props')" />
<Import Project="packages\cef.redist.x86.92.0.26\build\cef.redist.x86.props" Condition="Exists('packages\cef.redist.x86.92.0.26\build\cef.redist.x86.props')" />
<Import Project="packages\cef.redist.x64.92.0.26\build\cef.redist.x64.props" Condition="Exists('packages\cef.redist.x64.92.0.26\build\cef.redist.x64.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
Expand Down Expand Up @@ -63,16 +63,16 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Reference Include="CefSharp, Version=92.0.251.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=MSIL">
<HintPath>packages\CefSharp.Common.92.0.251\lib\net452\CefSharp.dll</HintPath>
<Reference Include="CefSharp, Version=92.0.260.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=MSIL">
<HintPath>packages\CefSharp.Common.92.0.260\lib\net452\CefSharp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="CefSharp.Core, Version=92.0.251.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=MSIL">
<HintPath>packages\CefSharp.Common.92.0.251\lib\net452\CefSharp.Core.dll</HintPath>
<Reference Include="CefSharp.Core, Version=92.0.260.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=MSIL">
<HintPath>packages\CefSharp.Common.92.0.260\lib\net452\CefSharp.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="CefSharp.WinForms, Version=92.0.251.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=MSIL">
<HintPath>packages\CefSharp.WinForms.92.0.251\lib\net452\CefSharp.WinForms.dll</HintPath>
<Reference Include="CefSharp.WinForms, Version=92.0.260.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=MSIL">
<HintPath>packages\CefSharp.WinForms.92.0.260\lib\net452\CefSharp.WinForms.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down Expand Up @@ -305,12 +305,12 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\cef.redist.x64.92.0.25\build\cef.redist.x64.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\cef.redist.x64.92.0.25\build\cef.redist.x64.props'))" />
<Error Condition="!Exists('packages\cef.redist.x86.92.0.25\build\cef.redist.x86.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\cef.redist.x86.92.0.25\build\cef.redist.x86.props'))" />
<Error Condition="!Exists('packages\CefSharp.Common.92.0.251\build\CefSharp.Common.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\CefSharp.Common.92.0.251\build\CefSharp.Common.props'))" />
<Error Condition="!Exists('packages\CefSharp.Common.92.0.251\build\CefSharp.Common.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\CefSharp.Common.92.0.251\build\CefSharp.Common.targets'))" />
<Error Condition="!Exists('packages\cef.redist.x64.92.0.26\build\cef.redist.x64.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\cef.redist.x64.92.0.26\build\cef.redist.x64.props'))" />
<Error Condition="!Exists('packages\cef.redist.x86.92.0.26\build\cef.redist.x86.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\cef.redist.x86.92.0.26\build\cef.redist.x86.props'))" />
<Error Condition="!Exists('packages\CefSharp.Common.92.0.260\build\CefSharp.Common.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\CefSharp.Common.92.0.260\build\CefSharp.Common.props'))" />
<Error Condition="!Exists('packages\CefSharp.Common.92.0.260\build\CefSharp.Common.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\CefSharp.Common.92.0.260\build\CefSharp.Common.targets'))" />
</Target>
<Import Project="packages\CefSharp.Common.92.0.251\build\CefSharp.Common.targets" Condition="Exists('packages\CefSharp.Common.92.0.251\build\CefSharp.Common.targets')" />
<Import Project="packages\CefSharp.Common.92.0.260\build\CefSharp.Common.targets" Condition="Exists('packages\CefSharp.Common.92.0.260\build\CefSharp.Common.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
77 changes: 77 additions & 0 deletions Browser/GlobalRefs.vb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,83 @@ Public Module GlobalRefs
End Module


'Authentication / Login Credentials, See: https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/RequestEventHandler/RequestEventHandler.cs
<CLSCompliant(False)> Public Class MyRequestHandler
Inherits CefSharp.Handler.RequestHandler

Public m_strUser As String = String.Empty
Public m_strPwd As String = String.Empty
Public m_bPropmtForCredentials As Boolean = True

'Login Credentials needed from the user. Return true to continue the request, get credentials if needed, then call callback.Continue(Username, Password). Return false to cancel the request.
Protected Overrides Function GetAuthCredentials(chromiumWebBrowser As CefSharp.IWebBrowser, browser As CefSharp.IBrowser, originUrl As String, isProxy As Boolean, host As String, port As Integer, realm As String, scheme As String, callback As CefSharp.IAuthCallback) As Boolean
Dim bReturn As Boolean = False

'Manual browsing (not automated). Preload the message box defaults with the username and password before calling this
If m_bPropmtForCredentials = True Then
'Message box prompt to get credentials
Dim TxtEntry As New TextEntryDialog
TxtEntry.Text = "Enter User/PW"
TxtEntry.MsgTextUpper.Text = "Enter Username (Top text box):"
TxtEntry.MsgTextLower.Text = "And Password (Bottom text box):"

TxtEntry.TextEnteredUpper.TextAlign = HorizontalAlignment.Center
TxtEntry.TextEnteredLower.TextAlign = HorizontalAlignment.Center
'Setup password text box
TxtEntry.TextEnteredLower.Visible = True
TxtEntry.TextEnteredLower.PasswordChar = "*"c
'Set default values
TxtEntry.TextEnteredUpper.Text = m_strUser
TxtEntry.TextEnteredLower.Text = m_strPwd
'Adjust the width of the dialog box
TxtEntry.Width = (TxtEntry.MsgTextLower.Left * 2) + TxtEntry.MsgTextLower.Width + 50
TxtEntry.TextEnteredLower.Visible = True
TxtEntry.MsgTextExtraBottomNote.Visible = False
TxtEntry.ActiveControl = TxtEntry.TextEnteredUpper
TxtEntry.StartPosition = FormStartPosition.CenterScreen

'Show modal dialog box
If TxtEntry.ShowDialog() = DialogResult.OK Then
m_strUser = TxtEntry.TextEnteredUpper.Text
m_strPwd = TxtEntry.TextEnteredLower.Text

'Send credentials to load the web page
callback.Continue(m_strUser, m_strPwd)
'Reset value
m_strPwd = String.Empty
'Good
bReturn = True
Else
'Invalid credentials
g_Main.Msg("Error: Invalid credentials: Dialog canceled")
bReturn = False
End If
TxtEntry.Dispose()

Else
'Automated: Set m_bPropmtForCredentials = false, and the username and password before loading the URL
If m_strUser.Length > 0 AndAlso m_strPwd.Length > 0 Then
callback.Continue(m_strUser, m_strPwd)
'Good
bReturn = True
Else
'Invalid credentials
g_Main.Msg("Error:Invalid credentials: Username or password were blank")
bReturn = False
End If
End If

Return bReturn
End Function

'Not used. Instead using the browser initialization: settings.CefCommandLineArgs.Add("ignore-certificate-errors")
''Handles requests for URLs with an invalid SSL certificate. Return true and call CefSharp.IRequestCallback.Continue(System.Boolean) either in this method or at a later time to continue or cancel the request. If "ignore-certificate-errors" is used, then all invalid certificates will be accepted without calling this.
'Protected Overrides Function OnCertificateError(ByVal browserControl As CefSharp.IWebBrowser, ByVal browser As CefSharp.IBrowser, ByVal errorCode As CefSharp.CefErrorCode, ByVal requestUrl As String, ByVal sslInfo As CefSharp.ISslInfo, ByVal callback As CefSharp.IRequestCallback) As Boolean
' Return True
'End Function
End Class


'File download, see: https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/DownloadHandler.cs
Public Class DownloadHandler
Implements CefSharp.IDownloadHandler
Expand Down
65 changes: 38 additions & 27 deletions Browser/Main.vb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
settings.LocalesDirPath = System.IO.Path.Combine(My.Application.Info.DirectoryPath, "locales")
settings.Locale = "en-US"
settings.AcceptLanguageList = settings.Locale & "," & settings.Locale.Substring(0, 2)
'Use the commandline instead of this (not working since v75): settings.IgnoreCertificateErrors = True
settings.CefCommandLineArgs.Add("ignore-certificate-errors")

'For Debugging, to log everything, use: Verbose. Can cause older versions of Cef.Shutdown() to hang
'settings.LogSeverity = CefSharp.LogSeverity.Verbose
Expand All @@ -79,14 +81,17 @@

CefSharp.Cef.EnableHighDPISupport()
If CefSharp.Cef.Initialize(settings) = True Then
Me.browser = New CefSharp.WinForms.ChromiumWebBrowser(URL_BLANK)
Me.browser = New CefSharp.WinForms.ChromiumWebBrowser(String.Empty)
'Add browser event handlers to pass events back to the main UI
AddHandler Me.browser.FrameLoadEnd, AddressOf OnBrowserFrameLoadEnd
AddHandler Me.browser.ConsoleMessage, AddressOf OnBrowserConsoleMessage
AddHandler Me.browser.LoadError, AddressOf OnBrowserLoadError
AddHandler Me.browser.StatusMessage, AddressOf OnBrowserStatusMessage
AddHandler Me.browser.LoadingStateChanged, AddressOf OnBrowserLoadingStateChanged
AddHandler Me.browser.TitleChanged, AddressOf OnBrowserTitleChanged
AddHandler Me.browser.AddressChanged, AddressOf OnBrowserAddressChanged
'Add Authentication / Login Credentials handler
Me.browser.RequestHandler = New MyRequestHandler()
'Add keypress handler: ESC to cancel Navigation, F5 to Refresh, CTRL+F for Find, ...
Me.browser.KeyboardHandler = New KeyboardHandler()
'Add download handler
Expand Down Expand Up @@ -613,18 +618,20 @@
'Keep this before CefSharp.Cef.Shutdown() to avoid the browser hanging (CefSharp v53.0.1): when exiting the wallet & click yes, then close the Browser (like the javascript running causes the hang)
'StopNavigaion() 'In v55, this appears to cause: Exception thrown: 'System.Exception' in CefSharp.dll
g_bCancelNav = True
ClearWebpage()
'Added in v63.0.3. Exiting the FAH Web control was hanging the Cef.Shutdown(). On shutdown, the FAH Web control error was happening, and reloading w/o cache when closing. 'Verbose' debug logging was getting cutoff too, and changing logging to 'info' helped fix it
g_bCancelNav = True
Delay(200)

If Me.browser IsNot Nothing Then
ClearWebpage()
'Added in v63.0.3. Exiting the FAH Web control was hanging the Cef.Shutdown(). On shutdown, the FAH Web control error was happening, and reloading w/o cache when closing. 'Verbose' debug logging was getting cutoff too, and changing logging to 'info' helped fix it
g_bCancelNav = True
Delay(200)

RemoveHandler Me.browser.FrameLoadEnd, AddressOf OnBrowserFrameLoadEnd
RemoveHandler Me.browser.ConsoleMessage, AddressOf OnBrowserConsoleMessage
RemoveHandler Me.browser.LoadError, AddressOf OnBrowserLoadError
RemoveHandler Me.browser.StatusMessage, AddressOf OnBrowserStatusMessage
RemoveHandler Me.browser.LoadingStateChanged, AddressOf OnBrowserLoadingStateChanged
RemoveHandler Me.browser.TitleChanged, AddressOf OnBrowserTitleChanged
RemoveHandler Me.browser.AddressChanged, AddressOf OnBrowserAddressChanged
Me.browser.RequestHandler = Nothing
Me.browser.KeyboardHandler = Nothing
Me.browser.DownloadHandler = Nothing

Expand Down Expand Up @@ -2974,23 +2981,33 @@
End If
End Sub

Private Sub OnBrowserStatusMessage(sender As Object, args As CefSharp.StatusMessageEventArgs)
Private Sub OnBrowserLoadError(sender As Object, e As CefSharp.LoadErrorEventArgs)
Me.Invoke(Sub()
If args.Value.Length = 0 Then
'Display the info
Dim strErrorMsg As String = "Error: " & e.ErrorText & ", URL: " & e.FailedUrl
Me.lblHoverURL.Text = strErrorMsg
Me.lblHoverURL.Visible = True
Me.txtMsg.AppendText("[" & Now.ToString(LogDateTimeFormat) & "] " & strErrorMsg & vbNewLine)
End Sub)
End Sub

Private Sub OnBrowserStatusMessage(sender As Object, e As CefSharp.StatusMessageEventArgs)
Me.Invoke(Sub()
If e.Value.Length = 0 Then
'Hide the info text
Me.lblHoverURL.Visible = False
Me.lblHoverURL.Text = ""
Else
'Display the info
Me.lblHoverURL.Text = args.Value
Me.lblHoverURL.Text = e.Value
Me.lblHoverURL.Visible = True
Me.txtMsg.AppendText("[" & Now.ToString(LogDateTimeFormat) & "] " & args.Value & vbNewLine)
Me.txtMsg.AppendText("[" & Now.ToString(LogDateTimeFormat) & "] " & e.Value & vbNewLine)
End If
End Sub)
End Sub

Private Sub OnBrowserLoadingStateChanged(sender As Object, args As CefSharp.LoadingStateChangedEventArgs)
enableButtons(args.CanGoForward, args.CanGoBack, Not args.CanReload)
Private Sub OnBrowserLoadingStateChanged(sender As Object, e As CefSharp.LoadingStateChangedEventArgs)
enableButtons(e.CanGoForward, e.CanGoBack, Not e.CanReload)
End Sub
Private Sub enableButtons(bForward As Boolean, bBack As Boolean, bLoading As Boolean)
Me.Invoke(Sub()
Expand All @@ -3003,17 +3020,17 @@
End Sub)
End Sub

Private Sub OnBrowserTitleChanged(sender As Object, args As CefSharp.TitleChangedEventArgs)
updateTitle(args.Title)
Private Sub OnBrowserTitleChanged(sender As Object, e As CefSharp.TitleChangedEventArgs)
updateTitle(e.Title)
End Sub
Private Sub updateTitle(strTitle As String)
Me.Invoke(Sub()
Me.Text = If(strTitle.Length = 0, "", strTitle & " - ") & g_strTitleEnd
End Sub)
End Sub

Private Sub OnBrowserAddressChanged(sender As Object, args As CefSharp.AddressChangedEventArgs)
updateURL(args.Address)
Private Sub OnBrowserAddressChanged(sender As Object, e As CefSharp.AddressChangedEventArgs)
updateURL(e.Address)
End Sub
Private Sub updateURL(strURL As String)
Me.Invoke(Sub()
Expand Down Expand Up @@ -3281,7 +3298,7 @@
#Enable Warning BC42358
End Sub

Private Sub btnStopNavigation_Click(sender As System.Object, e As System.EventArgs) Handles btnStopNav.Click
Private Sub btnStopNav_Click(sender As System.Object, e As System.EventArgs) Handles btnStopNav.Click
StopNavigaion()
End Sub

Expand Down Expand Up @@ -3464,7 +3481,9 @@
m_strFindText = ""
Me.pnlFind.Visible = False
End Sub
#End Region

#Region "Browser - Open URL"
'Open URL with the specified settings
Public Async Function OpenURL(sURL As String, Optional bShowErrorDialogBoxes As Boolean = False) As Threading.Tasks.Task(Of Boolean)
Try
Expand All @@ -3475,17 +3494,14 @@
'Update to the displayed text to the new URL
Me.txtURL.Text = sURL

'Accept Certs to avoid annoying prompts
System.Net.ServicePointManager.ServerCertificateValidationCallback = New Net.Security.RemoteCertificateValidationCallback(AddressOf ValidateCertificate)

'Focus the URL text box so the KeyPress events work for Enter / ESC.
Me.txtURL.BackColor = Color.FromKnownColor(KnownColor.Window)
Me.txtURL.Focus()
Me.txtURL.Select(Me.txtURL.Text.Length, 0)
'Reset flag
m_bPageLoaded = False
m_strPageURL = Me.txtURL.Text
Me.browser.Load(Me.txtURL.Text)
m_strPageURL = sURL
Me.browser.Load(sURL)
'Wait for the web page or 30 seconds
Await PageLoadWait()

Expand Down Expand Up @@ -3566,11 +3582,6 @@
Return False
End Function

Public Function ValidateCertificate(sender As Object, certificate As Security.Cryptography.X509Certificates.X509Certificate, chain As Security.Cryptography.X509Certificates.X509Chain, sslPolicyErrors As Net.Security.SslPolicyErrors) As Boolean
'Return True to force the certificate to be accepted.
Return True
End Function

'Clear Web page
Public Function ClearWebpage() As Boolean
ClearWebpage = False
Expand Down
2 changes: 1 addition & 1 deletion Browser/My Project/app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<assemblyIdentity version="21.0.0.0" name="FoldingBrowser.app" />
<assemblyIdentity version="23.0.0.0" name="FoldingBrowser.app" />

<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<dependency>
Expand Down
1 change: 0 additions & 1 deletion Browser/TextEntryDialog.vb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
Me.btnOK.PerformClick()
End If
End If

End Sub

Private Sub TextEnteredLower_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextEnteredLower.KeyPress
Expand Down
Loading

0 comments on commit 55f0157

Please sign in to comment.