seb-win-refactoring/SafeExamBrowser.Monitoring/Windows/WindowMonitor.cs

123 lines
3.4 KiB
C#
Raw Normal View History

2017-07-24 08:56:39 +02:00
/*
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
2017-07-24 08:56:39 +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-24 15:29:17 +02:00
using System.Collections.Generic;
2017-07-24 08:56:39 +02:00
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.Monitoring;
using SafeExamBrowser.Contracts.Monitoring.Events;
using SafeExamBrowser.Contracts.WindowsApi;
2017-07-24 08:56:39 +02:00
namespace SafeExamBrowser.Monitoring.Windows
{
public class WindowMonitor : IWindowMonitor
{
private IntPtr captureStartHookHandle;
private IntPtr foregroundHookHandle;
2017-07-24 08:56:39 +02:00
private ILogger logger;
2017-07-24 15:29:17 +02:00
private IList<Window> minimizedWindows = new List<Window>();
private INativeMethods nativeMethods;
2017-07-24 08:56:39 +02:00
public event WindowChangedEventHandler WindowChanged;
public WindowMonitor(ILogger logger, INativeMethods nativeMethods)
2017-07-24 08:56:39 +02:00
{
this.logger = logger;
this.nativeMethods = nativeMethods;
2017-07-24 08:56:39 +02:00
}
public void Close(IntPtr window)
{
var title = nativeMethods.GetWindowTitle(window);
nativeMethods.SendCloseMessageTo(window);
logger.Info($"Sent close message to window '{title}' with handle = {window}.");
}
public bool Hide(IntPtr window)
{
var title = nativeMethods.GetWindowTitle(window);
var success = nativeMethods.HideWindow(window);
if (success)
{
logger.Info($"Hid window '{title}' with handle = {window}.");
}
else
{
logger.Warn($"Failed to hide window '{title}' with handle = {window}!");
}
return success;
}
2017-07-24 08:56:39 +02:00
public void HideAllWindows()
{
2017-07-24 15:29:17 +02:00
logger.Info("Saving windows to be minimized...");
foreach (var handle in nativeMethods.GetOpenWindows())
2017-07-24 15:29:17 +02:00
{
var window = new Window
{
Handle = handle,
Title = nativeMethods.GetWindowTitle(handle)
2017-07-24 15:29:17 +02:00
};
minimizedWindows.Add(window);
logger.Info($"Saved window '{window.Title}' with handle = {window.Handle}.");
}
2017-07-24 08:56:39 +02:00
logger.Info("Minimizing all open windows...");
nativeMethods.MinimizeAllOpenWindows();
2017-07-24 08:56:39 +02:00
logger.Info("Open windows successfully minimized.");
}
public void RestoreHiddenWindows()
{
2017-07-24 15:29:17 +02:00
logger.Info("Restoring all minimized windows...");
foreach (var window in minimizedWindows)
{
nativeMethods.RestoreWindow(window.Handle);
2017-07-24 15:29:17 +02:00
logger.Info($"Restored window '{window.Title}' with handle = {window.Handle}.");
}
logger.Info("Minimized windows successfully restored.");
2017-07-24 08:56:39 +02:00
}
public void StartMonitoringWindows()
{
captureStartHookHandle = nativeMethods.RegisterSystemCaptureStartEvent(OnWindowChanged);
logger.Info($"Registered system capture start event with handle = {captureStartHookHandle}.");
foregroundHookHandle = nativeMethods.RegisterSystemForegroundEvent(OnWindowChanged);
logger.Info($"Registered system foreground event with handle = {foregroundHookHandle}.");
2017-07-24 08:56:39 +02:00
}
public void StopMonitoringWindows()
{
if (captureStartHookHandle != IntPtr.Zero)
{
2017-08-04 15:20:33 +02:00
nativeMethods.DeregisterSystemEvent(captureStartHookHandle);
logger.Info($"Unregistered system capture start event with handle = {captureStartHookHandle}.");
}
if (foregroundHookHandle != IntPtr.Zero)
{
2017-08-04 15:20:33 +02:00
nativeMethods.DeregisterSystemEvent(foregroundHookHandle);
logger.Info($"Unregistered system foreground event with handle = {foregroundHookHandle}.");
}
}
private void OnWindowChanged(IntPtr window)
{
WindowChanged?.Invoke(window);
2017-07-24 15:29:17 +02:00
}
2017-07-24 08:56:39 +02:00
}
}