diff --git a/SafeExamBrowser.Browser/BrowserApplicationInstance.cs b/SafeExamBrowser.Browser/BrowserApplicationInstance.cs index 6a02d851..b8d10e9d 100644 --- a/SafeExamBrowser.Browser/BrowserApplicationInstance.cs +++ b/SafeExamBrowser.Browser/BrowserApplicationInstance.cs @@ -9,6 +9,7 @@ using System; using System.Net.Http; using System.Threading.Tasks; +using CefSharp; using SafeExamBrowser.Applications.Contracts; using SafeExamBrowser.Applications.Contracts.Events; using SafeExamBrowser.Applications.Contracts.Resources.Icons; @@ -17,7 +18,6 @@ using SafeExamBrowser.Browser.Contracts.Filters; using SafeExamBrowser.Browser.Events; using SafeExamBrowser.Browser.Filters; using SafeExamBrowser.Browser.Handlers; -using SafeExamBrowser.Browser.Pages; using SafeExamBrowser.Configuration.Contracts; using SafeExamBrowser.I18n.Contracts; using SafeExamBrowser.Logging.Contracts; @@ -28,6 +28,9 @@ using SafeExamBrowser.UserInterface.Contracts.Browser; using SafeExamBrowser.UserInterface.Contracts.Browser.Data; using SafeExamBrowser.UserInterface.Contracts.FileSystemDialog; 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 { @@ -112,12 +115,10 @@ namespace SafeExamBrowser.Browser private void InitializeControl() { var contextMenuHandler = new ContextMenuHandler(); - var controlLogger = logger.CloneFor($"{nameof(BrowserControl)} #{Id}"); var dialogHandler = new DialogHandler(); var displayHandler = new DisplayHandler(); var downloadLogger = logger.CloneFor($"{nameof(DownloadHandler)} #{Id}"); var downloadHandler = new DownloadHandler(appConfig, settings, downloadLogger); - var htmlLoader = new HtmlLoader(text); var keyboardHandler = new KeyboardHandler(); var lifeSpanHandler = new LifeSpanHandler(); var requestFilter = new RequestFilter(); @@ -146,13 +147,12 @@ namespace SafeExamBrowser.Browser dialogHandler, displayHandler, downloadHandler, - htmlLoader, keyboardHandler, lifeSpanHandler, - controlLogger, requestHandler, startUrl); control.AddressChanged += Control_AddressChanged; + control.LoadFailed += Control_LoadFailed; control.LoadingStateChanged += Control_LoadingStateChanged; control.TitleChanged += Control_TitleChanged; @@ -214,6 +214,30 @@ namespace SafeExamBrowser.Browser window.UpdateAddress(address); } + /// + /// TODO: LoadError.html is not used, as navigating back from it doesn't work! Remove page if no better solution can be found. + /// + 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) { window.CanNavigateBackwards = WindowSettings.AllowBackwardNavigation && control.CanNavigateBackwards; diff --git a/SafeExamBrowser.Browser/BrowserControl.cs b/SafeExamBrowser.Browser/BrowserControl.cs index 4fb73e6c..1d5dd331 100644 --- a/SafeExamBrowser.Browser/BrowserControl.cs +++ b/SafeExamBrowser.Browser/BrowserControl.cs @@ -8,8 +8,6 @@ using CefSharp; using CefSharp.WinForms; -using SafeExamBrowser.Browser.Pages; -using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.UserInterface.Contracts.Browser; using SafeExamBrowser.UserInterface.Contracts.Browser.Events; @@ -21,14 +19,12 @@ namespace SafeExamBrowser.Browser private IDialogHandler dialogHandler; private IDisplayHandler displayHandler; private IDownloadHandler downloadHandler; - private string errorPage; - private HtmlLoader htmlLoader; private IKeyboardHandler keyboardHandler; private ILifeSpanHandler lifeSpanHandler; - private ILogger logger; private IRequestHandler requestHandler; private AddressChangedEventHandler addressChanged; + private LoadFailedEventHandler loadFailed; private LoadingStateChangedEventHandler loadingStateChanged; private TitleChangedEventHandler titleChanged; @@ -41,6 +37,12 @@ namespace SafeExamBrowser.Browser remove { addressChanged -= value; } } + event LoadFailedEventHandler IBrowserControl.LoadFailed + { + add { loadFailed += value; } + remove { loadFailed -= value; } + } + event LoadingStateChangedEventHandler IBrowserControl.LoadingStateChanged { add { loadingStateChanged += value; } @@ -58,10 +60,8 @@ namespace SafeExamBrowser.Browser IDialogHandler dialogHandler, IDisplayHandler displayHandler, IDownloadHandler downloadHandler, - HtmlLoader htmlLoader, IKeyboardHandler keyboardHandler, ILifeSpanHandler lifeSpanHandler, - ILogger logger, IRequestHandler requestHandler, string url) : base(url) { @@ -69,10 +69,8 @@ namespace SafeExamBrowser.Browser this.dialogHandler = dialogHandler; this.displayHandler = displayHandler; this.downloadHandler = downloadHandler; - this.htmlLoader = htmlLoader; this.keyboardHandler = keyboardHandler; this.lifeSpanHandler = lifeSpanHandler; - this.logger = logger; this.requestHandler = requestHandler; } @@ -90,8 +88,6 @@ namespace SafeExamBrowser.Browser LifeSpanHandler = lifeSpanHandler; MenuHandler = contextMenuHandler; RequestHandler = requestHandler; - - errorPage = htmlLoader.LoadErrorPage(); } public void NavigateBackwards() @@ -126,25 +122,7 @@ namespace SafeExamBrowser.Browser private void BrowserControl_LoadError(object sender, LoadErrorEventArgs e) { - if (e.ErrorCode == CefErrorCode.None) - { - 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); - } + loadFailed?.Invoke((int) e.ErrorCode, e.ErrorText, e.FailedUrl); } } } diff --git a/SafeExamBrowser.UserInterface.Contracts/Browser/Events/LoadFailedEventHandler.cs b/SafeExamBrowser.UserInterface.Contracts/Browser/Events/LoadFailedEventHandler.cs new file mode 100644 index 00000000..749b4146 --- /dev/null +++ b/SafeExamBrowser.UserInterface.Contracts/Browser/Events/LoadFailedEventHandler.cs @@ -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 +{ + /// + /// Indicates a load error for a browser request. + /// + public delegate void LoadFailedEventHandler(int errorCode, string errorText, string url); +} diff --git a/SafeExamBrowser.UserInterface.Contracts/Browser/IBrowserControl.cs b/SafeExamBrowser.UserInterface.Contracts/Browser/IBrowserControl.cs index edf314c9..8259bcde 100644 --- a/SafeExamBrowser.UserInterface.Contracts/Browser/IBrowserControl.cs +++ b/SafeExamBrowser.UserInterface.Contracts/Browser/IBrowserControl.cs @@ -36,6 +36,11 @@ namespace SafeExamBrowser.UserInterface.Contracts.Browser /// event AddressChangedEventHandler AddressChanged; + /// + /// Event fired when a load error occurs. + /// + event LoadFailedEventHandler LoadFailed; + /// /// Event fired when the loading state of the browser control changes. /// diff --git a/SafeExamBrowser.UserInterface.Contracts/SafeExamBrowser.UserInterface.Contracts.csproj b/SafeExamBrowser.UserInterface.Contracts/SafeExamBrowser.UserInterface.Contracts.csproj index a9be80e2..46e9f08d 100644 --- a/SafeExamBrowser.UserInterface.Contracts/SafeExamBrowser.UserInterface.Contracts.csproj +++ b/SafeExamBrowser.UserInterface.Contracts/SafeExamBrowser.UserInterface.Contracts.csproj @@ -56,6 +56,7 @@ +