source: branches/eraser6/pluginsRewrite/Eraser.Manager/ManagerLibrary.cs @ 2288

Revision 2288, 4.3 KB checked in by lowjoel, 4 years ago (diff)

First compilable version.

  • Whether plugins are allowed to be loaded is now a callback function which must be set by the initialising class, otherwise all plugins found are loaded
  • Split the PluginLoadedEventArgs? class to its own file
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Runtime.Serialization;
26
27using Eraser.Plugins;
28using Eraser.Util;
29
30namespace Eraser.Manager
31{
32    /// <summary>
33    /// The library instance which initializes and cleans up data required for the
34    /// library to function.
35    /// </summary>
36    public class ManagerLibrary : IDisposable
37    {
38        public ManagerLibrary(SettingsManager settings)
39        {
40            if (Instance != null)
41                throw new InvalidOperationException("Only one ManagerLibrary instance can " +
42                    "exist at any one time");
43
44            Instance = this;
45            SettingsManager = settings;
46
47            EntropySourceRegistrar = new EntropySourceRegistrar();
48            PrngRegistrar = new PrngRegistrar();
49            ErasureMethodRegistrar = new ErasureMethodRegistrar();
50            ErasureTargetRegistrar = new ErasureTargetRegistrar();
51            FileSystemRegistrar = new FileSystemRegistrar();
52        }
53
54        ~ManagerLibrary()
55        {
56            Dispose(false);
57        }
58
59        protected virtual void Dispose(bool disposing)
60        {
61            if (SettingsManager == null)
62                return;
63
64            if (disposing)
65            {
66                EntropySourceRegistrar.Poller.Abort();
67                Host.Instance.Dispose();
68                SettingsManager.Save();
69            }
70
71            SettingsManager = null;
72            Instance = null;
73        }
74
75        public void Dispose()
76        {
77            Dispose(true);
78            GC.SuppressFinalize(this);
79        }
80
81        private void OnPluginLoad(object sender, PluginLoadEventArgs e)
82        {
83            //If the plugin does not have an approval or denial, check for the presence of
84            //a valid signature.
85            IDictionary<Guid, bool> approvals = ManagerLibrary.Settings.PluginApprovals;
86            if (!approvals.ContainsKey(e.Plugin.AssemblyInfo.Guid) &&
87                (e.Plugin.Assembly.GetName().GetPublicKey().Length == 0 ||
88                !Security.VerifyStrongName(e.Plugin.Assembly.Location) ||
89                e.Plugin.AssemblyAuthenticode == null))
90            {
91                e.Load = false;
92            }
93
94            //Is there an approval or denial?
95            else if (approvals.ContainsKey(e.Plugin.AssemblyInfo.Guid))
96                e.Load = approvals[e.Plugin.AssemblyInfo.Guid];
97
98            //There's no approval or denial, what is the specified loading policy?
99            else
100                e.Load = e.Plugin.LoadingPolicy != LoadingPolicy.DefaultOff;
101        }
102
103        /// <summary>
104        /// The global library instance.
105        /// </summary>
106        public static ManagerLibrary Instance { get; private set; }
107
108        /// <summary>
109        /// The global instance of the EntropySource Manager
110        /// </summary>
111        public EntropySourceRegistrar EntropySourceRegistrar { get; private set; }
112
113        /// <summary>
114        /// The global instance of the PRNG Manager.
115        /// </summary>
116        public PrngRegistrar PrngRegistrar { get; private set; }
117
118        /// <summary>
119        /// The global instance of the Erasure method Manager.
120        /// </summary>
121        public ErasureMethodRegistrar ErasureMethodRegistrar { get; private set; }
122
123        /// <summary>
124        /// The global instance of the Erasure target Manager.
125        /// </summary>
126        public ErasureTargetRegistrar ErasureTargetRegistrar { get; private set; }
127
128        /// <summary>
129        /// The global instance of the File System manager.
130        /// </summary>
131        public FileSystemRegistrar FileSystemRegistrar { get; private set; }
132
133        /// <summary>
134        /// Global instance of the Settings manager.
135        /// </summary>
136        public SettingsManager SettingsManager { get; set; }
137
138        /// <summary>
139        /// Gets the settings object representing the settings for the Eraser
140        /// Manager. This is just shorthand for the local classes.
141        /// </summary>
142        public static ManagerSettings Settings
143        {
144            get
145            {
146                if (settingsInstance == null)
147                    settingsInstance = new ManagerSettings();
148                return settingsInstance;
149            }
150        }
151
152        /// <summary>
153        /// The singleton instance for <see cref="Settings"/>.
154        /// </summary>
155        private static ManagerSettings settingsInstance;
156    }
157}
Note: See TracBrowser for help on using the repository browser.