SEBWIN-395: Fixed issue with request filter blocking load error page.

This commit is contained in:
Damian Büchel 2020-05-15 16:13:15 +02:00
parent 03cf219c9b
commit 9ec8130a8a
5 changed files with 58 additions and 35 deletions

View file

@ -9,6 +9,7 @@
using System; using System;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using CefSharp;
using SafeExamBrowser.Applications.Contracts; using SafeExamBrowser.Applications.Contracts;
using SafeExamBrowser.Applications.Contracts.Events; using SafeExamBrowser.Applications.Contracts.Events;
using SafeExamBrowser.Applications.Contracts.Resources.Icons; using SafeExamBrowser.Applications.Contracts.Resources.Icons;
@ -17,7 +18,6 @@ using SafeExamBrowser.Browser.Contracts.Filters;
using SafeExamBrowser.Browser.Events; using SafeExamBrowser.Browser.Events;
using SafeExamBrowser.Browser.Filters; using SafeExamBrowser.Browser.Filters;
using SafeExamBrowser.Browser.Handlers; using SafeExamBrowser.Browser.Handlers;
using SafeExamBrowser.Browser.Pages;
using SafeExamBrowser.Configuration.Contracts; using SafeExamBrowser.Configuration.Contracts;
using SafeExamBrowser.I18n.Contracts; using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.Logging.Contracts;
@ -28,6 +28,9 @@ using SafeExamBrowser.UserInterface.Contracts.Browser;
using SafeExamBrowser.UserInterface.Contracts.Browser.Data; using SafeExamBrowser.UserInterface.Contracts.Browser.Data;
using SafeExamBrowser.UserInterface.Contracts.FileSystemDialog; using SafeExamBrowser.UserInterface.Contracts.FileSystemDialog;
using SafeExamBrowser.UserInterface.Contracts.MessageBox; using SafeExamBrowser.UserInterface.Contracts.MessageBox;
using BrowserSettings = SafeExamBrowser.Settings.Browser.BrowserSettings;
using Request = SafeExamBrowser.Browser.Contracts.Filters.Request;
using TitleChangedEventHandler = SafeExamBrowser.Applications.Contracts.Events.TitleChangedEventHandler;
namespace SafeExamBrowser.Browser namespace SafeExamBrowser.Browser
{ {
@ -112,12 +115,10 @@ namespace SafeExamBrowser.Browser
private void InitializeControl() private void InitializeControl()
{ {
var contextMenuHandler = new ContextMenuHandler(); var contextMenuHandler = new ContextMenuHandler();
var controlLogger = logger.CloneFor($"{nameof(BrowserControl)} #{Id}");
var dialogHandler = new DialogHandler(); var dialogHandler = new DialogHandler();
var displayHandler = new DisplayHandler(); var displayHandler = new DisplayHandler();
var downloadLogger = logger.CloneFor($"{nameof(DownloadHandler)} #{Id}"); var downloadLogger = logger.CloneFor($"{nameof(DownloadHandler)} #{Id}");
var downloadHandler = new DownloadHandler(appConfig, settings, downloadLogger); var downloadHandler = new DownloadHandler(appConfig, settings, downloadLogger);
var htmlLoader = new HtmlLoader(text);
var keyboardHandler = new KeyboardHandler(); var keyboardHandler = new KeyboardHandler();
var lifeSpanHandler = new LifeSpanHandler(); var lifeSpanHandler = new LifeSpanHandler();
var requestFilter = new RequestFilter(); var requestFilter = new RequestFilter();
@ -146,13 +147,12 @@ namespace SafeExamBrowser.Browser
dialogHandler, dialogHandler,
displayHandler, displayHandler,
downloadHandler, downloadHandler,
htmlLoader,
keyboardHandler, keyboardHandler,
lifeSpanHandler, lifeSpanHandler,
controlLogger,
requestHandler, requestHandler,
startUrl); startUrl);
control.AddressChanged += Control_AddressChanged; control.AddressChanged += Control_AddressChanged;
control.LoadFailed += Control_LoadFailed;
control.LoadingStateChanged += Control_LoadingStateChanged; control.LoadingStateChanged += Control_LoadingStateChanged;
control.TitleChanged += Control_TitleChanged; control.TitleChanged += Control_TitleChanged;
@ -214,6 +214,30 @@ namespace SafeExamBrowser.Browser
window.UpdateAddress(address); window.UpdateAddress(address);
} }
/// <summary>
/// TODO: LoadError.html is not used, as navigating back from it doesn't work! Remove page if no better solution can be found.
/// </summary>
private void Control_LoadFailed(int errorCode, string errorText, string url)
{
if (errorCode == (int) CefErrorCode.None)
{
logger.Info($"Request for '{url}' was successful.");
}
else if (errorCode == (int) CefErrorCode.Aborted)
{
logger.Info($"Request for '{url}' was aborted.");
}
else
{
var title = text.Get(TextKey.Browser_LoadErrorPageTitle);
var message = text.Get(TextKey.Browser_LoadErrorPageMessage).Replace("%%URL%%", url) + $" {errorText} ({errorCode})";
logger.Warn($"Request for '{url}' failed: {errorText} ({errorCode}).");
Task.Run(() => messageBox.Show(message, title, icon: MessageBoxIcon.Error, parent: window)).ContinueWith(_ => control.NavigateBackwards());
}
}
private void Control_LoadingStateChanged(bool isLoading) private void Control_LoadingStateChanged(bool isLoading)
{ {
window.CanNavigateBackwards = WindowSettings.AllowBackwardNavigation && control.CanNavigateBackwards; window.CanNavigateBackwards = WindowSettings.AllowBackwardNavigation && control.CanNavigateBackwards;

View file

@ -8,8 +8,6 @@
using CefSharp; using CefSharp;
using CefSharp.WinForms; using CefSharp.WinForms;
using SafeExamBrowser.Browser.Pages;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.UserInterface.Contracts.Browser; using SafeExamBrowser.UserInterface.Contracts.Browser;
using SafeExamBrowser.UserInterface.Contracts.Browser.Events; using SafeExamBrowser.UserInterface.Contracts.Browser.Events;
@ -21,14 +19,12 @@ namespace SafeExamBrowser.Browser
private IDialogHandler dialogHandler; private IDialogHandler dialogHandler;
private IDisplayHandler displayHandler; private IDisplayHandler displayHandler;
private IDownloadHandler downloadHandler; private IDownloadHandler downloadHandler;
private string errorPage;
private HtmlLoader htmlLoader;
private IKeyboardHandler keyboardHandler; private IKeyboardHandler keyboardHandler;
private ILifeSpanHandler lifeSpanHandler; private ILifeSpanHandler lifeSpanHandler;
private ILogger logger;
private IRequestHandler requestHandler; private IRequestHandler requestHandler;
private AddressChangedEventHandler addressChanged; private AddressChangedEventHandler addressChanged;
private LoadFailedEventHandler loadFailed;
private LoadingStateChangedEventHandler loadingStateChanged; private LoadingStateChangedEventHandler loadingStateChanged;
private TitleChangedEventHandler titleChanged; private TitleChangedEventHandler titleChanged;
@ -41,6 +37,12 @@ namespace SafeExamBrowser.Browser
remove { addressChanged -= value; } remove { addressChanged -= value; }
} }
event LoadFailedEventHandler IBrowserControl.LoadFailed
{
add { loadFailed += value; }
remove { loadFailed -= value; }
}
event LoadingStateChangedEventHandler IBrowserControl.LoadingStateChanged event LoadingStateChangedEventHandler IBrowserControl.LoadingStateChanged
{ {
add { loadingStateChanged += value; } add { loadingStateChanged += value; }
@ -58,10 +60,8 @@ namespace SafeExamBrowser.Browser
IDialogHandler dialogHandler, IDialogHandler dialogHandler,
IDisplayHandler displayHandler, IDisplayHandler displayHandler,
IDownloadHandler downloadHandler, IDownloadHandler downloadHandler,
HtmlLoader htmlLoader,
IKeyboardHandler keyboardHandler, IKeyboardHandler keyboardHandler,
ILifeSpanHandler lifeSpanHandler, ILifeSpanHandler lifeSpanHandler,
ILogger logger,
IRequestHandler requestHandler, IRequestHandler requestHandler,
string url) : base(url) string url) : base(url)
{ {
@ -69,10 +69,8 @@ namespace SafeExamBrowser.Browser
this.dialogHandler = dialogHandler; this.dialogHandler = dialogHandler;
this.displayHandler = displayHandler; this.displayHandler = displayHandler;
this.downloadHandler = downloadHandler; this.downloadHandler = downloadHandler;
this.htmlLoader = htmlLoader;
this.keyboardHandler = keyboardHandler; this.keyboardHandler = keyboardHandler;
this.lifeSpanHandler = lifeSpanHandler; this.lifeSpanHandler = lifeSpanHandler;
this.logger = logger;
this.requestHandler = requestHandler; this.requestHandler = requestHandler;
} }
@ -90,8 +88,6 @@ namespace SafeExamBrowser.Browser
LifeSpanHandler = lifeSpanHandler; LifeSpanHandler = lifeSpanHandler;
MenuHandler = contextMenuHandler; MenuHandler = contextMenuHandler;
RequestHandler = requestHandler; RequestHandler = requestHandler;
errorPage = htmlLoader.LoadErrorPage();
} }
public void NavigateBackwards() public void NavigateBackwards()
@ -126,25 +122,7 @@ namespace SafeExamBrowser.Browser
private void BrowserControl_LoadError(object sender, LoadErrorEventArgs e) private void BrowserControl_LoadError(object sender, LoadErrorEventArgs e)
{ {
if (e.ErrorCode == CefErrorCode.None) loadFailed?.Invoke((int) e.ErrorCode, e.ErrorText, e.FailedUrl);
{
logger.Info($"Request for '{e.FailedUrl}' was successful.");
}
else if (e.ErrorCode == CefErrorCode.Aborted)
{
logger.Info($"Request for '{e.FailedUrl}' was aborted.");
}
else
{
var html = string.Copy(errorPage);
logger.Warn($"Request for '{e.FailedUrl}' failed: {e.ErrorText} ({e.ErrorCode}).");
html = html.Replace("%%STATUS%%", $"{e.ErrorText} ({e.ErrorCode})");
html = html.Replace("%%URL%%", e.FailedUrl);
e.Frame.LoadHtml(html, true);
}
} }
} }
} }

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2020 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
namespace SafeExamBrowser.UserInterface.Contracts.Browser.Events
{
/// <summary>
/// Indicates a load error for a browser request.
/// </summary>
public delegate void LoadFailedEventHandler(int errorCode, string errorText, string url);
}

View file

@ -36,6 +36,11 @@ namespace SafeExamBrowser.UserInterface.Contracts.Browser
/// </summary> /// </summary>
event AddressChangedEventHandler AddressChanged; event AddressChangedEventHandler AddressChanged;
/// <summary>
/// Event fired when a load error occurs.
/// </summary>
event LoadFailedEventHandler LoadFailed;
/// <summary> /// <summary>
/// Event fired when the loading state of the browser control changes. /// Event fired when the loading state of the browser control changes.
/// </summary> /// </summary>

View file

@ -56,6 +56,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Browser\Data\DownloadItemState.cs" /> <Compile Include="Browser\Data\DownloadItemState.cs" />
<Compile Include="Browser\Events\AddressChangedEventHandler.cs" /> <Compile Include="Browser\Events\AddressChangedEventHandler.cs" />
<Compile Include="Browser\Events\LoadFailedEventHandler.cs" />
<Compile Include="Browser\Events\LoadingStateChangedEventHandler.cs" /> <Compile Include="Browser\Events\LoadingStateChangedEventHandler.cs" />
<Compile Include="Browser\Events\TitleChangedEventHandler.cs" /> <Compile Include="Browser\Events\TitleChangedEventHandler.cs" />
<Compile Include="Browser\IBrowserControl.cs" /> <Compile Include="Browser\IBrowserControl.cs" />