Started working on window monitoring.

This commit is contained in:
Damian Büchel 2017-07-24 08:56:39 +02:00
parent e7b8404254
commit f5d76980d0
9 changed files with 120 additions and 21 deletions

View file

@ -10,5 +10,25 @@ namespace SafeExamBrowser.Contracts.Monitoring
{ {
public interface IWindowMonitor public interface IWindowMonitor
{ {
/// <summary>
/// Hides all currently opened windows.
/// </summary>
void HideAllWindows();
/// <summary>
/// Restores all windows which were hidden during the startup procedure.
/// </summary>
void RestoreHiddenWindows();
/// <summary>
/// Starts monitoring application windows by subscribing to specific system events.
/// If a window is shown which is not supposed to do so, it will be automatically hidden.
/// </summary>
void StartMonitoringWindows();
/// <summary>
/// Stops monitoring windows and deregisters from any subscribed system events.
/// </summary>
void StopMonitoringWindows();
} }
} }

View file

@ -30,6 +30,11 @@ namespace SafeExamBrowser.Core.Behaviour.Operations
public void Perform() public void Perform()
{ {
logger.Info("--- Initializing process monitoring ---"); logger.Info("--- Initializing process monitoring ---");
SplashScreen.UpdateText(Key.SplashScreen_WaitExplorerTermination, true);
processMonitor.CloseExplorerShell();
processMonitor.StartMonitoringExplorer();
SplashScreen.UpdateText(Key.SplashScreen_InitializeProcessMonitoring); SplashScreen.UpdateText(Key.SplashScreen_InitializeProcessMonitoring);
// TODO // TODO
@ -41,6 +46,11 @@ namespace SafeExamBrowser.Core.Behaviour.Operations
SplashScreen.UpdateText(Key.SplashScreen_StopProcessMonitoring); SplashScreen.UpdateText(Key.SplashScreen_StopProcessMonitoring);
// TODO // TODO
SplashScreen.UpdateText(Key.SplashScreen_WaitExplorerStartup, true);
processMonitor.StopMonitoringExplorer();
processMonitor.StartExplorerShell();
} }
} }
} }

View file

@ -10,7 +10,6 @@ using SafeExamBrowser.Contracts.Behaviour;
using SafeExamBrowser.Contracts.Configuration; using SafeExamBrowser.Contracts.Configuration;
using SafeExamBrowser.Contracts.I18n; using SafeExamBrowser.Contracts.I18n;
using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.UserInterface; using SafeExamBrowser.Contracts.UserInterface;
namespace SafeExamBrowser.Core.Behaviour.Operations namespace SafeExamBrowser.Core.Behaviour.Operations
@ -18,16 +17,14 @@ namespace SafeExamBrowser.Core.Behaviour.Operations
public class WorkingAreaOperation : IOperation public class WorkingAreaOperation : IOperation
{ {
private ILogger logger; private ILogger logger;
private IProcessMonitor processMonitor;
private ITaskbar taskbar; private ITaskbar taskbar;
private IWorkingArea workingArea; private IWorkingArea workingArea;
public ISplashScreen SplashScreen { private get; set; } public ISplashScreen SplashScreen { private get; set; }
public WorkingAreaOperation(ILogger logger, IProcessMonitor processMonitor, ITaskbar taskbar, IWorkingArea workingArea) public WorkingAreaOperation(ILogger logger, ITaskbar taskbar, IWorkingArea workingArea)
{ {
this.logger = logger; this.logger = logger;
this.processMonitor = processMonitor;
this.taskbar = taskbar; this.taskbar = taskbar;
this.workingArea = workingArea; this.workingArea = workingArea;
} }
@ -35,16 +32,11 @@ namespace SafeExamBrowser.Core.Behaviour.Operations
public void Perform() public void Perform()
{ {
logger.Info("--- Initializing working area ---"); logger.Info("--- Initializing working area ---");
SplashScreen.UpdateText(Key.SplashScreen_WaitExplorerTermination, true); SplashScreen.UpdateText(Key.SplashScreen_InitializeWorkingArea);
processMonitor.CloseExplorerShell();
processMonitor.StartMonitoringExplorer();
// TODO // TODO
// - Minimizing all open windows
// - Emptying clipboard // - Emptying clipboard
SplashScreen.UpdateText(Key.SplashScreen_InitializeWorkingArea);
workingArea.InitializeFor(taskbar); workingArea.InitializeFor(taskbar);
} }
@ -54,15 +46,9 @@ namespace SafeExamBrowser.Core.Behaviour.Operations
SplashScreen.UpdateText(Key.SplashScreen_RestoreWorkingArea); SplashScreen.UpdateText(Key.SplashScreen_RestoreWorkingArea);
// TODO // TODO
// - Restore all windows?
// - Emptying clipboard // - Emptying clipboard
workingArea.Reset(); workingArea.Reset();
SplashScreen.UpdateText(Key.SplashScreen_WaitExplorerStartup, true);
processMonitor.StopMonitoringExplorer();
processMonitor.StartExplorerShell();
} }
} }
} }

View file

@ -42,6 +42,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Processes\ProcessMonitor.cs" /> <Compile Include="Processes\ProcessMonitor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Windows\WindowMonitor.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\SafeExamBrowser.Contracts\SafeExamBrowser.Contracts.csproj"> <ProjectReference Include="..\SafeExamBrowser.Contracts\SafeExamBrowser.Contracts.csproj">
@ -56,7 +57,6 @@
<ItemGroup> <ItemGroup>
<Folder Include="Keyboard\" /> <Folder Include="Keyboard\" />
<Folder Include="Mouse\" /> <Folder Include="Mouse\" />
<Folder Include="Windows\" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View file

@ -0,0 +1,50 @@
/*
* 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 SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.WindowsApi;
namespace SafeExamBrowser.Monitoring.Windows
{
public class WindowMonitor : IWindowMonitor
{
private ILogger logger;
public WindowMonitor(ILogger logger)
{
this.logger = logger;
// TODO: Make operation for window monitor OR operation for all desktop initialization?!
// ...
}
public void HideAllWindows()
{
logger.Info("Minimizing all open windows...");
User32.MinimizeAllOpenWindows();
logger.Info("Open windows successfully minimized.");
}
public void RestoreHiddenWindows()
{
throw new NotImplementedException();
}
public void StartMonitoringWindows()
{
throw new NotImplementedException();
}
public void StopMonitoringWindows()
{
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,16 @@
/*
* 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.WindowsApi.Constants
{
static class Constant
{
internal const int WM_COMMAND = 0x111;
internal const int MIN_ALL = 419;
}
}

View file

@ -40,6 +40,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Constants\Constant.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Constants\SPI.cs" /> <Compile Include="Constants\SPI.cs" />
<Compile Include="Constants\SPIF.cs" /> <Compile Include="Constants\SPIF.cs" />

View file

@ -35,7 +35,7 @@ namespace SafeExamBrowser.WindowsApi
/// <returns></returns> /// <returns></returns>
public static uint GetShellProcessId() public static uint GetShellProcessId()
{ {
var handle = FindWindow("Shell_TrayWnd", null); var handle = GetShellWindowHandle();
var threadId = GetWindowThreadProcessId(handle, out uint processId); var threadId = GetWindowThreadProcessId(handle, out uint processId);
return processId; return processId;
@ -60,6 +60,16 @@ namespace SafeExamBrowser.WindowsApi
return workingArea; return workingArea;
} }
/// <summary>
/// Minimizes all open windows.
/// </summary>
public static void MinimizeAllOpenWindows()
{
var handle = GetShellWindowHandle();
SendMessage(handle, Constant.WM_COMMAND, (IntPtr) Constant.MIN_ALL, IntPtr.Zero);
}
/// <summary> /// <summary>
/// Instructs the main Windows explorer process to shut down. /// Instructs the main Windows explorer process to shut down.
/// </summary> /// </summary>
@ -72,8 +82,8 @@ namespace SafeExamBrowser.WindowsApi
/// </remarks> /// </remarks>
public static void PostCloseMessageToShell() public static void PostCloseMessageToShell()
{ {
var taskbarHandle = FindWindow("Shell_TrayWnd", null); var handle = GetShellWindowHandle();
var success = PostMessage(taskbarHandle, 0x5B4, IntPtr.Zero, IntPtr.Zero); var success = PostMessage(handle, 0x5B4, IntPtr.Zero, IntPtr.Zero);
if (!success) if (!success)
{ {
@ -107,6 +117,9 @@ namespace SafeExamBrowser.WindowsApi
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, ref RECT pvParam, SPIF fWinIni); static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, ref RECT pvParam, SPIF fWinIni);

View file

@ -20,6 +20,7 @@ using SafeExamBrowser.Core.Behaviour.Operations;
using SafeExamBrowser.Core.I18n; using SafeExamBrowser.Core.I18n;
using SafeExamBrowser.Core.Logging; using SafeExamBrowser.Core.Logging;
using SafeExamBrowser.Monitoring.Processes; using SafeExamBrowser.Monitoring.Processes;
using SafeExamBrowser.Monitoring.Windows;
using SafeExamBrowser.UserInterface; using SafeExamBrowser.UserInterface;
namespace SafeExamBrowser namespace SafeExamBrowser
@ -36,6 +37,7 @@ namespace SafeExamBrowser
private IText text; private IText text;
private ITextResource textResource; private ITextResource textResource;
private IUiElementFactory uiFactory; private IUiElementFactory uiFactory;
private IWindowMonitor windowMonitor;
private IWorkingArea workingArea; private IWorkingArea workingArea;
public IShutdownController ShutdownController { get; private set; } public IShutdownController ShutdownController { get; private set; }
@ -59,13 +61,14 @@ namespace SafeExamBrowser
text = new Text(textResource); text = new Text(textResource);
aboutInfo = new AboutNotificationInfo(text); aboutInfo = new AboutNotificationInfo(text);
processMonitor = new ProcessMonitor(new ModuleLogger(logger, typeof(ProcessMonitor))); processMonitor = new ProcessMonitor(new ModuleLogger(logger, typeof(ProcessMonitor)));
windowMonitor = new WindowMonitor(new ModuleLogger(logger, typeof(WindowMonitor)));
workingArea = new WorkingArea(new ModuleLogger(logger, typeof(WorkingArea))); workingArea = new WorkingArea(new ModuleLogger(logger, typeof(WorkingArea)));
ShutdownController = new ShutdownController(logger, messageBox, processMonitor, settings, text, uiFactory, workingArea); ShutdownController = new ShutdownController(logger, messageBox, processMonitor, settings, text, uiFactory, workingArea);
StartupController = new StartupController(browserController, browserInfo, logger, messageBox, aboutInfo, processMonitor, settings, Taskbar, text, uiFactory, workingArea); StartupController = new StartupController(browserController, browserInfo, logger, messageBox, aboutInfo, processMonitor, settings, Taskbar, text, uiFactory, workingArea);
StartupOperations = new Queue<IOperation>(); StartupOperations = new Queue<IOperation>();
StartupOperations.Enqueue(new ProcessMonitoringOperation(logger, processMonitor)); StartupOperations.Enqueue(new ProcessMonitoringOperation(logger, processMonitor));
StartupOperations.Enqueue(new WorkingAreaOperation(logger, processMonitor, Taskbar, workingArea)); StartupOperations.Enqueue(new WorkingAreaOperation(logger, Taskbar, workingArea));
StartupOperations.Enqueue(new TaskbarInitializationOperation(logger, aboutInfo, Taskbar, uiFactory)); StartupOperations.Enqueue(new TaskbarInitializationOperation(logger, aboutInfo, Taskbar, uiFactory));
StartupOperations.Enqueue(new BrowserInitializationOperation(browserController, browserInfo, logger, Taskbar, uiFactory)); StartupOperations.Enqueue(new BrowserInitializationOperation(browserController, browserInfo, logger, Taskbar, uiFactory));
} }