// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Diagnostics.CodeAnalysis; namespace System.Management.Automation.Internal { /// /// CabinetExtractor interface, implemented by CabinetExtractor /// in native code to handle the extraction of cabinet files. /// internal abstract class ICabinetExtractor : IDisposable { /// /// Extracts a cabinet file. /// /// Cabinet file name. /// Cabinet directory name, must be back slash terminated. /// Destination directory name, must be back slash terminated. internal abstract bool Extract(string cabinetName, string srcPath, string destPath); #region IDisposable Interface // // This is a special case of the IDisposable pattern because the resource // to be disposed is managed by the derived class. The implementation here // enables derived classes to handle it cleanly. // /// /// Flag: Has Dispose already been called? /// private bool _disposed = false; /// /// Disposes the instance, callable by consumers. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (_disposed) { // Nothing to do since the resource has already been disposed. return; } // If this class had to free objects: // Free managed objects if disposing == true; // Free unmanaged objects regardless. _disposed = true; } ~ICabinetExtractor() { Dispose(false); } #endregion } /// /// Abstract class which defines a CabinetExtractor loader. An implementation /// of this class will be instantiated onetime from the C++/CLI /// assembly using reflection. /// /// The C++/CLI implementation of this class needs to be /// static internal abstract class ICabinetExtractorLoader { internal virtual ICabinetExtractor GetCabinetExtractor() { return null; } } /// /// Used to create a CabinetExtractor class. /// internal static class CabinetExtractorFactory { private static readonly ICabinetExtractorLoader s_cabinetLoader; internal static readonly ICabinetExtractor EmptyExtractor = new EmptyCabinetExtractor(); /// /// Static constructor. /// [SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom")] static CabinetExtractorFactory() { s_cabinetLoader = CabinetExtractorLoader.GetInstance(); } /// /// Provider a CabinetExtractor instance. /// /// Tracer instance. internal static ICabinetExtractor GetCabinetExtractor() { if (s_cabinetLoader != null) { return s_cabinetLoader.GetCabinetExtractor(); } else { return EmptyExtractor; } } } /// /// Dummy cabinet extractor implementation. /// internal sealed class EmptyCabinetExtractor : ICabinetExtractor { /// /// Extracts a cabinet file. /// /// Cabinet file name. /// Cabinet directory name, must be back slash terminated. /// Destination directory name, must be back slash terminated. internal override bool Extract(string cabinetName, string srcPath, string destPath) { // its intentional that this method has no definition return false; } /// /// Disposes the instance. /// /// protected override void Dispose(bool disposing) { // it's intentional that this method has no definition since there is nothing to dispose. // If a resource is added to this class, it should implement IDisposable for derived classes. } } }