The Labs \ Source Viewer \ SSCLI \ System.Security.Permissions \ HostProtectionPermission

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. // HostProtectionPermission.cs
  16. //
  17. namespace System.Security.Permissions
  18. {
  19.     using System;
  20.     using System.IO;
  21.     using System.Security.Util;
  22.     using System.Text;
  23.     using System.Threading;
  24.     using System.Runtime.Remoting;
  25.     using System.Security;
  26.     using System.Runtime.Serialization;
  27.     using System.Reflection;
  28.     using System.Globalization;
  29.    
  30.     // Keep this enum in sync with tools\ngen\ngen.cpp and inc\mscoree.idl
  31.    
  32.     [Flags(), Serializable()]
  33.     [System.Runtime.InteropServices.ComVisible(true)]
  34.     public enum HostProtectionResource
  35.     {
  36.         None = 0,
  37.         //--------------------------------
  38.         Synchronization = 1,
  39.         SharedState = 2,
  40.         ExternalProcessMgmt = 4,
  41.         SelfAffectingProcessMgmt = 8,
  42.         ExternalThreading = 16,
  43.         SelfAffectingThreading = 32,
  44.         SecurityInfrastructure = 64,
  45.         UI = 128,
  46.         MayLeakOnAbort = 256,
  47.         //---------------------------------
  48.         All = 511
  49.     }
  50.    
  51.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly | AttributeTargets.Delegate, AllowMultiple = true, Inherited = false)]
  52.     [System.Runtime.InteropServices.ComVisible(true)]
  53.     [Serializable()]
  54.     public sealed class HostProtectionAttribute : CodeAccessSecurityAttribute
  55.     {
  56.         private HostProtectionResource m_resources = HostProtectionResource.None;
  57.        
  58.         public HostProtectionAttribute() : base(SecurityAction.LinkDemand)
  59.         {
  60.         }
  61.        
  62.         public HostProtectionAttribute(SecurityAction action) : base(action)
  63.         {
  64.             if (action != SecurityAction.LinkDemand)
  65.                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"));
  66.         }
  67.        
  68.         public HostProtectionResource Resources {
  69.             get { return m_resources; }
  70.             set { m_resources = value; }
  71.         }
  72.        
  73.         public bool Synchronization {
  74.             get { return (m_resources & HostProtectionResource.Synchronization) != 0; }
  75.             set { m_resources = (value ? m_resources | HostProtectionResource.Synchronization : m_resources & ~HostProtectionResource.Synchronization); }
  76.         }
  77.        
  78.         public bool SharedState {
  79.             get { return (m_resources & HostProtectionResource.SharedState) != 0; }
  80.             set { m_resources = (value ? m_resources | HostProtectionResource.SharedState : m_resources & ~HostProtectionResource.SharedState); }
  81.         }
  82.        
  83.         public bool ExternalProcessMgmt {
  84.             get { return (m_resources & HostProtectionResource.ExternalProcessMgmt) != 0; }
  85.             set { m_resources = (value ? m_resources | HostProtectionResource.ExternalProcessMgmt : m_resources & ~HostProtectionResource.ExternalProcessMgmt); }
  86.         }
  87.        
  88.         public bool SelfAffectingProcessMgmt {
  89.             get { return (m_resources & HostProtectionResource.SelfAffectingProcessMgmt) != 0; }
  90.             set { m_resources = (value ? m_resources | HostProtectionResource.SelfAffectingProcessMgmt : m_resources & ~HostProtectionResource.SelfAffectingProcessMgmt); }
  91.         }
  92.        
  93.         public bool ExternalThreading {
  94.             get { return (m_resources & HostProtectionResource.ExternalThreading) != 0; }
  95.             set { m_resources = (value ? m_resources | HostProtectionResource.ExternalThreading : m_resources & ~HostProtectionResource.ExternalThreading); }
  96.         }
  97.        
  98.         public bool SelfAffectingThreading {
  99.             get { return (m_resources & HostProtectionResource.SelfAffectingThreading) != 0; }
  100.             set { m_resources = (value ? m_resources | HostProtectionResource.SelfAffectingThreading : m_resources & ~HostProtectionResource.SelfAffectingThreading); }
  101.         }
  102.        
  103.         [System.Runtime.InteropServices.ComVisible(true)]
  104.         public bool SecurityInfrastructure {
  105.             get { return (m_resources & HostProtectionResource.SecurityInfrastructure) != 0; }
  106.             set { m_resources = (value ? m_resources | HostProtectionResource.SecurityInfrastructure : m_resources & ~HostProtectionResource.SecurityInfrastructure); }
  107.         }
  108.        
  109.         public bool UI {
  110.             get { return (m_resources & HostProtectionResource.UI) != 0; }
  111.             set { m_resources = (value ? m_resources | HostProtectionResource.UI : m_resources & ~HostProtectionResource.UI); }
  112.         }
  113.        
  114.         public bool MayLeakOnAbort {
  115.             get { return (m_resources & HostProtectionResource.MayLeakOnAbort) != 0; }
  116.             set { m_resources = (value ? m_resources | HostProtectionResource.MayLeakOnAbort : m_resources & ~HostProtectionResource.MayLeakOnAbort); }
  117.         }
  118.        
  119.         public override IPermission CreatePermission()
  120.         {
  121.             if (m_unrestricted) {
  122.                 return new HostProtectionPermission(PermissionState.Unrestricted);
  123.             }
  124.             else {
  125.                 return new HostProtectionPermission(m_resources);
  126.             }
  127.         }
  128.     }
  129.    
  130.     [Serializable()]
  131.     internal sealed class HostProtectionPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission
  132.     {
  133.         //------------------------------------------------------
  134.         //
  135.         // GLOBALS
  136.         //
  137.         //------------------------------------------------------
  138.        
  139.         // This value is set by PermissionSet.FilterHostProtectionPermissions. It is only used for
  140.         // constructing a HostProtectionException object. Changing it will not affect HostProtection.
  141.         static internal HostProtectionResource protectedResources = HostProtectionResource.None;
  142.        
  143.         //------------------------------------------------------
  144.         //
  145.         // MEMBERS
  146.         //
  147.         //------------------------------------------------------
  148.         private HostProtectionResource m_resources;
  149.        
  150.         //------------------------------------------------------
  151.         //
  152.         // CONSTRUCTORS
  153.         //
  154.         //------------------------------------------------------
  155.         public HostProtectionPermission(PermissionState state)
  156.         {
  157.             if (state == PermissionState.Unrestricted)
  158.                 Resources = HostProtectionResource.All;
  159.             else if (state == PermissionState.None)
  160.                 Resources = HostProtectionResource.None;
  161.             else
  162.                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
  163.         }
  164.        
  165.         public HostProtectionPermission(HostProtectionResource resources)
  166.         {
  167.             Resources = resources;
  168.         }
  169.        
  170.         //------------------------------------------------------
  171.         //
  172.         // IPermission interface implementation
  173.         //
  174.         //------------------------------------------------------
  175.         public bool IsUnrestricted()
  176.         {
  177.             return Resources == HostProtectionResource.All;
  178.         }
  179.        
  180.         //------------------------------------------------------
  181.         //
  182.         // Properties
  183.         //
  184.         //------------------------------------------------------
  185.         public HostProtectionResource Resources {
  186.            
  187.             get { return m_resources; }
  188.             set {
  189.                 if (value < HostProtectionResource.None || value > HostProtectionResource.All)
  190.                     throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Arg_EnumIllegalVal"), (int)value));
  191.                 m_resources = value;
  192.             }
  193.         }
  194.        
  195.         //------------------------------------------------------
  196.         //
  197.         // IPermission interface implementation
  198.         //
  199.         //------------------------------------------------------
  200.         public override bool IsSubsetOf(IPermission target)
  201.         {
  202.             if (target == null)
  203.                 return m_resources == HostProtectionResource.None;
  204.             if (this.GetType() != target.GetType())
  205.                 throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName));
  206.             return ((uint)this.m_resources & (uint)((HostProtectionPermission)target).m_resources) == (uint)this.m_resources;
  207.         }
  208.        
  209.         public override IPermission Union(IPermission target)
  210.         {
  211.             if (target == null)
  212.                 return (this.Copy());
  213.             if (this.GetType() != target.GetType())
  214.                 throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName));
  215.             HostProtectionResource newResources = (HostProtectionResource)((uint)this.m_resources | (uint)((HostProtectionPermission)target).m_resources);
  216.             return new HostProtectionPermission(newResources);
  217.         }
  218.        
  219.         public override IPermission Intersect(IPermission target)
  220.         {
  221.             if (target == null)
  222.                 return null;
  223.             if (this.GetType() != target.GetType())
  224.                 throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName));
  225.             HostProtectionResource newResources = (HostProtectionResource)((uint)this.m_resources & (uint)((HostProtectionPermission)target).m_resources);
  226.             if (newResources == HostProtectionResource.None)
  227.                 return null;
  228.             return new HostProtectionPermission(newResources);
  229.         }
  230.        
  231.         public override IPermission Copy()
  232.         {
  233.             return new HostProtectionPermission(m_resources);
  234.         }
  235.        
  236.        
  237.         //------------------------------------------------------
  238.         //
  239.         // XML
  240.         //
  241.         //------------------------------------------------------
  242.         public override SecurityElement ToXml()
  243.         {
  244.             SecurityElement esd = CodeAccessPermission.CreatePermissionElement(this, this.GetType().FullName);
  245.             if (IsUnrestricted())
  246.                 esd.AddAttribute("Unrestricted", "true");
  247.             else
  248.                 esd.AddAttribute("Resources", XMLUtil.BitFieldEnumToString(typeof(HostProtectionResource), Resources));
  249.             return esd;
  250.         }
  251.        
  252.         public override void FromXml(SecurityElement esd)
  253.         {
  254.             CodeAccessPermission.ValidateElement(esd, this);
  255.             if (XMLUtil.IsUnrestricted(esd))
  256.                 Resources = HostProtectionResource.All;
  257.             else {
  258.                 string resources = esd.Attribute("Resources");
  259.                 if (resources == null)
  260.                     Resources = HostProtectionResource.None;
  261.                 else
  262.                     Resources = (HostProtectionResource)Enum.Parse(typeof(HostProtectionResource), resources);
  263.             }
  264.         }
  265.        
  266.         //------------------------------------------------------
  267.         //
  268.         // OBJECT OVERRIDES
  269.         //
  270.         //------------------------------------------------------
  271.        
  272.         /// <internalonly/>
  273.         int IBuiltInPermission.GetTokenIndex()
  274.         {
  275.             return HostProtectionPermission.GetTokenIndex();
  276.         }
  277.        
  278.         static internal int GetTokenIndex()
  279.         {
  280.             return BuiltInPermissionIndex.HostProtectionPermissionIndex;
  281.         }
  282.     }
  283. }

Developer Fusion