2017-07-14 10:28:59 +02:00
|
|
|
|
/*
|
2019-01-09 11:25:21 +01:00
|
|
|
|
* Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET)
|
2017-07-14 10:28:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-17 11:12:17 +01:00
|
|
|
|
using SafeExamBrowser.Browser.Events;
|
2018-03-08 07:35:58 +01:00
|
|
|
|
using SafeExamBrowser.Browser.Handlers;
|
2019-01-23 15:57:49 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Applications;
|
|
|
|
|
using SafeExamBrowser.Contracts.Applications.Events;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Browser;
|
2017-07-14 10:28:59 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
2018-08-31 15:29:36 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
2018-03-14 12:07:20 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface.Browser;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface.MessageBox;
|
2018-03-14 12:07:20 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface.Windows;
|
2017-07-14 10:28:59 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Browser
|
|
|
|
|
{
|
2018-03-08 07:35:58 +01:00
|
|
|
|
internal class BrowserApplicationInstance : IApplicationInstance
|
2017-07-14 10:28:59 +02:00
|
|
|
|
{
|
2018-06-29 09:50:20 +02:00
|
|
|
|
private AppConfig appConfig;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
private IBrowserControl control;
|
|
|
|
|
private IBrowserWindow window;
|
2018-03-14 11:13:30 +01:00
|
|
|
|
private bool isMainInstance;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
private IMessageBox messageBox;
|
2018-08-31 15:29:36 +02:00
|
|
|
|
private IModuleLogger logger;
|
2018-03-14 11:13:30 +01:00
|
|
|
|
private BrowserSettings settings;
|
|
|
|
|
private IText text;
|
|
|
|
|
private IUserInterfaceFactory uiFactory;
|
2019-01-23 09:37:47 +01:00
|
|
|
|
private string url;
|
|
|
|
|
|
|
|
|
|
private BrowserWindowSettings WindowSettings
|
|
|
|
|
{
|
|
|
|
|
get { return isMainInstance ? settings.MainWindowSettings : settings.AdditionalWindowSettings; }
|
|
|
|
|
}
|
2017-07-31 20:22:53 +02:00
|
|
|
|
|
2018-08-31 07:49:41 +02:00
|
|
|
|
public InstanceIdentifier Id { get; private set; }
|
2017-07-14 10:28:59 +02:00
|
|
|
|
public string Name { get; private set; }
|
2017-07-31 20:22:53 +02:00
|
|
|
|
public IWindow Window { get { return window; } }
|
2017-07-14 10:28:59 +02:00
|
|
|
|
|
2018-06-21 07:56:25 +02:00
|
|
|
|
public event DownloadRequestedEventHandler ConfigurationDownloadRequested;
|
2019-01-17 16:15:10 +01:00
|
|
|
|
public event IconChangedEventHandler IconChanged;
|
2018-07-06 15:57:38 +02:00
|
|
|
|
public event InstanceTerminatedEventHandler Terminated;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
public event NameChangedEventHandler NameChanged;
|
|
|
|
|
public event PopupRequestedEventHandler PopupRequested;
|
2017-07-28 09:12:17 +02:00
|
|
|
|
|
2018-06-21 07:56:25 +02:00
|
|
|
|
public BrowserApplicationInstance(
|
2018-06-29 09:50:20 +02:00
|
|
|
|
AppConfig appConfig,
|
2018-06-21 07:56:25 +02:00
|
|
|
|
BrowserSettings settings,
|
2018-08-31 07:49:41 +02:00
|
|
|
|
InstanceIdentifier id,
|
|
|
|
|
bool isMainInstance,
|
2019-01-17 11:12:17 +01:00
|
|
|
|
IMessageBox messageBox,
|
2018-08-31 15:29:36 +02:00
|
|
|
|
IModuleLogger logger,
|
2018-06-21 07:56:25 +02:00
|
|
|
|
IText text,
|
2019-01-23 09:37:47 +01:00
|
|
|
|
IUserInterfaceFactory uiFactory,
|
|
|
|
|
string url)
|
2018-03-14 11:13:30 +01:00
|
|
|
|
{
|
2018-06-29 09:50:20 +02:00
|
|
|
|
this.appConfig = appConfig;
|
2018-08-31 07:49:41 +02:00
|
|
|
|
this.Id = id;
|
2018-03-14 11:13:30 +01:00
|
|
|
|
this.isMainInstance = isMainInstance;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
this.messageBox = messageBox;
|
2018-08-31 15:29:36 +02:00
|
|
|
|
this.logger = logger;
|
2018-03-14 11:13:30 +01:00
|
|
|
|
this.settings = settings;
|
|
|
|
|
this.text = text;
|
|
|
|
|
this.uiFactory = uiFactory;
|
2019-01-23 09:37:47 +01:00
|
|
|
|
this.url = url;
|
2018-03-14 11:13:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Initialize()
|
2017-07-14 10:28:59 +02:00
|
|
|
|
{
|
2019-01-17 11:12:17 +01:00
|
|
|
|
var contextMenuHandler = new ContextMenuHandler(settings, text);
|
2019-01-17 16:15:10 +01:00
|
|
|
|
var displayHandler = new DisplayHandler();
|
2018-08-31 15:29:36 +02:00
|
|
|
|
var downloadLogger = logger.CloneFor($"{nameof(DownloadHandler)} {Id}");
|
|
|
|
|
var downloadHandler = new DownloadHandler(appConfig, settings, downloadLogger);
|
2019-01-17 11:12:17 +01:00
|
|
|
|
var keyboardHandler = new KeyboardHandler();
|
|
|
|
|
var lifeSpanHandler = new LifeSpanHandler();
|
|
|
|
|
var requestHandler = new RequestHandler(appConfig);
|
2018-06-21 07:56:25 +02:00
|
|
|
|
|
2019-01-17 16:15:10 +01:00
|
|
|
|
displayHandler.FaviconChanged += DisplayHandler_FaviconChanged;
|
2018-10-30 11:24:28 +01:00
|
|
|
|
downloadHandler.ConfigurationDownloadRequested += DownloadHandler_ConfigurationDownloadRequested;
|
2019-01-18 16:11:33 +01:00
|
|
|
|
keyboardHandler.ReloadRequested += ReloadRequested;
|
|
|
|
|
keyboardHandler.ZoomInRequested += ZoomInRequested;
|
|
|
|
|
keyboardHandler.ZoomOutRequested += ZoomOutRequested;
|
|
|
|
|
keyboardHandler.ZoomResetRequested += ZoomResetRequested;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
lifeSpanHandler.PopupRequested += LifeSpanHandler_PopupRequested;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
|
2019-01-23 09:37:47 +01:00
|
|
|
|
control = new BrowserControl(contextMenuHandler, displayHandler, downloadHandler, keyboardHandler, lifeSpanHandler, requestHandler, url);
|
2017-07-31 20:22:53 +02:00
|
|
|
|
control.AddressChanged += Control_AddressChanged;
|
2018-03-01 08:50:08 +01:00
|
|
|
|
control.LoadingStateChanged += Control_LoadingStateChanged;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
control.TitleChanged += Control_TitleChanged;
|
2019-01-11 08:25:40 +01:00
|
|
|
|
control.Initialize();
|
2017-07-31 20:22:53 +02:00
|
|
|
|
|
2018-08-31 15:29:36 +02:00
|
|
|
|
logger.Debug("Initialized browser control.");
|
|
|
|
|
|
2019-01-23 09:37:47 +01:00
|
|
|
|
window = uiFactory.CreateBrowserWindow(control, settings, isMainInstance);
|
2017-07-31 20:22:53 +02:00
|
|
|
|
window.Closing += () => Terminated?.Invoke(Id);
|
|
|
|
|
window.AddressChanged += Window_AddressChanged;
|
2019-01-18 16:11:33 +01:00
|
|
|
|
window.ReloadRequested += ReloadRequested;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
window.BackwardNavigationRequested += Window_BackwardNavigationRequested;
|
|
|
|
|
window.ForwardNavigationRequested += Window_ForwardNavigationRequested;
|
2019-01-18 16:11:33 +01:00
|
|
|
|
window.ZoomInRequested += ZoomInRequested;
|
|
|
|
|
window.ZoomOutRequested += ZoomOutRequested;
|
|
|
|
|
window.ZoomResetRequested += ZoomResetRequested;
|
2018-08-31 15:29:36 +02:00
|
|
|
|
|
|
|
|
|
logger.Debug("Initialized browser window.");
|
2017-07-31 20:22:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Control_AddressChanged(string address)
|
|
|
|
|
{
|
2019-01-17 11:12:17 +01:00
|
|
|
|
logger.Debug($"Navigated to '{address}'.");
|
2017-07-31 20:22:53 +02:00
|
|
|
|
window.UpdateAddress(address);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-01 08:50:08 +01:00
|
|
|
|
private void Control_LoadingStateChanged(bool isLoading)
|
|
|
|
|
{
|
2019-01-23 09:37:47 +01:00
|
|
|
|
window.CanNavigateBackwards = WindowSettings.AllowBackwardNavigation && control.CanNavigateBackwards;
|
|
|
|
|
window.CanNavigateForwards = WindowSettings.AllowForwardNavigation && control.CanNavigateForwards;
|
2018-03-01 08:50:08 +01:00
|
|
|
|
window.UpdateLoadingState(isLoading);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 20:22:53 +02:00
|
|
|
|
private void Control_TitleChanged(string title)
|
|
|
|
|
{
|
|
|
|
|
window.UpdateTitle(title);
|
|
|
|
|
NameChanged?.Invoke(title);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 16:15:10 +01:00
|
|
|
|
private void DisplayHandler_FaviconChanged(string uri)
|
|
|
|
|
{
|
|
|
|
|
var icon = new BrowserIconResource(uri);
|
|
|
|
|
|
|
|
|
|
IconChanged?.Invoke(icon);
|
|
|
|
|
window.UpdateIcon(icon);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 11:24:28 +01:00
|
|
|
|
private void DownloadHandler_ConfigurationDownloadRequested(string fileName, DownloadEventArgs args)
|
|
|
|
|
{
|
2018-11-08 09:39:52 +01:00
|
|
|
|
if (settings.AllowConfigurationDownloads)
|
|
|
|
|
{
|
|
|
|
|
args.BrowserWindow = window;
|
|
|
|
|
logger.Debug($"Forwarding download request for configuration file '{fileName}'.");
|
|
|
|
|
ConfigurationDownloadRequested?.Invoke(fileName, args);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Debug($"Discarded download request for configuration file '{fileName}'.");
|
|
|
|
|
}
|
2018-10-30 11:24:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 11:12:17 +01:00
|
|
|
|
private void LifeSpanHandler_PopupRequested(PopupRequestedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (settings.AllowPopups)
|
|
|
|
|
{
|
|
|
|
|
logger.Debug($"Forwarding request to open new window for '{args.Url}'...");
|
|
|
|
|
PopupRequested?.Invoke(args);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Debug($"Blocked attempt to open new window for '{args.Url}'.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 16:11:33 +01:00
|
|
|
|
private void ReloadRequested()
|
2017-07-31 20:22:53 +02:00
|
|
|
|
{
|
2019-01-23 09:37:47 +01:00
|
|
|
|
if (WindowSettings.AllowReloading && WindowSettings.ShowReloadWarning)
|
2019-01-18 16:11:33 +01:00
|
|
|
|
{
|
|
|
|
|
var result = messageBox.Show(TextKey.MessageBox_ReloadConfirmation, TextKey.MessageBox_ReloadConfirmationTitle, MessageBoxAction.YesNo, MessageBoxIcon.Question, window);
|
|
|
|
|
|
|
|
|
|
if (result == MessageBoxResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
logger.Debug("The user confirmed reloading the current page...");
|
|
|
|
|
control.Reload();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Debug("The user aborted reloading the current page.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-23 09:37:47 +01:00
|
|
|
|
else if (WindowSettings.AllowReloading)
|
2019-01-18 16:11:33 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Debug("Reloading current page...");
|
|
|
|
|
control.Reload();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Debug("Blocked reload attempt, as the user is not allowed to reload web pages.");
|
|
|
|
|
}
|
2017-07-31 20:22:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-18 16:11:33 +01:00
|
|
|
|
private void Window_AddressChanged(string address)
|
2017-07-31 20:22:53 +02:00
|
|
|
|
{
|
2019-01-18 16:11:33 +01:00
|
|
|
|
logger.Debug($"The user requested to navigate to '{address}'.");
|
|
|
|
|
control.NavigateTo(address);
|
2017-07-31 20:22:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Window_BackwardNavigationRequested()
|
|
|
|
|
{
|
2019-01-23 09:37:47 +01:00
|
|
|
|
logger.Debug($"Navigating backwards...");
|
2017-07-31 20:22:53 +02:00
|
|
|
|
control.NavigateBackwards();
|
2017-07-14 10:28:59 +02:00
|
|
|
|
}
|
2017-07-24 17:31:28 +02:00
|
|
|
|
|
2017-07-31 20:22:53 +02:00
|
|
|
|
private void Window_ForwardNavigationRequested()
|
2017-07-24 17:31:28 +02:00
|
|
|
|
{
|
2019-01-23 09:37:47 +01:00
|
|
|
|
logger.Debug($"Navigating forwards...");
|
2017-07-31 20:22:53 +02:00
|
|
|
|
control.NavigateForwards();
|
2017-07-24 17:31:28 +02:00
|
|
|
|
}
|
2019-01-18 16:11:33 +01:00
|
|
|
|
|
|
|
|
|
private void ZoomInRequested()
|
|
|
|
|
{
|
|
|
|
|
if (settings.AllowPageZoom)
|
|
|
|
|
{
|
|
|
|
|
control.ZoomIn();
|
|
|
|
|
logger.Debug("Increased page zoom.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ZoomOutRequested()
|
|
|
|
|
{
|
|
|
|
|
if (settings.AllowPageZoom)
|
|
|
|
|
{
|
|
|
|
|
control.ZoomOut();
|
|
|
|
|
logger.Debug("Decreased page zoom.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ZoomResetRequested()
|
|
|
|
|
{
|
|
|
|
|
if (settings.AllowPageZoom)
|
|
|
|
|
{
|
|
|
|
|
control.ZoomReset();
|
|
|
|
|
logger.Debug("Reset page zoom.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-14 10:28:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|