Ignore:
Timestamp:
3/14/2012 5:45:46 AM (2 years ago)
Author:
lowjoel
Message:

Redesign the Eraser Plugin framework.

  • Split the plugin-dependent interfaces and related support classes out to Eraser.Plugins assembly
    • The extension interfaces are in the Eraser.Plugins.ExtensionPoints? namespace.
      • New IClientTool interface for plugins to create Eraser-hosted tools (e.g. #147)
    • The Registrars are in the Eraser.Plugins.Registrars namespace
    • Base types are now interfaces, no longer abstract base classes
    • Version the Eraser.plugins assembly differently from the rest of the project so that other plugins can target different versions of Eraser with the same assembly. Change the Eraser.Plugins assembly version only if there is an API change.
  • Plugin loading behaviour:
    • Core plugins will be loaded before the rest of the plugins, but they will be loaded only by assembly name and not by path
    • Non-core plugin loading is handled by the Manager Library and not the Plugin code
    • The PluginInstance? class is now the PluginInfo? class
    • There is no longer a need to indicate the default file and unused space erasure methods, PRNGs etc; they are now hardcoded to prevent plugins from changing defaults.
  • The separate command line for the Eraser shell extension has been removed.
    • The Erase on Restart menu item has been removed (since it is a scheduling option and should be set in the Task Properties Dialog)
  • Task Progress information is now pulled from the Eraser core instead of pushed via events.
  • New persistent store class to generalise the use of the old Settings class
  • IDragAndDropConfigurerFactory for transforming drag-and-drop operations to erasure targets. This removes the Eraser.exe assembly's dependency on Eraser.DefaultPlugins?.

Closes #363.

Location:
trunk/eraser
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser

  • trunk/eraser/Eraser/Program.cs

    r2267 r2509  
    3131using System.Security.Principal; 
    3232using System.Text.RegularExpressions; 
     33using System.Text; 
    3334 
    3435using System.Reflection; 
     
    3940using Eraser.Manager; 
    4041using Eraser.Util; 
    41 using Eraser.DefaultPlugins; 
    42 using System.Text; 
     42using Eraser.Plugins; 
     43using Eraser.Plugins.ExtensionPoints; 
     44using Eraser.Plugins.Registrars; 
    4345 
    4446namespace Eraser 
     
    133135        } 
    134136 
    135         class AddTaskArguments : EraseArguments 
     137        class TaskArguments : EraseArguments 
    136138        { 
    137139            /// <summary> 
    138140            /// Constructor. 
    139141            /// </summary> 
    140             public AddTaskArguments() 
    141             { 
    142             } 
    143  
    144             /// <summary> 
    145             /// Constructs Add Task arguments from Erase arguments. 
     142            public TaskArguments() 
     143            { 
     144            } 
     145 
     146            /// <summary> 
     147            /// Constructs Task arguments from Erase arguments. 
    146148            /// </summary> 
    147149            /// <param name="arguments">The <see cref="EraseArguments"/> to use as a template 
    148150            /// for this instance.</param> 
    149             internal AddTaskArguments(EraseArguments arguments) 
     151            internal TaskArguments(EraseArguments arguments) 
    150152                : base(arguments) 
    151153            { 
     
    159161        } 
    160162 
    161         class ShellArguments : ConsoleArguments 
    162         { 
    163             /// <summary> 
    164             /// The action which the shell extension has requested. 
    165             /// </summary> 
    166             [Arg("action", "The action selected by the user", typeof(string), true, null, null)] 
    167             public ShellActions ShellAction { get; set; } 
    168  
    169             /// <summary> 
    170             /// Whether the recycle bin was specified on the command line. 
    171             /// </summary> 
    172             [Arg("recycleBin", "The recycle bin as an erasure target", typeof(string), false, null, null)] 
    173             public bool RecycleBin { get; set; } 
    174  
    175             /// <summary> 
    176             /// The destination for secure move operations, only valid when 
    177             /// <see cref="ShellAction"/> is <see cref="ShellActions.SecureMove"/> 
    178             /// </summary> 
    179             [Arg("destination", "The destination for secure move operations", typeof(string), false, null, null)] 
    180             public string Destination { get; set; } 
    181  
     163        class ShellArguments : TaskArguments 
     164        { 
    182165            /// <summary> 
    183166            /// The parent HWND which can be used as a parent to display dialogs. 
     
    185168            [Arg("parent", "The parent HWND which can be used as a parent to display dialogues", typeof(string), false, null, null)] 
    186169            public string Parent { get; set; } 
     170 
     171            /// <summary> 
     172            /// Whether we should display a confirmation dialog. 
     173            /// </summary> 
     174            [Arg("confirm", "Whether a confirmation dialog should be shown", typeof(bool), false, true, null)] 
     175            public bool Confirm { get; set; } 
    187176        } 
    188177 
     
    242231 
    243232            //Load the Eraser.Manager library 
    244             using (ManagerLibrary library = new ManagerLibrary(new Settings())) 
     233            using (ManagerLibrary library = new ManagerLibrary(Settings.Get())) 
    245234            { 
    246235                //Set our UI language 
     
    331320                        new ConsoleActionData(CommandErase, new EraseArguments()));  
    332321                    program.Handlers.Add("addtask", 
    333                         new ConsoleActionData(CommandAddTask, new AddTaskArguments())); 
     322                        new ConsoleActionData(CommandAddTask, new TaskArguments())); 
    334323                    program.Handlers.Add("importtasklist", 
    335324                        new ConsoleActionData(CommandImportTaskList, new ConsoleArguments())); 
     
    362351            //Get the command-line help for every erasure target 
    363352            StringBuilder targets = new StringBuilder(); 
    364             foreach (ErasureTarget target in ManagerLibrary.Instance.ErasureTargetRegistrar) 
     353            foreach (IErasureTarget target in Host.Instance.ErasureTargetFactories) 
    365354            { 
    366355                //Replace all \r\n with \n, and split into lines 
     
    379368 
    380369            //Generate the list of erasure methods. 
    381             foreach (ErasureMethod method in ManagerLibrary.Instance.ErasureMethodRegistrar) 
    382             { 
    383                 methods.AppendFormat(methodFormat, (method is UnusedSpaceErasureMethod) ? 
     370            foreach (IErasureMethod method in Host.Instance.ErasureTargetFactories) 
     371            { 
     372                methods.AppendFormat(methodFormat, (method is IUnusedSpaceErasureMethod) ? 
    384373                    "U" : "", method.Name, method.Guid); 
    385374            } 
     
    460449        private static void CommandErase(ConsoleArguments arg) 
    461450        { 
    462             AddTaskArguments arguments = new AddTaskArguments((EraseArguments)arg); 
     451            TaskArguments arguments = new TaskArguments((EraseArguments)arg); 
    463452            arguments.Schedule = "NOW"; 
    464453 
     
    473462        private static void CommandAddTask(ConsoleArguments arg) 
    474463        { 
    475             AddTaskArguments arguments = (AddTaskArguments)arg; 
    476  
     464            TaskArguments arguments = (TaskArguments)arg; 
     465            Task task = TaskFromCommandLine(arguments); 
     466 
     467            //Send the task out. 
     468            using (eraserClient = CommandConnect()) 
     469                eraserClient.Tasks.Add(task); 
     470        } 
     471 
     472        /// <summary> 
     473        /// Parses the command line for erasure targets and returns them as 
     474        /// a Task object. 
     475        /// </summary> 
     476        /// <param name="arguments">The arguments specified on the command line.</param> 
     477        /// <returns>The task represented on the command line.</returns> 
     478        private static Task TaskFromCommandLine(TaskArguments arguments) 
     479        { 
    477480            //Create the task 
    478481            Task task = new Task(); 
    479482 
    480483            //Get the erasure method the user wants to use 
    481             ErasureMethod method = string.IsNullOrEmpty(arguments.ErasureMethod) ? 
     484            IErasureMethod method = string.IsNullOrEmpty(arguments.ErasureMethod) ? 
    482485                ErasureMethodRegistrar.Default : 
    483486                ErasureMethodFromNameOrGuid(arguments.ErasureMethod); 
     
    504507            foreach (string argument in arguments.PositionalArguments) 
    505508            { 
    506                 ErasureTarget selectedTarget = null; 
     509                IErasureTarget selectedTarget = null; 
    507510 
    508511                //Iterate over every defined erasure target 
    509                 foreach (ErasureTarget target in ManagerLibrary.Instance.ErasureTargetRegistrar) 
     512                foreach (IErasureTarget target in Host.Instance.ErasureTargetFactories) 
    510513                { 
    511514                    //See if this argument can be handled by the target's configurer 
     
    545548                throw new ArgumentException(S._("Tasks must contain at least one erasure target.")); 
    546549 
    547             //Send the task out. 
    548             using (eraserClient = CommandConnect()) 
    549                 eraserClient.Tasks.Add(task); 
    550         } 
    551  
    552         private static ErasureMethod ErasureMethodFromNameOrGuid(string param) 
     550            return task; 
     551        } 
     552 
     553        private static IErasureMethod ErasureMethodFromNameOrGuid(string param) 
    553554        { 
    554555            try 
    555556            { 
    556                 return ManagerLibrary.Instance.ErasureMethodRegistrar[new Guid(param)]; 
     557                return Host.Instance.ErasureMethods[new Guid(param)]; 
    557558            } 
    558559            catch (FormatException) 
     
    560561                //Invalid GUID. Check every registered erasure method for the name 
    561562                string upperParam = param.ToUpperInvariant(); 
    562                 ErasureMethod result = null; 
    563                 foreach (ErasureMethod method in ManagerLibrary.Instance.ErasureMethodRegistrar) 
     563                IErasureMethod result = null; 
     564                foreach (IErasureMethod method in Host.Instance.ErasureMethods) 
    564565                { 
    565566                    if (method.Name.ToUpperInvariant() == upperParam) 
     
    595596        private static void CommandShell(ConsoleArguments args) 
    596597        { 
    597             switch (((ShellArguments)args).ShellAction) 
    598             { 
    599                 case ShellActions.SecureMove: 
    600                     CommandShellSecureMove((ShellArguments)args); 
    601                     break; 
    602  
    603                 default: 
    604                     CommandShellErase((ShellArguments)args); 
    605                     break; 
    606             } 
    607         } 
    608  
    609         /// <summary> 
    610         /// Handles the erasure of files from the Shell extension. 
    611         /// </summary> 
    612         /// <param name="args">The command line parameters passed to the program.</param> 
    613         private static void CommandShellErase(ShellArguments args) 
    614         { 
    615             //Construct a draft task. 
    616             Task task = new Task(); 
    617             switch (args.ShellAction) 
    618             { 
    619                 case ShellActions.EraseOnRestart: 
    620                     task.Schedule = Schedule.RunOnRestart; 
    621                     goto case ShellActions.EraseNow; 
    622  
    623                 case ShellActions.EraseNow: 
    624                     foreach (string path in args.PositionalArguments) 
    625                     { 
    626                         //If the path doesn't exist, skip the file 
    627                         if (!(File.Exists(path) || Directory.Exists(path))) 
    628                             continue; 
    629  
    630                         FileSystemObjectErasureTarget target = null; 
    631                         if ((File.GetAttributes(path) & FileAttributes.Directory) != 0) 
    632                         { 
    633                             target = new FolderErasureTarget(); 
    634                             target.Path = path; 
    635                         } 
    636                         else 
    637                         { 
    638                             target = new FileErasureTarget(); 
    639                             target.Path = path; 
    640                         } 
    641  
    642                         task.Targets.Add(target); 
    643                     } 
    644  
    645                     //Was the recycle bin specified? 
    646                     if (args.RecycleBin) 
    647                         task.Targets.Add(new RecycleBinErasureTarget()); 
    648                     break; 
    649  
    650                 case ShellActions.EraseUnusedSpace: 
    651                     foreach (string path in args.PositionalArguments) 
    652                     { 
    653                         UnusedSpaceErasureTarget target = new UnusedSpaceErasureTarget(); 
    654                         target.Drive = path; 
    655                         task.Targets.Add(target); 
    656                     } 
    657                     break; 
    658             } 
     598            ShellArguments arguments = (ShellArguments)args; 
     599            Task task = TaskFromCommandLine(arguments); 
    659600 
    660601            //Do we have a parent dialog? 
    661602            IWin32Window parent = null; 
    662             if (args.Parent != null) 
     603            if (arguments.Parent != null) 
    663604            { 
    664605                parent = new Win32Window((IntPtr)(ulong) 
    665                     Convert.ChangeType(args.Parent, typeof(ulong))); 
     606                    Convert.ChangeType(arguments.Parent, typeof(ulong))); 
    666607            } 
    667608 
    668609            //Confirm that the user wants the erase. 
    669             Application.EnableVisualStyles(); 
    670             using (Form dialog = new ShellConfirmationDialog(task)) 
    671             { 
    672                 if (dialog.ShowDialog(parent) != DialogResult.Yes) 
    673                     return; 
    674             } 
    675  
    676             //Then queue for erasure. 
    677             using (eraserClient = CommandConnect()) 
    678                 eraserClient.Tasks.Add(task); 
    679         } 
    680  
    681         /// <summary> 
    682         /// Handles the movement of files from the Shell extension. 
    683         /// </summary> 
    684         /// <param name="args">The command line parameters passed to the program.</param> 
    685         private static void CommandShellSecureMove(ShellArguments args) 
    686         { 
    687             //Construct a draft task. 
    688             Task task = new Task(); 
    689             foreach (string path in args.PositionalArguments) 
    690             { 
    691                 SecureMoveErasureTarget target = new SecureMoveErasureTarget(); 
    692                 target.Path = path; 
    693                 target.Destination = args.Destination; 
    694  
    695                 task.Targets.Add(target); 
     610            if (arguments.Confirm) 
     611            { 
     612                Application.EnableVisualStyles(); 
     613                using (Form dialog = new ShellConfirmationDialog(task)) 
     614                { 
     615                    if (dialog.ShowDialog(parent) != DialogResult.Yes) 
     616                        return; 
     617                } 
    696618            } 
    697619 
     
    852774        /// </summary> 
    853775        public const string SettingsPath = @"SOFTWARE\Eraser\Eraser 6"; 
     776 
     777        public static IEnumerable<IErasureTarget> ErasureTargetRegistrar { get; set; } 
    854778    } 
    855779 
Note: See TracChangeset for help on using the changeset viewer.