Restore SEBPatch

This commit is contained in:
2025-06-01 11:44:20 +02:00
commit 8c656e3137
1297 changed files with 142172 additions and 0 deletions

View File

@@ -0,0 +1,163 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 System.Collections.Generic;
using System.Linq;
using SafeExamBrowser.Core.Contracts.OperationModel;
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
using SafeExamBrowser.Logging.Contracts;
namespace SafeExamBrowser.Core.OperationModel
{
/// <summary>
/// Default implementation of the <see cref="IOperationSequence"/>.
/// </summary>
public class OperationSequence : IOperationSequence
{
protected ILogger logger;
protected Queue<IOperation> operations = new Queue<IOperation>();
protected Stack<IOperation> stack = new Stack<IOperation>();
public event ActionRequiredEventHandler ActionRequired
{
add { operations.ForEach(o => o.ActionRequired += value); }
remove { operations.ForEach(o => o.ActionRequired -= value); }
}
public event ProgressChangedEventHandler ProgressChanged;
public event StatusChangedEventHandler StatusChanged
{
add { operations.ForEach(o => o.StatusChanged += value); }
remove { operations.ForEach(o => o.StatusChanged -= value); }
}
public OperationSequence(ILogger logger, Queue<IOperation> operations)
{
this.logger = logger;
this.operations = new Queue<IOperation>(operations);
}
public virtual OperationResult TryPerform()
{
var result = OperationResult.Failed;
try
{
Initialize();
result = Perform();
if (result != OperationResult.Success)
{
Revert(true);
}
}
catch (Exception e)
{
logger.Error("Failed to perform operations!", e);
}
return result;
}
public virtual OperationResult TryRevert()
{
var result = OperationResult.Failed;
try
{
Initialize(true);
result = Revert();
}
catch (Exception e)
{
logger.Error("Failed to revert operations!", e);
}
return result;
}
protected virtual void Initialize(bool indeterminate = false)
{
if (indeterminate)
{
UpdateProgress(new ProgressChangedEventArgs { IsIndeterminate = true });
}
else
{
UpdateProgress(new ProgressChangedEventArgs { CurrentValue = 0, MaxValue = operations.Count });
}
}
protected virtual OperationResult Perform()
{
foreach (var operation in operations)
{
var result = OperationResult.Failed;
stack.Push(operation);
try
{
result = operation.Perform();
}
catch (Exception e)
{
logger.Error($"Caught unexpected exception while performing operation '{operation.GetType().Name}'!", e);
}
if (result != OperationResult.Success)
{
return result;
}
UpdateProgress(new ProgressChangedEventArgs { Progress = true });
}
return OperationResult.Success;
}
protected virtual OperationResult Revert(bool regress = false)
{
var success = true;
while (stack.Any())
{
var operation = stack.Pop();
try
{
var result = operation.Revert();
if (result != OperationResult.Success)
{
success = false;
}
}
catch (Exception e)
{
logger.Error($"Caught unexpected exception while reverting operation '{operation.GetType().Name}'!", e);
success = false;
}
if (regress)
{
UpdateProgress(new ProgressChangedEventArgs { Regress = true });
}
}
return success ? OperationResult.Success : OperationResult.Failed;
}
protected void UpdateProgress(ProgressChangedEventArgs args)
{
ProgressChanged?.Invoke(args);
}
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 System.Collections.Generic;
namespace SafeExamBrowser.Core.OperationModel
{
internal static class QueueExtensions
{
internal static void ForEach<T>(this Queue<T> queue, Action<T> action)
{
foreach (var element in queue)
{
action(element);
}
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 System.Collections.Generic;
using SafeExamBrowser.Core.Contracts.OperationModel;
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
using SafeExamBrowser.Logging.Contracts;
namespace SafeExamBrowser.Core.OperationModel
{
/// <summary>
/// Default implementation of the <see cref="IRepeatableOperationSequence"/>.
/// </summary>
public class RepeatableOperationSequence : OperationSequence, IRepeatableOperationSequence
{
private new Queue<IRepeatableOperation> operations;
public RepeatableOperationSequence(ILogger logger, Queue<IRepeatableOperation> operations) : base(logger, new Queue<IOperation>(operations))
{
this.operations = new Queue<IRepeatableOperation>(operations);
}
public OperationResult TryRepeat()
{
var result = OperationResult.Failed;
try
{
Initialize();
result = Repeat();
}
catch (Exception e)
{
logger.Error("Failed to repeat operations!", e);
}
return result;
}
private OperationResult Repeat()
{
foreach (var operation in operations)
{
var result = OperationResult.Failed;
try
{
result = operation.Repeat();
}
catch (Exception e)
{
logger.Error($"Caught unexpected exception while repeating operation '{operation.GetType().Name}'!", e);
}
if (result != OperationResult.Success)
{
return result;
}
UpdateProgress(new ProgressChangedEventArgs { Progress = true });
}
return OperationResult.Success;
}
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 SafeExamBrowser.Communication.Contracts;
using SafeExamBrowser.Core.Contracts.OperationModel;
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
namespace SafeExamBrowser.Core.Operations
{
/// <summary>
/// An operation to handle the lifetime of an <see cref="ICommunicationHost"/>. The host is started during <see cref="Perform"/>,
/// stopped and restarted during <see cref="Repeat"/> (if not running) and stopped during <see cref="Revert"/>.
/// </summary>
public class CommunicationHostOperation : IRepeatableOperation
{
private ICommunicationHost host;
private ILogger logger;
public event ActionRequiredEventHandler ActionRequired { add { } remove { } }
public event StatusChangedEventHandler StatusChanged;
public CommunicationHostOperation(ICommunicationHost host, ILogger logger)
{
this.host = host;
this.logger = logger;
}
public OperationResult Perform()
{
logger.Info("Starting communication host...");
StatusChanged?.Invoke(TextKey.OperationStatus_StartCommunicationHost);
host.Start();
return OperationResult.Success;
}
public OperationResult Repeat()
{
if (!host.IsRunning)
{
logger.Info("Restarting communication host...");
StatusChanged?.Invoke(TextKey.OperationStatus_RestartCommunicationHost);
host.Stop();
host.Start();
}
return OperationResult.Success;
}
public OperationResult Revert()
{
logger.Info("Stopping communication host...");
StatusChanged?.Invoke(TextKey.OperationStatus_StopCommunicationHost);
host.Stop();
return OperationResult.Success;
}
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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.Core.Contracts.OperationModel;
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
namespace SafeExamBrowser.Core.Operations
{
/// <summary>
/// A generic operation to allow for the (inline) definition of an operation via delegates. Useful if implementing a complete
/// <see cref="IOperation"/> would be an unnecessary overhead.
/// </summary>
public class DelegateOperation : IRepeatableOperation
{
private Action perform;
private Action repeat;
private Action revert;
public event ActionRequiredEventHandler ActionRequired { add { } remove { } }
public event StatusChangedEventHandler StatusChanged { add { } remove { } }
public DelegateOperation(Action perform, Action repeat = null, Action revert = null)
{
this.perform = perform;
this.repeat = repeat;
this.revert = revert;
}
public OperationResult Perform()
{
perform?.Invoke();
return OperationResult.Success;
}
public OperationResult Repeat()
{
repeat?.Invoke();
return OperationResult.Success;
}
public OperationResult Revert()
{
revert?.Invoke();
return OperationResult.Success;
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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 SafeExamBrowser.Core.Contracts.OperationModel;
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
namespace SafeExamBrowser.Core.Operations
{
/// <summary>
/// An operation to handle the initialization of an <see cref="IText"/> module with text data.
/// </summary>
public class I18nOperation : IOperation
{
private ILogger logger;
private IText text;
public event ActionRequiredEventHandler ActionRequired { add { } remove { } }
public event StatusChangedEventHandler StatusChanged { add { } remove { } }
public I18nOperation(ILogger logger, IText text)
{
this.logger = logger;
this.text = text;
}
public OperationResult Perform()
{
logger.Info($"Loading text data...");
text.Initialize();
return OperationResult.Success;
}
public OperationResult Revert()
{
return OperationResult.Success;
}
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (c) 2024 ETH Zürich, IT Services
*
* 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.Core.Contracts.OperationModel;
using SafeExamBrowser.Core.Contracts.OperationModel.Events;
namespace SafeExamBrowser.Core.Operations
{
/// <summary>
/// A wrapper operation to allow for a lazy (just-in-time) instantiation of an operation, initialized on <see cref="Perform"/>.
/// Is useful when e.g. dependencies for a certain operation are not available during execution of the composition root, but rather
/// only after a preceding operation within an <see cref="IOperationSequence"/> has finished.
/// </summary>
public class LazyInitializationOperation : IOperation
{
private Func<IOperation> initialize;
private IOperation operation;
private event ActionRequiredEventHandler ActionRequiredImpl;
private event StatusChangedEventHandler StatusChangedImpl;
public event ActionRequiredEventHandler ActionRequired
{
add
{
ActionRequiredImpl += value;
if (operation != null)
{
operation.ActionRequired += value;
}
}
remove
{
ActionRequiredImpl -= value;
if (operation != null)
{
operation.ActionRequired -= value;
}
}
}
public event StatusChangedEventHandler StatusChanged
{
add
{
StatusChangedImpl += value;
if (operation != null)
{
operation.StatusChanged += value;
}
}
remove
{
StatusChangedImpl -= value;
if (operation != null)
{
operation.StatusChanged -= value;
}
}
}
public LazyInitializationOperation(Func<IOperation> initialize)
{
this.initialize = initialize;
}
public OperationResult Perform()
{
operation = initialize.Invoke();
operation.ActionRequired += ActionRequiredImpl;
operation.StatusChanged += StatusChangedImpl;
return operation.Perform();
}
public OperationResult Revert()
{
return operation.Revert();
}
}
}

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SafeExamBrowser.Core")]
[assembly: AssemblyDescription("Safe Exam Browser")]
[assembly: AssemblyCompany("ETH Zürich")]
[assembly: AssemblyProduct("SafeExamBrowser.Core")]
[assembly: AssemblyCopyright("Copyright © 2024 ETH Zürich, IT Services")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
[assembly: InternalsVisibleTo("SafeExamBrowser.Core.UnitTests")]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3d6fdbb6-a4af-4626-bb2b-bf329d44f9cc")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0.0")]

View File

@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3D6FDBB6-A4AF-4626-BB2B-BF329D44F9CC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SafeExamBrowser.Core</RootNamespace>
<AssemblyName>SafeExamBrowser.Core</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="OperationModel\QueueExtensions.cs" />
<Compile Include="OperationModel\RepeatableOperationSequence.cs" />
<Compile Include="Operations\CommunicationHostOperation.cs" />
<Compile Include="Operations\LazyInitializationOperation.cs" />
<Compile Include="Operations\I18nOperation.cs" />
<Compile Include="Operations\DelegateOperation.cs" />
<Compile Include="OperationModel\OperationSequence.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SafeExamBrowser.Communication.Contracts\SafeExamBrowser.Communication.Contracts.csproj">
<Project>{0cd2c5fe-711a-4c32-afe0-bb804fe8b220}</Project>
<Name>SafeExamBrowser.Communication.Contracts</Name>
</ProjectReference>
<ProjectReference Include="..\SafeExamBrowser.Core.Contracts\SafeExamBrowser.Core.Contracts.csproj">
<Project>{fe0e1224-b447-4b14-81e7-ed7d84822aa0}</Project>
<Name>SafeExamBrowser.Core.Contracts</Name>
</ProjectReference>
<ProjectReference Include="..\SafeExamBrowser.I18n.Contracts\SafeExamBrowser.I18n.Contracts.csproj">
<Project>{1858ddf3-bc2a-4bff-b663-4ce2ffeb8b7d}</Project>
<Name>SafeExamBrowser.I18n.Contracts</Name>
</ProjectReference>
<ProjectReference Include="..\SafeExamBrowser.Logging.Contracts\SafeExamBrowser.Logging.Contracts.csproj">
<Project>{64ea30fb-11d4-436a-9c2b-88566285363e}</Project>
<Name>SafeExamBrowser.Logging.Contracts</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>