2017-07-07 15:46:32 +02:00
|
|
|
|
/*
|
2018-01-16 08:24:00 +01:00
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
2017-07-07 15:46:32 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-07-12 15:36:30 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour;
|
2017-08-14 17:44:16 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
|
|
|
|
|
2017-07-12 15:36:30 +02:00
|
|
|
|
namespace SafeExamBrowser.Core.Behaviour
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
|
|
|
|
public class StartupController : IStartupController
|
|
|
|
|
{
|
|
|
|
|
private ILogger logger;
|
2018-01-17 14:08:39 +01:00
|
|
|
|
private IRuntimeInfo runtimeInfo;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
private ISplashScreen splashScreen;
|
2017-08-14 17:44:16 +02:00
|
|
|
|
private ISystemInfo systemInfo;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
private IText text;
|
2017-07-27 14:45:54 +02:00
|
|
|
|
private IUserInterfaceFactory uiFactory;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
private Stack<IOperation> stack = new Stack<IOperation>();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2018-01-17 14:08:39 +01:00
|
|
|
|
public StartupController(ILogger logger, IRuntimeInfo runtimeInfo, ISystemInfo systemInfo, IText text, IUserInterfaceFactory uiFactory)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2017-07-17 16:59:50 +02:00
|
|
|
|
this.logger = logger;
|
2018-01-17 14:08:39 +01:00
|
|
|
|
this.runtimeInfo = runtimeInfo;
|
2017-08-14 17:44:16 +02:00
|
|
|
|
this.systemInfo = systemInfo;
|
2017-07-17 16:59:50 +02:00
|
|
|
|
this.text = text;
|
|
|
|
|
this.uiFactory = uiFactory;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
public bool TryInitializeApplication(Queue<IOperation> operations)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2018-01-19 14:04:12 +01:00
|
|
|
|
var success = false;
|
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-26 14:36:20 +02:00
|
|
|
|
Initialize(operations.Count);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
success = Perform(operations);
|
|
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
RevertOperations();
|
|
|
|
|
}
|
2017-07-12 15:36:30 +02:00
|
|
|
|
|
2018-01-19 14:04:12 +01:00
|
|
|
|
Finish(success);
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-07-21 10:04:27 +02:00
|
|
|
|
LogAndShowException(e);
|
2017-07-26 14:36:20 +02:00
|
|
|
|
Finish(false);
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
return success;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2018-01-19 14:04:12 +01:00
|
|
|
|
private bool Perform(Queue<IOperation> operations)
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
foreach (var operation in operations)
|
2017-07-21 10:04:27 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
stack.Push(operation);
|
2017-07-21 10:04:27 +02:00
|
|
|
|
operation.SplashScreen = splashScreen;
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
operation.Perform();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
LogAndShowException(e);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (operation.AbortStartup)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
splashScreen.Progress();
|
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
return true;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
private void RevertOperations()
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
while (stack.Any())
|
2017-07-21 10:04:27 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
var operation = stack.Pop();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2017-10-09 12:11:33 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
operation.Revert();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to revert operation '{operation.GetType().Name}'!", e);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
splashScreen.Regress();
|
|
|
|
|
}
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 14:36:20 +02:00
|
|
|
|
private void Initialize(int operationCount)
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2017-07-21 10:04:27 +02:00
|
|
|
|
logger.Info("--- Initiating startup procedure ---");
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2018-01-17 14:08:39 +01:00
|
|
|
|
splashScreen = uiFactory.CreateSplashScreen(runtimeInfo, text);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
splashScreen.SetMaxProgress(operationCount);
|
2017-08-03 15:35:22 +02:00
|
|
|
|
splashScreen.UpdateText(TextKey.SplashScreen_StartupProcedure);
|
2018-01-30 14:41:36 +01:00
|
|
|
|
splashScreen.Show();
|
2017-07-21 10:04:27 +02:00
|
|
|
|
}
|
2017-07-17 08:28:18 +02:00
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
private void LogAndShowException(Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to initialize application!", e);
|
2017-08-03 15:35:22 +02:00
|
|
|
|
uiFactory.Show(text.Get(TextKey.MessageBox_StartupError), text.Get(TextKey.MessageBox_StartupErrorTitle), icon: MessageBoxIcon.Error);
|
2017-07-21 10:04:27 +02:00
|
|
|
|
logger.Info("Reverting operations...");
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 14:36:20 +02:00
|
|
|
|
private void Finish(bool success = true)
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2017-07-21 10:04:27 +02:00
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("--- Application successfully initialized! ---");
|
2017-07-26 14:36:20 +02:00
|
|
|
|
logger.Log(string.Empty);
|
2017-07-21 10:04:27 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-01-19 14:04:12 +01:00
|
|
|
|
logger.Info("--- Startup procedure aborted! ---");
|
2017-07-21 10:04:27 +02:00
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2018-01-30 14:41:36 +01:00
|
|
|
|
splashScreen?.Close();
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|