2018-01-26 12:33:36 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-03-06 11:49:51 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour.OperationModel;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2018-01-30 14:41:36 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2018-01-26 12:33:36 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
2018-08-16 11:23:37 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.WindowsApi;
|
2018-01-26 12:33:36 +01:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Runtime.Behaviour.Operations
|
|
|
|
|
{
|
|
|
|
|
internal class KioskModeOperation : IOperation
|
|
|
|
|
{
|
2018-02-06 15:12:11 +01:00
|
|
|
|
private IConfigurationRepository configuration;
|
2018-08-16 11:23:37 +02:00
|
|
|
|
private IDesktopFactory desktopFactory;
|
2018-08-17 14:48:50 +02:00
|
|
|
|
private IExplorerShell explorerShell;
|
2018-01-30 14:41:36 +01:00
|
|
|
|
private KioskMode kioskMode;
|
2018-08-16 11:23:37 +02:00
|
|
|
|
private ILogger logger;
|
|
|
|
|
private IProcessFactory processFactory;
|
|
|
|
|
private IDesktop newDesktop;
|
|
|
|
|
private IDesktop originalDesktop;
|
2018-01-30 14:41:36 +01:00
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public IProgressIndicator ProgressIndicator { private get; set; }
|
2018-01-26 12:33:36 +01:00
|
|
|
|
|
2018-08-16 11:23:37 +02:00
|
|
|
|
public KioskModeOperation(
|
|
|
|
|
IConfigurationRepository configuration,
|
|
|
|
|
IDesktopFactory desktopFactory,
|
2018-08-17 14:48:50 +02:00
|
|
|
|
IExplorerShell explorerShell,
|
2018-08-16 11:23:37 +02:00
|
|
|
|
ILogger logger,
|
|
|
|
|
IProcessFactory processFactory)
|
2018-01-30 14:41:36 +01:00
|
|
|
|
{
|
2018-02-06 15:12:11 +01:00
|
|
|
|
this.configuration = configuration;
|
2018-08-16 11:23:37 +02:00
|
|
|
|
this.desktopFactory = desktopFactory;
|
2018-08-17 14:48:50 +02:00
|
|
|
|
this.explorerShell = explorerShell;
|
2018-08-16 11:23:37 +02:00
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.processFactory = processFactory;
|
2018-01-30 14:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
public OperationResult Perform()
|
2018-01-26 12:33:36 +01:00
|
|
|
|
{
|
2018-02-06 15:12:11 +01:00
|
|
|
|
kioskMode = configuration.CurrentSettings.KioskMode;
|
2018-01-30 14:41:36 +01:00
|
|
|
|
|
|
|
|
|
logger.Info($"Initializing kiosk mode '{kioskMode}'...");
|
2018-02-02 09:30:41 +01:00
|
|
|
|
ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_InitializeKioskMode);
|
2018-01-30 14:41:36 +01:00
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
switch (kioskMode)
|
2018-01-30 14:41:36 +01:00
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
case KioskMode.CreateNewDesktop:
|
|
|
|
|
CreateNewDesktop();
|
|
|
|
|
break;
|
|
|
|
|
case KioskMode.DisableExplorerShell:
|
2018-08-17 14:48:50 +02:00
|
|
|
|
TerminateExplorerShell();
|
2018-02-14 15:26:05 +01:00
|
|
|
|
break;
|
2018-01-30 14:41:36 +01:00
|
|
|
|
}
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
2018-01-26 12:33:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
public OperationResult Repeat()
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-08-17 14:48:50 +02:00
|
|
|
|
var oldMode = kioskMode;
|
|
|
|
|
var newMode = configuration.CurrentSettings.KioskMode;
|
|
|
|
|
|
|
|
|
|
if (newMode == oldMode)
|
|
|
|
|
{
|
|
|
|
|
logger.Info($"New kiosk mode '{newMode}' is equal to the currently active '{oldMode}', skipping re-initialization...");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Revert();
|
|
|
|
|
Perform();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kioskMode = newMode;
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 12:33:36 +01:00
|
|
|
|
public void Revert()
|
|
|
|
|
{
|
2018-01-30 14:41:36 +01:00
|
|
|
|
logger.Info($"Reverting kiosk mode '{kioskMode}'...");
|
2018-02-02 09:30:41 +01:00
|
|
|
|
ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_RevertKioskMode);
|
2018-01-30 14:41:36 +01:00
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
switch (kioskMode)
|
2018-01-30 14:41:36 +01:00
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
case KioskMode.CreateNewDesktop:
|
|
|
|
|
CloseNewDesktop();
|
|
|
|
|
break;
|
|
|
|
|
case KioskMode.DisableExplorerShell:
|
|
|
|
|
RestartExplorerShell();
|
|
|
|
|
break;
|
2018-01-30 14:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateNewDesktop()
|
|
|
|
|
{
|
2018-08-16 11:23:37 +02:00
|
|
|
|
originalDesktop = desktopFactory.GetCurrent();
|
2018-08-17 14:48:50 +02:00
|
|
|
|
logger.Info($"Current desktop is {originalDesktop}.");
|
|
|
|
|
|
2018-08-16 11:23:37 +02:00
|
|
|
|
newDesktop = desktopFactory.CreateNew(nameof(SafeExamBrowser));
|
2018-08-17 14:48:50 +02:00
|
|
|
|
logger.Info($"Created new desktop {newDesktop}.");
|
|
|
|
|
|
2018-08-16 11:23:37 +02:00
|
|
|
|
newDesktop.Activate();
|
|
|
|
|
processFactory.StartupDesktop = newDesktop;
|
2018-08-17 14:48:50 +02:00
|
|
|
|
logger.Info("Successfully activated new desktop.");
|
2018-01-30 14:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CloseNewDesktop()
|
|
|
|
|
{
|
2018-08-16 11:23:37 +02:00
|
|
|
|
if (originalDesktop != null)
|
|
|
|
|
{
|
|
|
|
|
originalDesktop.Activate();
|
|
|
|
|
processFactory.StartupDesktop = originalDesktop;
|
2018-08-17 14:48:50 +02:00
|
|
|
|
logger.Info($"Switched back to original desktop {originalDesktop}.");
|
2018-08-16 11:23:37 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-08-17 14:48:50 +02:00
|
|
|
|
logger.Warn($"No original desktop found when attempting to close new desktop!");
|
2018-08-16 11:23:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newDesktop != null)
|
|
|
|
|
{
|
|
|
|
|
newDesktop.Close();
|
2018-08-17 14:48:50 +02:00
|
|
|
|
logger.Info($"Closed new desktop {newDesktop}.");
|
2018-08-16 11:23:37 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-08-17 14:48:50 +02:00
|
|
|
|
logger.Warn($"No new desktop found when attempting to close new desktop!");
|
2018-08-16 11:23:37 +02:00
|
|
|
|
}
|
2018-01-30 14:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 14:48:50 +02:00
|
|
|
|
private void TerminateExplorerShell()
|
2018-01-30 14:41:36 +01:00
|
|
|
|
{
|
2018-08-17 14:48:50 +02:00
|
|
|
|
ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_WaitExplorerTermination, true);
|
|
|
|
|
explorerShell.Terminate();
|
2018-01-30 14:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RestartExplorerShell()
|
|
|
|
|
{
|
2018-08-17 14:48:50 +02:00
|
|
|
|
ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_WaitExplorerStartup, true);
|
|
|
|
|
explorerShell.Start();
|
2018-08-16 11:23:37 +02:00
|
|
|
|
}
|
2018-01-26 12:33:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|