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-01-30 14:41:36 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
2018-09-21 11:33:32 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Core.OperationModel;
|
2018-10-03 14:35:27 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Core.OperationModel.Events;
|
2018-01-30 14:41:36 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2018-08-16 11:23:37 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.WindowsApi;
|
2018-01-26 12:33:36 +01:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Runtime.Operations
|
2018-01-26 12:33:36 +01:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
internal class KioskModeOperation : SessionOperation
|
2018-01-26 12:33:36 +01:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
protected IDesktopFactory desktopFactory;
|
|
|
|
|
protected IExplorerShell explorerShell;
|
|
|
|
|
protected ILogger logger;
|
|
|
|
|
protected IProcessFactory processFactory;
|
2018-01-30 14:41:36 +01:00
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
private static IDesktop newDesktop;
|
|
|
|
|
private static IDesktop originalDesktop;
|
2018-01-26 12:33:36 +01:00
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
protected static KioskMode? ActiveMode { get; private set; }
|
2018-10-10 09:19:03 +02:00
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
public override event ActionRequiredEventHandler ActionRequired { add { } remove { } }
|
|
|
|
|
public override event StatusChangedEventHandler StatusChanged;
|
2018-10-10 09:19:03 +02:00
|
|
|
|
|
2018-08-16 11:23:37 +02:00
|
|
|
|
public KioskModeOperation(
|
|
|
|
|
IDesktopFactory desktopFactory,
|
2018-08-17 14:48:50 +02:00
|
|
|
|
IExplorerShell explorerShell,
|
2018-08-16 11:23:37 +02:00
|
|
|
|
ILogger logger,
|
2018-10-12 11:16:59 +02:00
|
|
|
|
IProcessFactory processFactory,
|
|
|
|
|
SessionContext sessionContext) : base(sessionContext)
|
2018-01-30 14:41:36 +01:00
|
|
|
|
{
|
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-10-12 11:16:59 +02:00
|
|
|
|
public override OperationResult Perform()
|
2018-01-26 12:33:36 +01:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
logger.Info($"Initializing kiosk mode '{Context.Next.Settings.KioskMode}'...");
|
2018-10-03 15:42:50 +02:00
|
|
|
|
StatusChanged?.Invoke(TextKey.OperationStatus_InitializeKioskMode);
|
2018-01-30 14:41:36 +01:00
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
switch (Context.Next.Settings.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
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
ActiveMode = Context.Next.Settings.KioskMode;
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
return OperationResult.Success;
|
2018-01-26 12:33:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
public override OperationResult Repeat()
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
var newMode = Context.Next.Settings.KioskMode;
|
2018-08-17 14:48:50 +02:00
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
if (ActiveMode == newMode)
|
2018-08-17 14:48:50 +02:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
logger.Info($"New kiosk mode '{newMode}' is already active, skipping initialization...");
|
2018-08-17 14:48:50 +02:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
return OperationResult.Success;
|
|
|
|
|
}
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
2018-10-10 09:19:03 +02:00
|
|
|
|
return Perform();
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
public override OperationResult Revert()
|
2018-01-26 12:33:36 +01:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
logger.Info($"Reverting kiosk mode '{ActiveMode}'...");
|
2018-10-03 15:42:50 +02:00
|
|
|
|
StatusChanged?.Invoke(TextKey.OperationStatus_RevertKioskMode);
|
2018-01-30 14:41:36 +01:00
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
switch (ActiveMode)
|
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
|
|
|
|
}
|
2018-10-10 09:19:03 +02:00
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
2018-01-30 14:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateNewDesktop()
|
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
originalDesktop = desktopFactory.GetCurrent();
|
|
|
|
|
logger.Info($"Current desktop is {originalDesktop}.");
|
2018-08-17 14:48:50 +02:00
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
newDesktop = desktopFactory.CreateNew(nameof(SafeExamBrowser));
|
|
|
|
|
logger.Info($"Created new desktop {newDesktop}.");
|
2018-08-17 14:48:50 +02:00
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
newDesktop.Activate();
|
|
|
|
|
processFactory.StartupDesktop = newDesktop;
|
2018-08-17 14:48:50 +02:00
|
|
|
|
logger.Info("Successfully activated new desktop.");
|
2018-09-21 11:33:32 +02:00
|
|
|
|
|
|
|
|
|
explorerShell.Suspend();
|
2018-01-30 14:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CloseNewDesktop()
|
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
if (originalDesktop != null)
|
2018-08-16 11:23:37 +02:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
originalDesktop.Activate();
|
|
|
|
|
processFactory.StartupDesktop = originalDesktop;
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
if (newDesktop != null)
|
2018-08-16 11:23:37 +02:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
newDesktop.Close();
|
|
|
|
|
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-09-21 11:33:32 +02:00
|
|
|
|
|
|
|
|
|
explorerShell.Resume();
|
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-10-03 15:42:50 +02:00
|
|
|
|
StatusChanged?.Invoke(TextKey.OperationStatus_WaitExplorerTermination);
|
2018-10-12 11:16:59 +02:00
|
|
|
|
|
|
|
|
|
// TODO: Hiding all windows must be done here, as the explorer shell is needed to do so!
|
|
|
|
|
|
2018-08-17 14:48:50 +02:00
|
|
|
|
explorerShell.Terminate();
|
2018-01-30 14:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RestartExplorerShell()
|
|
|
|
|
{
|
2018-10-03 15:42:50 +02:00
|
|
|
|
StatusChanged?.Invoke(TextKey.OperationStatus_WaitExplorerStartup);
|
2018-08-17 14:48:50 +02:00
|
|
|
|
explorerShell.Start();
|
2018-10-12 11:16:59 +02:00
|
|
|
|
|
|
|
|
|
// TODO: Restore all hidden windows!
|
2018-08-16 11:23:37 +02:00
|
|
|
|
}
|
2018-01-26 12:33:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|