From 714d3007589b1f611f123088c08362fc2ad03a75 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Fri, 28 Jul 2017 14:52:15 +0200 Subject: [PATCH] Working on browser configuration. --- .../BrowserApplicationController.cs | 7 +-- .../SafeExamBrowser.Configuration.csproj | 3 +- .../Settings/BrowserSettings.cs | 46 ++++++++++++++++ .../{Settings.cs => Settings/SettingsImpl.cs} | 32 ++++++----- .../Settings/IBrowserSettings.cs | 53 +++++++++++++++++++ .../Configuration/{ => Settings}/ISettings.cs | 21 +++++--- .../SafeExamBrowser.Contracts.csproj | 3 +- .../UserInterface/IUserInterfaceFactory.cs | 3 +- .../Behaviour/ShutdownControllerTests.cs | 2 +- .../Behaviour/StartupControllerTests.cs | 2 +- .../Behaviour/ShutdownController.cs | 2 +- .../Behaviour/StartupController.cs | 2 +- SafeExamBrowser.Core/Logging/LogFileWriter.cs | 2 +- .../BrowserWindow.xaml.cs | 23 ++++++-- .../ApplicationInstanceButton.xaml.cs | 1 + .../SplashScreen.xaml.cs | 2 +- .../UserInterfaceFactory.cs | 5 +- .../ViewModels/DateTimeViewModel.cs | 8 +-- SafeExamBrowser/App.cs | 8 +-- SafeExamBrowser/CompositionRoot.cs | 5 +- 20 files changed, 181 insertions(+), 49 deletions(-) create mode 100644 SafeExamBrowser.Configuration/Settings/BrowserSettings.cs rename SafeExamBrowser.Configuration/{Settings.cs => Settings/SettingsImpl.cs} (67%) create mode 100644 SafeExamBrowser.Contracts/Configuration/Settings/IBrowserSettings.cs rename SafeExamBrowser.Contracts/Configuration/{ => Settings}/ISettings.cs (73%) diff --git a/SafeExamBrowser.Browser/BrowserApplicationController.cs b/SafeExamBrowser.Browser/BrowserApplicationController.cs index fa304cd5..8ecced98 100644 --- a/SafeExamBrowser.Browser/BrowserApplicationController.cs +++ b/SafeExamBrowser.Browser/BrowserApplicationController.cs @@ -12,6 +12,7 @@ using System.Linq; using CefSharp; using SafeExamBrowser.Contracts.Behaviour; using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.UserInterface; namespace SafeExamBrowser.Browser @@ -33,8 +34,8 @@ namespace SafeExamBrowser.Browser { var cefSettings = new CefSettings { - CachePath = settings.BrowserCachePath, - LogFile = settings.BrowserLogFile + CachePath = settings.Browser.CachePath, + LogFile = settings.Browser.LogFile }; var success = Cef.Initialize(cefSettings, true, null); @@ -65,7 +66,7 @@ namespace SafeExamBrowser.Browser private void CreateNewInstance() { var control = new BrowserControl("www.duckduckgo.com"); - var window = uiFactory.CreateBrowserWindow(control); + var window = uiFactory.CreateBrowserWindow(control, settings.Browser); var instance = new BrowserApplicationInstance("DuckDuckGo"); instance.RegisterWindow(window); diff --git a/SafeExamBrowser.Configuration/SafeExamBrowser.Configuration.csproj b/SafeExamBrowser.Configuration/SafeExamBrowser.Configuration.csproj index 8ccf808b..7511636f 100644 --- a/SafeExamBrowser.Configuration/SafeExamBrowser.Configuration.csproj +++ b/SafeExamBrowser.Configuration/SafeExamBrowser.Configuration.csproj @@ -62,7 +62,8 @@ - + + diff --git a/SafeExamBrowser.Configuration/Settings/BrowserSettings.cs b/SafeExamBrowser.Configuration/Settings/BrowserSettings.cs new file mode 100644 index 00000000..9a1eb38d --- /dev/null +++ b/SafeExamBrowser.Configuration/Settings/BrowserSettings.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017 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/. + */ + +using System; +using System.IO; +using SafeExamBrowser.Contracts.Configuration.Settings; + +namespace SafeExamBrowser.Configuration.Settings +{ + public class BrowserSettings : IBrowserSettings + { + private ISettings settings; + + public BrowserSettings(ISettings settings) + { + this.settings = settings; + } + + public bool AllowAddressBar => true; + + public bool AllowBackwardNavigation => false; + + public bool AllowDebugConsole => true; + + public bool AllowForwardNavigation => false; + + public bool AllowReloading => false; + + public string CachePath + { + get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), settings.AppDataFolderName, "Cache"); } + } + + public bool FullScreenMode => throw new NotImplementedException(); + + public string LogFile + { + get { return Path.Combine(settings.LogFolderPath, $"{settings.RuntimeIdentifier}_Browser.txt"); } + } + } +} diff --git a/SafeExamBrowser.Configuration/Settings.cs b/SafeExamBrowser.Configuration/Settings/SettingsImpl.cs similarity index 67% rename from SafeExamBrowser.Configuration/Settings.cs rename to SafeExamBrowser.Configuration/Settings/SettingsImpl.cs index 1df2934d..e9e8977b 100644 --- a/SafeExamBrowser.Configuration/Settings.cs +++ b/SafeExamBrowser.Configuration/Settings/SettingsImpl.cs @@ -9,33 +9,35 @@ using System; using System.IO; using System.Reflection; -using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Configuration.Settings; +using SafeExamBrowser.Contracts.Configuration.Settings; namespace SafeExamBrowser.Configuration { - public class Settings : ISettings + /// + /// TODO: Replace with proper implementation once configuration aspects are clear... + /// + public class SettingsImpl : ISettings { - private const string AppDataFolder = "SafeExamBrowser"; private static readonly string LogFileDate = DateTime.Now.ToString("yyyy-MM-dd\\_HH\\hmm\\mss\\s"); + public SettingsImpl() + { + Browser = new BrowserSettings(this); + } + + public string AppDataFolderName => "SafeExamBrowser"; + public string ApplicationLogFile { - get { return Path.Combine(LogFolderPath, $"{LogFileDate}_Application.txt"); } + get { return Path.Combine(LogFolderPath, $"{RuntimeIdentifier}_Application.txt"); } } - public string BrowserLogFile - { - get { return Path.Combine(LogFolderPath, $"{LogFileDate}_Browser.txt"); } - } - - public string BrowserCachePath - { - get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AppDataFolder, "Cache"); } - } + public IBrowserSettings Browser { get; private set; } public string LogFolderPath { - get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AppDataFolder, "Logs"); } + get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AppDataFolderName, "Logs"); } } public string ProgramCopyright @@ -70,5 +72,7 @@ namespace SafeExamBrowser.Configuration return version; } } + + public string RuntimeIdentifier => LogFileDate; } } diff --git a/SafeExamBrowser.Contracts/Configuration/Settings/IBrowserSettings.cs b/SafeExamBrowser.Contracts/Configuration/Settings/IBrowserSettings.cs new file mode 100644 index 00000000..07aeb3c1 --- /dev/null +++ b/SafeExamBrowser.Contracts/Configuration/Settings/IBrowserSettings.cs @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2017 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.Contracts.Configuration.Settings +{ + public interface IBrowserSettings + { + /// + /// Determines whether the user should be allowed to change the URL of a browser window. + /// + bool AllowAddressBar { get; } + + /// + /// Determines whether the user should be allowed to navigate backwards in a browser window. + /// + bool AllowBackwardNavigation { get; } + + /// + /// Determines whether the user should be allowed to open the debug console of a browser window. + /// + bool AllowDebugConsole { get; } + + /// + /// Determines whether the user should be allowed to navigate forwards in a browser window. + /// + bool AllowForwardNavigation { get; } + + /// + /// Determines whether the user should be allowed to reload webpages. + /// + bool AllowReloading { get; } + + /// + /// The path where the browser cache is to be stored. + /// + string CachePath { get; } + + /// + /// The file path under which the browser log is to be stored. + /// + string LogFile { get; } + + /// + /// Determines whether the main browser window should be rendered in fullscreen mode, i.e. without window frame. + /// + bool FullScreenMode { get; } + } +} diff --git a/SafeExamBrowser.Contracts/Configuration/ISettings.cs b/SafeExamBrowser.Contracts/Configuration/Settings/ISettings.cs similarity index 73% rename from SafeExamBrowser.Contracts/Configuration/ISettings.cs rename to SafeExamBrowser.Contracts/Configuration/Settings/ISettings.cs index 9e577b69..6c6dae7a 100644 --- a/SafeExamBrowser.Contracts/Configuration/ISettings.cs +++ b/SafeExamBrowser.Contracts/Configuration/Settings/ISettings.cs @@ -6,24 +6,24 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -namespace SafeExamBrowser.Contracts.Configuration +namespace SafeExamBrowser.Contracts.Configuration.Settings { public interface ISettings { + /// + /// The name used for the application data folder. + /// + string AppDataFolderName { get; } + /// /// The file path under which the application log is to be stored. /// string ApplicationLogFile { get; } /// - /// The path where the browser cache is to be stored. + /// All browser-related settings. /// - string BrowserCachePath { get; } - - /// - /// The file path under which the browser log is to be stored. - /// - string BrowserLogFile { get; } + IBrowserSettings Browser { get; } /// /// The path where the log files are to be stored. @@ -44,5 +44,10 @@ namespace SafeExamBrowser.Contracts.Configuration /// The program version of the application (i.e. the executing assembly). /// string ProgramVersion { get; } + + /// + /// A string uniquely identifying the runtime of the application, used e.g. for the log file names. + /// + string RuntimeIdentifier { get; } } } diff --git a/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj b/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj index 9624e8e7..6850991e 100644 --- a/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj +++ b/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj @@ -66,7 +66,8 @@ - + + diff --git a/SafeExamBrowser.Contracts/UserInterface/IUserInterfaceFactory.cs b/SafeExamBrowser.Contracts/UserInterface/IUserInterfaceFactory.cs index 566a99ea..baa6c258 100644 --- a/SafeExamBrowser.Contracts/UserInterface/IUserInterfaceFactory.cs +++ b/SafeExamBrowser.Contracts/UserInterface/IUserInterfaceFactory.cs @@ -7,6 +7,7 @@ */ using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.I18n; namespace SafeExamBrowser.Contracts.UserInterface @@ -21,7 +22,7 @@ namespace SafeExamBrowser.Contracts.UserInterface /// /// Creates a new browser window loaded with the given browser control. /// - IBrowserWindow CreateBrowserWindow(IBrowserControl control); + IBrowserWindow CreateBrowserWindow(IBrowserControl control, IBrowserSettings settings); /// /// Creates a taskbar notification, initialized with the given notification information. diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/ShutdownControllerTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/ShutdownControllerTests.cs index 3215dc4d..a30f9e8b 100644 --- a/SafeExamBrowser.Core.UnitTests/Behaviour/ShutdownControllerTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Behaviour/ShutdownControllerTests.cs @@ -10,7 +10,7 @@ using System.Collections.Generic; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using SafeExamBrowser.Contracts.Behaviour; -using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.UserInterface; diff --git a/SafeExamBrowser.Core.UnitTests/Behaviour/StartupControllerTests.cs b/SafeExamBrowser.Core.UnitTests/Behaviour/StartupControllerTests.cs index 169c6073..24aa21e4 100644 --- a/SafeExamBrowser.Core.UnitTests/Behaviour/StartupControllerTests.cs +++ b/SafeExamBrowser.Core.UnitTests/Behaviour/StartupControllerTests.cs @@ -11,7 +11,7 @@ using System.Collections.Generic; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using SafeExamBrowser.Contracts.Behaviour; -using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.UserInterface; diff --git a/SafeExamBrowser.Core/Behaviour/ShutdownController.cs b/SafeExamBrowser.Core/Behaviour/ShutdownController.cs index 7a21295e..f9e3e664 100644 --- a/SafeExamBrowser.Core/Behaviour/ShutdownController.cs +++ b/SafeExamBrowser.Core/Behaviour/ShutdownController.cs @@ -9,7 +9,7 @@ using System; using System.Collections.Generic; using SafeExamBrowser.Contracts.Behaviour; -using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.UserInterface; diff --git a/SafeExamBrowser.Core/Behaviour/StartupController.cs b/SafeExamBrowser.Core/Behaviour/StartupController.cs index 692a7c2c..ace9aa86 100644 --- a/SafeExamBrowser.Core/Behaviour/StartupController.cs +++ b/SafeExamBrowser.Core/Behaviour/StartupController.cs @@ -10,7 +10,7 @@ using System; using System.Collections.Generic; using System.Linq; using SafeExamBrowser.Contracts.Behaviour; -using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.UserInterface; diff --git a/SafeExamBrowser.Core/Logging/LogFileWriter.cs b/SafeExamBrowser.Core/Logging/LogFileWriter.cs index c8db9014..9c5204ce 100644 --- a/SafeExamBrowser.Core/Logging/LogFileWriter.cs +++ b/SafeExamBrowser.Core/Logging/LogFileWriter.cs @@ -8,7 +8,7 @@ using System.IO; using System.Text; -using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.Logging; namespace SafeExamBrowser.Core.Logging diff --git a/SafeExamBrowser.UserInterface/BrowserWindow.xaml.cs b/SafeExamBrowser.UserInterface/BrowserWindow.xaml.cs index 197587ee..1cf2a2ff 100644 --- a/SafeExamBrowser.UserInterface/BrowserWindow.xaml.cs +++ b/SafeExamBrowser.UserInterface/BrowserWindow.xaml.cs @@ -7,20 +7,25 @@ */ using System.Windows; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.UserInterface; namespace SafeExamBrowser.UserInterface { public partial class BrowserWindow : Window, IBrowserWindow { - public BrowserWindow(IBrowserControl browserControl) + private IBrowserSettings settings; + + public event WindowCloseHandler OnClose; + + public BrowserWindow(IBrowserControl browserControl, IBrowserSettings settings) { + this.settings = settings; + InitializeComponent(); InitializeBrowserWindow(browserControl); } - public event WindowCloseHandler OnClose; - public void BringToForeground() { if (WindowState == WindowState.Minimized) @@ -38,6 +43,18 @@ namespace SafeExamBrowser.UserInterface BrowserControlHost.Child = browserControl as System.Windows.Forms.Control; } + UrlTextBox.IsEnabled = settings.AllowAddressBar; + UrlTextBox.Visibility = settings.AllowAddressBar ? Visibility.Visible : Visibility.Collapsed; + + ReloadButton.IsEnabled = settings.AllowReloading; + ReloadButton.Visibility = settings.AllowReloading ? Visibility.Visible : Visibility.Collapsed; + + BackButton.IsEnabled = settings.AllowBackwardNavigation; + BackButton.Visibility = settings.AllowBackwardNavigation ? Visibility.Visible : Visibility.Collapsed; + + ForwardButton.IsEnabled = settings.AllowForwardNavigation; + ForwardButton.Visibility = settings.AllowForwardNavigation ? Visibility.Visible : Visibility.Collapsed; + Closing += (o, args) => OnClose?.Invoke(); } } diff --git a/SafeExamBrowser.UserInterface/Controls/ApplicationInstanceButton.xaml.cs b/SafeExamBrowser.UserInterface/Controls/ApplicationInstanceButton.xaml.cs index 31ff5f69..58a20fb2 100644 --- a/SafeExamBrowser.UserInterface/Controls/ApplicationInstanceButton.xaml.cs +++ b/SafeExamBrowser.UserInterface/Controls/ApplicationInstanceButton.xaml.cs @@ -20,6 +20,7 @@ namespace SafeExamBrowser.UserInterface.Controls private IApplicationInstance instance; public delegate void OnClickHandler(Guid instanceId); + public event OnClickHandler Click; public ApplicationInstanceButton(IApplicationInstance instance, IApplicationInfo info) diff --git a/SafeExamBrowser.UserInterface/SplashScreen.xaml.cs b/SafeExamBrowser.UserInterface/SplashScreen.xaml.cs index 000b8ab6..71c8d631 100644 --- a/SafeExamBrowser.UserInterface/SplashScreen.xaml.cs +++ b/SafeExamBrowser.UserInterface/SplashScreen.xaml.cs @@ -8,7 +8,7 @@ using System.Windows; using System.Windows.Documents; -using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.UserInterface; using SafeExamBrowser.UserInterface.ViewModels; diff --git a/SafeExamBrowser.UserInterface/UserInterfaceFactory.cs b/SafeExamBrowser.UserInterface/UserInterfaceFactory.cs index 3101f714..0cde8fab 100644 --- a/SafeExamBrowser.UserInterface/UserInterfaceFactory.cs +++ b/SafeExamBrowser.UserInterface/UserInterfaceFactory.cs @@ -9,6 +9,7 @@ using System.Threading; using System.Windows; using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.UserInterface; using SafeExamBrowser.UserInterface.Controls; @@ -22,9 +23,9 @@ namespace SafeExamBrowser.UserInterface return new ApplicationButton(info); } - public IBrowserWindow CreateBrowserWindow(IBrowserControl control) + public IBrowserWindow CreateBrowserWindow(IBrowserControl control, IBrowserSettings settings) { - return new BrowserWindow(control); + return new BrowserWindow(control, settings); } public ITaskbarNotification CreateNotification(INotificationInfo info) diff --git a/SafeExamBrowser.UserInterface/ViewModels/DateTimeViewModel.cs b/SafeExamBrowser.UserInterface/ViewModels/DateTimeViewModel.cs index afa22efc..09e0ab1c 100644 --- a/SafeExamBrowser.UserInterface/ViewModels/DateTimeViewModel.cs +++ b/SafeExamBrowser.UserInterface/ViewModels/DateTimeViewModel.cs @@ -16,6 +16,10 @@ namespace SafeExamBrowser.UserInterface.ViewModels { private Timer timer; + public string Date { get; private set; } + public string Time { get; private set; } + public string ToolTip { get; private set; } + public event PropertyChangedEventHandler PropertyChanged; public DateTimeViewModel() @@ -25,10 +29,6 @@ namespace SafeExamBrowser.UserInterface.ViewModels timer.Start(); } - public string Date { get; private set; } - public string Time { get; private set; } - public string ToolTip { get; private set; } - private void Timer_Elapsed(object sender, ElapsedEventArgs e) { var date = DateTime.Now; diff --git a/SafeExamBrowser/App.cs b/SafeExamBrowser/App.cs index 9cb14d80..94fdce27 100644 --- a/SafeExamBrowser/App.cs +++ b/SafeExamBrowser/App.cs @@ -1,6 +1,6 @@ /* * Copyright (c) 2017 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/. @@ -17,7 +17,7 @@ namespace SafeExamBrowser { public class App : Application { - private static readonly Mutex mutex = new Mutex(true, "safe_exam_browser_single_instance_mutex"); + private static readonly Mutex Mutex = new Mutex(true, "safe_exam_browser_single_instance_mutex"); private CompositionRoot instances = new CompositionRoot(); [STAThread] @@ -33,7 +33,7 @@ namespace SafeExamBrowser } finally { - mutex.Close(); + Mutex.Close(); } } @@ -51,7 +51,7 @@ namespace SafeExamBrowser private static bool NoInstanceRunning() { - return mutex.WaitOne(TimeSpan.Zero, true); + return Mutex.WaitOne(TimeSpan.Zero, true); } protected override void OnStartup(StartupEventArgs e) diff --git a/SafeExamBrowser/CompositionRoot.cs b/SafeExamBrowser/CompositionRoot.cs index f6852dc2..c9494ec9 100644 --- a/SafeExamBrowser/CompositionRoot.cs +++ b/SafeExamBrowser/CompositionRoot.cs @@ -1,6 +1,6 @@ /* * Copyright (c) 2017 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/. @@ -11,6 +11,7 @@ using SafeExamBrowser.Browser; using SafeExamBrowser.Configuration; using SafeExamBrowser.Contracts.Behaviour; using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.Settings; using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.Monitoring; @@ -53,7 +54,7 @@ namespace SafeExamBrowser browserInfo = new BrowserApplicationInfo(); logger = new Logger(); nativeMethods = new NativeMethods(); - settings = new Settings(); + settings = new SettingsImpl(); Taskbar = new Taskbar(); textResource = new XmlTextResource(); uiFactory = new UserInterfaceFactory();