2017-07-26 14:36:20 +02:00
|
|
|
|
/*
|
2018-01-16 08:24:00 +01:00
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
2017-07-26 14:36:20 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-07-27 13:57:12 +02:00
|
|
|
|
using System;
|
2018-01-24 07:46:22 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour.Operations;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication;
|
2018-02-21 14:01:21 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.Monitoring;
|
2018-02-21 14:01:21 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
2017-08-15 15:30:31 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface.Taskbar;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
|
2018-01-18 08:16:20 +01:00
|
|
|
|
namespace SafeExamBrowser.Client.Behaviour
|
2017-07-26 14:36:20 +02:00
|
|
|
|
{
|
2018-01-18 08:16:20 +01:00
|
|
|
|
internal class ClientController : IClientController
|
2017-07-26 14:36:20 +02:00
|
|
|
|
{
|
2017-08-11 08:28:17 +02:00
|
|
|
|
private IDisplayMonitor displayMonitor;
|
2017-07-27 11:46:31 +02:00
|
|
|
|
private ILogger logger;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
private IOperationSequence operations;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
private IProcessMonitor processMonitor;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
private IRuntimeProxy runtime;
|
2018-02-21 14:01:21 +01:00
|
|
|
|
private Action shutdown;
|
|
|
|
|
private ISplashScreen splashScreen;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
private ITaskbar taskbar;
|
2018-02-21 14:01:21 +01:00
|
|
|
|
private IUserInterfaceFactory uiFactory;
|
2017-07-27 11:46:31 +02:00
|
|
|
|
private IWindowMonitor windowMonitor;
|
2018-02-21 14:01:21 +01:00
|
|
|
|
private RuntimeInfo runtimeInfo;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
public IClientHost ClientHost { private get; set; }
|
2018-02-21 14:01:21 +01:00
|
|
|
|
public Guid SessionId { private get; set; }
|
|
|
|
|
public Settings Settings { private get; set; }
|
|
|
|
|
|
|
|
|
|
public RuntimeInfo RuntimeInfo
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
runtimeInfo = value;
|
|
|
|
|
|
|
|
|
|
if (splashScreen != null)
|
|
|
|
|
{
|
|
|
|
|
splashScreen.RuntimeInfo = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-20 15:15:26 +01:00
|
|
|
|
|
2018-01-17 08:26:44 +01:00
|
|
|
|
public ClientController(
|
2017-08-11 08:28:17 +02:00
|
|
|
|
IDisplayMonitor displayMonitor,
|
2017-07-27 11:46:31 +02:00
|
|
|
|
ILogger logger,
|
2018-02-12 12:21:55 +01:00
|
|
|
|
IOperationSequence operations,
|
2017-07-27 11:46:31 +02:00
|
|
|
|
IProcessMonitor processMonitor,
|
2018-02-14 15:26:05 +01:00
|
|
|
|
IRuntimeProxy runtime,
|
2018-02-21 14:01:21 +01:00
|
|
|
|
Action shutdown,
|
2017-07-27 11:46:31 +02:00
|
|
|
|
ITaskbar taskbar,
|
2018-02-21 14:01:21 +01:00
|
|
|
|
IUserInterfaceFactory uiFactory,
|
2017-08-11 08:28:17 +02:00
|
|
|
|
IWindowMonitor windowMonitor)
|
2017-07-26 14:36:20 +02:00
|
|
|
|
{
|
2017-08-11 08:28:17 +02:00
|
|
|
|
this.displayMonitor = displayMonitor;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
this.logger = logger;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
this.operations = operations;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
this.processMonitor = processMonitor;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
this.runtime = runtime;
|
2018-02-21 14:01:21 +01:00
|
|
|
|
this.shutdown = shutdown;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
this.taskbar = taskbar;
|
2018-02-21 14:01:21 +01:00
|
|
|
|
this.uiFactory = uiFactory;
|
2017-07-27 11:46:31 +02:00
|
|
|
|
this.windowMonitor = windowMonitor;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
public bool TryStart()
|
2018-02-12 12:21:55 +01:00
|
|
|
|
{
|
2018-02-21 14:01:21 +01:00
|
|
|
|
splashScreen = uiFactory.CreateSplashScreen();
|
|
|
|
|
operations.ProgressIndicator = splashScreen;
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
var success = operations.TryPerform();
|
2018-02-14 15:26:05 +01:00
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
RegisterEvents();
|
|
|
|
|
runtime.InformClientReady();
|
2018-02-21 14:01:21 +01:00
|
|
|
|
splashScreen.Hide();
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
2018-02-12 12:21:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
public void Terminate()
|
2017-07-26 14:36:20 +02:00
|
|
|
|
{
|
2018-02-21 14:01:21 +01:00
|
|
|
|
splashScreen.Show();
|
|
|
|
|
splashScreen.BringToForeground();
|
2017-07-26 14:36:20 +02:00
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
// TODO
|
|
|
|
|
|
2018-02-21 14:01:21 +01:00
|
|
|
|
DeregisterEvents();
|
2018-02-20 15:15:26 +01:00
|
|
|
|
operations.TryRevert();
|
2018-02-21 14:01:21 +01:00
|
|
|
|
|
|
|
|
|
splashScreen?.Close();
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
2018-02-14 15:26:05 +01:00
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
private void RegisterEvents()
|
|
|
|
|
{
|
|
|
|
|
ClientHost.Shutdown += ClientHost_Shutdown;
|
|
|
|
|
displayMonitor.DisplayChanged += DisplayMonitor_DisplaySettingsChanged;
|
|
|
|
|
processMonitor.ExplorerStarted += ProcessMonitor_ExplorerStarted;
|
|
|
|
|
taskbar.QuitButtonClicked += Taskbar_QuitButtonClicked;
|
|
|
|
|
windowMonitor.WindowChanged += WindowMonitor_WindowChanged;
|
|
|
|
|
}
|
2018-02-14 15:26:05 +01:00
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
private void DeregisterEvents()
|
|
|
|
|
{
|
|
|
|
|
ClientHost.Shutdown -= ClientHost_Shutdown;
|
|
|
|
|
displayMonitor.DisplayChanged -= DisplayMonitor_DisplaySettingsChanged;
|
|
|
|
|
processMonitor.ExplorerStarted -= ProcessMonitor_ExplorerStarted;
|
|
|
|
|
taskbar.QuitButtonClicked -= Taskbar_QuitButtonClicked;
|
|
|
|
|
windowMonitor.WindowChanged -= WindowMonitor_WindowChanged;
|
2017-07-26 14:36:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-11 08:28:17 +02:00
|
|
|
|
private void DisplayMonitor_DisplaySettingsChanged()
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Reinitializing working area...");
|
|
|
|
|
displayMonitor.InitializePrimaryDisplay(taskbar.GetAbsoluteHeight());
|
|
|
|
|
logger.Info("Reinitializing taskbar bounds...");
|
|
|
|
|
taskbar.InitializeBounds();
|
|
|
|
|
logger.Info("Desktop successfully restored.");
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 14:36:20 +02:00
|
|
|
|
private void ProcessMonitor_ExplorerStarted()
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Trying to shut down explorer...");
|
|
|
|
|
processMonitor.CloseExplorerShell();
|
|
|
|
|
logger.Info("Reinitializing working area...");
|
2017-08-11 08:28:17 +02:00
|
|
|
|
displayMonitor.InitializePrimaryDisplay(taskbar.GetAbsoluteHeight());
|
2017-07-26 14:36:20 +02:00
|
|
|
|
logger.Info("Reinitializing taskbar bounds...");
|
|
|
|
|
taskbar.InitializeBounds();
|
2017-07-27 13:57:12 +02:00
|
|
|
|
logger.Info("Desktop successfully restored.");
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
private void ClientHost_Shutdown()
|
|
|
|
|
{
|
|
|
|
|
taskbar.Close();
|
2018-02-21 14:01:21 +01:00
|
|
|
|
shutdown.Invoke();
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Taskbar_QuitButtonClicked()
|
|
|
|
|
{
|
|
|
|
|
// TODO: MessageBox asking whether user really wants to quit -> args.Cancel
|
|
|
|
|
|
|
|
|
|
var acknowledged = runtime.RequestShutdown();
|
|
|
|
|
|
|
|
|
|
if (!acknowledged)
|
|
|
|
|
{
|
|
|
|
|
logger.Warn("The runtime did not acknowledge the shutdown request!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 13:57:12 +02:00
|
|
|
|
private void WindowMonitor_WindowChanged(IntPtr window)
|
|
|
|
|
{
|
|
|
|
|
var allowed = processMonitor.BelongsToAllowedProcess(window);
|
|
|
|
|
|
|
|
|
|
if (!allowed)
|
|
|
|
|
{
|
|
|
|
|
var success = windowMonitor.Hide(window);
|
|
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
windowMonitor.Close(window);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-26 14:36:20 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|